Skip to content

Commit 7389207

Browse files
authored
Remove nullable type after calling HeaderBag::has
1 parent c55237a commit 7389207

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

extension.neon

+10-1
Original file line numberDiff line numberDiff line change
@@ -298,5 +298,14 @@ services:
298298

299299
# InputBag::get() type specification
300300
-
301-
factory: PHPStan\Type\Symfony\InputBagTypeSpecifyingExtension
301+
factory: PHPStan\Type\Symfony\BagTypeSpecifyingExtension
302302
tags: [phpstan.typeSpecifier.methodTypeSpecifyingExtension]
303+
arguments:
304+
className: Symfony\Component\HttpFoundation\InputBag
305+
306+
# HeaderBag::get() type specification
307+
-
308+
factory: PHPStan\Type\Symfony\BagTypeSpecifyingExtension
309+
tags: [phpstan.typeSpecifier.methodTypeSpecifyingExtension]
310+
arguments:
311+
className: Symfony\Component\HttpFoundation\HeaderBag

src/Type/Symfony/InputBagTypeSpecifyingExtension.php src/Type/Symfony/BagTypeSpecifyingExtension.php

+13-4
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,30 @@
1111
use PHPStan\Reflection\MethodReflection;
1212
use PHPStan\Type\MethodTypeSpecifyingExtension;
1313
use PHPStan\Type\NullType;
14-
use Symfony\Component\HttpFoundation\InputBag;
1514

16-
final class InputBagTypeSpecifyingExtension implements MethodTypeSpecifyingExtension, TypeSpecifierAwareExtension
15+
final class BagTypeSpecifyingExtension implements MethodTypeSpecifyingExtension, TypeSpecifierAwareExtension
1716
{
1817

19-
private const INPUT_BAG_CLASS = InputBag::class;
2018
private const HAS_METHOD_NAME = 'has';
2119
private const GET_METHOD_NAME = 'get';
2220

2321
/** @var TypeSpecifier */
2422
private $typeSpecifier;
2523

24+
/** @var class-string */
25+
private $className;
26+
27+
/**
28+
* @param class-string $className
29+
*/
30+
public function __construct(string $className)
31+
{
32+
$this->className = $className;
33+
}
34+
2635
public function getClass(): string
2736
{
28-
return self::INPUT_BAG_CLASS;
37+
return $this->className;
2938
}
3039

3140
public function isMethodSupported(MethodReflection $methodReflection, MethodCall $node, TypeSpecifierContext $context): bool

tests/Type/Symfony/ExtensionTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class ExtensionTest extends TypeInferenceTestCase
1515
public function dataFileAsserts(): iterable
1616
{
1717
yield from $this->gatherAssertTypes(__DIR__ . '/data/envelope_all.php');
18-
yield from $this->gatherAssertTypes(__DIR__ . '/data/header_bag_get.php');
18+
yield from $this->gatherAssertTypes(__DIR__ . '/data/header_bag.php');
1919
yield from $this->gatherAssertTypes(__DIR__ . '/data/response_header_bag_get_cookies.php');
2020

2121
if (class_exists('Symfony\Component\HttpFoundation\InputBag')) {

tests/Type/Symfony/data/header_bag_get.php tests/Type/Symfony/data/header_bag.php

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
$bag = new \Symfony\Component\HttpFoundation\HeaderBag(['foo' => ['bar']]);
66

7+
if ($bag->has('bar')) {
8+
assertType('string', $bag->get('bar'));
9+
} else {
10+
assertType('null', $bag->get('bar'));
11+
}
12+
713
assertType('string|null', $bag->get('foo'));
814
assertType('string|null', $bag->get('foo', null));
915
assertType('string', $bag->get('foo', 'baz'));

0 commit comments

Comments
 (0)