File tree 4 files changed +111
-2
lines changed
data/NoSymfonySessionInConstructorRule
4 files changed +111
-2
lines changed Original file line number Diff line number Diff line change 1
1
rules :
2
2
- Shopware\PhpStan\Rule\ClassExtendUsesAbstractClassWhenExisting
3
3
- Shopware\PhpStan\Rule\DisallowDefaultContextCreation
4
- - Shopware\PhpStan\Rule\ForbidGlobBraceRule
5
4
- Shopware\PhpStan\Rule\DisallowFunctionsRule
5
+ - Shopware\PhpStan\Rule\ForbidGlobBraceRule
6
6
- Shopware\PhpStan\Rule\InternalClassExtendsRule
7
7
- Shopware\PhpStan\Rule\InternalFunctionCallRule
8
8
- Shopware\PhpStan\Rule\InternalMethodCallRule
9
9
- Shopware\PhpStan\Rule\MethodBecomesAbstractRule
10
- - Shopware\PhpStan\Rule\NoUserEntityGetStoreTokenRule
10
+ - Shopware\PhpStan\Rule\NoDALFilterByID
11
+ - Shopware\PhpStan\Rule\NoSymfonySessionInConstructor
11
12
- Shopware\PhpStan\Rule\NoUserEntityGetStoreTokenRule
12
13
- Shopware\PhpStan\Rule\ScheduledTaskTooLowIntervalRule
13
14
- Shopware\PhpStan\Rule\SetForeignKeyRule
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ namespace Shopware \PhpStan \Rule ;
6
+
7
+ use PhpParser \Node ;
8
+ use PhpParser \Node \Expr \MethodCall ;
9
+ use PHPStan \Analyser \Scope ;
10
+ use PHPStan \Rules \Rule ;
11
+ use PHPStan \Rules \RuleErrorBuilder ;
12
+ use PHPStan \Type \ObjectType ;
13
+
14
+ /**
15
+ * @implements Rule<MethodCall>
16
+ */
17
+ class NoSymfonySessionInConstructorRule implements Rule
18
+ {
19
+ public function getNodeType (): string
20
+ {
21
+ return MethodCall::class;
22
+ }
23
+
24
+ public function processNode (Node $ node , Scope $ scope ): array
25
+ {
26
+ if ($ scope ->getFunctionName () !== '__construct ' ) {
27
+ return [];
28
+ }
29
+
30
+ $ objectType = $ scope ->getType ($ node ->var );
31
+
32
+ $ sessionInterfaceType = new ObjectType (
33
+ 'Symfony \\Component \\HttpFoundation \\Session \\SessionInterface ' ,
34
+ );
35
+
36
+ if ($ objectType ->isSuperTypeOf ($ sessionInterfaceType )->maybe ()) {
37
+ return [
38
+ RuleErrorBuilder::message (
39
+ 'Symfony Session should not be called in constructor. Consider using it in the method where it \'s needed. ' ,
40
+ )
41
+ ->identifier ('shopware.sessionUsageInConstructor ' )
42
+ ->build (),
43
+ ];
44
+ }
45
+
46
+ return [];
47
+ }
48
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ namespace Shopware \PhpStan \Tests \Rule ;
6
+
7
+ use PHPStan \Rules \Rule ;
8
+ use PHPStan \Testing \RuleTestCase ;
9
+ use Shopware \PhpStan \Rule \NoSymfonySessionInConstructorRule ;
10
+
11
+ /**
12
+ * @extends RuleTestCase<NoSymfonySessionInConstructorRule>
13
+ */
14
+ class NoSymfonySessionInConstructorTest extends RuleTestCase
15
+ {
16
+ protected function getRule (): Rule
17
+ {
18
+ return new NoSymfonySessionInConstructorRule ();
19
+ }
20
+
21
+ public function testRule (): void
22
+ {
23
+ $ this ->analyse ([__DIR__ . '/../data/NoSymfonySessionInConstructorRule/NoSymfonySessionInConstructor.php ' ], [
24
+ [
25
+ 'Symfony Session should not be called in constructor. Consider using it in the method where it \'s needed. ' ,
26
+ 16 ,
27
+ ],
28
+ ]);
29
+ }
30
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ namespace YourNamespace \Tests \Rule \Data ;
6
+
7
+ use Symfony \Component \HttpFoundation \Session \Session ;
8
+
9
+ class ClassWithSessionInConstructor
10
+ {
11
+ private Session $ session ;
12
+
13
+ public function __construct (
14
+ Session $ session ,
15
+ ) {
16
+ $ session ->get ('test ' );
17
+ }
18
+ }
19
+
20
+ class ClassSessionInMethod
21
+ {
22
+ public function __construct (
23
+ private Session $ session ,
24
+ ) {}
25
+
26
+ public function someMethod (): void
27
+ {
28
+ $ this ->session ->get ('test ' );
29
+ }
30
+ }
You can’t perform that action at this time.
0 commit comments