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