Completed
Pull Request — master (#283)
by Oskar
02:45
created

PackageCollectingVisitor::leaveNode()   C

Complexity

Conditions 8
Paths 33

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 17
nc 33
nop 1
dl 0
loc 27
rs 5.3846
c 0
b 0
f 0
1
<?php
2
3
namespace Hal\Metric\Package;
4
5
use Hal\Metric\Metrics;
6
use Hal\Metric\PackageMetric;
7
use PhpParser\Node;
8
use PhpParser\Node\Stmt\Class_;
9
use PhpParser\Node\Stmt\Interface_;
10
use PhpParser\Node\Stmt\Namespace_;
11
use PhpParser\NodeVisitorAbstract;
12
13
class PackageCollectingVisitor extends NodeVisitorAbstract
14
{
15
    /** @var string */
16
    private $namespace = '';
17
18
    /** @var Metrics */
19
    private $metrics;
20
21
    public function __construct(Metrics $metrics)
22
    {
23
        $this->metrics = $metrics;
24
    }
25
26
    public function enterNode(Node $node)
27
    {
28
        if ($node instanceof Namespace_) {
29
            $this->namespace = (string) $node->name;
30
        }
31
    }
32
33
    public function leaveNode(Node $node)
34
    {
35
        if ($node instanceof Class_ || $node instanceof Interface_) {
36
            $package = $this->namespace;
37
38
            $docComment = $node->getDocComment();
39
            $docBlockText = $docComment ? $docComment->getText() : '';
40
            if (preg_match('/^\s*\* @package (.*)/m', $docBlockText, $matches)) {
41
                $package = $matches[1];
42
            }
43
            if (preg_match('/^\s*\* @subpackage (.*)/m', $docBlockText, $matches)) {
44
                $package = $package . '\\' . $matches[1];
45
            }
46
47
            $packageName = $package . '\\';
48
            if (! $packageMetric = $this->metrics->get($packageName)) {
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $packageMetric is correct as $this->metrics->get($packageName) (which targets Hal\Metric\Metrics::get()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
49
                $packageMetric = new PackageMetric($packageName);
50
                $this->metrics->attach($packageMetric);
51
            }
52
            /* @var PackageMetric $packageMetric */
53
            $elementName = isset($node->namespacedName) ? $node->namespacedName : 'anonymous@'.spl_object_hash($node);
54
            $elementName = (string) $elementName;
55
            $packageMetric->addClass((string) $elementName);
56
57
            $this->metrics->get($elementName)->set('package', $packageName);
0 ignored issues
show
Bug introduced by
The method set cannot be called on $this->metrics->get($elementName) (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
58
        }
59
    }
60
}
61