|
1
|
|
|
<?php |
|
2
|
|
|
namespace Hal\Metric\Class_\Structural; |
|
3
|
|
|
|
|
4
|
|
|
use Hal\Component\Tree\Graph; |
|
5
|
|
|
use Hal\Component\Tree\Node as TreeNode; |
|
6
|
|
|
use Hal\Metric\Metrics; |
|
7
|
|
|
use PhpParser\Node; |
|
8
|
|
|
use PhpParser\Node\Stmt; |
|
9
|
|
|
use PhpParser\NodeVisitorAbstract; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Lack of cohesion of methods |
|
13
|
|
|
* |
|
14
|
|
|
* Class ExternalsVisitor |
|
15
|
|
|
* @package Hal\Metric\Class_\Coupling |
|
16
|
|
|
*/ |
|
17
|
|
|
class LcomVisitor extends NodeVisitorAbstract |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var Metrics |
|
22
|
|
|
*/ |
|
23
|
|
|
private $metrics; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* ClassEnumVisitor constructor. |
|
27
|
|
|
* @param Metrics $metrics |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct(Metrics $metrics) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->metrics = $metrics; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @inheritdoc |
|
36
|
|
|
*/ |
|
37
|
|
|
public function leaveNode(Node $node) |
|
38
|
|
|
{ |
|
39
|
|
|
if ($node instanceof Stmt\Class_) { |
|
40
|
|
|
|
|
41
|
|
|
// we build a graph of internal dependencies in class |
|
42
|
|
|
$graph = new Graph(); |
|
43
|
|
|
$class = $this->metrics->get($node->namespacedName->toString()); |
|
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
foreach ($node->stmts as $stmt) { |
|
46
|
|
|
if ($stmt instanceof Stmt\ClassMethod) { |
|
47
|
|
|
|
|
48
|
|
|
if (!$graph->has($stmt->name . '()')) { |
|
49
|
|
|
$graph->insert(new TreeNode($stmt->name . '()')); |
|
50
|
|
|
} |
|
51
|
|
|
$from = $graph->get($stmt->name . '()'); |
|
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
\iterate_over_node($stmt, function ($node) use ($from, &$graph) { |
|
54
|
|
|
|
|
55
|
|
View Code Duplication |
if ($node instanceof Node\Expr\PropertyFetch && isset($node->var->name) && $node->var->name == 'this') { |
|
|
|
|
|
|
56
|
|
|
$name = getNameOfNode($node); |
|
57
|
|
|
// use of attribute $this->xxx; |
|
|
|
|
|
|
58
|
|
|
if (!$graph->has($name)) { |
|
59
|
|
|
$graph->insert(new TreeNode($name)); |
|
60
|
|
|
} |
|
61
|
|
|
$to = $graph->get($name); |
|
|
|
|
|
|
62
|
|
|
$graph->addEdge($from, $to); |
|
|
|
|
|
|
63
|
|
|
return; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
if ($node instanceof Node\Expr\MethodCall) { |
|
68
|
|
View Code Duplication |
if (!$node->var instanceof Node\Expr\New_ && isset($node->var->name) && getNameOfNode($node->var) === 'this') { |
|
|
|
|
|
|
69
|
|
|
// use of method call $this->xxx(); |
|
|
|
|
|
|
70
|
|
|
// use of attribute $this->xxx; |
|
|
|
|
|
|
71
|
|
|
$name = getNameOfNode($node->name) . '()'; |
|
72
|
|
|
if (!$graph->has($name)) { |
|
73
|
|
|
$graph->insert(new TreeNode($name)); |
|
74
|
|
|
} |
|
75
|
|
|
$to = $graph->get($name); |
|
|
|
|
|
|
76
|
|
|
$graph->addEdge($from, $to); |
|
|
|
|
|
|
77
|
|
|
return; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
}); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
// we count paths |
|
86
|
|
|
$paths = 0; |
|
87
|
|
|
foreach ($graph->all() as $node) { |
|
88
|
|
|
$paths += $this->traverse($node); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$class->set('lcom', $paths); |
|
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Traverse node, and return 1 if node has not been visited yet |
|
97
|
|
|
* |
|
98
|
|
|
* @param TreeNode $node |
|
99
|
|
|
* @return int |
|
100
|
|
|
*/ |
|
101
|
|
|
private function traverse(TreeNode $node) |
|
102
|
|
|
{ |
|
103
|
|
|
if ($node->visited) { |
|
104
|
|
|
return 0; |
|
105
|
|
|
} |
|
106
|
|
|
$node->visited = true; |
|
107
|
|
|
|
|
108
|
|
|
foreach ($node->getAdjacents() as $adjacent) { |
|
109
|
|
|
$this->traverse($adjacent); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return 1; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
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.