Skip to content

Commit

Permalink
[request] fix request and inputbag stub (#218)
Browse files Browse the repository at this point in the history
* add phpstan stub compatibility

* cs fix

Co-authored-by: Farhad Safarov <[email protected]>
  • Loading branch information
rgrassian and seferov authored Sep 6, 2021
1 parent ecbfc60 commit 7b19393
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 9 deletions.
7 changes: 6 additions & 1 deletion src/Handler/ContainerHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,12 @@ public static function afterMethodCallAnalysis(AfterMethodCallAnalysisEvent $eve
}

if (!$service->isPublic()) {
$isTestContainer = $context->parent && ('Symfony\Bundle\FrameworkBundle\Test\KernelTestCase' === $context->parent || is_subclass_of($context->parent, 'Symfony\Bundle\FrameworkBundle\Test\KernelTestCase'));
/** @var class-string $kernelTestCaseClass */
$kernelTestCaseClass = 'Symfony\Bundle\FrameworkBundle\Test\KernelTestCase';
$isTestContainer = $context->parent &&
($kernelTestCaseClass === $context->parent
|| is_subclass_of($context->parent, $kernelTestCaseClass)
);
if (!$isTestContainer) {
IssueBuffer::accepts(
new PrivateService($serviceId, new CodeLocation($statements_source, $expr->args[0]->value)),
Expand Down
7 changes: 5 additions & 2 deletions src/Stubs/5/Component/HttpFoundation/InputBag.stubphp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

namespace Symfony\Component\HttpFoundation;

/**
* @template T of string|int|float|bool
*/
final class InputBag extends ParameterBag
{
/**
* Returns a string input value by name.
*
* @template D of string|int|float|bool|null
* @template D of T|null
* @psalm-param D $default
* @psalm-return string|int|float|bool|D
* @psalm-return D|T
* @psalm-taint-source input
*/
public function get(string $key, $default = null) {}
Expand Down
10 changes: 10 additions & 0 deletions src/Stubs/5/Component/HttpFoundation/Request.stubphp
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,14 @@ class Request
* @psalm-var InputBag
*/
public $request;

/**
* @psalm-var InputBag<string>
*/
public $query;

/**
* @psalm-var InputBag<string>
*/
public $cookies;
}
43 changes: 37 additions & 6 deletions tests/acceptance/acceptance/InputBag.feature
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ Feature: InputBag get return type
use Symfony\Component\HttpFoundation\Request;
"""

Scenario Outline: Return type is not null if default argument is string.
Scenario: Return type is scalar for request property if default argument is string.
Given I have the following code
"""
class App
{
public function __invoke(Request $request): void
{
$string = $request-><property>->get('foo', 'bar');
$string = $request->request->get('foo', 'bar');
trim($string);
}
}
Expand All @@ -27,20 +27,34 @@ Feature: InputBag get return type
Then I see these errors
| Type | Message |
| InvalidScalarArgument | Argument 1 of trim expects string, scalar provided |

Scenario Outline: Return type is string if default argument is string.
Given I have the following code
"""
class App
{
public function __invoke(Request $request): void
{
$string = $request-><property>->get('foo', 'bar');
trim($string);
}
}
"""
When I run Psalm
Then I see no errors
Examples:
| property |
| query |
| cookies |
| request |

Scenario Outline: Return type is nullable if default argument is not provided.
Scenario: Return type is nullable for request property if default argument is not provided.
Given I have the following code
"""
class App
{
public function __invoke(Request $request): void
{
$nullableString = $request-><property>->get('foo');
$nullableString = $request->request->get('foo');
trim($nullableString);
}
}
Expand All @@ -50,8 +64,25 @@ Feature: InputBag get return type
| Type | Message |
| InvalidScalarArgument | Argument 1 of trim expects string, null\|scalar provided |
And I see no other errors

Scenario Outline: Return type is nullable if default argument is not provided.
Given I have the following code
"""
class App
{
public function __invoke(Request $request): void
{
$nullableString = $request-><property>->get('foo');
trim($nullableString);
}
}
"""
When I run Psalm
Then I see these errors
| Type | Message |
| PossiblyNullArgument | Argument 1 of trim cannot be null, possibly null value provided |
And I see no other errors
Examples:
| property |
| query |
| cookies |
| request |

0 comments on commit 7b19393

Please sign in to comment.