1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* (c) Jean-François Lépine <https://twitter.com/Halleck45> |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Hal\Metrics\Complexity\Structural\CardAndAgresti; |
11
|
|
|
|
12
|
|
|
use Hal\Component\OOP\Reflected\ReflectedClass; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Calculates Card And Agresti metric |
16
|
|
|
* |
17
|
|
|
* @author Jean-François Lépine <https://twitter.com/Halleck45> |
18
|
|
|
*/ |
19
|
|
|
class SystemComplexity { |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Calculates Card And Agresti metric |
23
|
|
|
* |
24
|
|
|
* Fan-out = Structural fan-out = Number of other procedures this procedure calls |
25
|
|
|
* v = number of input/output variables for a procedure |
26
|
|
|
* |
27
|
|
|
* (SC) Structural complexity = fan-out^2 |
28
|
|
|
* (DC) Data complexity = v / (fan-out + 1) |
29
|
|
|
* |
30
|
|
|
* @param ReflectedClass $class |
31
|
|
|
* @return Result |
32
|
|
|
*/ |
33
|
|
|
public function calculate(ReflectedClass $class) |
34
|
|
|
{ |
35
|
|
|
$result = new Result; |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
$sy = $dc = $sc = array(); // in system |
39
|
|
|
foreach($class->getMethods() as $method) { |
40
|
|
|
$fanout = sizeof($method->getDependencies(), COUNT_NORMAL); |
41
|
|
|
$v = sizeof($method->getArguments(), COUNT_NORMAL) + sizeof($method->getReturns(), COUNT_NORMAL); |
42
|
|
|
|
43
|
|
|
$ldc = $v / ($fanout + 1); |
44
|
|
|
$lsc = pow($fanout, 2); |
45
|
|
|
$sy[] = $ldc + $lsc; |
46
|
|
|
$dc[] = $ldc; |
47
|
|
|
$sc[] = $lsc; |
48
|
|
|
|
49
|
|
|
} |
50
|
|
|
$result |
51
|
|
|
->setRelativeStructuralComplexity(empty($sc) ? 0 : round(array_sum($sc) / sizeof($sc, COUNT_NORMAL), 2)) |
52
|
|
|
->setRelativeDataComplexity(empty($dc) ? 0 : round(array_sum($dc) / sizeof($dc, COUNT_NORMAL), 2)) |
53
|
|
|
->setRelativeSystemComplexity(empty($sy) ? 0 : round(array_sum($sy) / sizeof($sy, COUNT_NORMAL), 2)) |
54
|
|
|
->setTotalStructuralComplexity(round(array_sum($sc), 2)) |
55
|
|
|
->setTotalDataComplexity(round(array_sum($dc), 2)) |
56
|
|
|
->setTotalSystemComplexity(round(array_sum($dc) + array_sum($sc), 2)) |
57
|
|
|
; |
58
|
|
|
|
59
|
|
|
return $result; |
60
|
|
|
} |
61
|
|
|
} |