Skip to content

Commit 51c32a6

Browse files
authored
skip dynamic class const fetch (#224)
1 parent c3f69d8 commit 51c32a6

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

src/Rules/Doctrine/NoGetRepositoryOutsideServiceRule.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PhpParser\Node;
88
use PhpParser\Node\Expr\ClassConstFetch;
99
use PhpParser\Node\Expr\MethodCall;
10+
use PhpParser\Node\Name;
1011
use PhpParser\Node\Scalar\String_;
1112
use PHPStan\Analyser\Scope;
1213
use PHPStan\Rules\Rule;
@@ -76,6 +77,11 @@ private function isDynamicArg(MethodCall $methodCall): bool
7677
return false;
7778
}
7879

79-
return ! $firstArg->value instanceof ClassConstFetch;
80+
if ($firstArg->value instanceof ClassConstFetch) {
81+
$classConstFetch = $firstArg->value;
82+
return ! $classConstFetch->class instanceof Name;
83+
}
84+
85+
return true;
8086
}
8187
}
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 SkipDynamicClassConstFetch
11+
{
12+
public function run(
13+
EntityManager $entityManager,
14+
object $someObject
15+
) {
16+
$someRepository = $entityManager->getRepository($someObject::class);
17+
}
18+
}

tests/Rules/Doctrine/NoGetRepositoryOutsideServiceRule/Fixture/SkipDymamicFetch.php renamed to tests/Rules/Doctrine/NoGetRepositoryOutsideServiceRule/Fixture/SkipDynamicFetch.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Doctrine\ORM\EntityManager;
88
use Symplify\PHPStanRules\Tests\Rules\Doctrine\NoGetRepositoryOutsideServiceRule\Source\SomeRandomEntity;
99

10-
final readonly class SkipDymamicFetch
10+
final readonly class SkipDynamicFetch
1111
{
1212
public function run(
1313
EntityManager $entityManager,

tests/Rules/Doctrine/NoGetRepositoryOutsideServiceRule/NoGetRepositoryOutsideServiceRuleTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ public static function provideData(): Iterator
3434
]]];
3535

3636
yield [__DIR__ . '/Fixture/SkipInRepository.php', []];
37-
yield [__DIR__ . '/Fixture/SkipDymamicFetch.php', []];
37+
yield [__DIR__ . '/Fixture/SkipDynamicFetch.php', []];
38+
yield [__DIR__ . '/Fixture/SkipDynamicClassConstFetch.php', []];
3839
}
3940

4041
protected function getRule(): Rule

0 commit comments

Comments
 (0)