Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add new rules docs #155

Merged
merged 11 commits into from
Dec 18, 2024
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
576 changes: 574 additions & 2 deletions README.md

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions config/doctrine-rules.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
rules:
- Symplify\PHPStanRules\PHPStan\Rules\Doctrine\NoGetRepositoryOutsideServiceRule
- Symplify\PHPStanRules\PHPStan\Rules\Doctrine\NoParentRepositoryRule
- Symplify\PHPStanRules\PHPStan\Rules\Doctrine\NoRepositoryCallInDataFixtureRule
- Symplify\PHPStanRules\Rules\Doctrine\NoGetRepositoryOutsideServiceRule
- Symplify\PHPStanRules\Rules\Doctrine\NoParentRepositoryRule
- Symplify\PHPStanRules\Rules\Doctrine\NoRepositoryCallInDataFixtureRule
6 changes: 6 additions & 0 deletions config/phpunit-rules.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
rules:
- Symplify\PHPStanRules\Rules\PHPUnit\PublicStaticDataProviderRule
- Symplify\PHPStanRules\Rules\PHPUnit\NoMockOnlyTestRule

- Symplify\PHPStanRules\Rules\Doctrine\NoDocumentMockingRule
- Symplify\PHPStanRules\Rules\Doctrine\NoEntityMockingRule
10 changes: 10 additions & 0 deletions src/Enum/ClassName.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,14 @@ final class ClassName
* @var string
*/
public const DOCTRINE_FIXTURE_INTERFACE = 'Doctrine\Common\DataFixtures\FixtureInterface';

/**
* @var string
*/
public const ENTITY_REPOSITORY_CLASS = 'Doctrine\ORM\EntityRepository';

/**
* @var string
*/
public const MOCK_OBJECT_CLASS = 'PHPUnit\Framework\MockObject\MockObject';
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Symplify\PHPStanRules\PHPStan\Rules\Doctrine;
namespace Symplify\PHPStanRules\Rules\Doctrine;

use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@

declare(strict_types=1);

namespace Symplify\PHPStanRules\PHPStan\Rules\Doctrine;
namespace Symplify\PHPStanRules\Rules\Doctrine;

use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Class_;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use Symplify\PHPStanRules\Enum\ClassName;
use Symplify\PHPStanRules\Enum\RuleIdentifier;

/**
* Check if class extends repository class,
* the entity manager should be injected via constructor instead
*
* @see \Symplify\PHPStanRules\Tests\PHPStan\Rule\NoParentRepositoryRule\NoParentRepositoryRuleTest
* @see \Symplify\PHPStanRules\Tests\Rules\Doctrine\NoParentRepositoryRule\NoParentRepositoryRuleTest
*
* @implements Rule<Class_>
*/
Expand All @@ -27,11 +28,6 @@ final class NoParentRepositoryRule implements Rule
*/
public const ERROR_MESSAGE = 'Extending EntityRepository is not allowed, use constructor injection and pass entity manager instead';

/**
* @var string
*/
private const ENTITY_REPOSITORY_CLASS = 'Doctrine\ORM\EntityRepository';

public function getNodeType(): string
{
return Class_::class;
Expand All @@ -47,7 +43,7 @@ public function processNode(Node $node, Scope $scope): array
}

$parentClass = $node->extends->toString();
if ($parentClass !== self::ENTITY_REPOSITORY_CLASS) {
if ($parentClass !== ClassName::ENTITY_REPOSITORY_CLASS) {
return [];
}

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\PHPStan\Rules\Doctrine;
namespace Symplify\PHPStanRules\Rules\Doctrine;

use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
Expand Down
8 changes: 2 additions & 6 deletions src/Rules/PHPUnit/NoMockOnlyTestRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PHPStan\Node\InClassNode;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use Symplify\PHPStanRules\Enum\ClassName;
use Symplify\PHPStanRules\Enum\RuleIdentifier;
use Symplify\PHPStanRules\Testing\PHPUnitTestAnalyser;

Expand All @@ -26,11 +27,6 @@
*/
public const ERROR_MESSAGE = 'Test should have at least one non-mocked property, to test something';

/**
* @var string
*/
private const MOCK_OBJECT_CLASS = 'PHPUnit\Framework\MockObject\MockObject';

public function getNodeType(): string
{
return InClassNode::class;
Expand Down Expand Up @@ -63,7 +59,7 @@ public function processNode(Node $node, Scope $scope): array

$propertyClassName = $property->type->toString();

if ($propertyClassName !== self::MOCK_OBJECT_CLASS) {
if ($propertyClassName !== ClassName::MOCK_OBJECT_CLASS) {
$hasExclusivelyMockedProperties = false;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Rules/Symfony/NoRequiredOutsideClassRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Symplify\PHPStanRules\Enum\RuleIdentifier;

/**
* @see \Symplify\PHPStanRules\Tests\PHPStan\Rule\NoRequiredOutsideClassRule\NoRequiredOutsideClassRuleTest
* @see \Symplify\PHPStanRules\Tests\Rules\Symfony\NoRequiredOutsideClassRule\NoRequiredOutsideClassRuleTest
*
* @implements Rule<Trait_>
*/
Expand All @@ -23,7 +23,7 @@ final class NoRequiredOutsideClassRule implements Rule
/**
* @var string
*/
public const ERROR_MESSAGE = 'Symfony #[Require]/@required should be used only in classes to avoid missuse';
public const ERROR_MESSAGE = 'Symfony #[Require]/@required should be used only in classes to avoid misuse';

/**
* @var string
Expand Down
7 changes: 1 addition & 6 deletions src/Rules/Symfony/NoStringInGetSubscribedEventsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@
*/
final class NoStringInGetSubscribedEventsRule implements Rule
{
/**
* @var string
*/
private const EVENT_SUBSCRIBER_INTERFACE = 'Symfony\Component\EventDispatcher\EventSubscriberInterface';

/**
* @var string
*/
Expand Down Expand Up @@ -56,7 +51,7 @@ public function processNode(Node $node, Scope $scope): array
}

// only handle symfony one
if (! $classReflection->implementsInterface(self::EVENT_SUBSCRIBER_INTERFACE)) {
if (! $classReflection->implementsInterface(ClassName::EVENT_SUBSCRIBER_INTERFACE)) {
return [];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use PHPUnit\Framework\Attributes\DataProvider;
use Symplify\PHPStanRules\PHPStan\Rules\Doctrine\NoGetRepositoryOutsideServiceRule;
use Symplify\PHPStanRules\Rules\Doctrine\NoGetRepositoryOutsideServiceRule;

final class NoGetRepositoryOutsideServiceRuleTest extends RuleTestCase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use PHPUnit\Framework\Attributes\DataProvider;
use Symplify\PHPStanRules\PHPStan\Rules\Doctrine\NoParentRepositoryRule;
use Symplify\PHPStanRules\Rules\Doctrine\NoParentRepositoryRule;

final class NoParentRepositoryRuleTest extends RuleTestCase
{
/**
* @param mixed[] $expectedErrorMessagesWithLines
*/
#[DataProvider('provideData')]
public function testRule(string $filePath, array $expectedErrorMessagesWithLines): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use PHPUnit\Framework\Attributes\DataProvider;
use Symplify\PHPStanRules\PHPStan\Rules\Doctrine\NoRepositoryCallInDataFixtureRule;
use Symplify\PHPStanRules\Rules\Doctrine\NoRepositoryCallInDataFixtureRule;

final class NoRepositoryCallInDataFixtureRuleTest extends RuleTestCase
{
Expand Down
Loading