Repository navigation
Expand file tree
/
Copy pathIdentifierManipulator.php
More file actions
60 lines (51 loc) · 1.66 KB
/
Copy pathIdentifierManipulator.php
File metadata and controls
60 lines (51 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
declare(strict_types=1);
namespace Rector\PHPUnit\NodeAnalyzer;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\NodeNameResolver\NodeNameResolver;
/**
* This class renames node identifier, e.g. ClassMethod rename:
*
* -public function someMethod()
* +public function newMethod()
*/
final readonly class IdentifierManipulator
{
public function __construct(
private NodeNameResolver $nodeNameResolver
) {
}
/**
* @param array<string, string> $renameMethodMap
*/
public function renameNodeWithMap(
ClassConstFetch|MethodCall|PropertyFetch|StaticCall|ClassMethod $node,
array $renameMethodMap
): bool {
$oldNodeMethodName = $this->resolveOldMethodName($node);
if (! is_string($oldNodeMethodName)) {
return false;
}
// method names are case-insensitive in PHP, match the map the same way
foreach ($renameMethodMap as $oldName => $newName) {
if (strcasecmp($oldName, $oldNodeMethodName) === 0) {
$node->name = new Identifier($newName);
return true;
}
}
return false;
}
private function resolveOldMethodName(
ClassConstFetch|MethodCall|PropertyFetch|StaticCall|ClassMethod $node
): ?string {
if ($node instanceof StaticCall || $node instanceof MethodCall) {
return $this->nodeNameResolver->getName($node->name);
}
return $this->nodeNameResolver->getName($node);
}
}