From 1aab8d361f09a970cea756c7dd4556c76e5f1a4e Mon Sep 17 00:00:00 2001 From: ngiraud Date: Sun, 22 Dec 2019 13:31:11 +0100 Subject: [PATCH 1/2] Fix warning "Division by zero" when no package is defined. --- src/Hal/Metric/Consolidated.php | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/src/Hal/Metric/Consolidated.php b/src/Hal/Metric/Consolidated.php index 8f61ad1a..5c812551 100644 --- a/src/Hal/Metric/Consolidated.php +++ b/src/Hal/Metric/Consolidated.php @@ -119,22 +119,24 @@ public function __construct(Metrics $metrics) $avg->outgoingCDep = 0; $avg->outgoingPDep = 0; $avg->classesPerPackage = 0; - foreach (array_keys($packages) as $eachName) { - /* @var $eachPackage PackageMetric */ - $eachPackage = $metrics->get($eachName); - $avg->distance += $eachPackage->getDistance(); - $avg->incomingCDep += count($eachPackage->getIncomingClassDependencies()); - $avg->incomingPDep += count($eachPackage->getIncomingPackageDependencies()); - $avg->outgoingCDep += count($eachPackage->getOutgoingClassDependencies()); - $avg->outgoingPDep += count($eachPackage->getOutgoingPackageDependencies()); - $avg->classesPerPackage += count($eachPackage->getClasses()); + if ([] !== $packages) { + foreach (array_keys($packages) as $eachName) { + /* @var $eachPackage PackageMetric */ + $eachPackage = $metrics->get($eachName); + $avg->distance += $eachPackage->getDistance(); + $avg->incomingCDep += count($eachPackage->getIncomingClassDependencies()); + $avg->incomingPDep += count($eachPackage->getIncomingPackageDependencies()); + $avg->outgoingCDep += count($eachPackage->getOutgoingClassDependencies()); + $avg->outgoingPDep += count($eachPackage->getOutgoingPackageDependencies()); + $avg->classesPerPackage += count($eachPackage->getClasses()); + } + $avg->distance = round($avg->distance / $sum->nbPackages, 2); + $avg->incomingCDep = round($avg->incomingCDep / $sum->nbPackages, 2); + $avg->incomingPDep = round($avg->incomingPDep / $sum->nbPackages, 2); + $avg->outgoingCDep = round($avg->outgoingCDep / $sum->nbPackages, 2); + $avg->outgoingPDep = round($avg->outgoingPDep / $sum->nbPackages, 2); + $avg->classesPerPackage = round($avg->classesPerPackage / $sum->nbPackages, 2); } - $avg->distance = round($avg->distance / count($packages), 2); - $avg->incomingCDep = round($avg->incomingCDep / count($packages), 2); - $avg->incomingPDep = round($avg->incomingPDep / count($packages), 2); - $avg->outgoingCDep = round($avg->outgoingCDep / count($packages), 2); - $avg->outgoingPDep = round($avg->outgoingPDep / count($packages), 2); - $avg->classesPerPackage = round($avg->classesPerPackage / count($packages), 2); // sums of violations $violations = [ From 4bb56e25c370633518458127224e194cb4d97406 Mon Sep 17 00:00:00 2001 From: ngiraud Date: Sun, 22 Dec 2019 17:01:58 +0100 Subject: [PATCH 2/2] Replace comparison for number of packages. --- src/Hal/Metric/Consolidated.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Hal/Metric/Consolidated.php b/src/Hal/Metric/Consolidated.php index 5c812551..19ff93d6 100644 --- a/src/Hal/Metric/Consolidated.php +++ b/src/Hal/Metric/Consolidated.php @@ -119,7 +119,7 @@ public function __construct(Metrics $metrics) $avg->outgoingCDep = 0; $avg->outgoingPDep = 0; $avg->classesPerPackage = 0; - if ([] !== $packages) { + if (0 !== $sum->nbPackages) { foreach (array_keys($packages) as $eachName) { /* @var $eachPackage PackageMetric */ $eachPackage = $metrics->get($eachName);