|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Gskema\TypeSniff\Sniffs\CodeElement; |
|
4
|
|
|
|
|
5
|
|
|
use Gskema\TypeSniff\Core\Type\DocBlock\TypedArrayType; |
|
6
|
|
|
use PHP_CodeSniffer\Files\File; |
|
7
|
|
|
use Gskema\TypeSniff\Core\CodeElement\Element\AbstractFqcnPropElement; |
|
8
|
|
|
use Gskema\TypeSniff\Core\CodeElement\Element\ClassPropElement; |
|
9
|
|
|
use Gskema\TypeSniff\Core\CodeElement\Element\CodeElementInterface; |
|
10
|
|
|
use Gskema\TypeSniff\Core\CodeElement\Element\TraitPropElement; |
|
11
|
|
|
use Gskema\TypeSniff\Core\DocBlock\Tag\VarTag; |
|
12
|
|
|
use Gskema\TypeSniff\Core\DocBlock\UndefinedDocBlock; |
|
13
|
|
|
use Gskema\TypeSniff\Core\Type\Common\ArrayType; |
|
14
|
|
|
use Gskema\TypeSniff\Core\Type\Common\UndefinedType; |
|
15
|
|
|
use Gskema\TypeSniff\Core\Type\DocBlock\CompoundType; |
|
16
|
|
|
|
|
17
|
|
|
class FqcnPropSniff implements CodeElementSniffInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @inheritDoc |
|
21
|
|
|
*/ |
|
22
|
2 |
|
public function configure(array $config): void |
|
23
|
|
|
{ |
|
24
|
|
|
// nothing to do |
|
25
|
2 |
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @inheritDoc |
|
29
|
|
|
*/ |
|
30
|
2 |
|
public function register(): array |
|
31
|
|
|
{ |
|
32
|
|
|
return [ |
|
33
|
2 |
|
ClassPropElement::class, |
|
34
|
|
|
TraitPropElement::class, |
|
35
|
|
|
]; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @inheritDoc |
|
40
|
|
|
* @param AbstractFqcnPropElement $prop |
|
41
|
|
|
*/ |
|
42
|
1 |
|
public function process(File $file, CodeElementInterface $prop): void |
|
43
|
|
|
{ |
|
44
|
|
|
// @TODO Infer type from initial value? |
|
45
|
1 |
|
$docBlock = $prop->getDocBlock(); |
|
46
|
|
|
|
|
47
|
|
|
/** @var VarTag|null $varTag */ |
|
48
|
1 |
|
$varTag = $docBlock->getTagsByName('var')[0] ?? null; |
|
49
|
1 |
|
$docType = $varTag ? $varTag->getType() : null; |
|
50
|
|
|
|
|
51
|
1 |
|
$subject = 'property $'.$prop->getPropName(); |
|
|
|
|
|
|
52
|
|
|
|
|
53
|
1 |
|
if ($docBlock instanceof UndefinedDocBlock) { |
|
54
|
1 |
|
$file->addWarningOnLine( |
|
55
|
1 |
|
'Add PHPDoc for '.$subject, |
|
56
|
1 |
|
$prop->getLine(), |
|
57
|
1 |
|
'FqcnPropSniff' |
|
58
|
|
|
); |
|
59
|
1 |
|
} elseif (null === $varTag) { |
|
60
|
1 |
|
$file->addWarningOnLine( |
|
61
|
1 |
|
'Add @var tag for '.$subject, |
|
62
|
1 |
|
$prop->getLine(), |
|
63
|
1 |
|
'FqcnPropSniff' |
|
64
|
|
|
); |
|
65
|
1 |
|
} elseif ($docType instanceof UndefinedType) { |
|
66
|
1 |
|
$file->addWarningOnLine( |
|
67
|
1 |
|
'Add type hint to @var tag for '.$subject, |
|
68
|
1 |
|
$prop->getLine(), |
|
69
|
1 |
|
'FqcnPropSniff' |
|
70
|
|
|
); |
|
71
|
1 |
|
} elseif ($docType instanceof ArrayType |
|
72
|
1 |
|
|| ($docType instanceof CompoundType && $docType->containsType(ArrayType::class)) |
|
73
|
|
|
) { |
|
74
|
1 |
|
$file->addWarningOnLine( |
|
75
|
1 |
|
'Replace array type with typed array type in PHPDoc for '.$subject.'. Use mixed[] for generic arrays.', |
|
76
|
1 |
|
$prop->getLine(), |
|
77
|
1 |
|
'FqcnPropSniff' |
|
78
|
|
|
); |
|
79
|
1 |
|
} elseif (is_a($prop->getDefaultValueType(), ArrayType::class) |
|
|
|
|
|
|
80
|
1 |
|
&& !is_a($docType, TypedArrayType::class) |
|
81
|
|
|
) { |
|
82
|
1 |
|
$file->addWarningOnLine( |
|
83
|
1 |
|
'Add PHPDoc with typed array type hint for '.$subject.'. Use mixed[] for generic arrays.', |
|
84
|
1 |
|
$prop->getLine(), |
|
85
|
1 |
|
'FqcnPropSniff' |
|
86
|
|
|
); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
1 |
|
if ($varTag && null !== $varTag->getParamName()) { |
|
90
|
1 |
|
$file->addWarningOnLine( |
|
91
|
1 |
|
'Remove property name $'.$varTag->getParamName().' from @var tag', |
|
92
|
1 |
|
$prop->getLine(), |
|
93
|
1 |
|
'FqcnPropSniff' |
|
94
|
|
|
); |
|
95
|
|
|
} |
|
96
|
1 |
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|