Passed
Push — main ( 30dfbd...b8dd52 )
by mikhail
03:24
created

MissingGenericVisitor::analyzeExpression()   C

Complexity

Conditions 16
Paths 13

Size

Total Lines 26
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 23.2134

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 16
eloc 22
c 1
b 1
f 0
nc 13
nop 1
dl 0
loc 26
ccs 16
cts 23
cp 0.6957
crap 23.2134
rs 5.5666

How to fix   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
declare(strict_types=1);
4
5
namespace SavinMikhail\CommentsDensity\MissingDocblock;
6
7
use PhpParser\Node;
8
use PhpParser\Node\Expr\Array_;
9
use PhpParser\Node\Expr\Cast;
10
use PhpParser\Node\Expr\ConstFetch;
11
use PhpParser\Node\Expr\Instanceof_;
12
use PhpParser\Node\Expr\New_;
13
use PhpParser\Node\Expr\Variable;
14
use PhpParser\Node\Expr\FuncCall;
15
use PhpParser\Node\Expr\StaticCall;
16
use PhpParser\Node\Expr\Yield_;
17
use PhpParser\Node\Expr\YieldFrom;
18
use PhpParser\Node\Expr\MethodCall;
19
use PhpParser\Node\Expr\StaticPropertyFetch;
20
use PhpParser\Node\Name;
21
use PhpParser\Node\Stmt\Return_;
22
use PhpParser\NodeVisitorAbstract;
23
use PhpParser\Node\Expr\Assign;
24
25
use function array_unique;
26
27
class MissingGenericVisitor extends NodeVisitorAbstract
28
{
29
    public bool $hasConsistentTypes = false;
30
    public array $elementTypes = [];
31
32 13
    public function enterNode(Node $node): void
33
    {
34 13
        if ($node instanceof Return_) {
35 12
            $this->analyzeExpression($node->expr);
36
        }
37 13
        if ($node instanceof Yield_ || $node instanceof YieldFrom) {
38 1
            $this->analyzeExpression($node->value);
0 ignored issues
show
Bug introduced by
The property value does not seem to exist on PhpParser\Node\Expr\YieldFrom.
Loading history...
39
        }
40
    }
41
42 13
    private function analyzeExpression($expr): void
43
    {
44 13
        if ($expr instanceof Array_) {
45 7
            foreach ($expr->items as $item) {
46 6
                $this->analyzeExpression($item->value);
47
            }
48 12
        } elseif ($expr instanceof New_) {
49 8
            if ($expr->class instanceof Name) {
50 8
                $this->elementTypes[] = $expr->class->toString();
0 ignored issues
show
Bug introduced by
The method toString() does not exist on PhpParser\Node. It seems like you code against a sub-type of PhpParser\Node such as PhpParser\Node\Name or PhpParser\Node\Identifier. ( Ignorable by Annotation )

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

50
                /** @scrutinizer ignore-call */ 
51
                $this->elementTypes[] = $expr->class->toString();
Loading history...
51
            }
52 4
        } elseif ($expr instanceof Variable) {
53 2
            $this->elementTypes[] = 'variable';
54 2
        } elseif ($expr instanceof FuncCall || $expr instanceof StaticCall || $expr instanceof MethodCall) {
55
            $this->elementTypes[] = 'function_call';
56 2
        } elseif ($expr instanceof Cast) {
57
            $this->analyzeExpression($expr->expr);
58 2
        } elseif ($expr instanceof Yield_ || $expr instanceof YieldFrom) {
59
            $this->analyzeExpression($expr->value);
0 ignored issues
show
Bug introduced by
The property value does not seem to exist on PhpParser\Node\Expr\YieldFrom.
Loading history...
60 2
        } elseif ($expr instanceof StaticPropertyFetch) {
61
            $this->elementTypes[] = 'static_property';
62 2
        } elseif ($expr instanceof Assign) {
63
            $this->analyzeExpression($expr->expr);
64 2
        } elseif ($expr instanceof Instanceof_) {
65
            $this->analyzeExpression($expr->expr);
66 2
        } elseif ($expr instanceof ConstFetch) {
67
            $this->elementTypes[] = 'constant';
68
        }
69
    }
70
71 13
    public function leaveNode(Node $node): void
72
    {
73 13
        if (!empty($this->elementTypes)) {
74 10
            $this->hasConsistentTypes = count(array_unique($this->elementTypes)) === 1;
75
        }
76
    }
77
}
78