Completed
Pull Request — master (#283)
by Oskar
04:36
created

PackageInstability::calculate()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 34
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 20
c 0
b 0
f 0
nc 6
nop 1
dl 0
loc 34
rs 8.439
1
<?php
2
3
namespace Hal\Metric\Package;
4
5
use Hal\Metric\Metrics;
6
use Hal\Metric\PackageMetric;
7
8
class PackageInstability
9
{
10
    public function calculate(Metrics $metrics)
11
    {
12
        /* @var $packages PackageMetric[] */
13
        $packages = array_filter($metrics->all(), function ($metric) {
14
            return $metric instanceof PackageMetric;
15
        });
16
17
        // Calculate instability
18
        $instabilitiesByPackage = [];
19
        foreach ($packages as $eachPackage) {
20
            $afferentCoupling = count($eachPackage->getIncomingClassDependencies());
21
            $efferentCoupling = count($eachPackage->getOutgoingClassDependencies());
22
            if ($afferentCoupling + $efferentCoupling !== 0) {
23
                $eachPackage->setInstability(
24
                    $efferentCoupling / ($afferentCoupling + $efferentCoupling)
25
                );
26
                $instabilitiesByPackage[$eachPackage->getName()] = $eachPackage->getInstability();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $instabilitiesByPackage[$eachPackage->getName()] is correct as $eachPackage->getInstability() (which targets Hal\Metric\PackageMetric::getInstability()) 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...
27
            }
28
        }
29
30
        // Set depending instabilities
31
        foreach ($packages as $eachPackage) {
32
            $dependentInstabilities = array_map(function ($packageName) use ($instabilitiesByPackage) {
33
                return isset($instabilitiesByPackage[$packageName]) ? $instabilitiesByPackage[$packageName] : null;
34
            }, $eachPackage->getOutgoingPackageDependencies());
35
            $dependentInstabilities = array_combine(
36
                $eachPackage->getOutgoingPackageDependencies(),
37
                $dependentInstabilities
38
            );
39
            $dependentInstabilities = array_filter($dependentInstabilities);
40
            $eachPackage->setDependentInstabilities($dependentInstabilities);
41
        }
42
43
    }
44
}
45