| Conditions | 5 |
| Paths | 6 |
| Total Lines | 32 |
| Code Lines | 20 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 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(); |
||
| 27 | } |
||
| 28 | } |
||
| 29 | // Set depending instabilities |
||
| 30 | foreach ($packages as $eachPackage) { |
||
| 31 | $dependentInstabilities = array_map(function ($packageName) use ($instabilitiesByPackage) { |
||
| 32 | return isset($instabilitiesByPackage[$packageName]) ? $instabilitiesByPackage[$packageName] : null; |
||
| 33 | }, $eachPackage->getOutgoingPackageDependencies()); |
||
| 34 | $dependentInstabilities = array_combine( |
||
| 35 | $eachPackage->getOutgoingPackageDependencies(), |
||
| 36 | $dependentInstabilities |
||
| 37 | ); |
||
| 38 | $dependentInstabilities = array_filter($dependentInstabilities, 'is_float'); |
||
| 39 | $eachPackage->setDependentInstabilities($dependentInstabilities); |
||
| 40 | } |
||
| 41 | } |
||
| 42 | } |
||
| 43 |