-
-
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 NoConstructorAndRequiredTogetherRule (#168)
* [symfony] Add NoConstructorAndRequiredTogetherRule * docs
- Loading branch information
1 parent
12b7b11
commit 158f4d6
Showing
9 changed files
with
211 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
87 changes: 87 additions & 0 deletions
87
src/Rules/Symfony/NoConstructorAndRequiredTogetherRule.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,87 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symplify\PHPStanRules\Rules\Symfony; | ||
|
||
use PhpParser\Comment\Doc; | ||
use PhpParser\Node; | ||
use PhpParser\Node\Stmt\Class_; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\IdentifierRuleError; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use Symplify\PHPStanRules\Enum\MethodName; | ||
use Symplify\PHPStanRules\Enum\SymfonyRuleIdentifier; | ||
|
||
/** | ||
* @implements Rule<Class_> | ||
* | ||
* @see \Symplify\PHPStanRules\Tests\Rules\Symfony\NoConstructorAndRequiredTogetherRule\NoConstructorAndRequiredTogetherRuleTest | ||
*/ | ||
final class NoConstructorAndRequiredTogetherRule implements Rule | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
public const ERROR_MESSAGE = 'Avoid using __construct() and @required in the same class. Pick one to keep architecture clean'; | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Class_::class; | ||
} | ||
|
||
/** | ||
* @param Class_ $node | ||
* @return IdentifierRuleError[] | ||
*/ | ||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if ($node->isAnonymous()) { | ||
return []; | ||
} | ||
|
||
if (! $node->getMethod(MethodName::CONSTRUCTOR)) { | ||
return []; | ||
} | ||
|
||
if (! $this->hasAutowiredMethod($node)) { | ||
return []; | ||
} | ||
|
||
$ruleError = RuleErrorBuilder::message(self::ERROR_MESSAGE) | ||
->identifier(SymfonyRuleIdentifier::NO_CONSTRUCT_AND_REQUIRED) | ||
->build(); | ||
|
||
return [ | ||
$ruleError, | ||
]; | ||
} | ||
|
||
private function hasAutowiredMethod(Class_ $class): bool | ||
{ | ||
foreach ($class->getMethods() as $classMethod) { | ||
if (! $classMethod->isPublic()) { | ||
continue; | ||
} | ||
|
||
$docComment = $classMethod->getDocComment(); | ||
if (! $docComment instanceof Doc) { | ||
continue; | ||
} | ||
|
||
if (! str_contains($docComment->getText(), '@required')) { | ||
continue; | ||
} | ||
|
||
// special case when its allowed, to avoid circular references | ||
if (str_contains($docComment->getText(), 'circular')) { | ||
continue; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...fony/NoConstructorAndRequiredTogetherRule/Fixture/ConstructorAndRequiredInSingleClass.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,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symplify\PHPStanRules\Tests\Rules\Symfony\NoConstructorAndRequiredTogetherRule\Fixture; | ||
|
||
final class ConstructorAndRequiredInSingleClass | ||
{ | ||
public function __construct() | ||
{ | ||
} | ||
|
||
/** | ||
* @required | ||
*/ | ||
public function someRequired() | ||
{ | ||
|
||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...Symfony/NoConstructorAndRequiredTogetherRule/Fixture/SkipCircularDependencyPrevention.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,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symplify\PHPStanRules\Tests\Rules\Symfony\NoConstructorAndRequiredTogetherRule\Fixture; | ||
|
||
final class SkipCircularDependencyPrevention | ||
{ | ||
public function __construct() | ||
{ | ||
} | ||
|
||
/** | ||
* Avoid circular dependency | ||
* @required | ||
*/ | ||
public function someRequired() | ||
{ | ||
|
||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
tests/Rules/Symfony/NoConstructorAndRequiredTogetherRule/Fixture/SkipOtherSoleMethod.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\NoConstructorAndRequiredTogetherRule\Fixture; | ||
|
||
final class SkipOtherSoleMethod | ||
{ | ||
/** | ||
* @required | ||
*/ | ||
public function autowire() | ||
{ | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
tests/Rules/Symfony/NoConstructorAndRequiredTogetherRule/Fixture/SkipSoleMethod.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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symplify\PHPStanRules\Tests\Rules\Symfony\NoConstructorAndRequiredTogetherRule\Fixture; | ||
|
||
final class SkipSoleMethod | ||
{ | ||
public function __construct() | ||
{ | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...Symfony/NoConstructorAndRequiredTogetherRule/NoConstructorAndRequiredTogetherRuleTest.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,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symplify\PHPStanRules\Tests\Rules\Symfony\NoConstructorAndRequiredTogetherRule; | ||
|
||
use Iterator; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Symplify\PHPStanRules\Rules\Symfony\NoConstructorAndRequiredTogetherRule; | ||
|
||
final class NoConstructorAndRequiredTogetherRuleTest 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/ConstructorAndRequiredInSingleClass.php', [[ | ||
NoConstructorAndRequiredTogetherRule::ERROR_MESSAGE, | ||
7, | ||
]]]; | ||
|
||
yield [__DIR__ . '/Fixture/SkipSoleMethod.php', []]; | ||
yield [__DIR__ . '/Fixture/SkipOtherSoleMethod.php', []]; | ||
|
||
yield [__DIR__ . '/Fixture/SkipCircularDependencyPrevention.php', []]; | ||
} | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new NoConstructorAndRequiredTogetherRule(); | ||
} | ||
} |