Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8c1154f

Browse files
ruudkraalderink
andcommitted
Add support for InputBag (Symfony 5+)
Co-authored-by: raalderink <33425670+raalderink@users.noreply.github.com>
1 parent 454fe8b commit 8c1154f

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed

‎extension.neon‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ services:
7575
factory: PHPStan\Type\Symfony\RequestTypeSpecifyingExtension
7676
tags: [phpstan.typeSpecifier.methodTypeSpecifyingExtension]
7777

78+
# InputBag::get() return type
79+
-
80+
factory: PHPStan\Type\Symfony\InputBagDynamicReturnTypeExtension
81+
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]
82+
7883
# HeaderBag::get() return type
7984
-
8085
factory: PHPStan\Type\Symfony\HeaderBagDynamicReturnTypeExtension
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Type\Symfony;
4+
5+
use PhpParser\Node\Expr\MethodCall;
6+
use PHPStan\Analyser\Scope;
7+
use PHPStan\Reflection\MethodReflection;
8+
use PHPStan\Reflection\ParametersAcceptorSelector;
9+
use PHPStan\Type\DynamicMethodReturnTypeExtension;
10+
use PHPStan\Type\NullType;
11+
use PHPStan\Type\StringType;
12+
use PHPStan\Type\Type;
13+
14+
final class InputBagDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
15+
{
16+
17+
public function getClass(): string
18+
{
19+
return 'Symfony\Component\HttpFoundation\InputBag';
20+
}
21+
22+
public function isMethodSupported(MethodReflection $methodReflection): bool
23+
{
24+
return $methodReflection->getName() === 'get';
25+
}
26+
27+
public function getTypeFromMethodCall(
28+
MethodReflection $methodReflection,
29+
MethodCall $methodCall,
30+
Scope $scope
31+
): Type
32+
{
33+
if (isset($methodCall->args[1])) {
34+
$argType = $scope->getType($methodCall->args[1]->value);
35+
$isNull = (new NullType())->isSuperTypeOf($argType);
36+
$isString = (new StringType())->isSuperTypeOf($argType);
37+
$compare = $isNull->compareTo($isString);
38+
if ($compare === $isString) {
39+
return new StringType();
40+
}
41+
}
42+
43+
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
44+
}
45+
46+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Type\Symfony;
4+
5+
use Iterator;
6+
7+
final class InputBagDynamicReturnTypeExtensionTest extends ExtensionTestCase
8+
{
9+
10+
/**
11+
* @dataProvider getProvider
12+
*/
13+
public function testGet(string $expression, string $type): void
14+
{
15+
if (!class_exists('Symfony\\Component\\HttpFoundation\\InputBag')) {
16+
self::markTestSkipped('The test needs Symfony\Component\HttpFoundation\InputBag class.');
17+
}
18+
19+
$this->processFile(
20+
__DIR__ . '/input_bag_get.php',
21+
$expression,
22+
$type,
23+
new InputBagDynamicReturnTypeExtension()
24+
);
25+
}
26+
27+
/**
28+
* @return \Iterator<array{string, string}>
29+
*/
30+
public function getProvider(): Iterator
31+
{
32+
yield ['$test1', 'string|null'];
33+
yield ['$test2', 'string|null'];
34+
yield ['$test3', 'string'];
35+
yield ['$test4', 'string'];
36+
}
37+
38+
}

‎tests/Type/Symfony/input_bag_get.php‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php declare(strict_types = 1);
2+
3+
$bag = new \Symfony\Component\HttpFoundation\InputBag(['foo' => 'bar']);
4+
5+
$test1 = $bag->get('foo');
6+
$test2 = $bag->get('foo', null);
7+
$test3 = $bag->get('foo', '');
8+
$test4 = $bag->get('foo', 'baz');
9+
10+
die;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /