Skip to content

Commit ac3f91d

Browse files
authored
Load the ParameterBag stub until symfony/http-foundation 8.2 (#519)
ParameterBag::keys() and all() return the parameters' own keys, so a numeric parameter name comes back as an int on every released version, while Symfony 7.4+ documents keys() as list<string> and all() as array<string, mixed>. The stub was only loaded below 7.4, so a correct is_string() guard over those keys was reported as always true. Symfony 8.2 casts the keys to strings and documents all() as array<mixed> (symfony/symfony#66127), so the stub is now loaded below 8.2, and it covers all() as well. The InputBag and Request stubs keep their 7.4 gate.
1 parent f5d54e9 commit ac3f91d

4 files changed

Lines changed: 30 additions & 5 deletions

File tree

‎src/Stubs/Symfony/StubFilesExtensionLoader.php‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,17 @@ public function getFiles(): array
6868
$files[] = $stubsDir . '/Symfony/Component/HttpFoundation/Cookie.stub';
6969
}
7070

71-
if ($this->isInstalledVersionBelow('symfony/http-foundation', '7.4.0.0')) {
71+
// keys() returns int keys for numeric parameter names until symfony/http-foundation 8.2 casts them to string
72+
if ($this->isInstalledVersionBelow('symfony/http-foundation', '8.2.0.0')) {
7273
$files[] = $stubsDir . '/Symfony/Component/HttpFoundation/ParameterBag.stub';
74+
}
7375

74-
if (!$this->isInstalledVersionBelow('symfony/http-foundation', '5.1.0.0')) {
75-
$files[] = $stubsDir . '/Symfony/Component/HttpFoundation/InputBag.stub';
76-
$files[] = $stubsDir . '/Symfony/Component/HttpFoundation/Request.stub';
77-
}
76+
if (
77+
$this->isInstalledVersionBelow('symfony/http-foundation', '7.4.0.0')
78+
&& !$this->isInstalledVersionBelow('symfony/http-foundation', '5.1.0.0')
79+
) {
80+
$files[] = $stubsDir . '/Symfony/Component/HttpFoundation/InputBag.stub';
81+
$files[] = $stubsDir . '/Symfony/Component/HttpFoundation/Request.stub';
7882
}
7983

8084
if ($this->isInstalledVersionBelow('symfony/messenger', '6.1.0.0')) {

‎stubs/Symfony/Component/HttpFoundation/ParameterBag.stub‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ namespace Symfony\Component\HttpFoundation;
77
*/
88
class ParameterBag implements \IteratorAggregate
99
{
10+
/**
11+
* @return array<mixed>
12+
*/
13+
public function all(?string $key = null): array
14+
{
15+
}
16+
1017
/**
1118
* @return list<array-key>
1219
*/

‎tests/Type/Symfony/ExtensionTest.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public function dataFileAsserts(): iterable
2525
yield from $this->gatherAssertTypes(__DIR__ . '/data/traceable_message_bus.php');
2626

2727
yield from $this->gatherAssertTypes(__DIR__ . '/data/header_bag_get.php');
28+
yield from $this->gatherAssertTypes(__DIR__ . '/data/parameter_bag.php');
2829
yield from $this->gatherAssertTypes(__DIR__ . '/data/response_header_bag_get_cookies.php');
2930

3031
if (class_exists('Symfony\Component\HttpFoundation\InputBag')) {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php declare(strict_types = 1);
2+
3+
use function PHPStan\Testing\assertType;
4+
5+
$bag = new \Symfony\Component\HttpFoundation\ParameterBag(['foo' => 'bar']);
6+
7+
assertType('list<(int|string)>', $bag->keys());
8+
assertType('array<mixed>', $bag->all());
9+
assertType('array<mixed>', $bag->all('foo'));
10+
11+
foreach ($bag as $key => $value) {
12+
assertType('(int|string)', $key);
13+
}

0 commit comments

Comments
 (0)