RemoveFinal::leaveNode()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Kambo\Testing\ClassOpener\ClassManipulation\Node\Visitor;
4
5
use Kambo\Testing\ClassOpener\ClassManipulation\Node\Visitor;
6
7
use PhpParser\Node;
8
use PhpParser\NodeVisitorAbstract;
9
use PhpParser\Node\Stmt\Class_;
10
use PhpParser\Node\Stmt\ClassMethod;
11
12
/**
13
 * Remove 'final' modifier from the class and its methods
14
 *
15
 * @author  Bohuslav Simek <[email protected]>
16
 * @license MIT
17
 */
18
class RemoveFinal extends NodeVisitorAbstract implements Visitor
19
{
20
    protected $parsingTargetClass = false;
21
    protected $className;
22
23
    /**
24
     * Set the name of the class into the visitor.
25
     *
26
     * @param string $className name of the class
27
     *
28
     * @return void
29
     */
30 3
    public function setClassName(string $className)
31
    {
32 3
        $this->className = $className;
33 3
    }
34
35
    /**
36
     * Called when entering a node.
37
     *
38
     * @param Node $node Node
39
     *
40
     * @return void
41
     */
42 3
    public function enterNode(Node $node)
43
    {
44 3
        if (($node instanceof ClassMethod) && $node->isPublic() && $this->parsingTargetClass) {
45 3
            $node->type &= ~Class_::MODIFIER_FINAL;
46 3
        } elseif (($node instanceof Class_) && ($node->name == $this->className)) {
47 3
            $this->parsingTargetClass = true;
48
        }
49 3
    }
50
51
    /**
52
     * Called when leaving a node.
53
     *
54
     * @param Node $node Node
55
     *
56
     * @return void
57
     */
58 3
    public function leaveNode(Node $node)
59
    {
60 3
        if ($node instanceof Class_) {
61
            // If the node is a class node
62 3
            if ($node->name == $this->className) {
63 3
                $node->type &= ~Class_::MODIFIER_FINAL;
64
            }
65
66 3
            $this->parsingTargetClass = false;
67
        }
68 3
    }
69
}
70