Completed
Pull Request — master (#283)
by Oskar
04:36
created

PackageCollectingVisitor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
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\Node\Stmt\Trait_;
12
use PhpParser\NodeVisitorAbstract;
13
14
class PackageCollectingVisitor extends NodeVisitorAbstract
15
{
16
    /** @var string */
17
    private $namespace = '';
18
19
    /** @var Metrics */
20
    private $metrics;
21
22
    public function __construct(Metrics $metrics)
23
    {
24
        $this->metrics = $metrics;
25
    }
26
27
    public function enterNode(Node $node)
28
    {
29
        if ($node instanceof Namespace_) {
30
            $this->namespace = (string) $node->name;
31
        }
32
    }
33
34
    public function leaveNode(Node $node)
35
    {
36
        if ($node instanceof Class_ || $node instanceof Interface_) {
37
            $package = $this->namespace;
38
39
            $docComment = $node->getDocComment();
40
            $docBlockText = $docComment ? $docComment->getText() : '';
41
            if (preg_match('/^\s*\* @package (.*)/m', $docBlockText, $matches)) {
42
                $package = $matches[1];
43
            }
44
            if (preg_match('/^\s*\* @subpackage (.*)/m', $docBlockText, $matches)) {
45
                $package = $package . '\\' . $matches[1];
46
            }
47
48
            $packageName = $package . '\\';
49
            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...
50
                $packageMetric = new PackageMetric($packageName);
51
                $this->metrics->attach($packageMetric);
52
            }
53
            /* @var PackageMetric $packageMetric */
54
            $elementName = (string) (isset($node->namespacedName) ? $node->namespacedName : 'anonymous@'.spl_object_hash($node));
55
            $packageMetric->addClass($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