|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Gskema\TypeSniff\Sniffs\CodeElement; |
|
4
|
|
|
|
|
5
|
|
|
use Gskema\TypeSniff\Core\Type\Common\UndefinedType; |
|
6
|
|
|
use Gskema\TypeSniff\Core\Type\DocBlock\TypedArrayType; |
|
7
|
|
|
use Gskema\TypeSniff\Core\Type\TypeComparator; |
|
8
|
|
|
use Gskema\TypeSniff\Core\Type\TypeHelper; |
|
9
|
|
|
use PHP_CodeSniffer\Files\File; |
|
10
|
|
|
use Gskema\TypeSniff\Core\CodeElement\Element\AbstractFqcnConstElement; |
|
11
|
|
|
use Gskema\TypeSniff\Core\CodeElement\Element\ClassConstElement; |
|
12
|
|
|
use Gskema\TypeSniff\Core\CodeElement\Element\CodeElementInterface; |
|
13
|
|
|
use Gskema\TypeSniff\Core\CodeElement\Element\InterfaceConstElement; |
|
14
|
|
|
use Gskema\TypeSniff\Core\DocBlock\Tag\VarTag; |
|
15
|
|
|
use Gskema\TypeSniff\Core\Type\Common\ArrayType; |
|
16
|
|
|
|
|
17
|
|
|
class FqcnConstSniff implements CodeElementSniffInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @inheritDoc |
|
21
|
|
|
*/ |
|
22
|
6 |
|
public function configure(array $config): void |
|
23
|
|
|
{ |
|
24
|
|
|
// nothing to do |
|
25
|
6 |
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @inheritDoc |
|
29
|
|
|
*/ |
|
30
|
6 |
|
public function register(): array |
|
31
|
|
|
{ |
|
32
|
|
|
return [ |
|
33
|
6 |
|
ClassConstElement::class, |
|
34
|
|
|
InterfaceConstElement::class, |
|
35
|
|
|
]; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @inheritDoc |
|
40
|
|
|
* |
|
41
|
|
|
* @param AbstractFqcnConstElement $const |
|
42
|
|
|
*/ |
|
43
|
1 |
|
public function process(File $file, CodeElementInterface $const): void |
|
44
|
|
|
{ |
|
45
|
1 |
|
$docBlock = $const->getDocBlock(); |
|
46
|
|
|
|
|
47
|
|
|
/** @var VarTag|null $varTag */ |
|
48
|
1 |
|
$varTag = $docBlock->getTagsByName('var')[0] ?? null; |
|
49
|
1 |
|
$docType = $varTag ? $varTag->getType() : null; |
|
50
|
1 |
|
$docTypeLine = $varTag ? $varTag->getLine() : $const->getLine(); |
|
51
|
1 |
|
$valueType = $const->getValueType(); |
|
|
|
|
|
|
52
|
|
|
|
|
53
|
1 |
|
$warnings = []; |
|
54
|
1 |
|
if (TypeHelper::containsType($docType, ArrayType::class)) { |
|
55
|
1 |
|
$warnings[$docTypeLine][] = 'Replace array type with typed array type in PHPDoc for :subject:. Use mixed[] for generic arrays. Correct array depth must be specified.'; |
|
56
|
1 |
|
} elseif (is_a($valueType, ArrayType::class) |
|
57
|
1 |
|
&& !TypeHelper::containsType($docType, TypedArrayType::class) |
|
58
|
|
|
) { |
|
59
|
1 |
|
$warnings[$docTypeLine][] = 'Add PHPDoc with typed array type hint for :subject:. Use mixed[] for generic arrays. Correct array depth must be specified.'; |
|
60
|
1 |
|
} elseif ($fakeType = TypeHelper::getFakeTypedArrayType($docType)) { |
|
61
|
1 |
|
$warnings[$docTypeLine][] = sprintf( |
|
62
|
1 |
|
'Use a more specific type in typed array hint "%s" for :subject:. Correct array depth must be specified.', |
|
63
|
1 |
|
$fakeType->toString() |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
1 |
|
if ($redundantTypes = TypeComparator::getRedundantDocTypes($docType)) { |
|
68
|
1 |
|
$warnings[$docTypeLine][] = sprintf('Remove redundant :subject: type hints "%s"', TypeHelper::listRawTypes($redundantTypes)); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
1 |
|
if ($docType && $valueType) { |
|
72
|
1 |
|
[$wrongDocTypes, $missingDocTypes] = TypeComparator::compare($docType, new UndefinedType(), $valueType); |
|
73
|
|
|
|
|
74
|
1 |
|
if ($wrongDocTypes) { |
|
75
|
1 |
|
$warnings[$docTypeLine][] = sprintf( |
|
76
|
1 |
|
'Type %s "%s" %s not compatible with :subject: value type', |
|
77
|
1 |
|
isset($wrongDocTypes[1]) ? 'hints' : 'hint', |
|
78
|
1 |
|
TypeHelper::listRawTypes($wrongDocTypes), |
|
79
|
1 |
|
isset($wrongDocTypes[1]) ? 'are' : 'is' |
|
80
|
|
|
); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
1 |
|
if ($missingDocTypes) { |
|
84
|
1 |
|
$warnings[$docTypeLine][] = sprintf( |
|
85
|
1 |
|
'Missing "%s" %s in :subject: type hint', |
|
86
|
1 |
|
TypeHelper::listRawTypes($missingDocTypes), |
|
87
|
1 |
|
isset($missingDocTypes[1]) ? 'types' : 'type' |
|
88
|
|
|
); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
1 |
|
$subject = $const->getConstName().' constant'; |
|
93
|
1 |
|
foreach ($warnings as $line => $lineWarnings) { |
|
94
|
1 |
|
foreach ($lineWarnings as $warningTpl) { |
|
95
|
1 |
|
$warning = str_replace(':subject:', $subject, $warningTpl); |
|
96
|
1 |
|
$file->addWarningOnLine($warning, $line, 'FqcnConstSniff'); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
1 |
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|