Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⬆️ Bump phpstan/phpstan from 1.12.7 to 2.0.3 #282

Merged
merged 2 commits into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"require-dev": {
"doctrine/coding-standard": "^12.0",
"phpstan/phpstan": "^1.9",
"phpstan/phpstan": "^2.0",
"phpunit/phpunit": "^11.1",
"squizlabs/php_codesniffer": "^3.7"
},
Expand Down
14 changes: 7 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
includes:
- phar://phpstan.phar/conf/bleedingEdge.neon

parameters:
level: max
excludePaths:
Expand Down
41 changes: 40 additions & 1 deletion phpunit-tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,33 @@ public function testConfigEmptyFilesSolution(): void
$this->assertStringContainsString('.meta/config.json: `files.solution` key is empty', $display);
}

public function testConfigInvalidFilesSolutionValue(): void
public function testConfigInvalidFilesValueNotArray(): void
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessageMatches('#^\.meta/config\.json: missing or invalid `files\.solution` key$#');
$input = new InMemoryFilesystemAdapter();
$inputFs = new Filesystem($input);
$inputFs->write('.meta/config.json', '{"files":true}');
$output = new InMemoryFilesystemAdapter();

$application = new Application();
$application->represent($inputFs, new Filesystem($output), new NullLogger());
}

public function testConfigInvalidFilesMissingSolution(): void
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessageMatches('#^\.meta/config\.json: missing or invalid `files\.solution` key$#');
$input = new InMemoryFilesystemAdapter();
$inputFs = new Filesystem($input);
$inputFs->write('.meta/config.json', '{"files":{}}');
$output = new InMemoryFilesystemAdapter();

$application = new Application();
$application->represent($inputFs, new Filesystem($output), new NullLogger());
}

public function testConfigInvalidFilesSolutionValueNotArray(): void
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessageMatches('#^\.meta/config\.json: missing or invalid `files\.solution` key$#');
Expand All @@ -94,6 +120,19 @@ public function testConfigInvalidFilesSolutionValue(): void
$application->represent($inputFs, new Filesystem($output), new NullLogger());
}

public function testConfigInvalidFilesSolutionValueNotArrayOfString(): void
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessageMatches('#^\.meta/config\.json: missing or invalid `files\.solution` key$#');
$input = new InMemoryFilesystemAdapter();
$inputFs = new Filesystem($input);
$inputFs->write('.meta/config.json', '{"files":{"solution":[true]}}');
$output = new InMemoryFilesystemAdapter();

$application = new Application();
$application->represent($inputFs, new Filesystem($output), new NullLogger());
}

public function testConfigMissingFilesSolution(): void
{
$this->expectException(RuntimeException::class);
Expand Down
50 changes: 40 additions & 10 deletions src/DirectoryRepresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use function assert;
use function implode;
use function is_array;
use function is_string;
use function json_decode;

use const JSON_THROW_ON_ERROR;
Expand All @@ -32,16 +33,7 @@ public function __construct(
public function represent(): Result
{
$configJson = $this->solutionDir->read('/.meta/config.json');

$config = json_decode($configJson, true, flags: JSON_THROW_ON_ERROR);
assert(is_array($config), 'json_decode(..., true) should return an array');

if (! isset($config['files']['solution']) || ! is_array($config['files']['solution'])) {
throw new RuntimeException('.meta/config.json: missing or invalid `files.solution` key');
}

$solutions = $config['files']['solution'];
$this->logger->info('.meta/config.json: Solutions files: ' . implode(', ', $solutions));
$solutions = $this->parseSolutions($configJson);

$mapping = new Mapping();
$representer = new FilesRepresenter($mapping, $this->logger);
Expand All @@ -66,4 +58,42 @@ public function represent(): Result
$mapping->toJson(),
);
}

/** @return string[] */
private function parseSolutions(string $configJson): array
{
$config = json_decode($configJson, true, flags: JSON_THROW_ON_ERROR);
assert(is_array($config), 'json_decode(..., true) should return an array');
if (
! isset($config['files'])
|| ! is_array($config['files'])
|| ! isset($config['files']['solution'])
|| ! is_array($config['files']['solution'])
|| ! $this->isArrayOfString($config['files']['solution'])
) {
throw new RuntimeException('.meta/config.json: missing or invalid `files.solution` key');
}

$solutions = $config['files']['solution'];

$this->logger->info('.meta/config.json: Solutions files: ' . implode(', ', $solutions));

return $solutions;
}

/**
* @param mixed[] $array
*
* @phpstan-assert-if-true string[] $array
*/
private function isArrayOfString(array $array): bool
{
foreach ($array as $element) {
if (! is_string($element)) {
return false;
}
}

return true;
}
}
Loading