Skip to content

Commit

Permalink
Make PHPStan happier
Browse files Browse the repository at this point in the history
  • Loading branch information
dvdoug committed Aug 4, 2024
1 parent 9ef2b1f commit 7ef9b64
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 46 deletions.
38 changes: 9 additions & 29 deletions phpstan-baseline.neon.dist → phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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\\.$#"
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
includes:
- phpstan-baseline.neon.dist
- phpstan-baseline.neon

parameters:
level: 9
Expand Down
9 changes: 7 additions & 2 deletions src/CoordinateSystem/CoordinateSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}
9 changes: 5 additions & 4 deletions src/Point/ProjectedPoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion src/UnitOfMeasure/Angle/Degree.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
14 changes: 6 additions & 8 deletions src/UnitOfMeasure/Rate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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());
}
Expand Down
6 changes: 5 additions & 1 deletion src/UnitOfMeasure/Time/Year.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use DateTimeInterface;

use function round;
use function assert;

class Year extends Time
{
Expand All @@ -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
Expand Down

0 comments on commit 7ef9b64

Please sign in to comment.