From 7ef9b64bc7e04e8c3a27639f5106e8515f333869 Mon Sep 17 00:00:00 2001 From: Doug Wright Date: Sun, 4 Aug 2024 13:16:46 +0100 Subject: [PATCH] Make PHPStan happier --- ...aseline.neon.dist => phpstan-baseline.neon | 38 +++++-------------- phpstan.neon.dist | 2 +- src/CoordinateSystem/CoordinateSystem.php | 9 ++++- src/Point/ProjectedPoint.php | 9 +++-- src/UnitOfMeasure/Angle/Degree.php | 2 +- src/UnitOfMeasure/Rate.php | 14 +++---- src/UnitOfMeasure/Time/Year.php | 6 ++- 7 files changed, 34 insertions(+), 46 deletions(-) rename phpstan-baseline.neon.dist => phpstan-baseline.neon (80%) diff --git a/phpstan-baseline.neon.dist b/phpstan-baseline.neon similarity index 80% rename from phpstan-baseline.neon.dist rename to phpstan-baseline.neon index 3b5dbbbf2..bef38b50b 100644 --- a/phpstan-baseline.neon.dist +++ b/phpstan-baseline.neon @@ -41,24 +41,24 @@ parameters: path: src/Point/CompoundPoint.php - - message: "#^Call to an undefined method PHPCoord\\\\UnitOfMeasure\\\\UnitOfMeasure\\:\\:multiply\\(\\)\\.$#" - count: 14 + message: "#^Instanceof between PHPCoord\\\\CoordinateReferenceSystem\\\\Geocentric and PHPCoord\\\\CoordinateReferenceSystem\\\\Compound will always evaluate to false\\.$#" + count: 1 path: src/Point/GeocentricPoint.php - - message: "#^Cannot call method getUnitOfMeasureId\\(\\) on PHPCoord\\\\CoordinateSystem\\\\Axis\\|null\\.$#" - count: 3 + message: "#^Parameter \\#1 \\$unit of method PHPCoord\\\\UnitOfMeasure\\\\Angle\\\\Angle\\:\\:add\\(\\) expects PHPCoord\\\\UnitOfMeasure\\\\Angle\\\\Angle, PHPCoord\\\\UnitOfMeasure\\\\Angle\\\\Angle\\|PHPCoord\\\\UnitOfMeasure\\\\Length\\\\Length\\|PHPCoord\\\\UnitOfMeasure\\\\Scale\\\\Scale given\\.$#" + count: 6 path: src/Point/GeocentricPoint.php - - message: "#^Instanceof between PHPCoord\\\\CoordinateReferenceSystem\\\\Geocentric and PHPCoord\\\\CoordinateReferenceSystem\\\\Compound will always evaluate to false\\.$#" - count: 1 + message: "#^Parameter \\#1 \\$unit of method PHPCoord\\\\UnitOfMeasure\\\\Length\\\\Length\\:\\:add\\(\\) expects PHPCoord\\\\UnitOfMeasure\\\\Length\\\\Length, PHPCoord\\\\UnitOfMeasure\\\\Angle\\\\Angle\\|PHPCoord\\\\UnitOfMeasure\\\\Length\\\\Length\\|PHPCoord\\\\UnitOfMeasure\\\\Scale\\\\Scale given\\.$#" + count: 6 path: src/Point/GeocentricPoint.php - - message: "#^Cannot call method getUnitOfMeasureId\\(\\) on PHPCoord\\\\CoordinateSystem\\\\Axis\\|null\\.$#" - count: 3 - path: src/Point/GeographicPoint.php + message: "#^Parameter \\#1 \\$unit of method PHPCoord\\\\UnitOfMeasure\\\\Scale\\\\Scale\\:\\:add\\(\\) expects PHPCoord\\\\UnitOfMeasure\\\\Scale\\\\Scale, PHPCoord\\\\UnitOfMeasure\\\\Angle\\\\Angle\\|PHPCoord\\\\UnitOfMeasure\\\\Length\\\\Length\\|PHPCoord\\\\UnitOfMeasure\\\\Scale\\\\Scale given\\.$#" + count: 2 + path: src/Point/GeocentricPoint.php - message: "#^Instanceof between PHPCoord\\\\CoordinateReferenceSystem\\\\Geographic2D\\|PHPCoord\\\\CoordinateReferenceSystem\\\\Geographic3D and PHPCoord\\\\CoordinateReferenceSystem\\\\Compound will always evaluate to false\\.$#" @@ -104,23 +104,3 @@ parameters: message: "#^Parameter \\#2 \\$northing of class PHPCoord\\\\Point\\\\IrishTransverseMercatorPoint constructor expects PHPCoord\\\\UnitOfMeasure\\\\Length\\\\Length, PHPCoord\\\\UnitOfMeasure\\\\Length\\\\Length\\|null given\\.$#" count: 1 path: src/Point/ProjectedPoint.php - - - - message: "#^Left side of \\|\\| is always false\\.$#" - count: 1 - path: src/UnitOfMeasure/Angle/Degree.php - - - - message: "#^Call to an undefined method PHPCoord\\\\UnitOfMeasure\\\\UnitOfMeasure\\:\\:divide\\(\\)\\.$#" - count: 1 - path: src/UnitOfMeasure/Rate.php - - - - message: "#^Call to an undefined method PHPCoord\\\\UnitOfMeasure\\\\UnitOfMeasure\\:\\:multiply\\(\\)\\.$#" - count: 1 - path: src/UnitOfMeasure/Rate.php - - - - message: "#^Method PHPCoord\\\\UnitOfMeasure\\\\Time\\\\Year\\:\\:asDateTime\\(\\) should return DateTimeImmutable but returns DateTimeImmutable\\|false\\.$#" - count: 1 - path: src/UnitOfMeasure/Time/Year.php diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 47de3c2d2..4392d2485 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -1,5 +1,5 @@ includes: - - phpstan-baseline.neon.dist + - phpstan-baseline.neon parameters: level: 9 diff --git a/src/CoordinateSystem/CoordinateSystem.php b/src/CoordinateSystem/CoordinateSystem.php index 01a8cc805..e39d1785d 100644 --- a/src/CoordinateSystem/CoordinateSystem.php +++ b/src/CoordinateSystem/CoordinateSystem.php @@ -58,8 +58,13 @@ public function getAxes(): array return $this->axes; } - public function getAxisByName(string $name): ?Axis + public function getAxisByName(string $name): Axis { - return $this->axesByName[$name] ?? null; + return $this->axesByName[$name]; + } + + public function hasAxisByName(string $name): bool + { + return isset($this->axesByName[$name]); } } diff --git a/src/Point/ProjectedPoint.php b/src/Point/ProjectedPoint.php index 964b1bf54..b4b6df154 100644 --- a/src/Point/ProjectedPoint.php +++ b/src/Point/ProjectedPoint.php @@ -116,11 +116,12 @@ protected function __construct(Projected $crs, ?Length $easting, ?Length $northi } $this->crs = $crs; + $cs = $this->crs->getCoordinateSystem(); - $eastingAxis = $this->crs->getCoordinateSystem()->getAxisByName(Axis::EASTING); - $westingAxis = $this->crs->getCoordinateSystem()->getAxisByName(Axis::WESTING); - $northingAxis = $this->crs->getCoordinateSystem()->getAxisByName(Axis::NORTHING); - $southingAxis = $this->crs->getCoordinateSystem()->getAxisByName(Axis::SOUTHING); + $eastingAxis = $cs->hasAxisByName(Axis::EASTING) ? $cs->getAxisByName(Axis::EASTING) : null; + $westingAxis = $cs->hasAxisByName(Axis::WESTING) ? $cs->getAxisByName(Axis::WESTING) : null; + $northingAxis = $cs->hasAxisByName(Axis::NORTHING) ? $cs->getAxisByName(Axis::NORTHING) : null; + $southingAxis = $cs->hasAxisByName(Axis::SOUTHING) ? $cs->getAxisByName(Axis::SOUTHING) : null; if ($easting && $eastingAxis) { $this->easting = $easting::convert($easting, $eastingAxis->getUnitOfMeasureId()); diff --git a/src/UnitOfMeasure/Angle/Degree.php b/src/UnitOfMeasure/Angle/Degree.php index 4dbe4cb8b..5678d833f 100644 --- a/src/UnitOfMeasure/Angle/Degree.php +++ b/src/UnitOfMeasure/Angle/Degree.php @@ -157,7 +157,7 @@ private static function fromRegex(string $angle, string $regex): self $degrees += ((float) ($angleParts['arcseconds'] ?? 0) / 3600); $degrees += isset($angleParts['fractionarcseconds']) ? ((float) $angleParts['fractionarcseconds'] / 3600 / 10 ** strlen($angleParts['fractionarcseconds'])) : 0; - if ($angleParts['negative'] ?? '' || in_array($angleParts['hemisphere'] ?? [], ['S', 'W'], true)) { + if (($angleParts['negative'] ?? '') || in_array($angleParts['hemisphere'] ?? [], ['S', 'W'], true)) { $degrees *= -1; } diff --git a/src/UnitOfMeasure/Rate.php b/src/UnitOfMeasure/Rate.php index 4ebd939e5..97af8a562 100644 --- a/src/UnitOfMeasure/Rate.php +++ b/src/UnitOfMeasure/Rate.php @@ -9,15 +9,16 @@ namespace PHPCoord\UnitOfMeasure; -use PHPCoord\Exception\InvalidRateException; use PHPCoord\Exception\UnknownUnitOfMeasureException; use PHPCoord\UnitOfMeasure\Angle\Angle; use PHPCoord\UnitOfMeasure\Angle\ArcSecond; use PHPCoord\UnitOfMeasure\Length\Centimetre; +use PHPCoord\UnitOfMeasure\Length\Length; use PHPCoord\UnitOfMeasure\Length\Metre; use PHPCoord\UnitOfMeasure\Length\Millimetre; use PHPCoord\UnitOfMeasure\Scale\PartsPerBillion; use PHPCoord\UnitOfMeasure\Scale\PartsPerMillion; +use PHPCoord\UnitOfMeasure\Scale\Scale; use PHPCoord\UnitOfMeasure\Time\Time; use PHPCoord\UnitOfMeasure\Time\Year; @@ -104,25 +105,22 @@ class Rate implements UnitOfMeasure ], ]; - private UnitOfMeasure $change; + private Angle|Length|Scale $change; private Time $time; - public function __construct(UnitOfMeasure $change, Time $time) + public function __construct(Angle|Length|Scale $change, Time $time) { - if ($change instanceof Time) { - throw new InvalidRateException('A rate is a change per unit of time, the change cannot be time'); - } $this->change = $change; $this->time = $time; } - public function getChange(): UnitOfMeasure + public function getChange(): Angle|Length|Scale { return $this->change; } - public function getChangePerYear(): UnitOfMeasure + public function getChangePerYear(): Angle|Length|Scale { return $this->change->divide($this->time->asYears()->getValue()); } diff --git a/src/UnitOfMeasure/Time/Year.php b/src/UnitOfMeasure/Time/Year.php index 73e6a4e50..812aad15b 100644 --- a/src/UnitOfMeasure/Time/Year.php +++ b/src/UnitOfMeasure/Time/Year.php @@ -12,6 +12,7 @@ use DateTimeInterface; use function round; +use function assert; class Year extends Time { @@ -38,7 +39,10 @@ public function asDateTime(): DateTimeImmutable $yearPortion = $this->time - $year; $days = round($yearPortion * 365.25); - return DateTimeImmutable::createFromFormat('Yz', $year . $days); + $date = DateTimeImmutable::createFromFormat('Yz', $year . $days); + assert($date instanceof DateTimeImmutable); + + return $date; } public function getValue(): float