Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/set/code-quality/code-quality.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ services:
Rector\CodeQuality\Rector\For_\ForToForeachRector: ~
Rector\CodeQuality\Rector\FuncCall\CompactToVariablesRector: ~
Rector\CodeQuality\Rector\Class_\CompleteDynamicPropertiesRector: ~
Rector\CodeQuality\Rector\FuncCall\IsAWithStringWithThirdArgumentRector: ~
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php declare(strict_types=1);

namespace Rector\CodeQuality\Rector\FuncCall;

use PhpParser\Node;
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;
}

$node->args[2] = new Node\Arg($this->createTrue());

return $node;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Rector\CodeQuality\Tests\Rector\FuncCall\IsAWithStringWithThirdArgumentRector\Fixture;

class SomeClass
{
public function __construct(string $value, $anotherValue)
{
$isString = is_a($value, 'stdClass');
$isString = is_a($value, 'stdClass', true);

$maybe = is_a($anotherValue, 'stdClass');
}
}

?>
-----
<?php

namespace Rector\CodeQuality\Tests\Rector\FuncCall\IsAWithStringWithThirdArgumentRector\Fixture;

class SomeClass
{
public function __construct(string $value, $anotherValue)
{
$isString = is_a($value, 'stdClass', true);
$isString = is_a($value, 'stdClass', true);

$maybe = is_a($anotherValue, 'stdClass');
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php declare(strict_types=1);

namespace Rector\CodeQuality\Tests\Rector\FuncCall\IsAWithStringWithThirdArgumentRector;

use Rector\CodeQuality\Rector\FuncCall\IsAWithStringWithThirdArgumentRector;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class IsAWithStringWithThirdArgumentRectorTest extends AbstractRectorTestCase
{
public function test(): void
{
$this->doTestFiles([__DIR__ . '/Fixture/fixture.php.inc']);
}

protected function getRectorClass(): string
{
return IsAWithStringWithThirdArgumentRector::class;
}
}