Passed
Push — master ( 47e701...44fa06 )
by Gytis
03:06
created

FqcnPropSniff::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 1
dl 0
loc 2
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
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();
1 ignored issue
show
Bug introduced by
The method getPropName() does not exist on Gskema\TypeSniff\Core\Co...nt\CodeElementInterface. It seems like you code against a sub-type of Gskema\TypeSniff\Core\Co...nt\CodeElementInterface such as Gskema\TypeSniff\Core\Co...AbstractFqcnPropElement. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

51
        $subject = 'property $'.$prop->/** @scrutinizer ignore-call */ getPropName();
Loading history...
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)
1 ignored issue
show
Bug introduced by
The method getDefaultValueType() does not exist on Gskema\TypeSniff\Core\Co...nt\CodeElementInterface. It seems like you code against a sub-type of Gskema\TypeSniff\Core\Co...nt\CodeElementInterface such as Gskema\TypeSniff\Core\Co...AbstractFqcnPropElement. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

79
        } elseif (is_a($prop->/** @scrutinizer ignore-call */ getDefaultValueType(), ArrayType::class)
Loading history...
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