Skip to content

Commit

Permalink
Fix PHPStan issues
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcinKuklinskiBitBag committed Dec 2, 2023
1 parent f31330e commit 2a8dc4a
Show file tree
Hide file tree
Showing 18 changed files with 94 additions and 41 deletions.
1 change: 1 addition & 0 deletions phpstan.dist.neon
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ parameters:
- src/
- spec/
- tests/
treatPhpDocTypesAsCertain: false
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function it_does_nothing_when_mission_type_is_not_stationing(): void
$this->__invoke($event);
}

public function it_throws_exception_when_planet_has_not_been_found(
public function it_throws_exception_when_fleet_joining_the_planet_has_not_been_found(
FleetRepositoryInterface $fleetRepository,
): void {
$mission = "stationing";
Expand All @@ -48,6 +48,25 @@ public function it_throws_exception_when_planet_has_not_been_found(
$this->shouldThrow(InconsistentModelException::class)->during('__invoke', [$event]);
}

public function it_throws_exception_when_landing_target_planet_doesnt_exist(
FleetRepositoryInterface $fleetRepository,
NavigatorInterface $navigator,
Fleet $joiningFleet,
): void {
$mission = "stationing";
$fleetId = "22610343-1ee7-4ef5-84b5-4e285a68dba5";
$targetGalaxyPoint = "[1:2:3]";
$resourcesLoad = [];

$resolvedTargetGalaxyPoint = new GalaxyPoint(1, 2, 3);
$fleetRepository->find(new FleetId($fleetId))->willReturn($joiningFleet);
$navigator->getPlanetId($resolvedTargetGalaxyPoint)
->willReturn(null);

$event = new FleetHasReachedJourneyTargetPointEvent($mission, $fleetId, $targetGalaxyPoint, $resourcesLoad);
$this->shouldThrow(InconsistentModelException::class)->during('__invoke', [$event]);
}

public function it_lands_on_planet_when_no_fleet_exists_on_the_planet(
FleetRepositoryInterface $fleetRepository,
NavigatorInterface $navigator,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public function it_splits_ships_from_current_fleet_when_resolving_less_ships_tha
$journeyContext->calculateFuelRequirements(
$startGalaxyPoint,
$targetGalaxyPoint,
$fleetSplitResult,
$shipsTakingJourney,
)->willReturn($fuelRequirements);

$resourcesLoad->add($fuelRequirements)->shouldBeCalledOnce();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

final class StartJourneyCommand implements CommandInterface
{
/**
* @param array<string, int> $shipsTakingJourney
* @param array<string, int> $resourcesLoad
*/
public function __construct(
private readonly string $planetId,
private readonly string $targetGalaxyPoint,
Expand Down Expand Up @@ -44,7 +48,7 @@ public function getMissionType(): string
return $this->missionType;
}

/** @return array<string, int> $shipsTakingJourney */
/** @return array<string, int> */
public function getShipsTakingJourney(): array
{
return $this->shipsTakingJourney;
Expand Down
63 changes: 38 additions & 25 deletions src/Application/Component/FleetJourney/Domain/Entity/Fleet.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Fleet
{
private ?Journey $currentJourney = null;

/** @var array<ShipsGroupInterface> $ships */
/** @param array<ShipsGroupInterface> $ships */
public function __construct(
private readonly FleetIdInterface $fleetId,
private GalaxyPointInterface $stationingPoint,
Expand Down Expand Up @@ -49,7 +49,7 @@ public function merge(Fleet $fleet): void
$this->addShips($fleet->ships);
}

/** @var array<ShipsGroupInterface> $ships */
/** @param array<ShipsGroupInterface> $ships */
public function addShips(array $ships): void
{
if ($this->currentJourney !== null) {
Expand Down Expand Up @@ -85,7 +85,7 @@ public function getSpeed(): int
return $lowestSpeed;
}

/** @var array<string, int> $shipsToCompare */
/** @param array<string, int> $shipsToCompare */
public function hasEnoughShips(
array $shipsToCompare,
): bool {
Expand Down Expand Up @@ -114,7 +114,7 @@ public function hasEnoughShips(
return true;
}

/** @var array<string, int> $shipsToCompare */
/** @param array<string, int> $shipsToCompare */
public function hasMoreShipsThan(
array $shipsToCompare,
): bool {
Expand All @@ -141,7 +141,10 @@ public function hasMoreShipsThan(
return false;
}

/** @var array<string, int> $shipsToSplit */
/**
* @param array<string, int> $shipsToSplit
* @return array<ShipsGroupInterface>
*/
public function split(
array $shipsToSplit,
): array {
Expand All @@ -167,13 +170,22 @@ public function split(
return $splitShips;
}

private function getCurrentJourney(): Journey
{
if ($this->currentJourney === null) {
throw new FleetNotInJourneyYetException($this->fleetId);
}

return $this->currentJourney;
}

public function isDuringJourney(): bool
{
if ($this->currentJourney === null) {
return false;
}

if ($this->currentJourney->didReachTargetPoint() === false && $this->currentJourney->didReachReturnPoint() === false) {
if ($this->getCurrentJourney()->didReachTargetPoint() === false && $this->getCurrentJourney()->didReachReturnPoint() === false) {
return true;
}

Expand All @@ -195,7 +207,7 @@ public function getJourneyMissionType(): MissionType
throw new FleetNotInJourneyYetException($this->fleetId);
}

return $this->currentJourney->getMissionType();
return $this->getCurrentJourney()->getMissionType();
}

public function getJourneyStartPoint(): GalaxyPointInterface
Expand All @@ -204,7 +216,7 @@ public function getJourneyStartPoint(): GalaxyPointInterface
throw new FleetNotInJourneyYetException($this->fleetId);
}

return $this->currentJourney->getStartPoint();
return $this->getCurrentJourney()->getStartPoint();
}

public function getJourneyTargetPoint(): GalaxyPointInterface
Expand All @@ -213,7 +225,7 @@ public function getJourneyTargetPoint(): GalaxyPointInterface
throw new FleetNotInJourneyYetException($this->fleetId);
}

return $this->currentJourney->getTargetPoint();
return $this->getCurrentJourney()->getTargetPoint();
}

public function getJourneyReturnPoint(): GalaxyPointInterface
Expand All @@ -222,13 +234,13 @@ public function getJourneyReturnPoint(): GalaxyPointInterface
throw new FleetNotInJourneyYetException($this->fleetId);
}

return $this->currentJourney->getReturnPoint();
return $this->getCurrentJourney()->getReturnPoint();
}

public function didReachJourneyTargetPoint(): bool
{
return $this->currentJourney !== null
&& $this->currentJourney->didReachTargetPoint();
&& $this->getCurrentJourney()->didReachTargetPoint();
}

public function tryToReachJourneyTargetPoint(): void
Expand All @@ -238,25 +250,25 @@ public function tryToReachJourneyTargetPoint(): void
throw new FleetNotInJourneyYetException($this->fleetId);
}

$hasFinishedJourney = $this->currentJourney->didReachReturnPoint() === true;
$hasFinishedJourney = $this->getCurrentJourney()->didReachReturnPoint() === true;
if ($hasFinishedJourney) {
throw new FleetNotInJourneyYetException($this->fleetId);
}

if ($this->currentJourney->didReachTargetPoint() === false) {
if ($this->getCurrentJourney()->didReachTargetPoint() === false) {
return;
}

if ($this->currentJourney->doesPlanToStationOnTarget()) {
$this->stationingPoint = $this->currentJourney->getTargetPoint();
$this->currentJourney->reachTargetPoint();
if ($this->getCurrentJourney()->doesPlanToStationOnTarget()) {
$this->stationingPoint = $this->getCurrentJourney()->getTargetPoint();
$this->getCurrentJourney()->reachTargetPoint();

return;
} elseif ($this->currentJourney->doesFlyBack()) {
} elseif ($this->getCurrentJourney()->doesFlyBack()) {
return;
}

$this->currentJourney->reachTargetPoint();
$this->getCurrentJourney()->reachTargetPoint();
}

public function tryToReachJourneyReturnPoint(): void
Expand All @@ -267,21 +279,21 @@ public function tryToReachJourneyReturnPoint(): void
throw new FleetNotInJourneyYetException($this->fleetId);
}

if ($this->currentJourney->didReachTargetPoint() === false) {
if ($this->getCurrentJourney()->didReachTargetPoint() === false) {
throw new FleetHasNotYetReachedTheTargetPointException($this->fleetId);
}

if ($this->currentJourney->didReachReturnPoint() === false) {
if ($this->getCurrentJourney()->didReachReturnPoint() === false) {
return;
}

$this->currentJourney->reachReturnPoint();
$this->getCurrentJourney()->reachReturnPoint();
}

public function didReturnFromJourney(): bool
{
return $this->currentJourney !== null
&& $this->currentJourney->didReachReturnPoint();
&& $this->getCurrentJourney()->didReachReturnPoint();
}

public function doesFlyBack(): bool
Expand All @@ -290,16 +302,16 @@ public function doesFlyBack(): bool
return false;
}

return $this->currentJourney->doesFlyBack();
return $this->getCurrentJourney()->doesFlyBack();
}

public function cancelJourney(): void
{
if ($this->currentJourney !== null && $this->currentJourney->didReachTargetPoint()) {
if ($this->currentJourney !== null && $this->getCurrentJourney()->didReachTargetPoint()) {
throw new FleetNotInJourneyYetException($this->fleetId);
}

$this->currentJourney->cancel();
$this->getCurrentJourney()->cancel();
}

public function getLoadCapacity(): int
Expand All @@ -312,6 +324,7 @@ public function getLoadCapacity(): int
return $capacity;
}

/** @return array<string, int> */
public function getResourcesLoad(): array
{
return $this->resourcesLoad->toScalarArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

final class FleetHasCancelledJourneyEvent implements EventInterface
{
/** @var array<string, int> $resourcesLoad */
/** @param array<string, int> $resourcesLoad */
public function __construct(
private readonly string $fleetId,
private readonly string $targetGalaxyPoint,
Expand All @@ -32,6 +32,7 @@ public function getTargetGalaxyPoint(): string
return $this->targetGalaxyPoint;
}

/** @return array<string, int> */
public function getResourcesLoad(): array
{
return $this->resourcesLoad;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

final class FleetHasReachedJourneyReturnPointEvent implements EventInterface
{
/** @var array<string, int> $resourcesLoad */
/** @param array<string, int> $resourcesLoad */
public function __construct(
private readonly string $fleetId,
private readonly string $startGalaxyPoint,
Expand Down Expand Up @@ -44,6 +44,7 @@ public function getReturnGalaxyPoint(): string
return $this->returnGalaxyPoint;
}

/** @return array<string, int> */
public function getResourcesLoad(): array
{
return $this->resourcesLoad;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

final class FleetHasReachedJourneyTargetPointEvent implements EventInterface
{
/** @var array<string, int> $resourcesLoad */
/** @param array<string, int> $resourcesLoad */
public function __construct(
private readonly string $mission,
private readonly string $fleetId,
Expand Down Expand Up @@ -38,6 +38,7 @@ public function getTargetGalaxyPoint(): string
return $this->targetGalaxyPoint;
}

/** @return array<string, int> */
public function getResourcesLoad(): array
{
return $this->resourcesLoad;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@

final class FleetHasStartedJourneyEvent implements EventInterface
{
/** @var array<string, int> $fuelRequirements */
/**
* @param array<string, int> $fuelRequirements
* @param array<string, int> $resourcesLoad
*/
public function __construct(
private readonly string $planetId,
private readonly string $fleetId,
Expand Down Expand Up @@ -51,11 +54,13 @@ public function getTargetGalaxyPoint(): string
return $this->targetGalaxyPoint;
}

/** @return array<string, int> */
public function getFuelRequirements(): array
{
return $this->fuelRequirements;
}

/** @return array<string, int> */
public function getResourcesLoad(): array
{
return $this->resourcesLoad;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ final class CannotCancelFleetJourneyOnReachingReturnPointException extends Domai
public function __construct(FleetIdInterface $fleetId)
{
$message = sprintf(
'Cannot cancel fleet % journey (it\'s reaching return point)',
'Cannot cancel fleet %s journey (it\'s reaching return point)',
$fleetId->getUuid(),
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public function __construct(FleetIdInterface $fleetId, ?int $timeLeft = null)
$message = sprintf(
'Fleet %s has not yet reached the target point',
$fleetId->getUuid(),
$timeLeft,
);

if ($timeLeft !== null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ final class FleetNotOnFlyBackException extends DomainException
public function __construct(FleetIdInterface $fleetId)
{
$message = sprintf(
'Fleet is not on flyback',
'Fleet %s is not on flyback',
$fleetId->getUuid(),
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ final class FleetOnFlyBackException extends DomainException
public function __construct(FleetIdInterface $fleetId)
{
$message = sprintf(
'Fleet is on flyback',
'Fleet %s is on flyback',
$fleetId->getUuid(),
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public function __invoke(FleetHasReachedJourneyTargetPointEvent $event): void

$stationingPoint = GalaxyPoint::fromString($event->getTargetGalaxyPoint());
$planetId = $this->navigator->getPlanetId($stationingPoint);
if ($planetId === null) {
throw new InconsistentModelException(sprintf('Planet %s doesn\'t exist', $event->getTargetGalaxyPoint()));
}

$fleetCurrentlyStationingOnPlanet = $this->fleetRepository->findStationingOnPlanet($planetId);
if ($fleetCurrentlyStationingOnPlanet === null) {
$fleetJoiningPlanet->landOnPlanet($stationingPoint);
Expand Down
Loading

0 comments on commit 2a8dc4a

Please sign in to comment.