From e242e1b5a34b14a7ce25d890b02eaa5af7d74f24 Mon Sep 17 00:00:00 2001 From: Tacsipacsi Date: Thu, 25 Sep 2025 02:43:10 +0200 Subject: [PATCH] refactor: Fix most PHPStan errors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add missing return types. In closures touched anyway, also add parameter types. - Avoid capturing unused variables. They are used in the commented-out part; to make the commented-out part work out of the box if uncommented, use arrow functions (available since PHP 7.4) instead of traditional anonymous functions: arrow functions capture variables automatically, so the `use` block can simply be skipped. Use arrow functions for other touched closures as well. - Add return types to `JsonSerializable::jsonSerialize()` overrides. The lack of return types cause deprecation warnings on PHP 8. The recommended return type of `mixed` cannot be used on PHP 7.4 (which we still support according to `composer.json`), but fortunately all classes consistently return arrays, so `array` could be added as a return type instead (which is available in PHP 7.4 as well, and which is stricter than the type stated by the interface – covariant return types are allowed since PHP 7.4). - Fix a wrongly documented type: the nicknames are objects, not strings. Refactor the code a bit to make it easier to tell this PHPStan. I skipped one error that looks real and that I don’t know how to fix: the documentation states that `App\Model\Overpass\Element::$tags` is nullable but it’s used as if it wasn’t; I don’t know Overpass, so I have no idea which one is right. --- Command/Tool/CalendarCommand.php | 25 ++++++++++++++----------- Model/Details/Details.php | 2 +- Model/GeoJSON/Feature.php | 2 +- Model/GeoJSON/FeatureCollection.php | 2 +- Model/GeoJSON/Geometry/Geometry.php | 2 +- Model/GeoJSON/Properties.php | 2 +- Wikidata/Wikidata.php | 8 +++++--- 7 files changed, 24 insertions(+), 19 deletions(-) diff --git a/Command/Tool/CalendarCommand.php b/Command/Tool/CalendarCommand.php index 6a6a9f7..ad674ad 100644 --- a/Command/Tool/CalendarCommand.php +++ b/Command/Tool/CalendarCommand.php @@ -76,10 +76,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int } $time = sprintf('%02s:%02s', $parts[1], $parts[0]); - $duplicates = array_filter($data, function ($row) use ($day, $time, $cron): bool { - return $row[4]->getNextRunDate()->format('c') === $cron->getNextRunDate()->format('c'); - // return ($row[1] === $day && $row[3] === $time) || ($row[1] === '*' && $row[3] === $time) || ($day === '*' && $row[3] === $time); - }); + $duplicates = array_filter( + $data, + fn (array $row): bool => $row[4]->getNextRunDate()->format('c') === $cron->getNextRunDate()->format('c') + // fn (array $row): bool => ($row[1] === $day && $row[3] === $time) || ($row[1] === '*' && $row[3] === $time) || ($day === '*' && $row[3] === $time) + ); $warning = count($duplicates) > 0 ? '⚠ Duplicate' : null; $data[] = [ @@ -95,17 +96,18 @@ protected function execute(InputInterface $input, OutputInterface $output): int } } - $nextRun = array_map(function ($row) { - return $row[4]->getNextRunDate()->format('c'); - }, $data); + $nextRun = array_map( + fn (array $row): string => $row[4]->getNextRunDate()->format('c'), + $data + ); array_multisort( $nextRun, SORT_ASC, $data ); - $display = array_map(function ($row) { - return [ + $display = array_map( + fn (array $row): array => [ $row[0], $row[1], $row[2], @@ -113,8 +115,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int $row[4]->getExpression(), $row[4]->getNextRunDate()->format('d M Y H:i'), $row[5], - ]; - }, $data); + ], + $data + ); $table = new Table($output); $table->setHeaders(['City', 'Day of week', 'Day of month', 'Time', 'Cron', 'Next run', '']); diff --git a/Model/Details/Details.php b/Model/Details/Details.php index 93edc57..005ce5f 100644 --- a/Model/Details/Details.php +++ b/Model/Details/Details.php @@ -11,7 +11,7 @@ class Details public ?array $labels; /** @var null|array */ public ?array $descriptions; - /** @var array */ + /** @var array */ public ?array $nicknames; public ?int $birth; public ?int $death; diff --git a/Model/GeoJSON/Feature.php b/Model/GeoJSON/Feature.php index 69070af..9f904bb 100644 --- a/Model/GeoJSON/Feature.php +++ b/Model/GeoJSON/Feature.php @@ -12,7 +12,7 @@ class Feature implements JsonSerializable public Properties $properties; public ?Geometry $geometry; - public function jsonSerialize() + public function jsonSerialize(): array { return [ 'type' => $this->type, diff --git a/Model/GeoJSON/FeatureCollection.php b/Model/GeoJSON/FeatureCollection.php index 0b91d7b..d49277e 100644 --- a/Model/GeoJSON/FeatureCollection.php +++ b/Model/GeoJSON/FeatureCollection.php @@ -10,7 +10,7 @@ class FeatureCollection implements JsonSerializable /** @var Feature[] $features */ public array $features = []; - public function jsonSerialize() + public function jsonSerialize(): array { return [ 'type' => $this->type, diff --git a/Model/GeoJSON/Geometry/Geometry.php b/Model/GeoJSON/Geometry/Geometry.php index b50708f..493b86f 100644 --- a/Model/GeoJSON/Geometry/Geometry.php +++ b/Model/GeoJSON/Geometry/Geometry.php @@ -19,7 +19,7 @@ public function __construct(array $coordinates) $this->coordinates = $coordinates; } - public function jsonSerialize() + public function jsonSerialize(): array { return [ 'type' => $this->type, diff --git a/Model/GeoJSON/Properties.php b/Model/GeoJSON/Properties.php index 3438015..a7c9a50 100644 --- a/Model/GeoJSON/Properties.php +++ b/Model/GeoJSON/Properties.php @@ -14,7 +14,7 @@ class Properties implements JsonSerializable /** @var null|Details|Details[] */ public $details; - public function jsonSerialize() + public function jsonSerialize(): array { return [ 'name' => $this->name, diff --git a/Wikidata/Wikidata.php b/Wikidata/Wikidata.php index fb1c8d6..2bcc407 100644 --- a/Wikidata/Wikidata.php +++ b/Wikidata/Wikidata.php @@ -92,7 +92,7 @@ public static function extractSitelinks($entity, array $languages): array * @param Entity $entity * @param string[] $languages * - * @return null|array + * @return null|array */ public static function extractNicknames($entity, array $languages): ?array { @@ -101,10 +101,12 @@ public static function extractNicknames($entity, array $languages): ?array $claims = $entity->claims->P1449 ?? []; foreach ($claims as $value) { - $language = $value->mainsnak->datavalue->value->language; // @phpstan-ignore-line + /** @var \stdClass */ + $mainValue = $value->mainsnak->datavalue->value; // @phpstan-ignore-line + $language = $mainValue->language; if (in_array($language, $languages, true)) { - $nicknames[$language] = $value->mainsnak->datavalue->value; // @phpstan-ignore-line + $nicknames[$language] = $mainValue; } }