Completed
Push — master ( 368282...c4d7dc )
by Gytis
03:56
created

FqcnConstSniff::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 1
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
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