Skip to content

Commit 003dee4

Browse files
committed
Skip dynamic entity class names in NoGetRepositoryOutsideServiceRule
1 parent f9180bf commit 003dee4

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

src/Rules/Doctrine/NoGetRepositoryOutsideServiceRule.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
namespace Symplify\PHPStanRules\Rules\Doctrine;
66

77
use PhpParser\Node;
8+
use PhpParser\Node\Expr\ClassConstFetch;
89
use PhpParser\Node\Expr\MethodCall;
10+
use PhpParser\Node\Scalar\String_;
911
use PHPStan\Analyser\Scope;
1012
use PHPStan\Rules\Rule;
1113
use PHPStan\Rules\RuleErrorBuilder;
@@ -34,10 +36,18 @@ public function getNodeType(): string
3436
*/
3537
public function processNode(Node $node, Scope $scope): array
3638
{
39+
if ($node->isFirstClassCallable()) {
40+
return [];
41+
}
42+
3743
if (! NamingHelper::isName($node->name, 'getRepository')) {
3844
return [];
3945
}
4046

47+
if ($this->isDynamicArg($node)) {
48+
return [];
49+
}
50+
4151
if (! $scope->isInClass()) {
4252
$ruleError = RuleErrorBuilder::message(self::ERROR_MESSAGE)
4353
->identifier(DoctrineRuleIdentifier::NO_GET_REPOSITORY_OUTSIDE_SERVICE)
@@ -58,4 +68,14 @@ public function processNode(Node $node, Scope $scope): array
5868

5969
return [$ruleError];
6070
}
71+
72+
private function isDynamicArg(MethodCall $methodCall): bool
73+
{
74+
$firstArg = $methodCall->getArgs()[0];
75+
if ($firstArg->value instanceof String_) {
76+
return false;
77+
}
78+
79+
return ! $firstArg->value instanceof ClassConstFetch;
80+
}
6181
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Symplify\PHPStanRules\Tests\Rules\Doctrine\NoGetRepositoryOutsideServiceRule\Fixture;
6+
7+
use Doctrine\ORM\EntityManager;
8+
use Symplify\PHPStanRules\Tests\Rules\Doctrine\NoGetRepositoryOutsideServiceRule\Source\SomeRandomEntity;
9+
10+
final readonly class GetRepositoryRandomEntity
11+
{
12+
public function __construct(
13+
private EntityManager $entityManager
14+
) {
15+
}
16+
17+
public function run(): void
18+
{
19+
$someRepository = $this->entityManager->getRepository(SomeRandomEntity::class);
20+
}
21+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Symplify\PHPStanRules\Tests\Rules\Doctrine\NoGetRepositoryOutsideServiceRule\Fixture;
6+
7+
use Doctrine\ORM\EntityManager;
8+
use Symplify\PHPStanRules\Tests\Rules\Doctrine\NoGetRepositoryOutsideServiceRule\Source\SomeRandomEntity;
9+
10+
final readonly class SkipDymamicFetch
11+
{
12+
public function run(
13+
EntityManager $entityManager,
14+
string $className
15+
) {
16+
$someRepository = $entityManager->getRepository($className);
17+
}
18+
}

tests/Rules/Doctrine/NoGetRepositoryOutsideServiceRule/NoGetRepositoryOutsideServiceRuleTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,13 @@ public static function provideData(): Iterator
2828
18,
2929
]]];
3030

31+
yield [__DIR__ . '/Fixture/GetRepositoryRandomEntity.php', [[
32+
NoGetRepositoryOutsideServiceRule::ERROR_MESSAGE,
33+
19,
34+
]]];
35+
3136
yield [__DIR__ . '/Fixture/SkipInRepository.php', []];
37+
yield [__DIR__ . '/Fixture/SkipDymamicFetch.php', []];
3238
}
3339

3440
protected function getRule(): Rule

0 commit comments

Comments
 (0)