Traverser::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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();
0 ignored issues
show
Bug introduced by
The method removeVisitors() does not exist on PhpParser\NodeTraverserInterface. Did you maybe mean removeVisitor()?

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.

Loading history...
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