Skip to content

Commit 5c5db46

Browse files
committed
refactor: Fix most PHPStan errors
- 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.
1 parent b3b2a6a commit 5c5db46

File tree

7 files changed

+24
-19
lines changed

7 files changed

+24
-19
lines changed

Command/Tool/CalendarCommand.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7676
}
7777
$time = sprintf('%02s:%02s', $parts[1], $parts[0]);
7878

79-
$duplicates = array_filter($data, function ($row) use ($day, $time, $cron): bool {
80-
return $row[4]->getNextRunDate()->format('c') === $cron->getNextRunDate()->format('c');
81-
// return ($row[1] === $day && $row[3] === $time) || ($row[1] === '*' && $row[3] === $time) || ($day === '*' && $row[3] === $time);
82-
});
79+
$duplicates = array_filter(
80+
$data,
81+
fn (array $row): bool => $row[4]->getNextRunDate()->format('c') === $cron->getNextRunDate()->format('c')
82+
// fn (array $row): bool => ($row[1] === $day && $row[3] === $time) || ($row[1] === '*' && $row[3] === $time) || ($day === '*' && $row[3] === $time)
83+
);
8384
$warning = count($duplicates) > 0 ? '⚠ Duplicate' : null;
8485

8586
$data[] = [
@@ -95,26 +96,28 @@ protected function execute(InputInterface $input, OutputInterface $output): int
9596
}
9697
}
9798

98-
$nextRun = array_map(function ($row) {
99-
return $row[4]->getNextRunDate()->format('c');
100-
}, $data);
99+
$nextRun = array_map(
100+
fn (array $row): string => $row[4]->getNextRunDate()->format('c'),
101+
$data
102+
);
101103
array_multisort(
102104
$nextRun,
103105
SORT_ASC,
104106
$data
105107
);
106108

107-
$display = array_map(function ($row) {
108-
return [
109+
$display = array_map(
110+
fn (array $row): array => [
109111
$row[0],
110112
$row[1],
111113
$row[2],
112114
$row[3],
113115
$row[4]->getExpression(),
114116
$row[4]->getNextRunDate()->format('d M Y H:i'),
115117
$row[5],
116-
];
117-
}, $data);
118+
],
119+
$data
120+
);
118121

119122
$table = new Table($output);
120123
$table->setHeaders(['City', 'Day of week', 'Day of month', 'Time', 'Cron', 'Next run', '']);

Model/Details/Details.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Details
1111
public ?array $labels;
1212
/** @var null|array<string,LanguageValue> */
1313
public ?array $descriptions;
14-
/** @var array<string,string> */
14+
/** @var array<string,object> */
1515
public ?array $nicknames;
1616
public ?int $birth;
1717
public ?int $death;

Model/GeoJSON/Feature.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Feature implements JsonSerializable
1212
public Properties $properties;
1313
public ?Geometry $geometry;
1414

15-
public function jsonSerialize()
15+
public function jsonSerialize(): array
1616
{
1717
return [
1818
'type' => $this->type,

Model/GeoJSON/FeatureCollection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class FeatureCollection implements JsonSerializable
1010
/** @var Feature[] $features */
1111
public array $features = [];
1212

13-
public function jsonSerialize()
13+
public function jsonSerialize(): array
1414
{
1515
return [
1616
'type' => $this->type,

Model/GeoJSON/Geometry/Geometry.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function __construct(array $coordinates)
1919
$this->coordinates = $coordinates;
2020
}
2121

22-
public function jsonSerialize()
22+
public function jsonSerialize(): array
2323
{
2424
return [
2525
'type' => $this->type,

Model/GeoJSON/Properties.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Properties implements JsonSerializable
1414
/** @var null|Details|Details[] */
1515
public $details;
1616

17-
public function jsonSerialize()
17+
public function jsonSerialize(): array
1818
{
1919
return [
2020
'name' => $this->name,

Wikidata/Wikidata.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public static function extractSitelinks($entity, array $languages): array
9292
* @param Entity $entity
9393
* @param string[] $languages
9494
*
95-
* @return null|array<string,string>
95+
* @return null|array<string,object>
9696
*/
9797
public static function extractNicknames($entity, array $languages): ?array
9898
{
@@ -101,10 +101,12 @@ public static function extractNicknames($entity, array $languages): ?array
101101
$claims = $entity->claims->P1449 ?? [];
102102

103103
foreach ($claims as $value) {
104-
$language = $value->mainsnak->datavalue->value->language; // @phpstan-ignore-line
104+
/** @var \stdClass */
105+
$mainValue = $value->mainsnak->datavalue->value; // @phpstan-ignore-line
106+
$language = $mainValue->language;
105107

106108
if (in_array($language, $languages, true)) {
107-
$nicknames[$language] = $value->mainsnak->datavalue->value; // @phpstan-ignore-line
109+
$nicknames[$language] = $mainValue;
108110
}
109111
}
110112

0 commit comments

Comments
 (0)