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
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ parameters:

# used in tests
- '#Public constant "Symplify\\PHPStanRules\\(.*?)Rule\:\:ERROR_MESSAGE" is never used#'
- '#Public constant "Symplify\\PHPStanRules\\(.*?)Rule\:\:TEST_ERROR_MESSAGE" is never used#'

-
message: '#Generator expects value type array<string, mixed>, list<bool\|float\|int\|list<string>\|PHPStan\\TrinaryLogic\|string\|null> given#'
Expand Down
7 changes: 6 additions & 1 deletion src/Rules/Doctrine/NoGetRepositoryOutsideServiceRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use PHPStan\Rules\RuleErrorBuilder;
use Symplify\PHPStanRules\Enum\RuleIdentifier\DoctrineRuleIdentifier;
use Symplify\PHPStanRules\Helper\NamingHelper;
use Symplify\PHPStanRules\PHPUnit\TestClassDetector;

/**
* @see \Symplify\PHPStanRules\Tests\Rules\Doctrine\NoGetRepositoryOutsideServiceRule\NoGetRepositoryOutsideServiceRuleTest
Expand All @@ -25,6 +26,8 @@ final class NoGetRepositoryOutsideServiceRule implements Rule
{
public const string ERROR_MESSAGE = 'Instead of getting repository from EntityManager, use constructor injection and service pattern to keep code clean';

public const string TEST_ERROR_MESSAGE = 'Instead of getting repository from EntityManager, fetch the repository service directly from the test container';

public function getNodeType(): string
{
return MethodCall::class;
Expand Down Expand Up @@ -61,7 +64,9 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

$ruleError = RuleErrorBuilder::message(self::ERROR_MESSAGE)
$errorMessage = TestClassDetector::isTestClass($scope) ? self::TEST_ERROR_MESSAGE : self::ERROR_MESSAGE;

$ruleError = RuleErrorBuilder::message($errorMessage)
->identifier(DoctrineRuleIdentifier::NO_GET_REPOSITORY_OUTSIDE_SERVICE)
->build();

Expand Down
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 class GetRepositoryInsideContext
{
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
Expand Up @@ -36,6 +36,11 @@ public static function provideData(): Iterator
19,
]]];

yield [__DIR__ . '/Fixture/GetRepositoryInsideContext.php', [[
NoGetRepositoryOutsideServiceRule::TEST_ERROR_MESSAGE,
19,
]]];

yield [__DIR__ . '/Fixture/SkipInRepository.php', []];
yield [__DIR__ . '/Fixture/SkipDynamicFetch.php', []];
yield [__DIR__ . '/Fixture/SkipDynamicClassConstFetch.php', []];
Expand Down
Loading