FileCoupling::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 5
rs 9.4286
cc 1
eloc 3
nc 1
nop 2
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 by file: average for all declared classes in this file
16
 *
17
 * @author Jean-François Lépine <https://twitter.com/Halleck45>
18
 */
19
class FileCoupling {
20
21
22
    /**
23
     * @var \Hal\Component\OOP\Extractor\ClassMap
24
     */
25
    private $classMap;
26
27
    /**
28
     * @var ResultMap
29
     */
30
    private $couplingMap;
31
32
    /**
33
     * Constructor
34
     *
35
     * @param ClassMap $classMap
36
     * @param ResultMap $couplingMap
37
     */
38
    public function __construct(ClassMap $classMap, ResultMap $couplingMap)
39
    {
40
        $this->classMap = $classMap;
41
        $this->couplingMap = $couplingMap;
42
    }
43
44
45
    /**
46
     * Calculates coupling for file (and not for class)
47
     *
48
     * @param string $filename
49
     * @return Result
50
     */
51
    public function calculate($filename)
52
    {
53
        $rOOP = $this->classMap->getClassesIn($filename);
54
        $result = new Result($filename);
55
56
        // case of file doesn't contain any class
57
        if(null === $rOOP) {
58
            return $result;
59
        }
60
61
        $instability = $ce = $ca = 0;
62
63
        $classes = $rOOP->getClasses();
64
        foreach($classes as $declaredClass) {
65
            $declaredClassCoupling = $this->couplingMap->get($declaredClass->getFullname());
66
67
            $ce += $declaredClassCoupling->getEfferentCoupling();
68
            $ca += $declaredClassCoupling->getAfferentCoupling();
69
        }
70
71
        if($ca + $ce > 0) {
72
            $instability = $ce / ($ca + $ce);
73
        }
74
75
        $result
76
            ->setAfferentCoupling($ca)
77
            ->setEfferentCoupling($ce)
78
            ->setInstability($instability);
79
80
        return $result;
81
    }
82
};