Passed
Push — main ( 5aeeb8...5d6527 )
by mikhail
03:24
created

MissingGenericVisitor   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 80%

Importance

Changes 7
Bugs 4 Features 0
Metric Value
wmc 22
eloc 34
c 7
b 4
f 0
dl 0
loc 55
ccs 28
cts 35
cp 0.8
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
C analyzeExpression() 0 26 16
A enterNode() 0 12 4
A leaveNode() 0 7 2
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): null
33
    {
34 13
        if ($node instanceof Return_) {
35 12
            $this->analyzeExpression($node->expr);
36 12
            return null;
37
        }
38 13
        if ($node instanceof Yield_ || $node instanceof YieldFrom) {
39 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...
40 1
            return null;
41
        }
42
43 13
        return null;
44
    }
45
46 13
    private function analyzeExpression($expr): void
47
    {
48 13
        if ($expr instanceof Array_) {
49 7
            foreach ($expr->items as $item) {
50 6
                $this->analyzeExpression($item->value);
51
            }
52 12
        } elseif ($expr instanceof New_) {
53 8
            if ($expr->class instanceof Name) {
54 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

54
                /** @scrutinizer ignore-call */ 
55
                $this->elementTypes[] = $expr->class->toString();
Loading history...
55
            }
56 4
        } elseif ($expr instanceof Variable) {
57 2
            $this->elementTypes[] = 'variable';
58 2
        } elseif ($expr instanceof FuncCall || $expr instanceof StaticCall || $expr instanceof MethodCall) {
59
            $this->elementTypes[] = 'function_call';
60 2
        } elseif ($expr instanceof Cast) {
61
            $this->analyzeExpression($expr->expr);
62 2
        } elseif ($expr instanceof Yield_ || $expr instanceof YieldFrom) {
63
            $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...
64 2
        } elseif ($expr instanceof StaticPropertyFetch) {
65
            $this->elementTypes[] = 'static_property';
66 2
        } elseif ($expr instanceof Assign) {
67
            $this->analyzeExpression($expr->expr);
68 2
        } elseif ($expr instanceof Instanceof_) {
69
            $this->analyzeExpression($expr->expr);
70 2
        } elseif ($expr instanceof ConstFetch) {
71
            $this->elementTypes[] = 'constant';
72
        }
73
    }
74
75 13
    public function leaveNode(Node $node): null
76
    {
77 13
        if (!empty($this->elementTypes)) {
78 10
            $this->hasConsistentTypes = count(array_unique($this->elementTypes)) === 1;
79
        }
80
81 13
        return null;
82
    }
83
}
84