-
-
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 FormTypeClassNameRule to require clear naming for form …
…types (#169)
- Loading branch information
1 parent
97c5818
commit f1fc3d2
Showing
8 changed files
with
146 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
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,65 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symplify\PHPStanRules\Rules\Symfony; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Name; | ||
use PhpParser\Node\Stmt\Class_; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleError; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use PHPStan\Type\ObjectType; | ||
use Symplify\PHPStanRules\Enum\ClassName; | ||
use Symplify\PHPStanRules\Enum\SymfonyRuleIdentifier; | ||
|
||
/** | ||
* @implements Rule<Class_> | ||
* | ||
* @see \Symplify\PHPStanRules\Tests\Rules\Symfony\FormTypeClassNameRule\FormTypeClassNameRuleTest | ||
*/ | ||
final class FormTypeClassNameRule implements Rule | ||
{ | ||
public function getNodeType(): string | ||
{ | ||
return Class_::class; | ||
} | ||
|
||
/** | ||
* @param Class_ $node | ||
* @return RuleError[] | ||
*/ | ||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if (! $node->namespacedName instanceof Name) { | ||
return []; | ||
} | ||
|
||
// all good | ||
$className = $node->namespacedName->toString(); | ||
if (str_ends_with($className, 'FormType')) { | ||
return []; | ||
} | ||
|
||
$currentObjectType = new ObjectType($className); | ||
|
||
$parentObjectType = new ObjectType(ClassName::FORM_TYPE); | ||
if (! $parentObjectType->isSuperTypeOf($currentObjectType)->yes()) { | ||
return []; | ||
} | ||
|
||
$errorMessage = sprintf( | ||
'Class extends "%s" must have "FormType" suffix to make form explicit, "%s" given', | ||
ClassName::FORM_TYPE, | ||
$className | ||
); | ||
|
||
$identifierRuleError = RuleErrorBuilder::message($errorMessage) | ||
->identifier(SymfonyRuleIdentifier::FORM_TYPE_CLASS_NAME) | ||
->build(); | ||
|
||
return [$identifierRuleError]; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
tests/Rules/Symfony/FormTypeClassNameRule/Fixture/SomeFormType.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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symplify\PHPStanRules\Tests\Rules\Symfony\FormTypeClassNameRule\Fixture; | ||
|
||
use Symfony\Component\Form\AbstractType; | ||
|
||
final class SomeFormType extends AbstractType | ||
{ | ||
} |
11 changes: 11 additions & 0 deletions
11
tests/Rules/Symfony/FormTypeClassNameRule/Fixture/SomeType.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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symplify\PHPStanRules\Tests\Rules\Symfony\FormTypeClassNameRule\Fixture; | ||
|
||
use Symfony\Component\Form\AbstractType; | ||
|
||
final class SomeType extends AbstractType | ||
{ | ||
} |
40 changes: 40 additions & 0 deletions
40
tests/Rules/Symfony/FormTypeClassNameRule/FormTypeClassNameRuleTest.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,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symplify\PHPStanRules\Tests\Rules\Symfony\FormTypeClassNameRule; | ||
|
||
use Iterator; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Symplify\PHPStanRules\Rules\Symfony\FormTypeClassNameRule; | ||
|
||
final class FormTypeClassNameRuleTest extends RuleTestCase | ||
{ | ||
/** | ||
* @param mixed[] $expectedErrorMessagesWithLines | ||
*/ | ||
#[DataProvider('provideData')] | ||
public function testRule(string $filePath, array $expectedErrorMessagesWithLines): void | ||
{ | ||
$this->analyse([$filePath], $expectedErrorMessagesWithLines); | ||
} | ||
|
||
public static function provideData(): Iterator | ||
{ | ||
yield [__DIR__ . '/Fixture/SomeFormType.php', []]; | ||
|
||
yield [__DIR__ . '/Fixture/SomeType.php', [ | ||
[ | ||
'Class extends "Symfony\Component\Form\AbstractType" must have "FormType" suffix to make form explicit, "Symplify\PHPStanRules\Tests\Rules\Symfony\FormTypeClassNameRule\Fixture\SomeType" given', | ||
9, | ||
], | ||
]]; | ||
} | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new FormTypeClassNameRule(); | ||
} | ||
} |