Skip to content
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
10 changes: 6 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
strategy:
matrix:
php-version:
- '7.4'
- '8.1'

steps:
-
Expand Down Expand Up @@ -52,7 +52,7 @@ jobs:
strategy:
matrix:
php-version:
- '7.4'
- '8.1'

steps:
-
Expand Down Expand Up @@ -85,8 +85,10 @@ jobs:
strategy:
matrix:
php-version:
- '7.4'
- '8.0'
- '8.1'
- '8.2'
- '8.3'
- '8.4'

steps:
-
Expand Down
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/tests/Assets/workspace
/phpbench.json
/composer.lock
/.php_cs.cache
/stubs
/.vscode
.php_cs.cache
.phpunit.result.cache
.php-cs-fixer.cache
3 changes: 2 additions & 1 deletion .php_cs.dist → .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
])
;

return PhpCsFixer\Config::create()
return (new PhpCsFixer\Config())
->setRiskyAllowed(true)
->setRules([
'@PSR2' => true,
'no_unused_imports' => true,
Expand Down
14 changes: 7 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
}
],
"require": {
"php": "^7.3 || ^8.0"
"php": "^8.1"
},
"require-dev": {
"ergebnis/composer-normalize": "^2.0",
"friendsofphp/php-cs-fixer": "^2.17",
"infection/infection": "^0.18.0",
"phpstan/phpstan": "~0.12.0",
"phpunit/phpunit": "^9.0",
"symfony/var-dumper": "^6.1"
"phpstan/phpstan": "^2.0",
"phpunit/phpunit": "^10.0",
"symfony/var-dumper": "^6.0|^7.1",
"infection/infection": "^0.29.0",
"friendsofphp/php-cs-fixer": "^3.91"
},
"extra": {
"branch-alias": {
Expand All @@ -40,7 +40,7 @@
"scripts": {
"integrate": [
"./vendor/bin/phpunit",
"./vendor/bin/phpstan analyze",
"./vendor/bin/phpstan analyze --memory-limit=-1",
"./vendor/bin/php-cs-fixer fix --allow-risky=yes"
]
},
Expand Down
10 changes: 3 additions & 7 deletions lib/Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,7 @@ public function resolve(array $config): array

if ($diff = array_diff(array_keys($config), $allowedKeys)) {
$this->throwOrLogError(
new InvalidMap(sprintf(
'Key(s) "%s" are not known, known keys: "%s"',
implode('", "', ($diff)),
implode('", "', $allowedKeys)
))
UnknownKeys::fromKeysAndAllowedKeys(array_values($diff), $allowedKeys)
);

$config = $this->removeKeys($config, $diff);
Expand Down Expand Up @@ -227,12 +223,12 @@ public function errors(): ResolverErrors
}

/**
* @return array<string>
* @return list<string>
*/
private function resolveAllowedKeys(): array
{
$allowedKeys = array_merge(array_keys($this->defaults), $this->required);
return $allowedKeys;
return array_values($allowedKeys);
}

private function throwOrLogError(InvalidMap $error): void
Expand Down
44 changes: 44 additions & 0 deletions lib/UnknownKeys.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Phpactor\MapResolver;

class UnknownKeys extends InvalidMap
{
/**
* @param list<string> $allowedKeys
* @param list<string> $keys
*/
public function __construct(string $message, private array $keys, private array $allowedKeys)
{
parent::__construct($message);
}

/**
* @return list<string>
*/
public function additionalKeys(): array
{
return $this->keys;
}

/**
* @return list<string>
*/
public function allowedKeys(): array
{
return $this->allowedKeys;
}

/**
* @param list<string> $allowedKeys
* @param list<string> $diff
*/
public static function fromKeysAndAllowedKeys(array $diff, array $allowedKeys): self
{
return new self(sprintf(
'Key(s) "%s" are not known, known keys: "%s"',
implode('", "', ($diff)),
implode('", "', $allowedKeys),
), $diff, $allowedKeys);
}
}
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
parameters:
ignoreErrors:
-
message: "#^Method Phpactor\\\\MapResolver\\\\Resolver\\:\\:resolveDescriptions\\(\\) should return array\\<string, string\\> but returns array\\<int\\|string, string\\|false\\|null\\>\\.$#"
count: 1
path: lib/Resolver.php

19 changes: 13 additions & 6 deletions tests/Unit/ResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PHPUnit\Framework\TestCase;
use Phpactor\MapResolver\Definition;
use Phpactor\MapResolver\InvalidMap;
use Phpactor\MapResolver\UnknownKeys;
use Phpactor\MapResolver\Resolver;
use stdClass;

Expand All @@ -25,12 +26,18 @@ public function testThrowsExceptionOnUnknownKey(): void
$this->expectException(InvalidMap::class);
$this->expectExceptionMessage('Key(s) "three" are not known');

$resolver = new Resolver();
$resolver->setDefaults([
'one' => 1,
'two' => 2,
]);
$resolver->resolve(['three' => 3]);
try {
$resolver = new Resolver();
$resolver->setDefaults([
'one' => 1,
'two' => 2,
]);
$resolver->resolve(['three' => 3]);
} catch (UnknownKeys $e) {
self::assertEquals(['three'], $e->additionalKeys());
self::assertEquals(['one', 'two'], $e->allowedKeys());
throw $e;
}
}

public function testIgnoresUnknownKey(): void
Expand Down
Loading