Skip to content

Commit c7e0b43

Browse files
committed
feat: add NoUserEntityGetStoreTokenRule
1 parent ae49138 commit c7e0b43

6 files changed

Lines changed: 88 additions & 14 deletions

File tree

phpstan.neon

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,3 @@ parameters:
33
paths:
44
- src
55

6-
rules:
7-
- Shopware\PhpStan\Rule\InternalFunctionCallRule
8-
- Shopware\PhpStan\Rule\InternalMethodCallRule
9-
- Shopware\PhpStan\Rule\DisallowFunctionsRule
10-
- Shopware\PhpStan\Rule\DisallowDefaultContextCreation
11-
- Shopware\PhpStan\Rule\SetForeignKeyRule
12-
- Shopware\PhpStan\Rule\ScheduledTaskTooLowIntervalRule

rules.neon

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
rules:
2+
- Shopware\PhpStan\Rule\DisallowDefaultContextCreation
3+
- Shopware\PhpStan\Rule\ForbidGlobBraceRule
4+
- Shopware\PhpStan\Rule\DisallowFunctionsRule
5+
- Shopware\PhpStan\Rule\InternalClassExtendsRule
26
- Shopware\PhpStan\Rule\InternalFunctionCallRule
37
- Shopware\PhpStan\Rule\InternalMethodCallRule
4-
- Shopware\PhpStan\Rule\DisallowFunctionsRule
5-
- Shopware\PhpStan\Rule\DisallowDefaultContextCreation
6-
- Shopware\PhpStan\Rule\SetForeignKeyRule
78
- Shopware\PhpStan\Rule\MethodBecomesAbstractRule
9+
- Shopware\PhpStan\Rule\NoUserEntityGetStoreTokenRule
10+
- Shopware\PhpStan\Rule\NoUserEntityGetStoreTokenRule
811
- Shopware\PhpStan\Rule\ScheduledTaskTooLowIntervalRule
9-
- Shopware\PhpStan\Rule\InternalClassExtendsRule
10-
- Shopware\PhpStan\Rule\ForbidGlobBraceRule
12+
- Shopware\PhpStan\Rule\SetForeignKeyRule
1113

1214
services:
1315
-
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Shopware\PhpStan\Rule;
4+
5+
use PhpParser\Node;
6+
use PhpParser\Node\Expr\MethodCall;
7+
use PHPStan\Analyser\Scope;
8+
use PHPStan\Rules\Rule;
9+
use PHPStan\Rules\RuleErrorBuilder;
10+
use PHPStan\Type\ObjectType;
11+
use Shopware\Core\System\User\UserEntity;
12+
13+
/**
14+
* @implements Rule<MethodCall>
15+
*/
16+
class NoUserEntityGetStoreTokenRule implements Rule
17+
{
18+
public function getNodeType(): string
19+
{
20+
return MethodCall::class;
21+
}
22+
23+
public function processNode(Node $node, Scope $scope): array
24+
{
25+
if (!$node->name instanceof Node\Identifier) {
26+
return [];
27+
}
28+
29+
if ($node->name->toString() !== 'getStoreToken') {
30+
return [];
31+
}
32+
33+
$callerType = $scope->getType($node->var);
34+
$userEntityType = new ObjectType(UserEntity::class);
35+
36+
if (!$userEntityType->isSuperTypeOf($callerType)->yes()) {
37+
return [];
38+
}
39+
40+
return [
41+
RuleErrorBuilder::message('Its not allowed to gather the store token')
42+
->identifier('shopware.noUserEntityGetStoreToken')
43+
->line($node->getLine())
44+
->build(),
45+
];
46+
}
47+
}

src/Rule/SetForeignKeyRule.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PHPStan\Rules\Rule;
99
use PHPStan\Rules\RuleErrorBuilder;
1010
use Shopware\Core\Framework\Migration\MigrationStep;
11+
use Shopware\Core\Framework\Plugin;
1112

1213
/**
1314
* @implements Rule<Node\Stmt\ClassMethod>
@@ -27,14 +28,13 @@ public function processNode(Node $node, Scope $scope): array
2728
return [];
2829
}
2930

30-
3131
$class = $scope->getClassReflection();
3232

3333
if ($class === null) {
3434
return [];
3535
}
3636

37-
if ($class->getParentClass()?->getName() !== MigrationStep::class) {
37+
if ($class->getParentClass()?->getName() !== MigrationStep::class && $class->getParentClass()?->getName() !== Plugin::class) {
3838
return [];
3939
}
4040

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Shopware\PhpStan\Tests\Rule;
4+
5+
use PHPStan\Rules\Rule;
6+
use PHPStan\Testing\RuleTestCase;
7+
use Shopware\PhpStan\Rule\NoUserEntityGetStoreTokenRule;
8+
9+
class NoUserEntityGetStoreTokenRuleTest extends RuleTestCase
10+
{
11+
public function testAnalyse(): void
12+
{
13+
$this->analyse([__DIR__ . '/../data/NoUserEntityGetStoreTokenRule/context.php'], [
14+
[
15+
'Its not allowed to gather the store token',
16+
6,
17+
],
18+
]);
19+
}
20+
21+
22+
protected function getRule(): Rule
23+
{
24+
return new NoUserEntityGetStoreTokenRule();
25+
}
26+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
use Shopware\Core\System\User\UserEntity;
4+
5+
$user = new UserEntity();
6+
$user->getStoreToken();

0 commit comments

Comments
 (0)