Test Failed
Push — main ( 46eee6...5832e5 )
by mikhail
03:28
created

MethodRegistrar   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 16
c 1
b 0
f 0
dl 0
loc 33
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B registerClassMethod() 0 18 8
A __construct() 0 2 1
A getVariableTypes() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SavinMikhail\CommentsDensity\MissingDocblock\Visitors;
6
7
use PhpParser\Node\Stmt\ClassMethod;
8
use PhpParser\Node\Stmt\Class_;
9
use PhpParser\Node\Expr\Assign;
10
use PhpParser\Node\Expr\New_;
11
use PhpParser\Node\Expr\Variable;
12
use PhpParser\Node\Stmt\Expression;
13
14
final class MethodRegistrar
15
{
16
    private array $variableTypes = [];
17
18
    public function __construct(private readonly ?Class_ $class)
19
    {
20
    }
21
22
    public function registerClassMethod(ClassMethod $node): void
23
    {
24
        if ($this->class) {
25
            $className = $this->class->namespacedName->toString();
0 ignored issues
show
Bug introduced by
The method toString() does not exist on null. ( Ignorable by Annotation )

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

25
            /** @scrutinizer ignore-call */ 
26
            $className = $this->class->namespacedName->toString();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
26
            $this->variableTypes['this'] = $className;
27
        }
28
        $stmts = $node->getStmts();
29
        if (!$stmts) {
30
            return;
31
        }
32
        foreach ($stmts as $stmt) {
33
            if (!($stmt instanceof Expression && $stmt->expr instanceof Assign)) {
34
                continue;
35
            }
36
            $var = $stmt->expr->var;
37
            $expr = $stmt->expr->expr;
38
            if ($var instanceof Variable && $expr instanceof New_) {
39
                $this->variableTypes[$var->name] = $expr->class->name;
0 ignored issues
show
Bug introduced by
Accessing name on the interface PhpParser\Node suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
40
            }
41
        }
42
    }
43
44
    public function getVariableTypes(): array
45
    {
46
        return $this->variableTypes;
47
    }
48
}
49