2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2019-07-04 21:02:52 +00:00
|
|
|
|
|
|
|
namespace Rector\CodeQuality\Rector\FuncCall;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
2019-07-05 15:06:28 +00:00
|
|
|
use PhpParser\Node\Arg;
|
2019-07-04 21:02:52 +00:00
|
|
|
use PhpParser\Node\Expr\FuncCall;
|
|
|
|
use PHPStan\Type\StringType;
|
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;
|
2019-07-04 21:02:52 +00:00
|
|
|
|
2019-09-03 09:11:45 +00:00
|
|
|
/**
|
|
|
|
* @see \Rector\CodeQuality\Tests\Rector\FuncCall\IsAWithStringWithThirdArgumentRector\IsAWithStringWithThirdArgumentRectorTest
|
|
|
|
*/
|
2019-07-04 21:02:52 +00:00
|
|
|
final class IsAWithStringWithThirdArgumentRector extends AbstractRector
|
|
|
|
{
|
2020-11-16 17:50:38 +00:00
|
|
|
public function getRuleDefinition(): RuleDefinition
|
2019-07-04 21:02:52 +00:00
|
|
|
{
|
2020-11-16 17:50:38 +00:00
|
|
|
return new RuleDefinition(
|
|
|
|
'Complete missing 3rd argument in case is_a() function in case of strings',
|
|
|
|
[
|
|
|
|
new CodeSample(
|
|
|
|
<<<'CODE_SAMPLE'
|
2019-07-04 21:02:52 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function __construct(string $value)
|
|
|
|
{
|
|
|
|
return is_a($value, 'stdClass');
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2020-11-16 17:50:38 +00:00
|
|
|
,
|
|
|
|
<<<'CODE_SAMPLE'
|
2019-07-04 21:02:52 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function __construct(string $value)
|
|
|
|
{
|
|
|
|
return is_a($value, 'stdClass', true);
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2020-11-16 17:50:38 +00:00
|
|
|
),
|
|
|
|
|
|
|
|
]);
|
2019-07-04 21:02:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
2021-01-30 21:41:25 +00:00
|
|
|
$node->args[2] = new Arg($this->nodeFactory->createTrue());
|
2019-07-04 21:02:52 +00:00
|
|
|
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
}
|