Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Simple\Rector\ClassMethod\DemoObjectMethodCallRenameRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class DemoObjectMethodCallRenameRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Rector\Tests\Simple\Rector\ClassMethod\DemoObjectMethodCallRenameRector\Fixture;

class AssignedNew
{
public function run()
{
$dateTime = new \DateTime();
$dateTime->oldMethod();
}
}

?>
-----
<?php

namespace Rector\Tests\Simple\Rector\ClassMethod\DemoObjectMethodCallRenameRector\Fixture;

class AssignedNew
{
public function run()
{
$dateTime = new \DateTime();
$dateTime->newMethod();
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Rector\Tests\Simple\Rector\ClassMethod\DemoObjectMethodCallRenameRector\Fixture;

class SkipOtherClass
{
public function run()
{
$value = new \stdClass();
$value->oldMethod();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\Simple\Rector\ClassMethod\DemoObjectMethodCallRenameRector\Fixture;

class SkipUnknownVariable
{
public function run($dateTime)
{
$dateTime->oldMethod();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\Simple\Rector\ClassMethod\DemoObjectMethodCallRenameRector\Fixture;

class TypedParam
{
public function run(\DateTime $dateTime)
{
$dateTime->oldMethod();
}
}

?>
-----
<?php

namespace Rector\Tests\Simple\Rector\ClassMethod\DemoObjectMethodCallRenameRector\Fixture;

class TypedParam
{
public function run(\DateTime $dateTime)
{
$dateTime->newMethod();
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Simple\Rector\ClassMethod\DemoObjectMethodCallRenameRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(DemoObjectMethodCallRenameRector::class);
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Rector\DeadCode\Rector\ClassMethod;

use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocChildNode;
use PhpParser\Node;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
Expand All @@ -13,6 +12,7 @@
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Return_;
use PhpParser\NodeVisitor;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocChildNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ExtendedMethodReflection;
Expand Down Expand Up @@ -117,7 +117,7 @@ public function refactor(Node $node): ?int
private function hasRefiningDocblock(ClassMethod $classMethod): bool
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($classMethod);
return array_any($phpDocInfo->getPhpDocNode()->children, fn(PhpDocChildNode $phpDocChildNode): bool => $phpDocChildNode instanceof PhpDocTagNode);
return array_any($phpDocInfo->getPhpDocNode()->children, fn (PhpDocChildNode $phpDocChildNode): bool => $phpDocChildNode instanceof PhpDocTagNode);
}

private function matchParentMethodReflection(ClassMethod $classMethod): ?ExtendedMethodReflection
Expand Down
115 changes: 115 additions & 0 deletions rules/Simple/Rector/ClassMethod/DemoObjectMethodCallRenameRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

declare(strict_types=1);

namespace Rector\Simple\Rector\ClassMethod;

use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\Rector\AbstractRector;
use Rector\SimpleScope\SimpleScope;
use Rector\SimpleScope\SimpleScopeResolver;
use Rector\SimpleType\ObjectType;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

// POC: type resolution done via SimpleScope, no PHPStan
/**
* @see \Rector\Tests\Simple\Rector\ClassMethod\DemoObjectMethodCallRenameRector\DemoObjectMethodCallRenameRectorTest
*/
final class DemoObjectMethodCallRenameRector extends AbstractRector
{
private const string TARGET_CLASS = 'DateTime';

private const string OLD_METHOD = 'oldMethod';

private const string NEW_METHOD = 'newMethod';

public function __construct(
private readonly SimpleScopeResolver $simpleScopeResolver
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Demo of SimpleScope: rename oldMethod() to newMethod() on DateTime calls, type resolved without PHPStan',
[
new CodeSample(
<<<'CODE_SAMPLE'
$dateTime = new DateTime();
$dateTime->oldMethod();
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$dateTime = new DateTime();
$dateTime->newMethod();
CODE_SAMPLE
),
]
);
}

public function getNodeTypes(): array
{
return [ClassMethod::class];
}

/**
* @param ClassMethod $node
*/
public function refactor(Node $node): ?Node
{
$stmts = $node->stmts;
if ($stmts === null) {
return null;
}

$simpleScope = $this->simpleScopeResolver->resolve([$node]);

$hasChanged = false;
$this->traverseNodesWithCallable($stmts, function (Node $subNode) use ($simpleScope, &$hasChanged): null {
if ($this->refactorMethodCall($subNode, $simpleScope)) {
$hasChanged = true;
}

return null;
});

if (! $hasChanged) {
return null;
}

return $node;
}

private function refactorMethodCall(Node $node, SimpleScope $simpleScope): bool
{
if (! $node instanceof MethodCall) {
return false;
}

if ($node->isFirstClassCallable()) {
return false;
}

if (! $this->isName($node->name, self::OLD_METHOD)) {
return false;
}

$simpleType = $simpleScope->getType($node->var);
if (! $simpleType instanceof ObjectType) {
return false;
}

if ($simpleType->getClassName() !== self::TARGET_CLASS) {
return false;
}

$node->name = new Identifier(self::NEW_METHOD);

return true;
}
}
80 changes: 80 additions & 0 deletions src/SimpleScope/SimpleScope.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

declare(strict_types=1);

namespace Rector\SimpleScope;

use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\Int_;
use PhpParser\Node\Scalar\String_;
use Rector\SimpleType\ArrayType;
use Rector\SimpleType\BooleanType;
use Rector\SimpleType\Contract\SimpleTypeInterface;
use Rector\SimpleType\IntegerType;
use Rector\SimpleType\MixedType;
use Rector\SimpleType\NullType;
use Rector\SimpleType\ObjectType;
use Rector\SimpleType\StringType;

// PHPStan-free scope; holds variable types resolved by SimpleScopeResolver
final class SimpleScope
{
/**
* @var array<string, SimpleTypeInterface>
*/
private array $variableTypes = [];

public function setVariableType(string $name, SimpleTypeInterface $simpleType): void
{
$this->variableTypes[$name] = $simpleType;
}

public function getType(Expr $expr): SimpleTypeInterface
{
if ($expr instanceof String_) {
return new StringType();
}

if ($expr instanceof Int_) {
return new IntegerType();
}

if ($expr instanceof Array_) {
return new ArrayType();
}

if ($expr instanceof ConstFetch) {
return $this->resolveConstFetchType($expr);
}

if ($expr instanceof New_ && $expr->class instanceof Name) {
return new ObjectType($expr->class->toString());
}

if ($expr instanceof Variable && is_string($expr->name)) {
return $this->variableTypes[$expr->name] ?? new MixedType();
}

return new MixedType();
}

private function resolveConstFetchType(ConstFetch $constFetch): SimpleTypeInterface
{
$constantName = strtolower($constFetch->name->toString());

if ($constantName === 'null') {
return new NullType();
}

if ($constantName === 'true' || $constantName === 'false') {
return new BooleanType();
}

return new MixedType();
}
}
Loading
Loading