Skip to content

Commit c4595df

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

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

src/Rules/Doctrine/NoGetRepositoryOutsideServiceRule.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PhpParser\Node;
88
use PhpParser\Node\Expr\MethodCall;
9+
use PhpParser\Node\Scalar\String_;
910
use PHPStan\Analyser\Scope;
1011
use PHPStan\Rules\Rule;
1112
use PHPStan\Rules\RuleErrorBuilder;
@@ -34,10 +35,18 @@ public function getNodeType(): string
3435
*/
3536
public function processNode(Node $node, Scope $scope): array
3637
{
38+
if ($node->isFirstClassCallable()) {
39+
return [];
40+
}
41+
3742
if (! NamingHelper::isName($node->name, 'getRepository')) {
3843
return [];
3944
}
4045

46+
if ($this->isDynamicArg($node)) {
47+
return [];
48+
}
49+
4150
if (! $scope->isInClass()) {
4251
$ruleError = RuleErrorBuilder::message(self::ERROR_MESSAGE)
4352
->identifier(DoctrineRuleIdentifier::NO_GET_REPOSITORY_OUTSIDE_SERVICE)
@@ -58,4 +67,14 @@ public function processNode(Node $node, Scope $scope): array
5867

5968
return [$ruleError];
6069
}
70+
71+
private function isDynamicArg(MethodCall $methodCall): bool
72+
{
73+
$firstArg = $methodCall->getArgs()[0];
74+
if ($firstArg->value instanceof String_) {
75+
return false;
76+
}
77+
78+
return ! $firstArg->value instanceof Node\Expr\ClassConstFetch;
79+
}
6180
}
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)