Skip to content

Commit

Permalink
GH-139 Handle empty fleet array detection in the parser
Browse files Browse the repository at this point in the history
  • Loading branch information
mdziekon committed May 26, 2022
1 parent 3a4916f commit 0d4bee1
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 12 deletions.
8 changes: 3 additions & 5 deletions fleet1.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@
case 'SHIP_COUNT_EXCEEDS_AVAILABLE':
$errorMessage = $_Lang['fl1_NoEnoughShips'];
break;
case 'NO_SHIPS':
$errorMessage = $_Lang['fl1_NoShipsGiven'];
break;
default:
$errorMessage = $_Lang['fleet_generic_errors_unknown'];
break;
Expand Down Expand Up @@ -151,11 +154,6 @@
}
}

if($Fleet['count'] <= 0)
{
message($_Lang['fl1_NoShipsGiven'], $ErrorTitle, 'fleet.php', 3);
}

$slowestShipSpeed = min(
array_map_withkeys($shipsDetails, function ($shipDetails) {
return $shipDetails['speed'];
Expand Down
6 changes: 3 additions & 3 deletions fleet2.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@
case 'SHIP_COUNT_EXCEEDS_AVAILABLE':
$errorMessage = $_Lang['fl1_NoEnoughShips'];
break;
case 'NO_SHIPS':
$errorMessage = $_Lang['fl1_NoShipsGiven'];
break;
default:
$errorMessage = $_Lang['fleet_generic_errors_unknown'];
break;
Expand All @@ -217,9 +220,6 @@
}
}

if ($Fleet['count'] <= 0) {
message($_Lang['fl1_NoShipsGiven'], $ErrorTitle, 'fleet.php', 3);
}
$Fleet['array'] = $FleetArray;
unset($FleetArray);

Expand Down
3 changes: 0 additions & 3 deletions fleet3.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,6 @@ function messageRed($Text, $Title)
}
}

if ($Fleet['count'] <= 0) {
messageRed($_Lang['fl1_NoShipsGiven'], $ErrorTitle);
}
$Fleet['array'] = $FleetArray;
unset($FleetArray);

Expand Down
1 change: 1 addition & 0 deletions modules/flightControl/utils/errors/fleetArray.utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ function mapFleetArrayValidationErrorToReadableMessage($error) {
'SHIP_WITH_NO_ENGINE' => $_Lang['fl1_CantSendUnflyable'],
'INVALID_SHIP_COUNT' => $_Lang['fleet_generic_errors_invalidshipcount'],
'SHIP_COUNT_EXCEEDS_AVAILABLE' => $_Lang['fl1_NoEnoughShips'],
'NO_SHIPS' => $_Lang['fl1_NoShipsGiven'],
];

if (!isset($knownErrorsByCode[$errorCode])) {
Expand Down
10 changes: 9 additions & 1 deletion modules/flightControl/utils/validators/fleetArray.validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,17 @@ function parseFleetArray ($props) {
]);
}

if ($shipCount == 0) {
continue;
}

$parsedFleet[$shipId] = $shipCount;
}

continue;
if (empty($parsedFleet)) {
return $isInvalid([
[ 'errorCode' => 'NO_SHIPS', ],
]);
}

return $isValid([
Expand Down

0 comments on commit 0d4bee1

Please sign in to comment.