2021-05-10 00:23:30 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare (strict_types=1);
|
2023-06-24 13:18:21 +00:00
|
|
|
namespace Rector\Symfony\Symfony44\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;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\NodeAnalyzer\TerminatedNodeAnalyzer;
|
|
|
|
use Rector\PhpParser\Node\Value\ValueResolver;
|
|
|
|
use Rector\Rector\AbstractRector;
|
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
|
|
|
/**
|
2023-05-11 07:56:32 +00:00
|
|
|
* @changelog https://github.com/symfony/symfony/pull/33775/files
|
2023-06-24 13:18:21 +00:00
|
|
|
* @see \Rector\Symfony\Tests\Symfony44\Rector\ClassMethod\ConsoleExecuteReturnIntRector\ConsoleExecuteReturnIntRectorTest
|
2021-05-10 00:23:30 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class ConsoleExecuteReturnIntRector extends AbstractRector
|
2021-05-10 00:23:30 +00:00
|
|
|
{
|
2022-09-11 14:23:52 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\NodeAnalyzer\TerminatedNodeAnalyzer
|
2022-09-11 14:23:52 +00:00
|
|
|
*/
|
|
|
|
private $terminatedNodeAnalyzer;
|
2023-09-23 12:32:25 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\PhpParser\Node\Value\ValueResolver
|
2023-09-23 12:32:25 +00:00
|
|
|
*/
|
|
|
|
private $valueResolver;
|
2023-06-08 22:00:17 +00:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $hasChanged = \false;
|
2023-09-23 12:32:25 +00:00
|
|
|
public function __construct(TerminatedNodeAnalyzer $terminatedNodeAnalyzer, ValueResolver $valueResolver)
|
2022-09-11 14:23:52 +00:00
|
|
|
{
|
|
|
|
$this->terminatedNodeAnalyzer = $terminatedNodeAnalyzer;
|
2023-09-23 12:32:25 +00:00
|
|
|
$this->valueResolver = $valueResolver;
|
2022-09-11 14:23:52 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2021-05-10 00:23:30 +00:00
|
|
|
{
|
2023-05-29 14:08:28 +00:00
|
|
|
return new RuleDefinition('Returns int from Command::execute() command', [new CodeSample(<<<'CODE_SAMPLE'
|
|
|
|
use Symfony\Component\Console\Command\Command;
|
|
|
|
|
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'
|
2023-05-29 14:08:28 +00:00
|
|
|
use Symfony\Component\Console\Command\Command;
|
|
|
|
|
2021-05-10 00:23:30 +00:00
|
|
|
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-12-21 17:45:30 +00:00
|
|
|
return [Class_::class];
|
2021-05-10 00:23:30 +00:00
|
|
|
}
|
|
|
|
/**
|
2022-12-21 17:45:30 +00:00
|
|
|
* @param Class_ $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
|
|
|
{
|
2022-12-21 17:45:30 +00:00
|
|
|
if (!$this->isObjectType($node, new ObjectType('Symfony\\Component\\Console\\Command\\Command'))) {
|
2021-05-10 00:23:30 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-12-21 17:45:30 +00:00
|
|
|
$executeClassMethod = $node->getMethod('execute');
|
|
|
|
if (!$executeClassMethod instanceof ClassMethod) {
|
2021-05-10 00:23:30 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-12-21 17:45:30 +00:00
|
|
|
$this->refactorReturnTypeDeclaration($executeClassMethod);
|
2023-05-29 14:08:28 +00:00
|
|
|
$this->addReturn0ToExecuteClassMethod($executeClassMethod);
|
2022-11-24 14:41:27 +00:00
|
|
|
if ($this->hasChanged) {
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
return null;
|
2021-05-10 00:23:30 +00:00
|
|
|
}
|
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');
|
2022-11-24 14:41:27 +00:00
|
|
|
$this->hasChanged = \true;
|
2021-05-10 00:23:30 +00:00
|
|
|
}
|
2023-05-29 14:08:28 +00:00
|
|
|
private function addReturn0ToExecuteClassMethod(ClassMethod $classMethod) : void
|
2021-05-10 00:23:30 +00:00
|
|
|
{
|
2023-05-29 14:08:28 +00:00
|
|
|
if ($classMethod->stmts === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$this->traverseNodesWithCallable($classMethod->stmts, function (Node $node) : ?int {
|
|
|
|
// skip anonymous class/function
|
|
|
|
if ($node instanceof FunctionLike || $node instanceof Class_) {
|
2022-12-21 17:45:30 +00:00
|
|
|
return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
|
2021-05-10 00:23:30 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$node instanceof Return_) {
|
2021-05-10 00:23:30 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-12-22 11:35:32 +00:00
|
|
|
if ($this->isReturnIntegerType($node->expr)) {
|
|
|
|
return null;
|
2021-05-10 00:23:30 +00:00
|
|
|
}
|
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
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$this->setReturnTo0InsteadOfNull($node);
|
|
|
|
return null;
|
|
|
|
});
|
2023-05-29 14:08:28 +00:00
|
|
|
$this->processReturn0ToMethod($classMethod);
|
2021-05-10 00:23:30 +00:00
|
|
|
}
|
2022-12-22 11:35:32 +00:00
|
|
|
private function isReturnIntegerType(?Expr $expr) : bool
|
|
|
|
{
|
|
|
|
if ($expr instanceof Expr) {
|
|
|
|
$returnedType = $this->getType($expr);
|
|
|
|
if ($returnedType instanceof IntegerType) {
|
|
|
|
return \true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return \false;
|
|
|
|
}
|
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
|
|
|
}
|
2023-05-29 14:08:28 +00:00
|
|
|
private function processReturn0ToMethod(ClassMethod $classMethod) : void
|
2021-05-10 00:23:30 +00:00
|
|
|
{
|
2022-09-11 14:23:52 +00:00
|
|
|
$stmts = (array) $classMethod->stmts;
|
|
|
|
\end($stmts);
|
|
|
|
$lastKey = \key($stmts);
|
2024-03-08 06:34:02 +00:00
|
|
|
\reset($stmts);
|
2022-09-11 14:23:52 +00:00
|
|
|
$return = new Return_(new LNumber(0));
|
|
|
|
if ($lastKey !== null && (isset($classMethod->stmts[$lastKey]) && $this->terminatedNodeAnalyzer->isAlwaysTerminated($classMethod, $classMethod->stmts[$lastKey], $return))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$classMethod->stmts[] = $return;
|
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
|
|
|
{
|
2023-03-30 09:04:49 +00:00
|
|
|
if (!$return->expr instanceof Expr) {
|
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;
|
|
|
|
}
|
|
|
|
}
|