-
-
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.
Add 15 PHPStan rules from Handyman, mostly Symfony, Doctrine and PHPU…
…nit related (#153) * Add 15 phpstan rules from handyman, mostly Symfony, Doctrine and PHPUnit related * moving symfony and doctrine rules * improve symfony rules location * remove abstract symplify rule * use rule identifiers * add fixture stubs to make fixture repository rule work
- Loading branch information
1 parent
b691662
commit 025b86d
Showing
85 changed files
with
2,075 additions
and
205 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
rules: | ||
- Symplify\PHPStanRules\PHPStan\Rules\Doctrine\NoGetRepositoryOutsideServiceRule | ||
- Symplify\PHPStanRules\PHPStan\Rules\Doctrine\NoParentRepositoryRule | ||
- Symplify\PHPStanRules\PHPStan\Rules\Doctrine\NoRepositoryCallInDataFixtureRule |
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,7 @@ | ||
rules: | ||
- Symplify\PHPStanRules\Rules\Symfony\NoAbstractControllerConstructorRule | ||
- Symplify\PHPStanRules\Rules\Symfony\NoRequiredOutsideClassRule | ||
- Symplify\PHPStanRules\Rules\Symfony\SingleArgEventDispatchRule | ||
- Symplify\PHPStanRules\Rules\Symfony\NoListenerWithoutContractRule | ||
- Symplify\PHPStanRules\Rules\Symfony\NoStringInGetSubscribedEventsRule | ||
- Symplify\PHPStanRules\Rules\Symfony\RequireInvokableControllerRule |
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 was deleted.
Oops, something went wrong.
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,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symplify\PHPStanRules\Doctrine; | ||
|
||
use PHPStan\PhpDoc\ResolvedPhpDocBlock; | ||
use PHPStan\Reflection\ClassReflection; | ||
|
||
final readonly class DoctrineEntityDocumentAnalyser | ||
{ | ||
/** | ||
* @var string[] | ||
*/ | ||
private const ENTITY_DOCBLOCK_MARKERS = ['@Document', '@ORM\\Document', '@Entity', '@ORM\\Entity']; | ||
|
||
public static function isEntityClass(ClassReflection $classReflection): bool | ||
{ | ||
$resolvedPhpDocBlock = $classReflection->getResolvedPhpDoc(); | ||
if (! $resolvedPhpDocBlock instanceof ResolvedPhpDocBlock) { | ||
return false; | ||
} | ||
|
||
foreach (self::ENTITY_DOCBLOCK_MARKERS as $entityDocBlockMarkers) { | ||
if (str_contains($resolvedPhpDocBlock->getPhpDocString(), $entityDocBlockMarkers)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
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
65 changes: 65 additions & 0 deletions
65
src/PHPStan/Rules/Doctrine/NoGetRepositoryOutsideServiceRule.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,65 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symplify\PHPStanRules\PHPStan\Rules\Doctrine; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Identifier; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use Symplify\PHPStanRules\Enum\RuleIdentifier; | ||
|
||
/** | ||
* @see \Symplify\PHPStanRules\Tests\Rules\Doctrine\NoGetRepositoryOutsideServiceRule\NoGetRepositoryOutsideServiceRuleTest | ||
* | ||
* @implements Rule<MethodCall> | ||
*/ | ||
final class NoGetRepositoryOutsideServiceRule implements Rule | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
public const ERROR_MESSAGE = 'Instead of getting repository from EntityManager, use constructor injection and service pattern to keep code clean'; | ||
|
||
public function getNodeType(): string | ||
{ | ||
return MethodCall::class; | ||
} | ||
|
||
/** | ||
* @param MethodCall $node | ||
*/ | ||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if (! $node->name instanceof Identifier) { | ||
return []; | ||
} | ||
|
||
if ($node->name->toString() !== 'getRepository') { | ||
return []; | ||
} | ||
|
||
if (! $scope->isInClass()) { | ||
$ruleError = RuleErrorBuilder::message(self::ERROR_MESSAGE) | ||
->identifier(RuleIdentifier::DOCTRINE_NO_GET_REPOSITORY_OUTSIDE_SERVICE) | ||
->build(); | ||
|
||
return [$ruleError]; | ||
} | ||
|
||
// dummy check | ||
$classReflection = $scope->getClassReflection(); | ||
if (str_ends_with($classReflection->getName(), 'Repository')) { | ||
return []; | ||
} | ||
|
||
$ruleError = RuleErrorBuilder::message(self::ERROR_MESSAGE) | ||
->identifier(RuleIdentifier::DOCTRINE_NO_GET_REPOSITORY_OUTSIDE_SERVICE) | ||
->build(); | ||
|
||
return [$ruleError]; | ||
} | ||
} |
Oops, something went wrong.