/
MissingTypehintCheck.php
112 lines (96 loc) · 3.29 KB
/
MissingTypehintCheck.php
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php declare(strict_types = 1);
namespace PHPStan\Rules;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\Generic\TemplateTypeHelper;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeTraverser;
class MissingTypehintCheck
{
public const TURN_OFF_MISSING_ITERABLE_VALUE_TYPE_TIP = "Consider adding something like <fg=cyan>%s<Foo></> to the PHPDoc.\nYou can turn off this check by setting <fg=cyan>checkMissingIterableValueType: false</> in your <fg=cyan>%%configurationFile%%</>.";
public const TURN_OFF_NON_GENERIC_CHECK_TIP = 'You can turn this off by setting <fg=cyan>checkGenericClassInNonGenericObjectType: false</> in your <fg=cyan>%configurationFile%</>.';
/** @var bool */
private $checkMissingIterableValueType;
/** @var bool */
private $checkGenericClassInNonGenericObjectType;
public function __construct(
bool $checkMissingIterableValueType,
bool $checkGenericClassInNonGenericObjectType
)
{
$this->checkMissingIterableValueType = $checkMissingIterableValueType;
$this->checkGenericClassInNonGenericObjectType = $checkGenericClassInNonGenericObjectType;
}
/**
* @param \PHPStan\Type\Type $type
* @return \PHPStan\Type\Type[]
*/
public function getIterableTypesWithMissingValueTypehint(Type $type): array
{
if (!$this->checkMissingIterableValueType) {
return [];
}
$iterablesWithMissingValueTypehint = [];
TypeTraverser::map($type, static function (Type $type, callable $traverse) use (&$iterablesWithMissingValueTypehint): Type {
if ($type->isIterable()->yes()) {
$iterableValue = $type->getIterableValueType();
if ($iterableValue instanceof MixedType && !$iterableValue->isExplicitMixed()) {
$iterablesWithMissingValueTypehint[] = $type;
}
return $type;
}
return $traverse($type);
});
return $iterablesWithMissingValueTypehint;
}
/**
* @param \PHPStan\Type\Type $type
* @return array<int, array{string, string[]}>
*/
public function getNonGenericObjectTypesWithGenericClass(Type $type): array
{
if (!$this->checkGenericClassInNonGenericObjectType) {
return [];
}
$objectTypes = [];
TypeTraverser::map($type, static function (Type $type, callable $traverse) use (&$objectTypes): Type {
if ($type instanceof GenericObjectType) {
return $type;
}
if ($type instanceof ObjectType) {
$classReflection = $type->getClassReflection();
if ($classReflection === null) {
return $type;
}
if (in_array($classReflection->getName(), [
\Traversable::class,
\Iterator::class,
\IteratorAggregate::class,
\Generator::class,
], true)) {
// checked by getIterableTypesWithMissingValueTypehint() already
return $type;
}
if ($classReflection->isTrait()) {
return $type;
}
if (!$classReflection->isGeneric()) {
return $type;
}
$resolvedType = TemplateTypeHelper::resolveToBounds($type);
if (!$resolvedType instanceof ObjectType) {
throw new \PHPStan\ShouldNotHappenException();
}
$objectTypes[] = [
sprintf('%s %s', $classReflection->isInterface() ? 'interface' : 'class', $classReflection->getDisplayName(false)),
array_keys($classReflection->getTemplateTypeMap()->getTypes()),
];
return $type;
}
$traverse($type);
return $type;
});
return $objectTypes;
}
}