Completed
Push — master ( bdaaad...4f4372 )
by personal
06:57 queued 04:36
created

SystemComplexityVisitor::__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
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Hal\Metric\Class_\Structural;
3
4
use Hal\Metric\Metrics;
5
use PhpParser\Node;
6
use PhpParser\Node\Stmt;
7
use PhpParser\NodeVisitorAbstract;
8
9
/**
10
 * Calculates Card And Agresti metric
11
 *
12
 *      Fan-out = Structural fan-out = Number of other procedures this procedure calls
13
 *      v = number of input/output variables for a procedure
14
 *
15
 *      (SC) Structural complexity = fan-out^2
16
 *      (DC) Data complexity = v / (fan-out + 1)
17
 *
18
 * @author Jean-François Lépine <https://twitter.com/Halleck45>
19
 */
20
class SystemComplexityVisitor extends NodeVisitorAbstract
21
{
22
23
    /**
24
     * @var Metrics
25
     */
26
    private $metrics;
27
28
    /**
29
     * ClassEnumVisitor constructor.
30
     * @param Metrics $metrics
31
     */
32
    public function __construct(Metrics $metrics)
33
    {
34
        $this->metrics = $metrics;
35
    }
36
37
    /**
38
     * @inheritdoc
39
     */
40
    public function leaveNode(Node $node)
41
    {
42
        if ($node instanceof Stmt\Class_) {
43
44
            $class = $this->metrics->get($node->namespacedName->toString());
0 ignored issues
show
Bug introduced by
The property namespacedName does not seem to exist in PhpParser\Node\Stmt\Class_.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Bug introduced by
Are you sure the assignment to $class is correct as $this->metrics->get($nod...spacedName->toString()) (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...
45
46
            $sy = $dc = $sc = array();
47
48
            foreach ($node->stmts as $stmt) {
49
                if ($stmt instanceof Stmt\ClassMethod) {
50
51
                    // number of returns and calls
52
                    $output = 0;
53
                    $fanout = [];
54
55
                    iterate_over_node($node, function ($node) use (&$output, &$fanout) {
56 View Code Duplication
                        switch (true) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
                            case $node instanceof Stmt\Return_:
58
                                $output++;
59
                                break;
60
                            case $node instanceof Node\Expr\StaticCall:
61
                            case $node instanceof Node\Expr\MethodCall:
62
                                array_push($fanout, getNameOfNode($node));
63
                        }
64
                    });
65
66
                    $fanout = sizeof(array_unique($fanout));
67
                    $v = sizeof($stmt->params) + $output;
68
                    $ldc = $v / ($fanout + 1);
69
                    $lsc = pow($fanout, 2);
70
                    $sy[] = $ldc + $lsc;
71
                    $dc[] = $ldc;
72
                    $sc[] = $lsc;
73
                }
74
            }
75
76
            // average for class
77
            $class
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...
78
                ->set('relativeStructuralComplexity', empty($sc) ? 0 : round(array_sum($sc) / sizeof($sc), 2))
79
                ->set('relativeDataComplexity', empty($dc) ? 0 : round(array_sum($dc) / sizeof($dc), 2))
80
                ->set('relativeSystemComplexity', empty($sy) ? 0 : round(array_sum($sy) / sizeof($sy), 2))
81
                ->set('totalStructuralComplexity', round(array_sum($sc), 2))
82
                ->set('totalDataComplexity', round(array_sum($dc), 2))
83
                ->set('totalSystemComplexity', round(array_sum($dc) + array_sum($sc), 2));
84
        }
85
    }
86
}
87