Traverser   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 93.33%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 53
ccs 14
cts 15
cp 0.9333
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A transform() 0 12 2
A getClassName() 0 10 2
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