Passed
Push — master ( 17dafe...3e29f4 )
by Gytis
02:42 queued 11s
created

FqcnConstSniff::process()   F

Complexity

Conditions 17
Paths 480

Size

Total Lines 54
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 36
CRAP Score 17

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 17
eloc 35
c 2
b 0
f 1
nc 480
nop 2
dl 0
loc 54
ccs 36
cts 36
cp 1
crap 17
rs 1.7722

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Gskema\TypeSniff\Sniffs\CodeElement;
4
5
use Gskema\TypeSniff\Core\Type\Common\UndefinedType;
6
use Gskema\TypeSniff\Core\Type\DocBlock\TypedArrayType;
7
use Gskema\TypeSniff\Core\Type\TypeComparator;
8
use Gskema\TypeSniff\Core\Type\TypeHelper;
9
use PHP_CodeSniffer\Files\File;
10
use Gskema\TypeSniff\Core\CodeElement\Element\AbstractFqcnConstElement;
11
use Gskema\TypeSniff\Core\CodeElement\Element\ClassConstElement;
12
use Gskema\TypeSniff\Core\CodeElement\Element\CodeElementInterface;
13
use Gskema\TypeSniff\Core\CodeElement\Element\InterfaceConstElement;
14
use Gskema\TypeSniff\Core\DocBlock\Tag\VarTag;
15
use Gskema\TypeSniff\Core\Type\Common\ArrayType;
16
17
class FqcnConstSniff implements CodeElementSniffInterface
18
{
19
    /**
20
     * @inheritDoc
21
     */
22 6
    public function configure(array $config): void
23
    {
24
        // nothing to do
25 6
    }
26
27
    /**
28
     * @inheritDoc
29
     */
30 6
    public function register(): array
31
    {
32
        return [
33 6
            ClassConstElement::class,
34
            InterfaceConstElement::class,
35
        ];
36
    }
37
38
    /**
39
     * @inheritDoc
40
     *
41
     * @param AbstractFqcnConstElement $const
42
     */
43 1
    public function process(File $file, CodeElementInterface $const): void
44
    {
45 1
        $docBlock = $const->getDocBlock();
46
47
        /** @var VarTag|null $varTag */
48 1
        $varTag = $docBlock->getTagsByName('var')[0] ?? null;
49 1
        $docType = $varTag ? $varTag->getType() : null;
50 1
        $docTypeLine = $varTag ? $varTag->getLine() : $const->getLine();
51 1
        $valueType = $const->getValueType();
0 ignored issues
show
Bug introduced by
The method getValueType() 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...nt\Element\ConstElement or Gskema\TypeSniff\Core\Co...bstractFqcnConstElement. ( Ignorable by Annotation )

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

51
        /** @scrutinizer ignore-call */ 
52
        $valueType = $const->getValueType();
Loading history...
52
53 1
        $warnings = [];
54 1
        if (TypeHelper::containsType($docType, ArrayType::class)) {
55 1
            $warnings[$docTypeLine][] = 'Replace array type with typed array type in PHPDoc for :subject:. Use mixed[] for generic arrays. Correct array depth must be specified.';
56 1
        } elseif (is_a($valueType, ArrayType::class)
57 1
              && !TypeHelper::containsType($docType, TypedArrayType::class)
58
        ) {
59 1
            $warnings[$docTypeLine][] = 'Add PHPDoc with typed array type hint for :subject:. Use mixed[] for generic arrays. Correct array depth must be specified.';
60 1
        } elseif ($fakeType = TypeHelper::getFakeTypedArrayType($docType)) {
61 1
            $warnings[$docTypeLine][] = sprintf(
62 1
                'Use a more specific type in typed array hint "%s" for :subject:. Correct array depth must be specified.',
63 1
                $fakeType->toString()
64
            );
65
        }
66
67 1
        if ($redundantTypes = TypeComparator::getRedundantDocTypes($docType)) {
68 1
            $warnings[$docTypeLine][] = sprintf('Remove redundant :subject: type hints "%s"', TypeHelper::listRawTypes($redundantTypes));
69
        }
70
71 1
        if ($docType && $valueType) {
72 1
            [$wrongDocTypes, $missingDocTypes] = TypeComparator::compare($docType, new UndefinedType(), $valueType);
73
74 1
            if ($wrongDocTypes) {
75 1
                $warnings[$docTypeLine][] = sprintf(
76 1
                    'Type %s "%s" %s not compatible with :subject: value type',
77 1
                    isset($wrongDocTypes[1]) ? 'hints' : 'hint',
78 1
                    TypeHelper::listRawTypes($wrongDocTypes),
79 1
                    isset($wrongDocTypes[1]) ? 'are' : 'is'
80
                );
81
            }
82
83 1
            if ($missingDocTypes) {
84 1
                $warnings[$docTypeLine][] = sprintf(
85 1
                    'Missing "%s" %s in :subject: type hint',
86 1
                    TypeHelper::listRawTypes($missingDocTypes),
87 1
                    isset($missingDocTypes[1]) ? 'types' : 'type'
88
                );
89
            }
90
        }
91
92 1
        $subject = $const->getConstName().' constant';
93 1
        foreach ($warnings as $line => $lineWarnings) {
94 1
            foreach ($lineWarnings as $warningTpl) {
95 1
                $warning = str_replace(':subject:', $subject, $warningTpl);
96 1
                $file->addWarningOnLine($warning, $line, 'FqcnConstSniff');
97
            }
98
        }
99 1
    }
100
}
101