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