Coupling   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 9
lcom 0
cbo 3
dl 0
loc 57
rs 10
c 1
b 1
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A calculate() 0 13 3
B extractCoupling() 0 27 6
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\HenryAndKafura;
11
use Hal\Component\OOP\Extractor\ClassMap;
12
13
14
/**
15
 * Estimates coupling (based on work of Henry And Kafura)
16
 *
17
 * @author Jean-François Lépine <https://twitter.com/Halleck45>
18
 */
19
class Coupling {
20
21
    /**
22
     * Calculates coupling
23
     *
24
     * @param ClassMap $result
25
     * @return ResultMap
26
     */
27
    public function calculate(ClassMap $result)
28
    {
29
        $map = $this->extractCoupling($result);
30
31
        // instability
32
        foreach($map as &$class) {
33
            if($class->getAfferentCoupling() + $class->getEfferentCoupling() > 0) {
34
                $class->setInstability($class->getEfferentCoupling() / ($class->getAfferentCoupling() + $class->getEfferentCoupling()));
35
            }
36
        }
37
38
        return new ResultMap($map);
39
    }
40
41
42
    /**
43
     * Extracts afferent and efferent coupling
44
     *
45
     * @param ClassMap $result
46
     * @return array
47
     */
48
    private function extractCoupling(ClassMap $result) {
49
        $results = $result->all();
50
51
        $classes = array();
52
        foreach($results as $result) {
53
            $classes = array_merge($classes, $result->getClasses());
54
        }
55
56
        $map = array();
57
        foreach($classes as $class) {
58
59
            if(!isset($map[$class->getFullname()])) {
60
                $map[$class->getFullname()] = new Result($class->getFullname());
61
            }
62
63
            $dependencies = $class->getDependencies();
64
            $map[$class->getFullname()]->setEfferentCoupling(sizeof($dependencies, COUNT_NORMAL));
65
66
            foreach($dependencies as $dependency) {
67
                if(!isset($map[$dependency])) {
68
                    $map[$dependency] = new Result($dependency);
69
                }
70
                $map[$dependency]->setAfferentCoupling($map[$dependency]->getAfferentCoupling() + 1);
71
            }
72
        }
73
        return $map;
74
    }
75
};