Skip to content

Commit

Permalink
Try PHP5 compatibility again
Browse files Browse the repository at this point in the history
  • Loading branch information
dvdoug committed Sep 15, 2019
1 parent bc850d6 commit 660a027
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
29 changes: 18 additions & 11 deletions src/BoxList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
6 changes: 3 additions & 3 deletions tests/BoxListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 660a027

Please sign in to comment.