Completed
Pull Request — master (#106)
by Kenji
02:46
created

NodeVisitor::enterNode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
/**
3
 * Part of ci-phpunit-test
4
 *
5
 * @author     Kenji Suzuki <https://github.com/kenjis>
6
 * @license    MIT License
7
 * @copyright  2016 Kenji Suzuki
8
 * @link       https://github.com/kenjis/ci-phpunit-test
9
 */
10
11
namespace Kenjis\MonkeyPatch\Patcher\ConstantPatcher;
12
13
use PhpParser\Node;
14
use PhpParser\Node\Expr\ConstFetch;
15
use PhpParser\Node\Name;
16
use PhpParser\Node\Name\FullyQualified;
17
use PhpParser\NodeVisitorAbstract;
18
19
use Kenjis\MonkeyPatch\Patcher\ConstantPatcher;
20
21
class NodeVisitor extends NodeVisitorAbstract
22
{
23
	private $disable_const_rewrite_level = 0;
24
25
	public function enterNode(Node $node)
26
	{
27
		$callback = [$this, 'before' . ucfirst($node->getType())];
28
		if (is_callable($callback)) {
29
			call_user_func_array($callback, [$node]);
30
		}
31
	}
32
33
	public function leaveNode(Node $node)
34
	{
35
		if (! ($node instanceof ConstFetch))
0 ignored issues
show
Bug introduced by
The class PhpParser\Node\Expr\ConstFetch does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
36
		{
37
			$callback = [$this, 'rewrite' . ucfirst($node->getType())];
38
			if (is_callable($callback)) {
39
				call_user_func_array($callback, [$node]);
40
			}
41
42
			return;
43
		}
44
45
		if ($this->disable_const_rewrite_level > 0)
46
		{
47
			return;
48
		}
49
50
		if (! ($node->name instanceof Name))
0 ignored issues
show
Bug introduced by
The class PhpParser\Node\Name does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
51
		{
52
			return;
53
		}
54
55
		if (! $node->name->isUnqualified())
56
		{
57
			return;
58
		}
59
60
		$replacement = new FullyQualified(array());
61
		$replacement->set(
62
			'\__ConstProxy__::get(\'' . (string) $node->name . '\')'
63
		);
64
65
		$pos = $node->getAttribute('startTokenPos');
66
		ConstantPatcher::$replacement[$pos] = 
67
			'\__ConstProxy__::get(\'' . (string) $node->name .'\')';
68
69
		$node->name = $replacement;
70
	}
71
72
	/**
73
	 * The following logic is from:
74
	 * <https://github.com/badoo/soft-mocks/blob/06fe26a2c9ab4cae17b88648439952ab0586438f/src/QA/SoftMocks.php#L1572>
75
	 * Thank you.
76
	 * 
77
	 * The MIT License (MIT)
78
	 * Copyright (c) 2016 Badoo Development
79
	 */
80
	// Cannot rewrite constants that are used as default values in function arguments
81
	public function beforeParam()
82
	{
83
		$this->disable_const_rewrite_level++;
84
	}
85
86
	public function rewriteParam()
87
	{
88
		$this->disable_const_rewrite_level--;
89
	}
90
91
	// Cannot rewrite constants that are used as default values in constant declarations
92
	public function beforeConst()
93
	{
94
		$this->disable_const_rewrite_level++;
95
	}
96
97
	public function rewriteConst()
98
	{
99
		$this->disable_const_rewrite_level--;
100
	}
101
102
	// Cannot rewrite constants that are used as default values in property declarations
103
	public function beforeStmt_PropertyProperty()
104
	{
105
		$this->disable_const_rewrite_level++;
106
	}
107
108
	public function rewriteStmt_PropertyProperty()
109
	{
110
		$this->disable_const_rewrite_level--;
111
	}
112
113
	// Cannot rewrite constants that are used as default values in static variable declarations
114
	public function beforeStmt_StaticVar()
115
	{
116
		$this->disable_const_rewrite_level++;
117
	}
118
119
	public function rewriteStmt_StaticVar()
120
	{
121
		$this->disable_const_rewrite_level--;
122
	}
123
}
124