|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kambo\Testing\ClassOpener\ClassManipulation\Transformer; |
|
4
|
|
|
|
|
5
|
|
|
use PhpParser\NodeTraverserInterface; |
|
6
|
|
|
|
|
7
|
|
|
use Kambo\Testing\ClassOpener\ClassManipulation\Transformer; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Transform class nodes by applying provided visitors. |
|
11
|
|
|
* |
|
12
|
|
|
* @author Bohuslav Simek <[email protected]> |
|
13
|
|
|
* @license MIT |
|
14
|
|
|
*/ |
|
15
|
|
|
class Traverser implements Transformer |
|
16
|
|
|
{ |
|
17
|
|
|
private $traverser; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Constructor |
|
21
|
|
|
* |
|
22
|
|
|
* @param NodeTraverserInterface $traverser Node traverser |
|
23
|
|
|
*/ |
|
24
|
4 |
|
public function __construct(NodeTraverserInterface $traverser) |
|
25
|
|
|
{ |
|
26
|
4 |
|
$this->traverser = $traverser; |
|
27
|
4 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Transform class nodes by applying provided class visitors. |
|
31
|
|
|
* |
|
32
|
|
|
* @param string $className A fully qualified class name |
|
33
|
|
|
* @param Node[] $nodes Array of AST nodes |
|
34
|
|
|
* @param Visitor[] $visitors List of the node visitors |
|
35
|
|
|
*/ |
|
36
|
3 |
|
public function transform(string $className, array $nodes, array $visitors) : array |
|
37
|
|
|
{ |
|
38
|
3 |
|
$this->traverser->removeVisitors(); |
|
|
|
|
|
|
39
|
3 |
|
foreach ($visitors as $nodeVisitor) { |
|
40
|
3 |
|
$nodeVisitor->setClassName($this->getClassName($className)); |
|
41
|
3 |
|
$this->traverser->addVisitor($nodeVisitor); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
3 |
|
$newClassAst = $this->traverser->traverse($nodes); |
|
45
|
|
|
|
|
46
|
3 |
|
return $newClassAst; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Get non qualified name of the class from the fully qualified class name |
|
51
|
|
|
* eg.: from 'foo\bar\qaz' extract 'qaz' |
|
52
|
|
|
* |
|
53
|
|
|
* @param string $className A fully qualified class name |
|
54
|
|
|
* |
|
55
|
|
|
* @return string non qualified name of the class |
|
56
|
|
|
*/ |
|
57
|
3 |
|
private function getClassName(string $className) : string |
|
58
|
|
|
{ |
|
59
|
3 |
|
$parsed = explode('\\', $className); |
|
60
|
|
|
|
|
61
|
3 |
|
if (!is_array($parsed)) { |
|
62
|
|
|
return $className; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
3 |
|
return end($parsed); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.