-
-
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.
Showing
8 changed files
with
124 additions
and
166 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,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symplify\PHPStanRules\NodeAnalyzer; | ||
|
||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Expr\Variable; | ||
use PhpParser\Node\Identifier; | ||
|
||
final class MethodCallNameAnalyzer | ||
{ | ||
public static function isThisMethodCall(MethodCall $methodCall, string $methodName): bool | ||
{ | ||
if (! $methodCall->name instanceof Identifier) { | ||
return false; | ||
} | ||
|
||
if ($methodCall->name->toString() !== $methodName) { | ||
return false; | ||
} | ||
|
||
// is "$this"? | ||
if (! $methodCall->var instanceof Variable) { | ||
return false; | ||
} | ||
|
||
if (! is_string($methodCall->var->name)) { | ||
return false; | ||
} | ||
|
||
return $methodCall->var->name === 'this'; | ||
} | ||
} |
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,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symplify\PHPStanRules\Rules\Symfony; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use Symplify\PHPStanRules\Enum\RuleIdentifier; | ||
use Symplify\PHPStanRules\NodeAnalyzer\MethodCallNameAnalyzer; | ||
use Symplify\PHPStanRules\Symfony\NodeAnalyzer\SymfonyControllerAnalyzer; | ||
|
||
/** | ||
* @implements Rule<MethodCall> | ||
*/ | ||
final class NoGetDoctrineInControllerRule implements Rule | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private const ERROR_MESSAGE = 'Do not use $this->getDoctrine() method in controller. Use __construct(EntityManagerInterface $entityManager) instead'; | ||
|
||
public function getNodeType(): string | ||
{ | ||
return MethodCall::class; | ||
} | ||
|
||
/** | ||
* @param MethodCall $node | ||
*/ | ||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if (! MethodCallNameAnalyzer::isThisMethodCall($node, 'getDoctrine')) { | ||
return []; | ||
} | ||
|
||
if (! SymfonyControllerAnalyzer::isControllerScope($scope)) { | ||
return []; | ||
} | ||
|
||
$ruleError = RuleErrorBuilder::message(self::ERROR_MESSAGE) | ||
->file($scope->getFile()) | ||
->line($node->getStartLine()) | ||
->identifier(RuleIdentifier::NO_GET_DOCTRINE_IN_CONTROLLER) | ||
->build(); | ||
|
||
return [$ruleError]; | ||
} | ||
} |
Oops, something went wrong.