File tree Expand file tree Collapse file tree 3 files changed +101
-0
lines changed Expand file tree Collapse file tree 3 files changed +101
-0
lines changed Original file line number Diff line number Diff line change
1
+ <?php declare (strict_types = 1 );
2
+
3
+ namespace PHPStan \Type \Symfony ;
4
+
5
+ use PHPStan \Rules \Rule ;
6
+ use PHPStan \Testing \RuleTestCase ;
7
+ use PHPStan \Type \MethodTypeSpecifyingExtension ;
8
+ use Symfony \Component \HttpFoundation \Session \SessionInterface ;
9
+
10
+ final class RequestTypeSpecifyingExtensionTest extends RuleTestCase
11
+ {
12
+
13
+ protected function getRule (): Rule
14
+ {
15
+ return new VariableTypeReportingRule ();
16
+ }
17
+
18
+ /** @return MethodTypeSpecifyingExtension[] */
19
+ protected function getMethodTypeSpecifyingExtensions (): array
20
+ {
21
+ return [
22
+ new RequestTypeSpecifyingExtension (),
23
+ ];
24
+ }
25
+
26
+ public function testGetSession (): void
27
+ {
28
+ $ this ->analyse ([__DIR__ . '/request_get_session.php ' ], [
29
+ [
30
+ 'Variable $session1 is: ' . SessionInterface::class . '|null ' ,
31
+ 11 ,
32
+ ],
33
+ [
34
+ 'Variable $session2 is: ' . SessionInterface::class,
35
+ 13 ,
36
+ ],
37
+ ]);
38
+ }
39
+
40
+ }
Original file line number Diff line number Diff line change
1
+ <?php declare (strict_types = 1 );
2
+
3
+ namespace PHPStan \Type \Symfony ;
4
+
5
+ use PhpParser \Node ;
6
+ use PhpParser \Node \Expr \Variable ;
7
+ use PHPStan \Analyser \Scope ;
8
+ use PHPStan \Rules \Rule ;
9
+ use PHPStan \Type \VerbosityLevel ;
10
+
11
+ final class VariableTypeReportingRule implements Rule
12
+ {
13
+
14
+ public function getNodeType (): string
15
+ {
16
+ return Variable::class;
17
+ }
18
+
19
+ /**
20
+ * @param Node $node
21
+ * @param Scope $scope
22
+ * @return (string|RuleError)[] errors
23
+ */
24
+ public function processNode (Node $ node , Scope $ scope ): array
25
+ {
26
+ if (!is_string ($ node ->name )) {
27
+ return [];
28
+ }
29
+ if (!$ scope ->isInFirstLevelStatement ()) {
30
+ return [];
31
+ };
32
+ if ($ scope ->isInExpressionAssign ($ node )) {
33
+ return [];
34
+ }
35
+ return [
36
+ sprintf (
37
+ 'Variable $%s is: %s ' ,
38
+ $ node ->name ,
39
+ $ scope ->getType ($ node )->describe (VerbosityLevel::value ())
40
+ ),
41
+ ];
42
+ }
43
+
44
+ }
Original file line number Diff line number Diff line change
1
+ <?php declare (strict_types = 1 );
2
+
3
+ namespace PHPStan \Type \Symfony ;
4
+
5
+ use Symfony \Component \HttpFoundation \Request ;
6
+
7
+ class Foo {
8
+
9
+ public function doFoo (Request $ request ): void
10
+ {
11
+ $ session1 = $ request ->getSession ();
12
+ if ($ request ->hasSession ()) {
13
+ $ session2 = $ request ->getSession ();
14
+ }
15
+ }
16
+
17
+ }
You can’t perform that action at this time.
0 commit comments