2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2018-12-31 11:15:48 +00:00
|
|
|
|
|
|
|
namespace Rector\CodeQuality\Rector\FuncCall;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\Array_;
|
2020-12-21 02:12:42 +00:00
|
|
|
use PhpParser\Node\Expr\ArrayItem;
|
2018-12-31 11:15:48 +00:00
|
|
|
use PhpParser\Node\Expr\BinaryOp\Equal;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\Identical;
|
|
|
|
use PhpParser\Node\Expr\FuncCall;
|
2020-02-06 21:48:18 +00:00
|
|
|
use Rector\Core\Rector\AbstractRector;
|
2020-11-16 17:50:38 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2018-12-31 11:15:48 +00:00
|
|
|
|
2019-09-03 09:11:45 +00:00
|
|
|
/**
|
|
|
|
* @see \Rector\CodeQuality\Tests\Rector\FuncCall\SingleInArrayToCompareRector\SingleInArrayToCompareRectorTest
|
|
|
|
*/
|
2018-12-31 11:15:48 +00:00
|
|
|
final class SingleInArrayToCompareRector extends AbstractRector
|
|
|
|
{
|
2020-11-16 17:50:38 +00:00
|
|
|
public function getRuleDefinition(): RuleDefinition
|
2018-12-31 11:15:48 +00:00
|
|
|
{
|
2020-11-16 17:50:38 +00:00
|
|
|
return new RuleDefinition(
|
|
|
|
'Changes in_array() with single element to ===',
|
|
|
|
[
|
|
|
|
new CodeSample(
|
|
|
|
<<<'CODE_SAMPLE'
|
2018-12-31 11:15:48 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
if (in_array(strtolower($type), ['$this'], true)) {
|
|
|
|
return strtolower($type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2020-11-16 17:50:38 +00:00
|
|
|
,
|
|
|
|
<<<'CODE_SAMPLE'
|
2018-12-31 11:15:48 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
if (strtolower($type) === '$this') {
|
|
|
|
return strtolower($type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2020-11-16 17:50:38 +00:00
|
|
|
),
|
|
|
|
|
|
|
|
]);
|
2018-12-31 11:15:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getNodeTypes(): array
|
|
|
|
{
|
|
|
|
return [FuncCall::class];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param FuncCall $node
|
|
|
|
*/
|
|
|
|
public function refactor(Node $node): ?Node
|
|
|
|
{
|
|
|
|
if (! $this->isName($node, 'in_array')) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! $node->args[1]->value instanceof Array_) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var Array_ $arrayNode */
|
|
|
|
$arrayNode = $node->args[1]->value;
|
2020-12-25 00:22:45 +00:00
|
|
|
if (count($arrayNode->items) !== 1) {
|
2018-12-31 11:15:48 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-12-21 02:12:42 +00:00
|
|
|
$firstArrayItem = $arrayNode->items[0];
|
|
|
|
if (! $firstArrayItem instanceof ArrayItem) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$firstArrayItemValue = $firstArrayItem->value;
|
2020-05-12 15:20:40 +00:00
|
|
|
// strict
|
|
|
|
if (isset($node->args[2])) {
|
2020-12-21 02:12:42 +00:00
|
|
|
return new Identical($node->args[0]->value, $firstArrayItemValue);
|
2018-12-31 11:15:48 +00:00
|
|
|
}
|
|
|
|
|
2020-12-21 02:12:42 +00:00
|
|
|
return new Equal($node->args[0]->value, $firstArrayItemValue);
|
2018-12-31 11:15:48 +00:00
|
|
|
}
|
|
|
|
}
|