Skip to content

Commit

Permalink
🚨 Fix PHPStan's issues
Browse files Browse the repository at this point in the history
  • Loading branch information
homersimpsons committed Dec 1, 2024
1 parent 4b08045 commit 8bc567f
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 11 deletions.
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;
}
}

0 comments on commit 8bc567f

Please sign in to comment.