2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\Php72\Rector\Unset_;
|
2018-10-17 03:21:02 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\Assign;
|
|
|
|
use PhpParser\Node\Expr\Cast\Unset_;
|
2023-05-24 09:42:43 +00:00
|
|
|
use PhpParser\Node\Expr\FuncCall;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Stmt\Expression;
|
2023-06-05 09:32:57 +00:00
|
|
|
use PhpParser\NodeTraverser;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Rector\AbstractRector;
|
|
|
|
use Rector\ValueObject\PhpVersionFeature;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2019-09-03 09:11:45 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\Php72\Rector\Unset_\UnsetCastRector\UnsetCastRectorTest
|
2019-09-03 09:11:45 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class UnsetCastRector extends AbstractRector implements MinPhpVersionInterface
|
2018-10-17 03:21:02 +00:00
|
|
|
{
|
2021-09-24 14:11:08 +00:00
|
|
|
public function provideMinPhpVersion() : int
|
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return PhpVersionFeature::NO_UNSET_CAST;
|
2021-09-24 14:11:08 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2018-10-17 03:21:02 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Removes (unset) cast', [new CodeSample(<<<'CODE_SAMPLE'
|
2019-09-25 23:38:52 +00:00
|
|
|
$different = (unset) $value;
|
|
|
|
|
2018-10-17 03:21:02 +00:00
|
|
|
$value = (unset) $value;
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2019-09-25 23:38:52 +00:00
|
|
|
$different = null;
|
|
|
|
|
|
|
|
unset($value);
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2018-10-17 03:21:02 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2018-10-17 03:21:02 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2018-10-17 03:21:02 +00:00
|
|
|
{
|
2023-05-24 09:42:43 +00:00
|
|
|
return [Unset_::class, Assign::class, Expression::class];
|
2018-10-17 03:21:02 +00:00
|
|
|
}
|
|
|
|
/**
|
2023-05-24 09:42:43 +00:00
|
|
|
* @param Unset_|Assign|Expression $node
|
2023-06-05 09:32:57 +00:00
|
|
|
* @return int|null|\PhpParser\Node
|
2018-10-17 03:21:02 +00:00
|
|
|
*/
|
2023-06-05 09:32:57 +00:00
|
|
|
public function refactor(Node $node)
|
2018-10-17 03:21:02 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($node instanceof Assign) {
|
2023-05-24 09:42:43 +00:00
|
|
|
return $this->refactorAssign($node);
|
2019-09-25 23:38:52 +00:00
|
|
|
}
|
2023-05-24 09:42:43 +00:00
|
|
|
if ($node instanceof Expression) {
|
|
|
|
if (!$node->expr instanceof Unset_) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-06-05 09:32:57 +00:00
|
|
|
return NodeTraverser::REMOVE_NODE;
|
2019-09-25 23:38:52 +00:00
|
|
|
}
|
2021-01-30 21:41:25 +00:00
|
|
|
return $this->nodeFactory->createNull();
|
2018-10-17 03:21:02 +00:00
|
|
|
}
|
2023-05-24 09:42:43 +00:00
|
|
|
private function refactorAssign(Assign $assign) : ?FuncCall
|
|
|
|
{
|
|
|
|
if (!$assign->expr instanceof Unset_) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$unset = $assign->expr;
|
|
|
|
if (!$this->nodeComparator->areNodesEqual($assign->var, $unset->expr)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $this->nodeFactory->createFuncCall('unset', [$assign->var]);
|
|
|
|
}
|
2018-10-17 03:21:02 +00:00
|
|
|
}
|