Skip to content

Commit 7b19393

Browse files
rgrassianseferov
andauthored
[request] fix request and inputbag stub (#218)
* add phpstan stub compatibility * cs fix Co-authored-by: Farhad Safarov <farhad.safarov@gmail.com>
1 parent ecbfc60 commit 7b19393

File tree

4 files changed

+58
-9
lines changed

4 files changed

+58
-9
lines changed

src/Handler/ContainerHandler.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,12 @@ public static function afterMethodCallAnalysis(AfterMethodCallAnalysisEvent $eve
118118
}
119119

120120
if (!$service->isPublic()) {
121-
$isTestContainer = $context->parent && ('Symfony\Bundle\FrameworkBundle\Test\KernelTestCase' === $context->parent || is_subclass_of($context->parent, 'Symfony\Bundle\FrameworkBundle\Test\KernelTestCase'));
121+
/** @var class-string $kernelTestCaseClass */
122+
$kernelTestCaseClass = 'Symfony\Bundle\FrameworkBundle\Test\KernelTestCase';
123+
$isTestContainer = $context->parent &&
124+
($kernelTestCaseClass === $context->parent
125+
|| is_subclass_of($context->parent, $kernelTestCaseClass)
126+
);
122127
if (!$isTestContainer) {
123128
IssueBuffer::accepts(
124129
new PrivateService($serviceId, new CodeLocation($statements_source, $expr->args[0]->value)),

src/Stubs/5/Component/HttpFoundation/InputBag.stubphp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22

33
namespace Symfony\Component\HttpFoundation;
44

5+
/**
6+
* @template T of string|int|float|bool
7+
*/
58
final class InputBag extends ParameterBag
69
{
710
/**
811
* Returns a string input value by name.
912
*
10-
* @template D of string|int|float|bool|null
13+
* @template D of T|null
1114
* @psalm-param D $default
12-
* @psalm-return string|int|float|bool|D
15+
* @psalm-return D|T
1316
* @psalm-taint-source input
1417
*/
1518
public function get(string $key, $default = null) {}

src/Stubs/5/Component/HttpFoundation/Request.stubphp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,14 @@ class Request
88
* @psalm-var InputBag
99
*/
1010
public $request;
11+
12+
/**
13+
* @psalm-var InputBag<string>
14+
*/
15+
public $query;
16+
17+
/**
18+
* @psalm-var InputBag<string>
19+
*/
20+
public $cookies;
1121
}

tests/acceptance/acceptance/InputBag.feature

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ Feature: InputBag get return type
1111
use Symfony\Component\HttpFoundation\Request;
1212
"""
1313

14-
Scenario Outline: Return type is not null if default argument is string.
14+
Scenario: Return type is scalar for request property if default argument is string.
1515
Given I have the following code
1616
"""
1717
class App
1818
{
1919
public function __invoke(Request $request): void
2020
{
21-
$string = $request-><property>->get('foo', 'bar');
21+
$string = $request->request->get('foo', 'bar');
2222
trim($string);
2323
}
2424
}
@@ -27,20 +27,34 @@ Feature: InputBag get return type
2727
Then I see these errors
2828
| Type | Message |
2929
| InvalidScalarArgument | Argument 1 of trim expects string, scalar provided |
30+
31+
Scenario Outline: Return type is string if default argument is string.
32+
Given I have the following code
33+
"""
34+
class App
35+
{
36+
public function __invoke(Request $request): void
37+
{
38+
$string = $request-><property>->get('foo', 'bar');
39+
trim($string);
40+
}
41+
}
42+
"""
43+
When I run Psalm
44+
Then I see no errors
3045
Examples:
3146
| property |
3247
| query |
3348
| cookies |
34-
| request |
3549

36-
Scenario Outline: Return type is nullable if default argument is not provided.
50+
Scenario: Return type is nullable for request property if default argument is not provided.
3751
Given I have the following code
3852
"""
3953
class App
4054
{
4155
public function __invoke(Request $request): void
4256
{
43-
$nullableString = $request-><property>->get('foo');
57+
$nullableString = $request->request->get('foo');
4458
trim($nullableString);
4559
}
4660
}
@@ -50,8 +64,25 @@ Feature: InputBag get return type
5064
| Type | Message |
5165
| InvalidScalarArgument | Argument 1 of trim expects string, null\|scalar provided |
5266
And I see no other errors
67+
68+
Scenario Outline: Return type is nullable if default argument is not provided.
69+
Given I have the following code
70+
"""
71+
class App
72+
{
73+
public function __invoke(Request $request): void
74+
{
75+
$nullableString = $request-><property>->get('foo');
76+
trim($nullableString);
77+
}
78+
}
79+
"""
80+
When I run Psalm
81+
Then I see these errors
82+
| Type | Message |
83+
| PossiblyNullArgument | Argument 1 of trim cannot be null, possibly null value provided |
84+
And I see no other errors
5385
Examples:
5486
| property |
5587
| query |
5688
| cookies |
57-
| request |

0 commit comments

Comments
 (0)