Skip to content

Commit

Permalink
Fix: Adjust Methods\FinalInAbstractClassRule to ignore Doctrine entities
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Dec 1, 2024
1 parent 449466b commit cbfe4b0
Show file tree
Hide file tree
Showing 32 changed files with 476 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ For a full diff see [`2.5.0...main`][2.5.0...main].
### Fixed

- Returned rule with error identifier ([#882]), by [@localheinz]
- Adjusted `Methods\FinalInAbstractClassRule` to ignore Doctrine embeddables and entities ([#396]), by [@localheinz]

## [`2.5.0`][2.5.0]

Expand Down Expand Up @@ -531,6 +532,7 @@ For a full diff see [`362c7ea...0.1.0`][362c7ea...0.1.0].
[#294]: https://github.com/ergebnis/phpstan-rules/pull/294
[#381]: https://github.com/ergebnis/phpstan-rules/pull/381
[#395]: https://github.com/ergebnis/phpstan-rules/pull/395
[#396]: https://github.com/ergebnis/phpstan-rules/pull/396
[#496]: https://github.com/ergebnis/phpstan-rules/pull/496
[#498]: https://github.com/ergebnis/phpstan-rules/pull/498
[#499]: https://github.com/ergebnis/phpstan-rules/pull/498
Expand Down
2 changes: 2 additions & 0 deletions composer-require-checker.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
"PhpParser\\Node\\UnionType",
"PHPStan\\Analyser\\Scope",
"PHPStan\\Node\\FileNode",
"PHPStan\\PhpDoc\\ResolvedPhpDocBlock",
"PHPStan\\PhpDocParser\\Ast\\PhpDoc\\PhpDocTagNode"
"PHPStan\\Reflection\\ClassReflection",
"PHPStan\\Reflection\\ReflectionProvider",
"PHPStan\\Rules\\Rule",
Expand Down
48 changes: 48 additions & 0 deletions src/Methods/FinalInAbstractClassRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use Ergebnis\PHPStan\Rules\ErrorIdentifier;
use PhpParser\Node;
use PHPStan\Analyser;
use PHPStan\PhpDoc;
use PHPStan\PhpDocParser;
use PHPStan\Reflection;
use PHPStan\Rules;

Expand All @@ -24,6 +26,19 @@
*/
final class FinalInAbstractClassRule implements Rules\Rule
{
private const DOCTRINE_ATTRIBUTE_NAMES = [
'Doctrine\\ORM\\Mapping\\Embeddable',
'Doctrine\\ORM\\Mapping\\Entity',
];
private const DOCTRINE_TAG_NAMES = [
'@ORM\\Mapping\\Embeddable',
'@ORM\\Embeddable',
'@Embeddable',
'@ORM\\Mapping\\Entity',
'@ORM\\Entity',
'@Entity',
];

public function getNodeType(): string
{
return Node\Stmt\ClassMethod::class;
Expand All @@ -36,6 +51,10 @@ public function processNode(
/** @var Reflection\ClassReflection $containingClass */
$containingClass = $scope->getClassReflection();

if (self::isDoctrineEntity($containingClass)) {
return [];
}

if (!$containingClass->isAbstract()) {
return [];
}
Expand Down Expand Up @@ -70,4 +89,33 @@ public function processNode(
$ruleErrorBuilder->identifier(ErrorIdentifier::finalInAbstractClass()->toString())->build(),
];
}

private static function isDoctrineEntity(Reflection\ClassReflection $containingClass): bool
{
$attributes = $containingClass->getNativeReflection()->getAttributes();

foreach ($attributes as $attribute) {
if (\in_array($attribute->getName(), self::DOCTRINE_ATTRIBUTE_NAMES, true)) {
return true;
}
}

$resolvedPhpDocBlock = $containingClass->getResolvedPhpDoc();

if ($resolvedPhpDocBlock instanceof PhpDoc\ResolvedPhpDocBlock) {
foreach ($resolvedPhpDocBlock->getPhpDocNodes() as $phpDocNode) {
foreach ($phpDocNode->children as $child) {
if (!$child instanceof PhpDocParser\Ast\PhpDoc\PhpDocTagNode) {
continue;

Check warning on line 109 in src/Methods/FinalInAbstractClassRule.php

View check run for this annotation

Codecov / codecov/patch

src/Methods/FinalInAbstractClassRule.php#L109

Added line #L109 was not covered by tests
}

if (\in_array($child->name, self::DOCTRINE_TAG_NAMES, true)) {
return true;
}
}
}
}

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

declare(strict_types=1);

namespace Ergebnis\PHPStan\Rules\Test\Fixture\Methods\FinalInAbstractClassRule\Success;

/** @Embeddable */
abstract class AbstractClassWithProtectedMethodAndEmbeddableAnnotationInInlineDocBlock
{
protected function method(): void
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Ergebnis\PHPStan\Rules\Test\Fixture\Methods\FinalInAbstractClassRule\Success;

/**
* @Embeddable
*/
abstract class AbstractClassWithProtectedMethodAndEmbeddableAnnotationInMultilineDocBlock
{
protected function method(): void
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Ergebnis\PHPStan\Rules\Test\Fixture\Methods\FinalInAbstractClassRule\Success;

use Doctrine\ORM;

#[ORM\Mapping\Embeddable]
abstract class AbstractClassWithProtectedMethodAndEmbeddableAttribute
{
protected function method(): void
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Ergebnis\PHPStan\Rules\Test\Fixture\Methods\FinalInAbstractClassRule\Success;

/** @Entity */
abstract class AbstractClassWithProtectedMethodAndEntityAnnotationInInlineDocBlock
{
protected function method(): void
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Ergebnis\PHPStan\Rules\Test\Fixture\Methods\FinalInAbstractClassRule\Success;

/**
* @Entity
*/
abstract class AbstractClassWithProtectedMethodAndEntityAnnotationInMultilineDocBlock
{
protected function method(): void
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Ergebnis\PHPStan\Rules\Test\Fixture\Methods\FinalInAbstractClassRule\Success;

use Doctrine\ORM;

#[ORM\Mapping\Entity]
abstract class AbstractClassWithProtectedMethodAndEntityAttribute
{
protected function method(): void
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Ergebnis\PHPStan\Rules\Test\Fixture\Methods\FinalInAbstractClassRule\Success;

/** @ORM\Embeddable */
abstract class AbstractClassWithProtectedMethodAndOrmEmbeddableAnnotationInInlineDocBlock
{
protected function method(): void
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Ergebnis\PHPStan\Rules\Test\Fixture\Methods\FinalInAbstractClassRule\Success;

/**
* @ORM\Embeddable
*/
abstract class AbstractClassWithProtectedMethodAndOrmEmbeddableAnnotationInMultilineDocBlock
{
protected function method(): void
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Ergebnis\PHPStan\Rules\Test\Fixture\Methods\FinalInAbstractClassRule\Success;

/** @ORM\Entity */
abstract class AbstractClassWithProtectedMethodAndOrmEntityAnnotationInInlineDocBlock
{
protected function method(): void
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Ergebnis\PHPStan\Rules\Test\Fixture\Methods\FinalInAbstractClassRule\Success;

/**
* @ORM\Entity
*/
abstract class AbstractClassWithProtectedMethodAndOrmEntityAnnotationInMultilineDocBlock
{
protected function method(): void
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Ergebnis\PHPStan\Rules\Test\Fixture\Methods\FinalInAbstractClassRule\Success;

/** @ORM\Mapping\Embeddable */
abstract class AbstractClassWithProtectedMethodAndOrmMappingEmbeddableAnnotationInInlineDocBlock
{
protected function method(): void
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Ergebnis\PHPStan\Rules\Test\Fixture\Methods\FinalInAbstractClassRule\Success;

/**
* @ORM\Mapping\Embeddable
*/
abstract class AbstractClassWithProtectedMethodAndOrmMappingEmbeddableAnnotationInMultilineDocBlock
{
protected function method(): void
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Ergebnis\PHPStan\Rules\Test\Fixture\Methods\FinalInAbstractClassRule\Success;

/** @ORM\Mapping\Entity */
abstract class AbstractClassWithProtectedMethodAndOrmMappingEntityAnnotationInInlineDocBlock
{
protected function method(): void
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Ergebnis\PHPStan\Rules\Test\Fixture\Methods\FinalInAbstractClassRule\Success;

/**
* @ORM\Mapping\Entity
*/
abstract class AbstractClassWithProtectedMethodAndOrmMappingEntityAnnotationInMultilineDocBlock
{
protected function method(): void
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Ergebnis\PHPStan\Rules\Test\Fixture\Methods\FinalInAbstractClassRule\Success;

/** @Embeddable */
abstract class AbstractClassWithPublicMethodAndEmbeddableAnnotationInInlineDocBlock
{
public function method(): void
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Ergebnis\PHPStan\Rules\Test\Fixture\Methods\FinalInAbstractClassRule\Success;

/**
* @Embeddable
*/
abstract class AbstractClassWithPublicMethodAndEmbeddableAnnotationInMultilineDocBlock
{
public function method(): void
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Ergebnis\PHPStan\Rules\Test\Fixture\Methods\FinalInAbstractClassRule\Success;

use Doctrine\ORM;

#[ORM\Mapping\Embeddable]
abstract class AbstractClassWithPublicMethodAndEmbeddableAttribute
{
public function method(): void
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Ergebnis\PHPStan\Rules\Test\Fixture\Methods\FinalInAbstractClassRule\Success;

/** @Entity */
abstract class AbstractClassWithPublicMethodAndEntityAnnotationInInlineDocBlock
{
public function method(): void
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Ergebnis\PHPStan\Rules\Test\Fixture\Methods\FinalInAbstractClassRule\Success;

/**
* @Entity
*/
abstract class AbstractClassWithPublicMethodAndEntityAnnotationInMultilineDocBlock
{
public function method(): void
{
}
}
Loading

0 comments on commit cbfe4b0

Please sign in to comment.