-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[symfony] Add RequiredOnlyInAbstractRule (#164)
- Loading branch information
1 parent
2b10e91
commit 588c6c6
Showing
6 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symplify\PHPStanRules\Rules\Symfony; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Stmt\Class_; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use Symplify\PHPStanRules\Enum\SymfonyRuleIdentifier; | ||
use Symplify\PHPStanRules\NodeAnalyzer\SymfonyRequiredMethodAnalyzer; | ||
|
||
/** | ||
* @see \Symplify\PHPStanRules\Tests\Rules\Symfony\RequiredOnlyInAbstractRule\RequiredOnlyInAbstractRuleTest | ||
* | ||
* @implements Rule<Class_> | ||
*/ | ||
final class RequiredOnlyInAbstractRule implements Rule | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
public const ERROR_MESSAGE = '#@required is reserved exclusively for abstract classes. For the rest of classes, use clean constructor injection'; | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Class_::class; | ||
} | ||
|
||
/** | ||
* @param Class_ $node | ||
*/ | ||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
foreach ($node->getMethods() as $classMethod) { | ||
if (! SymfonyRequiredMethodAnalyzer::detect($classMethod)) { | ||
continue; | ||
} | ||
|
||
if ($node->isAbstract()) { | ||
continue; | ||
} | ||
|
||
$identifierRuleError = RuleErrorBuilder::message(self::ERROR_MESSAGE) | ||
->line($classMethod->getLine()) | ||
->identifier(SymfonyRuleIdentifier::SYMFONY_REQUIRED_ONLY_IN_ABSTRACT) | ||
->build(); | ||
|
||
return [$identifierRuleError]; | ||
} | ||
|
||
return []; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
tests/Rules/Symfony/RequiredOnlyInAbstractRule/Fixture/NonAbstractControllerWithRequired.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symplify\PHPStanRules\Tests\Rules\Symfony\RequiredOnlyInAbstractRule\Fixture; | ||
|
||
final class NonAbstractControllerWithRequired | ||
{ | ||
/** | ||
* @required | ||
*/ | ||
public function someMethod() | ||
{ | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
tests/Rules/Symfony/RequiredOnlyInAbstractRule/Fixture/SkipAbstractClass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symplify\PHPStanRules\Tests\Rules\Symfony\RequiredOnlyInAbstractRule\Fixture; | ||
|
||
abstract class SkipAbstractClass | ||
{ | ||
/** | ||
* @required | ||
*/ | ||
public function someMethod() | ||
{ | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
tests/Rules/Symfony/RequiredOnlyInAbstractRule/RequiredOnlyInAbstractRuleTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symplify\PHPStanRules\Tests\Rules\Symfony\RequiredOnlyInAbstractRule; | ||
|
||
use Iterator; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Symplify\PHPStanRules\Rules\Symfony\RequiredOnlyInAbstractRule; | ||
|
||
final class RequiredOnlyInAbstractRuleTest extends RuleTestCase | ||
{ | ||
#[DataProvider('provideData')] | ||
public function testRule(string $filePath, array $expectedErrorMessagesWithLines): void | ||
{ | ||
$this->analyse([$filePath], $expectedErrorMessagesWithLines); | ||
} | ||
|
||
public static function provideData(): Iterator | ||
{ | ||
yield [__DIR__ . '/Fixture/NonAbstractControllerWithRequired.php', [[ | ||
RequiredOnlyInAbstractRule::ERROR_MESSAGE, | ||
12, | ||
]]]; | ||
|
||
yield [__DIR__ . '/Fixture/SkipAbstractClass.php', []]; | ||
} | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new RequiredOnlyInAbstractRule(); | ||
} | ||
} |