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

SystemComplexityVisitor   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 67
Duplicated Lines 11.94 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 8
loc 67
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
D leaveNode() 8 46 10

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace Hal\Metric\Class_\Structural;
3
4
use Hal\Metric\Helper\MetricClassNameGenerator;
5
use Hal\Metric\Metrics;
6
use PhpParser\Node;
7
use PhpParser\Node\Stmt;
8
use PhpParser\NodeVisitorAbstract;
9
10
/**
11
 * Calculates Card And Agresti metric
12
 *
13
 *      Fan-out = Structural fan-out = Number of other procedures this procedure calls
14
 *      v = number of input/output variables for a procedure
15
 *
16
 *      (SC) Structural complexity = fan-out^2
17
 *      (DC) Data complexity = v / (fan-out + 1)
18
 *
19
 * @author Jean-François Lépine <https://twitter.com/Halleck45>
20
 */
21
class SystemComplexityVisitor extends NodeVisitorAbstract
22
{
23
24
    /**
25
     * @var Metrics
26
     */
27
    private $metrics;
28
29
    /**
30
     * ClassEnumVisitor constructor.
31
     * @param Metrics $metrics
32
     */
33
    public function __construct(Metrics $metrics)
34
    {
35
        $this->metrics = $metrics;
36
    }
37
38
    /**
39
     * @inheritdoc
40
     */
41
    public function leaveNode(Node $node)
42
    {
43
        if ($node instanceof Stmt\Class_) {
44
45
            $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...
46
47
            $sy = $dc = $sc = array();
48
49
            foreach ($node->stmts as $stmt) {
50
                if ($stmt instanceof Stmt\ClassMethod) {
51
52
                    // number of returns and calls
53
                    $output = 0;
54
                    $fanout = [];
55
56
                    iterate_over_node($node, function ($node) use (&$output, &$fanout) {
57 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...
58
                            case $node instanceof Stmt\Return_:
59
                                $output++;
60
                                break;
61
                            case $node instanceof Node\Expr\StaticCall:
62
                            case $node instanceof Node\Expr\MethodCall:
63
                                array_push($fanout, getNameOfNode($node));
64
                        }
65
                    });
66
67
                    $fanout = sizeof(array_unique($fanout));
68
                    $v = sizeof($stmt->params) + $output;
69
                    $ldc = $v / ($fanout + 1);
70
                    $lsc = pow($fanout, 2);
71
                    $sy[] = $ldc + $lsc;
72
                    $dc[] = $ldc;
73
                    $sc[] = $lsc;
74
                }
75
            }
76
77
            // average for class
78
            $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...
79
                ->set('relativeStructuralComplexity', empty($sc) ? 0 : round(array_sum($sc) / sizeof($sc), 2))
80
                ->set('relativeDataComplexity', empty($dc) ? 0 : round(array_sum($dc) / sizeof($dc), 2))
81
                ->set('relativeSystemComplexity', empty($sy) ? 0 : round(array_sum($sy) / sizeof($sy), 2))
82
                ->set('totalStructuralComplexity', round(array_sum($sc), 2))
83
                ->set('totalDataComplexity', round(array_sum($dc), 2))
84
                ->set('totalSystemComplexity', round(array_sum($dc) + array_sum($sc), 2));
85
        }
86
    }
87
}
88