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

Fix: Adjust Methods\FinalInAbstractClassRule to ignore Doctrine embeddables and entities #396

Merged
merged 1 commit into from
Dec 1, 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
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,11 @@ parameters:

This rule reports an error when a concrete `public` or `protected `method in an `abstract` class is not `final`.

:bulb: This rule ignores

- Doctrine embeddables
- Doctrine entities

##### Disabling the rule

You can set the the `enabled` parameter to `false` to disable this rule.
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;
}

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