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

HalsteadVisitor   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 114
Duplicated Lines 5.26 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 6
loc 114
rs 10
c 0
b 0
f 0
wmc 22
lcom 1
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
D leaveNode() 6 93 21

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_\Text;
3
4
use Hal\Metric\FunctionMetric;
5
use Hal\Metric\Metrics;
6
use Hoa\Ruler\Model\Bag\Scalar;
7
use PhpParser\Node;
8
use PhpParser\Node\Stmt;
9
use PhpParser\NodeVisitorAbstract;
10
11
/**
12
 * Calculates Halstead complexity
13
 *
14
 *      According Wikipedia, "Halstead complexity measures are software metrics introduced by Maurice Howard Halstead in
15
 *      1977 as part of his treatise on establishing an empirical science of software development.
16
 *      Halstead makes the observation that metrics of the software should reflect the implementation or
17
 *      expression of algorithms in different languages, but be independent of their execution on a specific platform.
18
 *      These metrics are therefore computed statically from the code."
19
 *
20
 * @author Jean-François Lépine <https://twitter.com/Halleck45>
21
 * Class HalsteadVisitor
22
 * @package Hal\Metric\Class_\Coupling
23
 */
24
class HalsteadVisitor extends NodeVisitorAbstract
25
{
26
27
    /**
28
     * @var Metrics
29
     */
30
    private $metrics;
31
32
    /**
33
     * ClassEnumVisitor constructor.
34
     * @param Metrics $metrics
35
     */
36
    public function __construct(Metrics $metrics)
37
    {
38
        $this->metrics = $metrics;
39
    }
40
41
    /**
42
     * @inheritdoc
43
     */
44
    public function leaveNode(Node $node)
45
    {
46
        if ($node instanceof Stmt\Class_ || $node instanceof Stmt\Function_) {
47
48 View Code Duplication
            if ($node instanceof Stmt\Class_) {
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...
49
                $classOrFunction = $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 $classOrFunction 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...
50
            } else {
51
                $classOrFunction = new FunctionMetric($node->name);
52
                $this->metrics->attach($classOrFunction);
53
            }
54
55
            // search for operands and operators
56
            $operands = [];
57
            $operators = [];
58
59
            iterate_over_node($node, function ($node) use (&$operators, &$operands) {
60
61
                if (
62
                    $node instanceof Node\Expr\BinaryOp
63
                    || $node instanceof Node\Expr\AssignOp
64
                    || $node instanceof Stmt\If_
65
                    || $node instanceof Stmt\For_
66
                    || $node instanceof Stmt\Switch_
67
                    || $node instanceof Stmt\Catch_
68
                    || $node instanceof Stmt\Return_
69
                    || $node instanceof Stmt\While_
70
                    || $node instanceof Node\Expr\Assign
71
                ) {
72
                    // operators
73
                    array_push($operators, get_class($node));
74
                }
75
76
                if (
77
                    $node instanceof Node\Expr\Cast
78
                    || $node instanceof Node\Expr\Variable
79
                    || $node instanceof Node\Param
80
                    || $node instanceof Node\Scalar
81
                ) {
82
                    // operands
83
                    if (isset($node->value)) {
84
                        $name = $node->value;
85
                    } elseif (isset($node->name)) {
86
                        $name = $node->name;
87
                    } else {
88
                        $name = get_class($node);
89
                    }
90
                    array_push($operands, $name);
91
                }
92
            });
93
94
            // calculate halstead metrics
95
            $uniqueOperators = array_map('unserialize', array_unique(array_map('serialize', $operators)));
96
            $uniqueOperands = array_map('unserialize', array_unique(array_map('serialize', $operands)));
97
98
            $n1 = sizeof($uniqueOperators, COUNT_NORMAL);
99
            $n2 = sizeof($uniqueOperands, COUNT_NORMAL);
100
            $N1 = sizeof($operators, COUNT_NORMAL);
101
            $N2 = sizeof($operands, COUNT_NORMAL);
102
103
            if (($n2 == 0) || ($N2 == 0)) {
104
                // files without operators
105
                $V = $n1 = $n2 = $N1 = $N2 = $E = $D = $B = $T = $I = $L = 0;
106
            } else {
107
                $devAbility = 3000;
108
                $N = $N1 + $N2;
109
                $n = $n1 + $n2;
110
                $V = $N * log($n, 2);
111
                $L = (2 / max(1, $n1)) * ($n2 / $N2);
112
                $D = ($n1 / 2) * ($N2 / $n2);
113
                $E = $V * $D;
114
                $B = $V / $devAbility;
115
                $T = $E / 18;
116
                $I = $L * $V;
117
            }
118
119
            // save result
120
            $classOrFunction
121
                ->set('length', $N1 + $N2)
122
                ->set('vocabulary', $n1 + $n2)
123
                ->set('volume', round($V, 2))
124
                ->set('difficulty', round($D, 2))
125
                ->set('effort', round($E, 2))
126
                ->set('level', round($L, 2))
127
                ->set('bugs', round($B, 2))
128
                ->set('time', round($T))
129
                ->set('intelligentContent', round($I, 2))
130
                ->set('number_operators', $N1)
131
                ->set('number_operands', $N2)
132
                ->set('number_operators_unique', $n1)
133
                ->set('number_operands_unique', $n2);
134
            $this->metrics->attach($classOrFunction);
135
        }
136
    }
137
}
138