rector/rules/code-quality/src/Rector/FuncCall/IsAWithStringWithThirdArgumentRector.php

78 lines
1.7 KiB
PHP
Raw Normal View History

2019-10-13 05:59:52 +00:00
<?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\Core\Rector\AbstractRector;
use Rector\Core\RectorDefinition\CodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;
2019-09-03 09:11:45 +00:00
/**
* @see \Rector\CodeQuality\Tests\Rector\FuncCall\IsAWithStringWithThirdArgumentRector\IsAWithStringWithThirdArgumentRectorTest
*/
final class IsAWithStringWithThirdArgumentRector extends AbstractRector
{
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Complete missing 3rd argument in case is_a() function in case of strings', [
new CodeSample(
2019-09-18 06:14:35 +00:00
<<<'PHP'
class SomeClass
{
public function __construct(string $value)
{
return is_a($value, 'stdClass');
}
}
2019-09-18 06:14:35 +00:00
PHP
,
2019-09-18 06:14:35 +00:00
<<<'PHP'
class SomeClass
{
public function __construct(string $value)
{
return is_a($value, 'stdClass', true);
}
}
2019-09-18 06:14:35 +00:00
PHP
),
]);
}
/**
* @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;
}
}