Skip to content

Commit

Permalink
[TASK] Migrate codebase to PHPStan level 10
Browse files Browse the repository at this point in the history
  • Loading branch information
eliashaeussler committed Dec 4, 2024
1 parent 0b30938 commit e545341
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 17 deletions.
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ includes:
- .build/vendor/phpstan/phpstan/conf/bleedingEdge.neon

parameters:
level: 9
level: max
paths:
- src
- tests/src
Expand Down
7 changes: 5 additions & 2 deletions src/DependencyInjection/CompilerPass/EventListenerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,16 @@ public function process(DependencyInjection\ContainerBuilder $container): void
continue;
}

if (null === ($className = $service->getClass())) {
/** @var class-string|null $className */
$className = $service->getClass();

if (null === $className) {
throw Exception\ShouldNotHappenException::create();
}

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

$dispatcher->addMethodCall(
Expand Down
1 change: 1 addition & 0 deletions src/Exception/InvalidConfigurationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public static function forValidationErrors(?JsonSchema\Errors\ValidationError $e

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

foreach ($formattedErrors as $path => $errorMessage) {
Expand Down
3 changes: 3 additions & 0 deletions src/Helper/ArrayHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ public static function trimExplode(string $subject, string $delimiter = ','): ar
);
}

/**
* @phpstan-assert-if-true array<string, mixed>|ArrayObject<string, mixed> $subject
*/
private static function pathSegmentExists(mixed $subject, string $pathSegment): bool
{
if (!is_array($subject) && !($subject instanceof ArrayObject)) {
Expand Down
3 changes: 2 additions & 1 deletion src/Resource/Http/PhpApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Psr\Http\Message;
use stdClass;

use function is_string;
use function property_exists;

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

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

Expand Down
9 changes: 6 additions & 3 deletions src/Resource/Local/Git.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use SebastianFeldmann\Cli;

use function reset;
use function trim;

/**
Expand Down Expand Up @@ -66,13 +67,15 @@ private function readConfig(string $configPath): ?string
private function run(Cli\Command $command): ?string
{
$result = $this->runner->run($command);
/** @var list<string> $output */
$output = $result->getBufferedOutput();

if (!$result->isSuccessful() || [] === $result->getBufferedOutput()) {
if (!$result->isSuccessful() || [] === $output) {
return null;
}

$output = $result->getBufferedOutput()[0];
$output = reset($output);

return '' !== trim((string) $output) ? trim((string) $output) : null;
return '' !== trim($output) ? trim($output) : null;
}
}
4 changes: 3 additions & 1 deletion src/Template/Provider/BaseProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,10 @@ protected function isPackageSupported(Package\BasePackage $package): bool
return false;
}

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

Expand Down
1 change: 1 addition & 0 deletions src/Twig/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function render(
?string $template = null,
array $variables = [],
): string {
/** @var array<string, mixed> $mergedVariables */
$mergedVariables = array_replace_recursive($instructions->getTemplateVariables(), $variables);
$event = new Event\BeforeTemplateRenderedEvent($this->twig, $instructions, $mergedVariables);
$template ??= $this->defaultTemplate;
Expand Down
36 changes: 27 additions & 9 deletions tests/src/Template/Provider/BaseProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
use function array_map;
use function array_reduce;
use function dirname;
use function is_array;
use function json_encode;
use function reset;
use function sprintf;
Expand Down Expand Up @@ -276,24 +277,35 @@ public static function listTemplateSourcesListsAllAvailableTemplateSourcesDataPr
],
[],
];

$package1 = self::createPackage('foo/baz-2');
$package2 = self::createPackage('foo/baz-3');

yield 'unsupported and supported packages' => [
[
self::createPackage('foo/baz-1', 'library'),
$package1 = self::createPackage('foo/baz-2'),
$package2 = self::createPackage('foo/baz-3'),
$package1,
$package2,
],
[
$package1,
$package2,
],
];

$abandonedPackage1 = self::createPackage(name: 'foo/baz-1', abandoned: true);
$abandonedPackage2 = self::createPackage(name: 'foo/baz-3', abandoned: 'foo/bar-3');
$package1 = self::createPackage('foo/baz-2');
$package2 = self::createPackage('foo/baz-4');
$package3 = self::createPackage('foo/baz-5');

yield 'abandoned packages after maintained' => [
[
$abandonedPackage1 = self::createPackage(name: 'foo/baz-1', abandoned: true),
$package1 = self::createPackage('foo/baz-2'),
$abandonedPackage2 = self::createPackage(name: 'foo/baz-3', abandoned: 'foo/bar-3'),
$package2 = self::createPackage('foo/baz-4'),
$package3 = self::createPackage('foo/baz-5'),
$abandonedPackage1,
$package1,
$abandonedPackage2,
$package2,
$package3,
],
[
$package1,
Expand Down Expand Up @@ -385,8 +397,14 @@ private function mockPackagesServerResponse(array $packages): void
[
'packages' => array_reduce(
$packages,
function (array $carry, Package\PackageInterface $package) use ($dumper): array {
$carry[$package->getName()][$package->getPrettyVersion()] = $dumper->dump($package);
static function (array $carry, Package\PackageInterface $package) use ($dumper): array {
$packageName = $package->getName();

if (!is_array($carry[$packageName] ?? null)) {
$carry[$packageName] = [];
}

$carry[$packageName][$package->getPrettyVersion()] = $dumper->dump($package);

return $carry;
},
Expand Down

0 comments on commit e545341

Please sign in to comment.