Skip to content
Open
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
1 change: 1 addition & 0 deletions extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ parameters:
- stubs/Money/MoneyParser.stub
rules:
- Ibexa\PHPStan\Rules\NoConfigResolverParametersInConstructorRule
- Ibexa\PHPStan\Rules\FinalClassRule
77 changes: 77 additions & 0 deletions rules/FinalClassRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\PHPStan\Rules;

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;

/**
* @implements \PHPStan\Rules\Rule<\PhpParser\Node\Stmt\Class_>
*/
final class FinalClassRule implements Rule
{
private ReflectionProvider $reflectionProvider;

public function __construct(
ReflectionProvider $reflectionProvider
) {
$this->reflectionProvider = $reflectionProvider;
}

public function getNodeType(): string
{
return Node\Stmt\Class_::class;
}

public function processNode(Node $node, Scope $scope): array
{
// Skip anonymous classes
if (!isset($node->namespacedName)) {
return [];
}

$className = $node->namespacedName->toString();

if (!$this->reflectionProvider->hasClass($className)) {
return [];
}

$reflection = $this->reflectionProvider->getClass($className);

// Skip if already final
if ($reflection->isFinal()) {
return [];
}

// Skip if abstract (abstract classes shouldn't be final)
if ($reflection->isAbstract()) {
return [];
}

// Skip interfaces and traits
if ($reflection->isInterface() || $reflection->isTrait()) {
return [];
}

return [
RuleErrorBuilder::message(
sprintf(
'Class %s is not final. All non-abstract classes should be final.',
$reflection->getName()
)
)
->identifier('class.notFinal')
->tip('Add "final" keyword to the class declaration.')
->build(),
];
}
}
53 changes: 53 additions & 0 deletions tests/rules/FinalClassRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Tests\PHPStan\Rules;

use Ibexa\PHPStan\Rules\FinalClassRule;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;

/**
* @extends \PHPStan\Testing\RuleTestCase<\Ibexa\PHPStan\Rules\FinalClassRule>
*/
final class FinalClassRuleTest extends RuleTestCase
{
protected function getRule(): Rule
{
return new FinalClassRule($this->createReflectionProvider());
}

public function testRule(): void
{
$this->analyse(
[
__DIR__ . '/Fixtures/FinalClass/NonFinalClass.php',
],
[
[
'Class Ibexa\Tests\PHPStan\Rules\Fixtures\FinalClass\NonFinalClass is not final. All non-abstract classes should be final.',
11,
'Add "final" keyword to the class declaration.',
],
]
);
}

public function testNoErrorsOnFinalAndAbstractClassesAndInterfaces(): void
{
$this->analyse(
[
__DIR__ . '/Fixtures/FinalClass/FinalClass.php',
__DIR__ . '/Fixtures/FinalClass/AbstractClass.php',
__DIR__ . '/Fixtures/FinalClass/SomeInterface.php',
__DIR__ . '/Fixtures/FinalClass/SomeTrait.php',
],
[]
);
}
}
13 changes: 13 additions & 0 deletions tests/rules/Fixtures/FinalClass/AbstractClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Tests\PHPStan\Rules\Fixtures\FinalClass;

abstract class AbstractClass
{
}
13 changes: 13 additions & 0 deletions tests/rules/Fixtures/FinalClass/FinalClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Tests\PHPStan\Rules\Fixtures\FinalClass;

final class FinalClass
{
}
13 changes: 13 additions & 0 deletions tests/rules/Fixtures/FinalClass/NonFinalClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Tests\PHPStan\Rules\Fixtures\FinalClass;

class NonFinalClass
{
}
13 changes: 13 additions & 0 deletions tests/rules/Fixtures/FinalClass/SomeInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Tests\PHPStan\Rules\Fixtures\FinalClass;

interface SomeInterface
{
}
16 changes: 16 additions & 0 deletions tests/rules/Fixtures/FinalClass/SomeTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Tests\PHPStan\Rules\Fixtures\FinalClass;

/**
* @phpstan-ignore trait.unused
*/
trait SomeTrait
{
}
Loading