Skip to content

Commit 082b142

Browse files
committed
feat: add NoSymfonySessionInConstructor rule and update rules configuration
1 parent 55d13ea commit 082b142

File tree

4 files changed

+111
-2
lines changed

4 files changed

+111
-2
lines changed

rules.neon

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
rules:
22
- Shopware\PhpStan\Rule\ClassExtendUsesAbstractClassWhenExisting
33
- Shopware\PhpStan\Rule\DisallowDefaultContextCreation
4-
- Shopware\PhpStan\Rule\ForbidGlobBraceRule
54
- Shopware\PhpStan\Rule\DisallowFunctionsRule
5+
- Shopware\PhpStan\Rule\ForbidGlobBraceRule
66
- Shopware\PhpStan\Rule\InternalClassExtendsRule
77
- Shopware\PhpStan\Rule\InternalFunctionCallRule
88
- Shopware\PhpStan\Rule\InternalMethodCallRule
99
- Shopware\PhpStan\Rule\MethodBecomesAbstractRule
10-
- Shopware\PhpStan\Rule\NoUserEntityGetStoreTokenRule
10+
- Shopware\PhpStan\Rule\NoDALFilterByID
11+
- Shopware\PhpStan\Rule\NoSymfonySessionInConstructor
1112
- Shopware\PhpStan\Rule\NoUserEntityGetStoreTokenRule
1213
- Shopware\PhpStan\Rule\ScheduledTaskTooLowIntervalRule
1314
- Shopware\PhpStan\Rule\SetForeignKeyRule
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
}

0 commit comments

Comments
 (0)