Skip to content

Commit a1ed625

Browse files
authored
Bump and exception (#11)
- Add support for a dedicated exception for additional keys - Bump to 8.1
1 parent c15205f commit a1ed625

File tree

8 files changed

+77
-32
lines changed

8 files changed

+77
-32
lines changed

.github/workflows/ci.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
strategy:
2020
matrix:
2121
php-version:
22-
- '7.4'
22+
- '8.1'
2323

2424
steps:
2525
-
@@ -52,7 +52,7 @@ jobs:
5252
strategy:
5353
matrix:
5454
php-version:
55-
- '7.4'
55+
- '8.1'
5656

5757
steps:
5858
-
@@ -85,8 +85,10 @@ jobs:
8585
strategy:
8686
matrix:
8787
php-version:
88-
- '7.4'
89-
- '8.0'
88+
- '8.1'
89+
- '8.2'
90+
- '8.3'
91+
- '8.4'
9092

9193
steps:
9294
-

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/tests/Assets/workspace
44
/phpbench.json
55
/composer.lock
6-
/.php_cs.cache
76
/stubs
87
/.vscode
9-
.php_cs.cache
8+
.phpunit.result.cache
9+
.php-cs-fixer.cache

.php_cs.dist renamed to .php-cs-fixer.dist.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
])
99
;
1010

11-
return PhpCsFixer\Config::create()
11+
return (new PhpCsFixer\Config())
12+
->setRiskyAllowed(true)
1213
->setRules([
1314
'@PSR2' => true,
1415
'no_unused_imports' => true,

composer.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010
}
1111
],
1212
"require": {
13-
"php": "^7.3 || ^8.0"
13+
"php": "^8.1"
1414
},
1515
"require-dev": {
1616
"ergebnis/composer-normalize": "^2.0",
17-
"friendsofphp/php-cs-fixer": "^2.17",
18-
"infection/infection": "^0.18.0",
19-
"phpstan/phpstan": "~0.12.0",
20-
"phpunit/phpunit": "^9.0",
21-
"symfony/var-dumper": "^6.1"
17+
"phpstan/phpstan": "^2.0",
18+
"phpunit/phpunit": "^10.0",
19+
"symfony/var-dumper": "^6.0|^7.1",
20+
"infection/infection": "^0.29.0",
21+
"friendsofphp/php-cs-fixer": "^3.91"
2222
},
2323
"extra": {
2424
"branch-alias": {
@@ -40,7 +40,7 @@
4040
"scripts": {
4141
"integrate": [
4242
"./vendor/bin/phpunit",
43-
"./vendor/bin/phpstan analyze",
43+
"./vendor/bin/phpstan analyze --memory-limit=-1",
4444
"./vendor/bin/php-cs-fixer fix --allow-risky=yes"
4545
]
4646
},

lib/Resolver.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,7 @@ public function resolve(array $config): array
118118

119119
if ($diff = array_diff(array_keys($config), $allowedKeys)) {
120120
$this->throwOrLogError(
121-
new InvalidMap(sprintf(
122-
'Key(s) "%s" are not known, known keys: "%s"',
123-
implode('", "', ($diff)),
124-
implode('", "', $allowedKeys)
125-
))
121+
UnknownKeys::fromKeysAndAllowedKeys(array_values($diff), $allowedKeys)
126122
);
127123

128124
$config = $this->removeKeys($config, $diff);
@@ -227,12 +223,12 @@ public function errors(): ResolverErrors
227223
}
228224

229225
/**
230-
* @return array<string>
226+
* @return list<string>
231227
*/
232228
private function resolveAllowedKeys(): array
233229
{
234230
$allowedKeys = array_merge(array_keys($this->defaults), $this->required);
235-
return $allowedKeys;
231+
return array_values($allowedKeys);
236232
}
237233

238234
private function throwOrLogError(InvalidMap $error): void

lib/UnknownKeys.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Phpactor\MapResolver;
4+
5+
class UnknownKeys extends InvalidMap
6+
{
7+
/**
8+
* @param list<string> $allowedKeys
9+
* @param list<string> $keys
10+
*/
11+
public function __construct(string $message, private array $keys, private array $allowedKeys)
12+
{
13+
parent::__construct($message);
14+
}
15+
16+
/**
17+
* @return list<string>
18+
*/
19+
public function additionalKeys(): array
20+
{
21+
return $this->keys;
22+
}
23+
24+
/**
25+
* @return list<string>
26+
*/
27+
public function allowedKeys(): array
28+
{
29+
return $this->allowedKeys;
30+
}
31+
32+
/**
33+
* @param list<string> $allowedKeys
34+
* @param list<string> $diff
35+
*/
36+
public static function fromKeysAndAllowedKeys(array $diff, array $allowedKeys): self
37+
{
38+
return new self(sprintf(
39+
'Key(s) "%s" are not known, known keys: "%s"',
40+
implode('", "', ($diff)),
41+
implode('", "', $allowedKeys),
42+
), $diff, $allowedKeys);
43+
}
44+
}

phpstan-baseline.neon

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,2 @@
11
parameters:
22
ignoreErrors:
3-
-
4-
message: "#^Method Phpactor\\\\MapResolver\\\\Resolver\\:\\:resolveDescriptions\\(\\) should return array\\<string, string\\> but returns array\\<int\\|string, string\\|false\\|null\\>\\.$#"
5-
count: 1
6-
path: lib/Resolver.php
7-

tests/Unit/ResolverTest.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PHPUnit\Framework\TestCase;
66
use Phpactor\MapResolver\Definition;
77
use Phpactor\MapResolver\InvalidMap;
8+
use Phpactor\MapResolver\UnknownKeys;
89
use Phpactor\MapResolver\Resolver;
910
use stdClass;
1011

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

28-
$resolver = new Resolver();
29-
$resolver->setDefaults([
30-
'one' => 1,
31-
'two' => 2,
32-
]);
33-
$resolver->resolve(['three' => 3]);
29+
try {
30+
$resolver = new Resolver();
31+
$resolver->setDefaults([
32+
'one' => 1,
33+
'two' => 2,
34+
]);
35+
$resolver->resolve(['three' => 3]);
36+
} catch (UnknownKeys $e) {
37+
self::assertEquals(['three'], $e->additionalKeys());
38+
self::assertEquals(['one', 'two'], $e->allowedKeys());
39+
throw $e;
40+
}
3441
}
3542

3643
public function testIgnoresUnknownKey(): void

0 commit comments

Comments
 (0)