Skip to content

Commit 47b7dae

Browse files
Merge pull request #666 from CPS-IT/task/phpstan-level-10
[TASK] Migrate codebase to PHPStan level 10
2 parents 0b30938 + e545341 commit 47b7dae

File tree

9 files changed

+49
-17
lines changed

9 files changed

+49
-17
lines changed

phpstan.neon

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ includes:
22
- .build/vendor/phpstan/phpstan/conf/bleedingEdge.neon
33

44
parameters:
5-
level: 9
5+
level: max
66
paths:
77
- src
88
- tests/src

src/DependencyInjection/CompilerPass/EventListenerPass.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,16 @@ public function process(DependencyInjection\ContainerBuilder $container): void
6262
continue;
6363
}
6464

65-
if (null === ($className = $service->getClass())) {
65+
/** @var class-string|null $className */
66+
$className = $service->getClass();
67+
68+
if (null === $className) {
6669
throw Exception\ShouldNotHappenException::create();
6770
}
6871

72+
/** @var array{method?: string, event?: class-string} $tag */
6973
foreach ($tags as $tag) {
7074
$method = $tag['method'] ?? '__invoke';
71-
/** @var class-string $className */
7275
$event = $tag['event'] ?? $this->determineEventFromClassMethod($className, $method);
7376

7477
$dispatcher->addMethodCall(

src/Exception/InvalidConfigurationException.php

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public static function forValidationErrors(?JsonSchema\Errors\ValidationError $e
8686

8787
if (null !== $error) {
8888
$formatter = new JsonSchema\Errors\ErrorFormatter();
89+
/** @var array<string, string> $formattedErrors */
8990
$formattedErrors = $formatter->format($error, false);
9091

9192
foreach ($formattedErrors as $path => $errorMessage) {

src/Helper/ArrayHelper.php

+3
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ public static function trimExplode(string $subject, string $delimiter = ','): ar
9696
);
9797
}
9898

99+
/**
100+
* @phpstan-assert-if-true array<string, mixed>|ArrayObject<string, mixed> $subject
101+
*/
99102
private static function pathSegmentExists(mixed $subject, string $pathSegment): bool
100103
{
101104
if (!is_array($subject) && !($subject instanceof ArrayObject)) {

src/Resource/Http/PhpApiClient.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use Psr\Http\Message;
3131
use stdClass;
3232

33+
use function is_string;
3334
use function property_exists;
3435

3536
/**
@@ -65,7 +66,7 @@ public function getLatestStableVersion(string $branch): string
6566
$json = Utils::jsonDecode((string) $response->getBody());
6667

6768
// Fall back to .0 release if version cannot be determined via API
68-
if (!($json instanceof stdClass) || !property_exists($json, 'version')) {
69+
if (!($json instanceof stdClass) || !property_exists($json, 'version') || !is_string($json->version)) {
6970
return $branch.'.0';
7071
}
7172

src/Resource/Local/Git.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
use SebastianFeldmann\Cli;
2727

28+
use function reset;
2829
use function trim;
2930

3031
/**
@@ -66,13 +67,15 @@ private function readConfig(string $configPath): ?string
6667
private function run(Cli\Command $command): ?string
6768
{
6869
$result = $this->runner->run($command);
70+
/** @var list<string> $output */
71+
$output = $result->getBufferedOutput();
6972

70-
if (!$result->isSuccessful() || [] === $result->getBufferedOutput()) {
73+
if (!$result->isSuccessful() || [] === $output) {
7174
return null;
7275
}
7376

74-
$output = $result->getBufferedOutput()[0];
77+
$output = reset($output);
7578

76-
return '' !== trim((string) $output) ? trim((string) $output) : null;
79+
return '' !== trim($output) ? trim($output) : null;
7780
}
7881
}

src/Template/Provider/BaseProvider.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,10 @@ protected function isPackageSupported(Package\BasePackage $package): bool
244244
return false;
245245
}
246246

247+
/** @var array<string, mixed> $extra */
248+
$extra = $package->getExtra();
247249
$excludeFromListing = (bool) Helper\ArrayHelper::getValueByPath(
248-
$package->getExtra(),
250+
$extra,
249251
'cpsit/project-builder.exclude-from-listing',
250252
);
251253

src/Twig/Renderer.php

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public function render(
5656
?string $template = null,
5757
array $variables = [],
5858
): string {
59+
/** @var array<string, mixed> $mergedVariables */
5960
$mergedVariables = array_replace_recursive($instructions->getTemplateVariables(), $variables);
6061
$event = new Event\BeforeTemplateRenderedEvent($this->twig, $instructions, $mergedVariables);
6162
$template ??= $this->defaultTemplate;

tests/src/Template/Provider/BaseProviderTest.php

+27-9
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
use function array_map;
4040
use function array_reduce;
4141
use function dirname;
42+
use function is_array;
4243
use function json_encode;
4344
use function reset;
4445
use function sprintf;
@@ -276,24 +277,35 @@ public static function listTemplateSourcesListsAllAvailableTemplateSourcesDataPr
276277
],
277278
[],
278279
];
280+
281+
$package1 = self::createPackage('foo/baz-2');
282+
$package2 = self::createPackage('foo/baz-3');
283+
279284
yield 'unsupported and supported packages' => [
280285
[
281286
self::createPackage('foo/baz-1', 'library'),
282-
$package1 = self::createPackage('foo/baz-2'),
283-
$package2 = self::createPackage('foo/baz-3'),
287+
$package1,
288+
$package2,
284289
],
285290
[
286291
$package1,
287292
$package2,
288293
],
289294
];
295+
296+
$abandonedPackage1 = self::createPackage(name: 'foo/baz-1', abandoned: true);
297+
$abandonedPackage2 = self::createPackage(name: 'foo/baz-3', abandoned: 'foo/bar-3');
298+
$package1 = self::createPackage('foo/baz-2');
299+
$package2 = self::createPackage('foo/baz-4');
300+
$package3 = self::createPackage('foo/baz-5');
301+
290302
yield 'abandoned packages after maintained' => [
291303
[
292-
$abandonedPackage1 = self::createPackage(name: 'foo/baz-1', abandoned: true),
293-
$package1 = self::createPackage('foo/baz-2'),
294-
$abandonedPackage2 = self::createPackage(name: 'foo/baz-3', abandoned: 'foo/bar-3'),
295-
$package2 = self::createPackage('foo/baz-4'),
296-
$package3 = self::createPackage('foo/baz-5'),
304+
$abandonedPackage1,
305+
$package1,
306+
$abandonedPackage2,
307+
$package2,
308+
$package3,
297309
],
298310
[
299311
$package1,
@@ -385,8 +397,14 @@ private function mockPackagesServerResponse(array $packages): void
385397
[
386398
'packages' => array_reduce(
387399
$packages,
388-
function (array $carry, Package\PackageInterface $package) use ($dumper): array {
389-
$carry[$package->getName()][$package->getPrettyVersion()] = $dumper->dump($package);
400+
static function (array $carry, Package\PackageInterface $package) use ($dumper): array {
401+
$packageName = $package->getName();
402+
403+
if (!is_array($carry[$packageName] ?? null)) {
404+
$carry[$packageName] = [];
405+
}
406+
407+
$carry[$packageName][$package->getPrettyVersion()] = $dumper->dump($package);
390408

391409
return $carry;
392410
},

0 commit comments

Comments
 (0)