2021-10-05 08:51:30 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\Strict\Rector\Ternary;
|
2021-10-05 08:51:30 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
use PhpParser\Node\Expr\FuncCall;
|
|
|
|
use PhpParser\Node\Expr\Ternary;
|
|
|
|
use PHPStan\Analyser\Scope;
|
|
|
|
use Rector\Strict\NodeFactory\ExactCompareFactory;
|
|
|
|
use Rector\Strict\Rector\AbstractFalsyScalarRuleFixerRector;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2021-10-05 08:51:30 +00:00
|
|
|
/**
|
|
|
|
* Fixer Rector for PHPStan rule:
|
|
|
|
* https://github.com/phpstan/phpstan-strict-rules/blob/master/src/Rules/DisallowedConstructs/DisallowedShortTernaryRule.php
|
|
|
|
*
|
|
|
|
* @see \Rector\Tests\Strict\Rector\Ternary\DisallowedShortTernaryRuleFixerRector\DisallowedShortTernaryRuleFixerRectorTest
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class DisallowedShortTernaryRuleFixerRector extends AbstractFalsyScalarRuleFixerRector
|
2021-10-05 08:51:30 +00:00
|
|
|
{
|
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-10-05 08:51:30 +00:00
|
|
|
* @var \Rector\Strict\NodeFactory\ExactCompareFactory
|
|
|
|
*/
|
|
|
|
private $exactCompareFactory;
|
2023-08-03 17:04:00 +00:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $hasChanged = \false;
|
2022-06-07 08:22:29 +00:00
|
|
|
public function __construct(ExactCompareFactory $exactCompareFactory)
|
2021-10-05 08:51:30 +00:00
|
|
|
{
|
|
|
|
$this->exactCompareFactory = $exactCompareFactory;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2021-10-05 08:51:30 +00:00
|
|
|
{
|
|
|
|
$errorMessage = \sprintf('Fixer for PHPStan reports by strict type rule - "%s"', 'PHPStan\\Rules\\DisallowedConstructs\\DisallowedShortTernaryRule');
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition($errorMessage, [new ConfiguredCodeSample(<<<'CODE_SAMPLE'
|
2021-10-05 08:51:30 +00:00
|
|
|
final class ShortTernaryArray
|
|
|
|
{
|
|
|
|
public function run(array $array)
|
|
|
|
{
|
|
|
|
return $array ?: 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
|
|
|
, <<<'CODE_SAMPLE'
|
|
|
|
final class ShortTernaryArray
|
|
|
|
{
|
|
|
|
public function run(array $array)
|
|
|
|
{
|
|
|
|
return $array !== [] ? $array : 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
2023-12-16 15:42:57 +00:00
|
|
|
, [\Rector\Strict\Rector\Ternary\DisallowedShortTernaryRuleFixerRector::TREAT_AS_NON_EMPTY => \false])]);
|
2021-10-05 08:51:30 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @return array<class-string<Node>>
|
|
|
|
*/
|
|
|
|
public function getNodeTypes() : array
|
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [Ternary::class];
|
2021-10-05 08:51:30 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param Ternary $node
|
2021-10-05 08:51:30 +00:00
|
|
|
*/
|
2023-05-11 08:30:47 +00:00
|
|
|
public function refactorWithScope(Node $node, Scope $scope) : ?Ternary
|
2021-10-05 08:51:30 +00:00
|
|
|
{
|
2023-08-03 17:04:00 +00:00
|
|
|
$this->hasChanged = \false;
|
2021-10-05 08:51:30 +00:00
|
|
|
// skip non-short ternary
|
2023-03-23 23:21:34 +00:00
|
|
|
if ($node->if instanceof Expr) {
|
2021-10-05 08:51:30 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// special case for reset() function
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($node->cond instanceof FuncCall && $this->isName($node->cond, 'reset')) {
|
2021-10-05 08:51:30 +00:00
|
|
|
$this->refactorResetFuncCall($node, $node->cond, $scope);
|
2023-08-03 17:04:00 +00:00
|
|
|
if (!$this->hasChanged) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-10-05 08:51:30 +00:00
|
|
|
return $node;
|
|
|
|
}
|
2023-08-03 17:04:00 +00:00
|
|
|
$exprType = $scope->getNativeType($node->cond);
|
2021-10-05 15:59:39 +00:00
|
|
|
$compareExpr = $this->exactCompareFactory->createNotIdenticalFalsyCompare($exprType, $node->cond, $this->treatAsNonEmpty);
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$compareExpr instanceof Expr) {
|
2021-10-05 08:51:30 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$node->if = $node->cond;
|
2021-10-05 15:59:39 +00:00
|
|
|
$node->cond = $compareExpr;
|
2021-10-05 08:51:30 +00:00
|
|
|
return $node;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function refactorResetFuncCall(Ternary $ternary, FuncCall $resetFuncCall, Scope $scope) : void
|
2021-10-05 08:51:30 +00:00
|
|
|
{
|
|
|
|
$ternary->if = $ternary->cond;
|
2023-05-08 12:54:32 +00:00
|
|
|
if ($resetFuncCall->isFirstClassCallable()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$firstArgValue = $resetFuncCall->getArgs()[0]->value;
|
2023-08-03 17:04:00 +00:00
|
|
|
$firstArgType = $scope->getNativeType($firstArgValue);
|
2021-10-05 15:59:39 +00:00
|
|
|
$falsyCompareExpr = $this->exactCompareFactory->createNotIdenticalFalsyCompare($firstArgType, $firstArgValue, $this->treatAsNonEmpty);
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$falsyCompareExpr instanceof Expr) {
|
2021-10-05 08:51:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
$ternary->cond = $falsyCompareExpr;
|
2023-08-03 17:04:00 +00:00
|
|
|
$this->hasChanged = \true;
|
2021-10-05 08:51:30 +00:00
|
|
|
}
|
|
|
|
}
|