Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"squizlabs/php_codesniffer": "^3.3.2",
"symfony/console": "^4.0",
"symfony/framework-bundle": "^4.0",
"symfony/http-foundation": "^4.0",
"symfony/http-foundation": "^4.0 || ^5.0",
"symfony/messenger": "^4.2",
"symfony/serializer": "^4.0"
},
Expand Down
5 changes: 5 additions & 0 deletions extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ services:
factory: PHPStan\Type\Symfony\RequestTypeSpecifyingExtension
tags: [phpstan.typeSpecifier.methodTypeSpecifyingExtension]

# InputBag::get() return type
-
factory: PHPStan\Type\Symfony\InputBagDynamicReturnTypeExtension
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]

# HeaderBag::get() return type
-
factory: PHPStan\Type\Symfony\HeaderBagDynamicReturnTypeExtension
Expand Down
46 changes: 46 additions & 0 deletions src/Type/Symfony/InputBagDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Symfony;

use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\NullType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;

final class InputBagDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
{

public function getClass(): string
{
return 'Symfony\Component\HttpFoundation\InputBag';
}

public function isMethodSupported(MethodReflection $methodReflection): bool
{
return $methodReflection->getName() === 'get';
}

public function getTypeFromMethodCall(
MethodReflection $methodReflection,
MethodCall $methodCall,
Scope $scope
): Type
{
if (isset($methodCall->args[1])) {
$argType = $scope->getType($methodCall->args[1]->value);
$isNull = (new NullType())->isSuperTypeOf($argType);
$isString = (new StringType())->isSuperTypeOf($argType);
$compare = $isNull->compareTo($isString);
if ($compare === $isString) {
return new StringType();
}
}

return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
}

}
34 changes: 34 additions & 0 deletions tests/Type/Symfony/InputBagDynamicReturnTypeExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Symfony;

use Iterator;

final class InputBagDynamicReturnTypeExtensionTest extends ExtensionTestCase
{

/**
* @dataProvider getProvider
*/
public function testGet(string $expression, string $type): void
{
$this->processFile(
__DIR__ . '/input_bag_get.php',
$expression,
$type,
new InputBagDynamicReturnTypeExtension()
);
}

/**
* @return \Iterator<array{string, string}>
*/
public function getProvider(): Iterator
{
yield ['$test1', 'string|null'];
yield ['$test2', 'string|null'];
yield ['$test3', 'string'];
yield ['$test4', 'string'];
}

}
10 changes: 10 additions & 0 deletions tests/Type/Symfony/input_bag_get.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php declare(strict_types = 1);

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

$test1 = $bag->get('foo');
$test2 = $bag->get('foo', null);
$test3 = $bag->get('foo', '');
$test4 = $bag->get('foo', 'baz');

die;