Test Failed
Push — main ( 948518...e81ed5 )
by mikhail
03:09
created

MissingGenericVisitor::leaveNode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 4
rs 10
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\New_;
10
use PhpParser\Node\Expr\Variable;
11
use PhpParser\NodeVisitorAbstract;
12
13
use function array_unique;
14
15
class MissingGenericVisitor extends NodeVisitorAbstract {
16
    public bool $hasConsistentTypes = false;
17
    public array $elementTypes = [];
18
19
    public function enterNode(Node $node): void
20
    {
21
        if ($node instanceof Node\Stmt\Return_ && $node->expr instanceof Array_) {
22
            foreach ($node->expr->items as $item) {
23
                if ($item->value instanceof New_) {
24
                    $this->elementTypes[] = $item->value->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

24
                    /** @scrutinizer ignore-call */ 
25
                    $this->elementTypes[] = $item->value->class->toString();
Loading history...
25
                } elseif ($item->value instanceof Variable) {
26
                    // Here you can add logic to infer variable types if necessary
27
                    $this->elementTypes[] = 'variable';
28
                }
29
            }
30
        }
31
    }
32
33
    public function leaveNode(Node $node): void
34
    {
35
        if (!empty($this->elementTypes)) {
36
            $this->hasConsistentTypes = count(array_unique($this->elementTypes)) === 1;
37
        }
38
    }
39
}