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 22ce9e7 commit 2cbb996
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 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
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'])

Check warning on line 68 in src/DirectoryRepresenter.php

View workflow job for this annotation

GitHub Actions / continuous-integration

Escaped Mutant for Mutator "LogicalOr": @@ @@ { $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'])) { + 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'];
|| ! 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) {

Check warning on line 91 in src/DirectoryRepresenter.php

View workflow job for this annotation

GitHub Actions / continuous-integration

Escaped Mutant for Mutator "Foreach_": @@ @@ */ private function isArrayOfString(array $array): bool { - foreach ($array as $element) { + foreach ([] as $element) { if (!is_string($element)) { return false; }
if (! is_string($element)) {
return false;
}
}

return true;
}
}

0 comments on commit 2cbb996

Please sign in to comment.