From 2cbb996fcacc0ffe4b99622761c25e6c23a21479 Mon Sep 17 00:00:00 2001 From: homersimpsons Date: Sun, 1 Dec 2024 19:58:15 +0100 Subject: [PATCH] :rotating_light: Fix PHPStan's issues --- phpstan.neon | 3 +++ src/DirectoryRepresenter.php | 50 ++++++++++++++++++++++++++++-------- 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index e45aeff..9f6b4bb 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,3 +1,6 @@ +includes: + - phar://phpstan.phar/conf/bleedingEdge.neon + parameters: level: max excludePaths: diff --git a/src/DirectoryRepresenter.php b/src/DirectoryRepresenter.php index 628a7c3..aca6ca2 100644 --- a/src/DirectoryRepresenter.php +++ b/src/DirectoryRepresenter.php @@ -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; @@ -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); @@ -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; + } }