Completed
Pull Request — master (#299)
by Atlas
03:31
created

KanDefectVisitor   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C leaveNode() 0 31 8
1
<?php
2
namespace Hal\Metric\Class_\Complexity;
3
4
use Hal\Component\Reflected\Method;
5
use Hal\Metric\Helper\MetricClassNameGenerator;
6
use Hal\Metric\Metrics;
7
use PhpParser\Node;
8
use PhpParser\Node\Stmt;
9
use PhpParser\NodeVisitorAbstract;
10
11
/**
12
 * Calculate Kan's defects
13
 *
14
 * defects = 0.15 + 0.23 *  number of do…while() + 0.22 *  number of select() + 0.07 * number of if()
15
 */
16
class KanDefectVisitor extends NodeVisitorAbstract
17
{
18
    /**
19
     * @var Metrics
20
     */
21
    private $metrics;
22
23
    /**
24
     * ClassEnumVisitor constructor.
25
     * @param Metrics $metrics
26
     */
27
    public function __construct(Metrics $metrics)
28
    {
29
        $this->metrics = $metrics;
30
    }
31
32
    /**
33
     * @inheritdoc
34
     */
35
    public function leaveNode(Node $node)
36
    {
37
        if ($node instanceof Stmt\Class_
38
            || $node instanceof Stmt\Interface_
39
        ) {
40
41
            $class = $this->metrics->get(MetricClassNameGenerator::getName($node));
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $class is correct as $this->metrics->get(\Hal...erator::getName($node)) (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...
42
43
44
            $select = $while = $if = 0;
45
46
            iterate_over_node($node, function ($node) use (&$while, &$select, &$if) {
47
                switch (true) {
48
                    case $node instanceof Stmt\Do_:
49
                    case $node instanceof Stmt\Foreach_:
50
                    case $node instanceof Stmt\While_:
51
                        $while++;
52
                        break;
53
                    case $node instanceof Stmt\If_:
54
                        $if++;
55
                        break;
56
                    case $node instanceof Stmt\Switch_:
57
                        $select++;
58
                        break;
59
                }
60
            });
61
62
            $defect = 0.15 + 0.23 *  $while + 0.22 *  $select + 0.07 * $if;
63
            $class->set('kanDefect', round($defect, 2));
0 ignored issues
show
Bug introduced by
The method set cannot be called on $class (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...
64
        }
65
    }
66
}
67