-
Notifications
You must be signed in to change notification settings - Fork 574
Expand file tree
/
Copy pathTooWideThrowTypeCheck.php
More file actions
56 lines (45 loc) · 1.28 KB
/
TooWideThrowTypeCheck.php
File metadata and controls
56 lines (45 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Exceptions;
use PHPStan\Analyser\ThrowPoint;
use PHPStan\DependencyInjection\AutowiredParameter;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Type\NeverType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeUtils;
use PHPStan\Type\VerbosityLevel;
use function array_map;
#[AutowiredService]
final class TooWideThrowTypeCheck
{
public function __construct(
#[AutowiredParameter(ref: '%exceptions.implicitThrows%')]
private bool $implicitThrows,
)
{
}
/**
* @param ThrowPoint[] $throwPoints
* @return string[]
*/
public function check(Type $throwType, array $throwPoints): array
{
if ($throwType->isVoid()->yes()) {
return [];
}
$throwPointType = TypeCombinator::union(...array_map(function (ThrowPoint $throwPoint): Type {
if (!$this->implicitThrows && !$throwPoint->isExplicit()) {
return new NeverType();
}
return $throwPoint->getType();
}, $throwPoints));
$throwClasses = [];
foreach (TypeUtils::flattenTypes($throwType) as $type) {
if (!$throwPointType instanceof NeverType && !$type->isSuperTypeOf($throwPointType)->no()) {
continue;
}
$throwClasses[] = $type->describe(VerbosityLevel::typeOnly());
}
return $throwClasses;
}
}