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
2 changes: 1 addition & 1 deletion src/Rules/Doctrine/RequireQueryBuilderOnRepositoryRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class RequireQueryBuilderOnRepositoryRule implements Rule
/**
* @var string
*/
private const ERROR_MESSAGE = 'Avoid calling ->createQueryBuilder() directly on EntityManager as it requires select() + from() calls with specific values. Use $repository->createQueryBuilder() to be safe instead';
public const ERROR_MESSAGE = 'Avoid calling ->createQueryBuilder() directly on EntityManager as it requires select() + from() calls with specific values. Use $repository->createQueryBuilder() to be safe instead';

public function getNodeType(): string
{
Expand Down
6 changes: 6 additions & 0 deletions stubs/Doctrine/ORM/EntityManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,11 @@

interface EntityManagerInterface
{
/**
* @param class-string $class
* @return EntityRepository
*/
public function getRepository(string $class): object;

public function createQueryBuilder();
}
3 changes: 3 additions & 0 deletions stubs/Doctrine/ORM/EntityRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@

class EntityRepository
{
public function createQueryBuilder()
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

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

use Doctrine\ORM\EntityManagerInterface;
use Symplify\PHPStanRules\Tests\Rules\Doctrine\RequireQueryBuilderOnRepositoryRule\Source\RandomEntity;

final class ReportOnEntityManager
{
public function process(EntityManagerInterface $entityManager)
{
$someRepository = $entityManager->createQueryBuilder();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

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

use Doctrine\ORM\EntityManagerInterface;
use Symplify\PHPStanRules\Tests\Rules\Doctrine\RequireQueryBuilderOnRepositoryRule\Source\RandomEntity;

final class SkipCreateQueryBuilderOnRepository
{
public function process(EntityManagerInterface $entityManager)
{
$someRepository = $entityManager->getRepository(RandomEntity::class);

$queryBuilder = $someRepository
->createQueryBuilder();
}

public function directlyOnCall(EntityManagerInterface $entityManager)
{
$queryBuilder = $entityManager->getRepository(RandomEntity::class)
->createQueryBuilder();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Symplify\PHPStanRules\Tests\Rules\Doctrine\RequireQueryBuilderOnRepositoryRule;

use Iterator;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use PHPUnit\Framework\Attributes\DataProvider;
use Symplify\PHPStanRules\Rules\Doctrine\RequireQueryBuilderOnRepositoryRule;

final class RequireQueryBuilderOnRepositoryRuleTest extends RuleTestCase
{
#[DataProvider('provideData')]
public function testRule(string $filePath, array $expectedErrorsWithLines): void
{
$this->analyse([$filePath], $expectedErrorsWithLines);
}

public static function provideData(): Iterator
{
yield [__DIR__ . '/Fixture/SkipCreateQueryBuilderOnRepository.php', []];

yield [__DIR__ . '/Fixture/ReportOnEntityManager.php', [
[RequireQueryBuilderOnRepositoryRule::ERROR_MESSAGE, 14],
]];
}

protected function getRule(): Rule
{
return new RequireQueryBuilderOnRepositoryRule();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Symplify\PHPStanRules\Tests\Rules\Doctrine\RequireQueryBuilderOnRepositoryRule\Source;

class RandomEntity
{

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Symplify\PHPStanRules\Tests\Rules\Doctrine\RequireServiceRepositoryParentRuleTest\Fixture;
namespace Symplify\PHPStanRules\Tests\Rules\Doctrine\RequireServiceRepositoryParentRule\Fixture;

use Doctrine\Bundle\MongoDBBundle\Repository\ServiceDocumentRepositoryInterface;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Symplify\PHPStanRules\Tests\Rules\Doctrine\RequireServiceRepositoryParentRuleTest\Fixture;
namespace Symplify\PHPStanRules\Tests\Rules\Doctrine\RequireServiceRepositoryParentRule\Fixture;

use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Symplify\PHPStanRules\Tests\Rules\Doctrine\RequireServiceRepositoryParentRuleTest\Fixture;
namespace Symplify\PHPStanRules\Tests\Rules\Doctrine\RequireServiceRepositoryParentRule\Fixture;

final class SomeRepository
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Symplify\PHPStanRules\Tests\Rules\Doctrine\RequireServiceRepositoryParentRuleTest;
namespace Symplify\PHPStanRules\Tests\Rules\Doctrine\RequireServiceRepositoryParentRule;

use Iterator;
use PHPStan\Rules\Rule;
Expand Down