rector/packages/CodeQuality/src/Rector/FuncCall/IsAWithStringWithThirdArgumentRector.php

73 lines
1.5 KiB
PHP
Raw Normal View History

<?php declare(strict_types=1);
namespace Rector\CodeQuality\Rector\FuncCall;
use PhpParser\Node;
2019-07-05 15:06:28 +00:00
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Type\StringType;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
final class IsAWithStringWithThirdArgumentRector extends AbstractRector
{
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('', [
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function __construct(string $value)
{
return is_a($value, 'stdClass');
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function __construct(string $value)
{
return is_a($value, 'stdClass', true);
}
}
CODE_SAMPLE
),
]);
}
/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [FuncCall::class];
}
/**
* @param FuncCall $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->isName($node, 'is_a')) {
return null;
}
if (isset($node->args[2])) {
return null;
}
$firstArgumentStaticType = $this->getStaticType($node->args[0]->value);
if (! $firstArgumentStaticType instanceof StringType) {
return null;
}
2019-07-05 15:06:28 +00:00
$node->args[2] = new Arg($this->createTrue());
return $node;
}
}