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
32 changes: 30 additions & 2 deletions src/Rules/Doctrine/NoDocumentMockingRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,26 @@
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\Constant\ConstantStringType;
use Symplify\PHPStanRules\Enum\RuleIdentifier\PHPUnitRuleIdentifier;
use Symplify\PHPStanRules\Helper\NamingHelper;

/**
* @implements Rule<MethodCall>
* @see \Symplify\PHPStanRules\Tests\Rules\Doctrine\NoDocumentMockingRule\NoDocumentMockingRuleTest
*/
final class NoDocumentMockingRule implements Rule
final readonly class NoDocumentMockingRule implements Rule
{
public const string ERROR_MESSAGE = 'Instead of document mocking, create object directly to get better type support';

public function __construct(
private ReflectionProvider $reflectionProvider
) {
}

public function getNodeType(): string
{
return MethodCall::class;
Expand All @@ -40,7 +48,11 @@ public function processNode(Node $node, Scope $scope): array
$firstArg = $node->getArgs()[0];
$mockedClassType = $scope->getType($firstArg->value);
foreach ($mockedClassType->getConstantStrings() as $constantStringType) {
if (! str_contains($constantStringType->getValue(), '\\Document\\')) {
if (! str_contains($constantStringType->getValue(), '\\Document\\') && ! str_contains($constantStringType->getValue(), '\\Entity\\')) {
continue;
}

if ($this->shouldSkipDocumentClass($constantStringType)) {
continue;
}

Expand All @@ -53,4 +65,20 @@ public function processNode(Node $node, Scope $scope): array

return [];
}

private function shouldSkipDocumentClass(ConstantStringType $constantStringType): bool
{
if ($this->reflectionProvider->hasClass($constantStringType->getValue())) {
$classReflection = $this->reflectionProvider->getClass($constantStringType->getValue());
if ($classReflection->isAbstract()) {
return true;
}

if ($classReflection->isInterface()) {
return true;
}
}

return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

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

use PHPUnit\Framework\TestCase;
use Symplify\PHPStanRules\Tests\Rules\Doctrine\NoDocumentMockingRule\Source\Entity\AbstractSomeEntity;
use Symplify\PHPStanRules\Tests\Rules\Doctrine\NoDocumentMockingRule\Source\Entity\SomeEntity;

final class SomeAbstractEntityMocking extends TestCase
{
public function test()
{
$someMock = $this->createMock(AbstractSomeEntity::class);
}
}
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\NoDocumentMockingRule\Fixture;

use PHPUnit\Framework\TestCase;
use Symplify\PHPStanRules\Tests\Rules\Doctrine\NoDocumentMockingRule\Source\Entity\SomeEntity;

final class SomeEntityMocking extends TestCase
{
public function test()
{
$someMock = $this->createMock(SomeEntity::class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

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

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

final class NoDocumentMockingRuleTest extends RuleTestCase
{
/**
* @param array<int, array<string|int>> $expectedErrorMessagesWithLines
*/
#[DataProvider('provideData')]
public function testRule(string $filePath, array $expectedErrorMessagesWithLines): void
{
$this->analyse([$filePath], $expectedErrorMessagesWithLines);
}

/**
* @return Iterator<array<array<int, mixed>, mixed>>
*/
public static function provideData(): Iterator
{
yield [__DIR__ . '/Fixture/SomeEntityMocking.php', [[
NoDocumentMockingRule::ERROR_MESSAGE,
14,
]]];

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

/**
* @return string[]
*/
#[Override]
public static function getAdditionalConfigFiles(): array
{
return [__DIR__ . '/config/configured_rule.neon'];
}

protected function getRule(): Rule
{
return self::getContainer()->getByType(NoDocumentMockingRule::class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Symplify\PHPStanRules\Tests\Rules\Doctrine\NoDocumentMockingRule\Source\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity()
*/
abstract class AbstractSomeEntity
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Symplify\PHPStanRules\Tests\Rules\Doctrine\NoDocumentMockingRule\Source\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity()
*/
class SomeEntity
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
includes:
- ../../../../config/included_services.neon

rules:
- Symplify\PHPStanRules\Rules\Doctrine\NoDocumentMockingRule