Passed
Push — main ( 87f70c...0bc0e4 )
by mikhail
06:42 queued 02:51
created

MethodRegistrar   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
eloc 16
dl 0
loc 33
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B registerClassMethod() 0 18 8
A getVariableTypes() 0 3 1
A __construct() 0 2 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SavinMikhail\CommentsDensity\MissingDocblock\Visitors\Checkers;
6
7
use PhpParser\Node\Expr\Assign;
8
use PhpParser\Node\Expr\New_;
9
use PhpParser\Node\Expr\Variable;
10
use PhpParser\Node\Stmt\Class_;
11
use PhpParser\Node\Stmt\ClassMethod;
12
use PhpParser\Node\Stmt\Expression;
13
14
final class MethodRegistrar
15
{
16
    private array $variableTypes = [];
17
18 41
    public function __construct(private readonly ?Class_ $class)
19
    {
20 41
    }
21
22 34
    public function registerClassMethod(ClassMethod $node): void
23
    {
24 34
        if ($this->class) {
25 34
            $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 34
            $this->variableTypes['this'] = $className;
27
        }
28 34
        $stmts = $node->getStmts();
29 34
        if (!$stmts) {
30 4
            return;
31
        }
32 30
        foreach ($stmts as $stmt) {
33 30
            if (!($stmt instanceof Expression && $stmt->expr instanceof Assign)) {
34 30
                continue;
35
            }
36 6
            $var = $stmt->expr->var;
37 6
            $expr = $stmt->expr->expr;
38 6
            if ($var instanceof Variable && $expr instanceof New_) {
39 6
                $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 7
    public function getVariableTypes(): array
45
    {
46 7
        return $this->variableTypes;
47
    }
48
}
49