1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gskema\TypeSniff\Sniffs\CodeElement; |
4
|
|
|
|
5
|
|
|
use Gskema\TypeSniff\Core\DocBlock\Tag\VarTag; |
6
|
|
|
use Gskema\TypeSniff\Inspection\DocTypeInspector; |
7
|
|
|
use Gskema\TypeSniff\Inspection\Subject\ConstTypeSubject; |
8
|
|
|
use PHP_CodeSniffer\Files\File; |
9
|
|
|
use Gskema\TypeSniff\Core\CodeElement\Element\AbstractFqcnConstElement; |
10
|
|
|
use Gskema\TypeSniff\Core\CodeElement\Element\ClassConstElement; |
11
|
|
|
use Gskema\TypeSniff\Core\CodeElement\Element\CodeElementInterface; |
12
|
|
|
use Gskema\TypeSniff\Core\CodeElement\Element\InterfaceConstElement; |
13
|
|
|
|
14
|
|
|
class FqcnConstSniff implements CodeElementSniffInterface |
15
|
|
|
{ |
16
|
|
|
protected const CODE = 'FqcnConstSniff'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @inheritDoc |
20
|
|
|
*/ |
21
|
6 |
|
public function configure(array $config): void |
22
|
|
|
{ |
23
|
|
|
// nothing to do |
24
|
6 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @inheritDoc |
28
|
|
|
*/ |
29
|
6 |
|
public function register(): array |
30
|
|
|
{ |
31
|
|
|
return [ |
32
|
6 |
|
ClassConstElement::class, |
33
|
|
|
InterfaceConstElement::class, |
34
|
|
|
]; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @inheritDoc |
39
|
|
|
* |
40
|
|
|
* @param AbstractFqcnConstElement $const |
41
|
|
|
*/ |
42
|
2 |
|
public function process(File $file, CodeElementInterface $const): void |
43
|
|
|
{ |
44
|
2 |
|
$subject = ConstTypeSubject::fromElement($const); |
45
|
|
|
|
46
|
2 |
|
DocTypeInspector::reportMandatoryTypes($subject); |
47
|
2 |
|
DocTypeInspector::reportReplaceableTypes($subject); |
48
|
|
|
|
49
|
2 |
|
DocTypeInspector::reportRemovableTypes($subject); |
50
|
2 |
|
DocTypeInspector::reportMissingOrWrongTypes($subject); |
51
|
|
|
|
52
|
2 |
|
static::reportUselessDocBlock($subject); |
53
|
|
|
|
54
|
2 |
|
$subject->writeWarningsTo($file, static::CODE); |
55
|
2 |
|
} |
56
|
|
|
|
57
|
2 |
|
protected static function reportUselessDocBlock(ConstTypeSubject $subject): void |
58
|
|
|
{ |
59
|
2 |
|
if (!$subject->hasDefinedDocBlock()) { |
60
|
1 |
|
return; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** @var VarTag|null $varTag */ |
64
|
2 |
|
$varTag = $subject->getDocBlock()->getTagsByName('var')[0] ?? null; |
65
|
|
|
|
66
|
2 |
|
$isUseful = $subject->getDocBlock()->hasDescription() |
67
|
2 |
|
|| ($varTag && $varTag->hasDescription()) |
68
|
2 |
|
|| ($varTag && $varTag->getType() != $subject->getValueType()); |
69
|
|
|
|
70
|
2 |
|
if (!$isUseful) { |
71
|
1 |
|
$subject->addFnTypeWarning('Useless PHPDoc'); |
72
|
|
|
} |
73
|
2 |
|
} |
74
|
|
|
} |
75
|
|
|
|