2021-05-10 00:23:30 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\Symfony\Rector\ClassMethod;
|
2021-05-10 00:23:30 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\Coalesce;
|
|
|
|
use PhpParser\Node\Expr\Cast\Int_;
|
|
|
|
use PhpParser\Node\Expr\Ternary;
|
|
|
|
use PhpParser\Node\FunctionLike;
|
|
|
|
use PhpParser\Node\Identifier;
|
|
|
|
use PhpParser\Node\Scalar\LNumber;
|
|
|
|
use PhpParser\Node\Stmt\Class_;
|
|
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
|
|
|
use PhpParser\Node\Stmt\Return_;
|
|
|
|
use PhpParser\NodeTraverser;
|
|
|
|
use PHPStan\Type\IntegerType;
|
|
|
|
use PHPStan\Type\ObjectType;
|
|
|
|
use Rector\Core\Rector\AbstractRector;
|
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2021-05-10 00:23:30 +00:00
|
|
|
/**
|
|
|
|
* @see https://github.com/symfony/symfony/pull/33775/files
|
|
|
|
* @see \Rector\Symfony\Tests\Rector\ClassMethod\ConsoleExecuteReturnIntRector\ConsoleExecuteReturnIntRectorTest
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class ConsoleExecuteReturnIntRector extends AbstractRector
|
2021-05-10 00:23:30 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2021-05-10 00:23:30 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Returns int from Command::execute command', [new CodeSample(<<<'CODE_SAMPLE'
|
2021-05-10 00:23:30 +00:00
|
|
|
class SomeCommand extends Command
|
|
|
|
{
|
|
|
|
public function execute(InputInterface $input, OutputInterface $output)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
|
|
|
, <<<'CODE_SAMPLE'
|
|
|
|
class SomeCommand extends Command
|
|
|
|
{
|
|
|
|
public function execute(InputInterface $input, OutputInterface $output): int
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
|
|
|
)]);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @return array<class-string<Node>>
|
|
|
|
*/
|
|
|
|
public function getNodeTypes() : array
|
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [ClassMethod::class];
|
2021-05-10 00:23:30 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param ClassMethod $node
|
2021-05-10 00:23:30 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2021-05-10 00:23:30 +00:00
|
|
|
{
|
|
|
|
if (!$this->isName($node, 'execute')) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
$class = $this->betterNodeFinder->findParentType($node, Class_::class);
|
|
|
|
if (!$class instanceof Class_) {
|
2021-05-10 00:23:30 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 09:46:15 +00:00
|
|
|
if (!$this->isObjectType($class, new ObjectType('Symfony\\Component\\Console\\Command\\Command'))) {
|
2021-05-10 00:23:30 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$this->refactorReturnTypeDeclaration($node);
|
|
|
|
$this->addReturn0ToMethod($node);
|
|
|
|
return $node;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function refactorReturnTypeDeclaration(ClassMethod $classMethod) : void
|
2021-05-10 00:23:30 +00:00
|
|
|
{
|
|
|
|
// already set
|
|
|
|
if ($classMethod->returnType !== null && $this->isName($classMethod->returnType, 'int')) {
|
|
|
|
return;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
$classMethod->returnType = new Identifier('int');
|
2021-05-10 00:23:30 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function addReturn0ToMethod(ClassMethod $classMethod) : void
|
2021-05-10 00:23:30 +00:00
|
|
|
{
|
|
|
|
$hasReturn = \false;
|
2022-06-07 08:22:29 +00:00
|
|
|
$this->traverseNodesWithCallable((array) $classMethod->getStmts(), function (Node $node) use($classMethod, &$hasReturn) : ?int {
|
|
|
|
if ($node instanceof FunctionLike) {
|
|
|
|
return NodeTraverser::DONT_TRAVERSE_CHILDREN;
|
2021-05-10 00:23:30 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
|
|
|
|
if ($parentNode instanceof Node && $this->isReturnWithExprIntEquals($parentNode, $node)) {
|
2021-05-10 00:23:30 +00:00
|
|
|
$hasReturn = \true;
|
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$node instanceof Return_) {
|
2021-05-10 00:23:30 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($node->expr instanceof Int_) {
|
2021-05-10 00:23:30 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($node->expr instanceof Ternary && $this->isIntegerTernaryIfElse($node->expr)) {
|
2021-05-10 00:23:30 +00:00
|
|
|
$hasReturn = \true;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// is there return without nesting?
|
|
|
|
if ($this->nodeComparator->areNodesEqual($parentNode, $classMethod)) {
|
|
|
|
$hasReturn = \true;
|
|
|
|
}
|
|
|
|
$this->setReturnTo0InsteadOfNull($node);
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
$this->processReturn0ToMethod($hasReturn, $classMethod);
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function isIntegerTernaryIfElse(Ternary $ternary) : bool
|
2021-05-10 00:23:30 +00:00
|
|
|
{
|
|
|
|
/** @var Expr|null $if */
|
|
|
|
$if = $ternary->if;
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$if instanceof Expr) {
|
2021-05-10 00:23:30 +00:00
|
|
|
$if = $ternary->cond;
|
|
|
|
}
|
|
|
|
/** @var Expr $else */
|
|
|
|
$else = $ternary->else;
|
2021-10-07 19:06:33 +00:00
|
|
|
$ifType = $this->getType($if);
|
|
|
|
$elseType = $this->getType($else);
|
2022-06-07 08:22:29 +00:00
|
|
|
return $ifType instanceof IntegerType && $elseType instanceof IntegerType;
|
2021-05-10 00:23:30 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function processReturn0ToMethod(bool $hasReturn, ClassMethod $classMethod) : void
|
2021-05-10 00:23:30 +00:00
|
|
|
{
|
|
|
|
if ($hasReturn) {
|
|
|
|
return;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
$classMethod->stmts[] = new Return_(new LNumber(0));
|
2021-05-10 00:23:30 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function isReturnWithExprIntEquals(Node $parentNode, Node $node) : bool
|
2021-05-10 00:23:30 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$parentNode instanceof Return_) {
|
2021-05-10 00:23:30 +00:00
|
|
|
return \false;
|
|
|
|
}
|
|
|
|
if (!$this->nodeComparator->areNodesEqual($parentNode->expr, $node)) {
|
|
|
|
return \false;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
return $node instanceof Int_;
|
2021-05-10 00:23:30 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function setReturnTo0InsteadOfNull(Return_ $return) : void
|
2021-05-10 00:23:30 +00:00
|
|
|
{
|
|
|
|
if ($return->expr === null) {
|
2022-06-07 08:22:29 +00:00
|
|
|
$return->expr = new LNumber(0);
|
2021-05-10 00:23:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ($this->valueResolver->isNull($return->expr)) {
|
2022-06-07 08:22:29 +00:00
|
|
|
$return->expr = new LNumber(0);
|
2021-05-10 00:23:30 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($return->expr instanceof Coalesce && $this->valueResolver->isNull($return->expr->right)) {
|
|
|
|
$return->expr->right = new LNumber(0);
|
2021-05-10 00:23:30 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($return->expr instanceof Ternary) {
|
2021-05-10 00:23:30 +00:00
|
|
|
$hasChanged = $this->isSuccessfulRefactorTernaryReturn($return->expr);
|
|
|
|
if ($hasChanged) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2021-10-07 19:06:33 +00:00
|
|
|
$staticType = $this->getType($return->expr);
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$staticType instanceof IntegerType) {
|
|
|
|
$return->expr = new Int_($return->expr);
|
2021-05-10 00:23:30 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function isSuccessfulRefactorTernaryReturn(Ternary $ternary) : bool
|
2021-05-10 00:23:30 +00:00
|
|
|
{
|
|
|
|
$hasChanged = \false;
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($ternary->if instanceof Expr && $this->valueResolver->isNull($ternary->if)) {
|
|
|
|
$ternary->if = new LNumber(0);
|
2021-05-10 00:23:30 +00:00
|
|
|
$hasChanged = \true;
|
|
|
|
}
|
|
|
|
if ($this->valueResolver->isNull($ternary->else)) {
|
2022-06-07 08:22:29 +00:00
|
|
|
$ternary->else = new LNumber(0);
|
2021-05-10 00:23:30 +00:00
|
|
|
$hasChanged = \true;
|
|
|
|
}
|
|
|
|
return $hasChanged;
|
|
|
|
}
|
|
|
|
}
|