From 660a0278c87d127a1c0697b4c0ec3dc4caf3358f Mon Sep 17 00:00:00 2001 From: Doug Wright Date: Sun, 15 Sep 2019 17:19:38 +0100 Subject: [PATCH] Try PHP5 compatibility again --- src/BoxList.php | 29 ++++++++++++++++++----------- tests/BoxListTest.php | 6 +++--- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/BoxList.php b/src/BoxList.php index d0e0a1c8..39863177 100644 --- a/src/BoxList.php +++ b/src/BoxList.php @@ -29,23 +29,30 @@ public function compare($boxA, $boxB) $boxAVolume = $boxA->getInnerWidth() * $boxA->getInnerLength() * $boxA->getInnerDepth(); $boxBVolume = $boxB->getInnerWidth() * $boxB->getInnerLength() * $boxB->getInnerDepth(); - $volumeDecider = $boxBVolume - $boxAVolume; // try smallest box first - $emptyWeightDecider = $boxA->getEmptyWeight() - $boxB->getEmptyWeight(); // with smallest empty weight + // try smallest box first + if ($boxBVolume > $boxAVolume) { + return 1; + } + if ($boxAVolume > $boxBVolume) { + return -1; + } - if ($volumeDecider !== 0) { - return $volumeDecider; + // smallest empty weight + if ($boxA->getEmptyWeight() > $boxB->getEmptyWeight()) { + return 1; } - if ($emptyWeightDecider !== 0) { - return $emptyWeightDecider; + if ($boxB->getEmptyWeight() > $boxA->getEmptyWeight()) { + return -1; } // maximum weight capacity as fallback decider - $maxWeightDecider = ($boxB->getMaxWeight() - $boxB->getEmptyWeight()) - ($boxA->getMaxWeight() - $boxA->getEmptyWeight()); - - if ($maxWeightDecider === 0) { - return PHP_MAJOR_VERSION > 5 ? -1 : 1; + if (($boxA->getMaxWeight() - $boxA->getEmptyWeight()) > ($boxB->getMaxWeight() - $boxB->getEmptyWeight())) { + return -1; + } + if (($boxB->getMaxWeight() - $boxB->getEmptyWeight()) > ($boxA->getMaxWeight() - $boxA->getEmptyWeight())) { + return 1; } - return $maxWeightDecider; + return 0; } } diff --git a/tests/BoxListTest.php b/tests/BoxListTest.php index 6903d6a0..f4fa14ae 100644 --- a/tests/BoxListTest.php +++ b/tests/BoxListTest.php @@ -80,9 +80,9 @@ public function testIssue30B() */ public function testIssue163() { - $box2 = new TestBox('Box3', 202, 152, 32, 10, 200, 150, 30, 100); - $box4 = new TestBox('Box2', 202, 152, 32, 5, 200, 150, 30, 100); - $box3 = new TestBox('Box4', 202, 152, 32, 10, 200, 150, 30, 250); + $box2 = new TestBox('Box2', 202, 152, 32, 10, 200, 150, 30, 100); + $box4 = new TestBox('Box4', 202, 152, 32, 5, 200, 150, 30, 100); + $box3 = new TestBox('Box3', 202, 152, 32, 10, 200, 150, 30, 250); $box1 = new TestBox('Box1', 202, 152, 32, 10, 200, 150, 30, 50); $list = new BoxList();