Skip to content

Commit 178ff92

Browse files
committed
qa: remove psalm baseline and add better types
Signed-off-by: Maximilian Bösing <[email protected]>
1 parent 52e4c9e commit 178ff92

9 files changed

+146
-198
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"laminas/laminas-coding-standard": "^2.3",
2929
"laminas/laminas-config": "^3.1",
3030
"phpunit/phpunit": "^9.5",
31-
"psalm/plugin-phpunit": "^0.15.1",
31+
"psalm/plugin-phpunit": "^0.16.0",
3232
"vimeo/psalm": "^4.7"
3333
},
3434
"autoload": {

composer.lock

Lines changed: 76 additions & 113 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

psalm-baseline.xml

Lines changed: 0 additions & 50 deletions
This file was deleted.

psalm.xml

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,19 @@
11
<?xml version="1.0"?>
22
<psalm
3-
totallyTyped="true"
43
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
54
xmlns="https://getpsalm.org/schema/config"
65
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
7-
errorBaseline="psalm-baseline.xml"
6+
errorLevel="1"
87
>
98
<projectFiles>
109
<directory name="src"/>
1110
<directory name="test"/>
1211
<ignoreFiles>
13-
<directory name=".github"/>
14-
<directory name="docs"/>
1512
<directory name="vendor"/>
1613
</ignoreFiles>
1714
</projectFiles>
1815

19-
<issueHandlers>
20-
<InternalMethod>
21-
<errorLevel type="suppress">
22-
<referencedMethod name="PHPUnit\Framework\MockObject\Builder\InvocationMocker::method"/>
23-
</errorLevel>
24-
<errorLevel type="suppress">
25-
<referencedMethod name="PHPUnit\Framework\MockObject\Builder\InvocationMocker::willReturn"/>
26-
</errorLevel>
27-
<errorLevel type="suppress">
28-
<referencedMethod name="PHPUnit\Framework\MockObject\Builder\InvocationMocker::with"/>
29-
</errorLevel>
30-
</InternalMethod>
31-
</issueHandlers>
3216
<plugins>
3317
<pluginClass class="Psalm\PhpUnitPlugin\Plugin"/>
3418
</plugins>
35-
</psalm>
19+
</psalm>

src/LazyParameterPostProcessor.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,21 @@
44

55
namespace Laminas\ConfigAggregatorParameters;
66

7+
/**
8+
* @template TParameters of array<string,mixed>
9+
* @psalm-import-type ProcessedConfig from ParameterPostProcessor
10+
*/
711
final class LazyParameterPostProcessor
812
{
9-
/** @var callable */
13+
/**
14+
* @var callable():array<string,mixed>
15+
* @psalm-var callable():TParameters
16+
*/
1017
private $parameterProvider;
1118

1219
/**
13-
* @psalm-param callable():array<string,mixed> $parameterProvider
20+
* @param callable():array<string,mixed> $parameterProvider
21+
* @psalm-param callable():TParameters $parameterProvider
1422
*/
1523
public function __construct(callable $parameterProvider)
1624
{
@@ -20,6 +28,7 @@ public function __construct(callable $parameterProvider)
2028
/**
2129
* @param array<string,mixed> $config
2230
* @return array<string,mixed>
31+
* @psalm-return ProcessedConfig
2332
*/
2433
public function __invoke(array $config): array
2534
{

src/ParameterNotFoundException.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use InvalidArgumentException;
88
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException as BaseException;
9+
use Throwable;
910

1011
use function sprintf;
1112

@@ -14,14 +15,20 @@ class ParameterNotFoundException extends InvalidArgumentException
1415
/** @var string */
1516
private $key;
1617

18+
public function __construct(string $message = "", int $code = 0, ?Throwable $previous = null, string $key = '')
19+
{
20+
parent::__construct($message, $code, $previous);
21+
$this->key = $key;
22+
}
23+
1724
public static function fromException(BaseException $e): self
1825
{
19-
$toReturn = new self(sprintf(
26+
$code = (int) $e->getCode();
27+
$key = (string) $e->getKey();
28+
return new self(sprintf(
2029
'Found key "%s" within configuration, but it has no associated parameter defined',
21-
$e->getKey()
22-
), $e->getCode(), $e);
23-
$toReturn->key = $e->getKey();
24-
return $toReturn;
30+
$key
31+
), $code, $e, $key);
2532
}
2633

2734
public function getKey(): string

src/ParameterPostProcessor.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,45 +11,66 @@
1111
use function is_array;
1212
use function is_numeric;
1313

14+
/**
15+
* @template TParameters of array<string,mixed>
16+
* @psalm-type ProcessedConfig=array<string,mixed>&array{parameters:array<string,mixed>}
17+
*/
1418
class ParameterPostProcessor
1519
{
16-
/** @var array */
20+
/**
21+
* @var array<string,mixed>
22+
* @psalm-var TParameters
23+
*/
1724
private $parameters;
1825

1926
/**
20-
* @param array $parameters
27+
* @param array<string,mixed> $parameters
28+
* @psalm-param TParameters $parameters
2129
*/
2230
public function __construct(array $parameters)
2331
{
2432
$this->parameters = $parameters;
2533
}
2634

35+
/**
36+
* @template TConfig of array<string,mixed>
37+
* @param array<string,mixed> $config
38+
* @return array<string,mixed>
39+
* @psalm-return ProcessedConfig
40+
*/
2741
public function __invoke(array $config): array
2842
{
2943
try {
3044
$parameters = $this->getResolvedParameters();
3145

32-
array_walk_recursive($config, function (&$value) use ($parameters) {
46+
/** @psalm-suppress MissingClosureParamType */
47+
array_walk_recursive($config, static function (&$value) use ($parameters) {
48+
/** @psalm-suppress MixedAssignment */
3349
$value = $parameters->unescapeValue($parameters->resolveValue($value));
3450
});
3551
} catch (SymfonyParameterNotFoundException $exception) {
3652
throw ParameterNotFoundException::fromException($exception);
3753
}
3854

39-
$config['parameters'] = $parameters->all();
55+
/** @var array<string,mixed> $allParameters */
56+
$allParameters = $parameters->all();
57+
$config['parameters'] = $allParameters;
4058

59+
/** @psalm-var ProcessedConfig $config */
4160
return $config;
4261
}
4362

4463
private function resolveNestedParameters(array $values, string $prefix = ''): array
4564
{
4665
$convertedValues = [];
66+
/** @psalm-suppress MixedAssignment */
4767
foreach ($values as $key => $value) {
4868
// Do not provide numeric keys as single parameter
4969
if (is_numeric($key)) {
5070
continue;
5171
}
5272

73+
/** @psalm-suppress MixedAssignment */
5374
$convertedValues[$prefix . $key] = $value;
5475
if (is_array($value)) {
5576
$convertedValues += $this->resolveNestedParameters($value, $prefix . $key . '.');

test/LazyParameterPostProcessorTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public function testParameterProviderNotCalledDuringInstantiation(): void
1616
throw new RuntimeException('Parameter provider must not be called during instantiation!');
1717
};
1818

19+
/** @psalm-suppress MixedArgumentTypeCoercion */
1920
new LazyParameterPostProcessor($callable);
2021

2122
self::assertTrue(true);

test/ParameterPostProcessorTest.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ class ParameterPostProcessorTest extends TestCase
1313
{
1414
use ArraySubsetAsserts;
1515

16+
/**
17+
* @psalm-return array<
18+
* non-empty-string,
19+
* array{
20+
* 0: array<string,mixed>,
21+
* 1: array<string,mixed>,
22+
* 2: array<string,mixed>
23+
* }
24+
* >
25+
*/
1626
public function parameterProvider(): array
1727
{
1828
return [
@@ -57,23 +67,26 @@ public function parameterProvider(): array
5767

5868
/**
5969
* @dataProvider parameterProvider
70+
* @param array<string,mixed> $parameters
71+
* @param array<string,mixed> $configuration
72+
* @param array<string,mixed> $expected
6073
*/
61-
public function testCanApplyParameters(array $parameters, array $configuration, array $expected)
74+
public function testCanApplyParameters(array $parameters, array $configuration, array $expected): void
6275
{
6376
$processor = new ParameterPostProcessor($parameters);
6477
$processed = $processor($configuration);
6578

66-
$this->assertArraySubset($expected, $processed);
79+
self::assertArraySubset($expected, $processed);
6780
}
6881

69-
public function testCanDetectMissingParameter()
82+
public function testCanDetectMissingParameter(): void
7083
{
7184
$processor = new ParameterPostProcessor([]);
7285
$this->expectException(ParameterNotFoundException::class);
7386
$processor(['foo' => '%foo%']);
7487
}
7588

76-
public function testResolvesParameterizedParameters()
89+
public function testResolvesParameterizedParameters(): void
7790
{
7891
$processor = new ParameterPostProcessor([
7992
'foo' => 'bar',
@@ -97,7 +110,7 @@ public function testResolvesParameterizedParameters()
97110
], $processed);
98111
}
99112

100-
public function testResolvesParameterizedParametersInReversedOrder()
113+
public function testResolvesParameterizedParametersInReversedOrder(): void
101114
{
102115
$processor = new ParameterPostProcessor([
103116
'foo' => '%bar%',

0 commit comments

Comments
 (0)