Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/Rules/Doctrine/NoGetRepositoryOutsideServiceRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
namespace Symplify\PHPStanRules\Rules\Doctrine;

use PhpParser\Node;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Scalar\String_;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
Expand Down Expand Up @@ -34,10 +36,18 @@ public function getNodeType(): string
*/
public function processNode(Node $node, Scope $scope): array
{
if ($node->isFirstClassCallable()) {
return [];
}

if (! NamingHelper::isName($node->name, 'getRepository')) {
return [];
}

if ($this->isDynamicArg($node)) {
return [];
}

if (! $scope->isInClass()) {
$ruleError = RuleErrorBuilder::message(self::ERROR_MESSAGE)
->identifier(DoctrineRuleIdentifier::NO_GET_REPOSITORY_OUTSIDE_SERVICE)
Expand All @@ -58,4 +68,14 @@ public function processNode(Node $node, Scope $scope): array

return [$ruleError];
}

private function isDynamicArg(MethodCall $methodCall): bool
{
$firstArg = $methodCall->getArgs()[0];
if ($firstArg->value instanceof String_) {
return false;
}

return ! $firstArg->value instanceof ClassConstFetch;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Symplify\PHPStanRules\Tests\Rules\Doctrine\NoGetRepositoryOutsideServiceRule\Fixture;

use Doctrine\ORM\EntityManager;
use Symplify\PHPStanRules\Tests\Rules\Doctrine\NoGetRepositoryOutsideServiceRule\Source\SomeRandomEntity;

final readonly class GetRepositoryRandomEntity
{
public function __construct(
private EntityManager $entityManager
) {
}

public function run(): void
{
$someRepository = $this->entityManager->getRepository(SomeRandomEntity::class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Symplify\PHPStanRules\Tests\Rules\Doctrine\NoGetRepositoryOutsideServiceRule\Fixture;

use Doctrine\ORM\EntityManager;
use Symplify\PHPStanRules\Tests\Rules\Doctrine\NoGetRepositoryOutsideServiceRule\Source\SomeRandomEntity;

final readonly class SkipDymamicFetch
{
public function run(
EntityManager $entityManager,
string $className
) {
$someRepository = $entityManager->getRepository($className);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ public static function provideData(): Iterator
18,
]]];

yield [__DIR__ . '/Fixture/GetRepositoryRandomEntity.php', [[
NoGetRepositoryOutsideServiceRule::ERROR_MESSAGE,
19,
]]];

yield [__DIR__ . '/Fixture/SkipInRepository.php', []];
yield [__DIR__ . '/Fixture/SkipDymamicFetch.php', []];
}

protected function getRule(): Rule
Expand Down