Completed
Push — master ( 616f8e...1b9ae8 )
by Kenji
02:32
created

NodeVisitor::leaveNode()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 41
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 41
rs 6.7272
cc 7
eloc 20
nc 7
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
		if (! ConstantPatcher::isBlacklisted((string) $node->name))
61
		{
62
			$replacement = new FullyQualified(array());
63
			$replacement->set(
64
				'\__ConstProxy__::get(\'' . (string) $node->name . '\')'
65
			);
66
67
			$pos = $node->getAttribute('startTokenPos');
68
			ConstantPatcher::$replacement[$pos] = 
69
				'\__ConstProxy__::get(\'' . (string) $node->name .'\')';
70
71
			$node->name = $replacement;
72
		}
73
	}
74
75
	/**
76
	 * The following logic is from:
77
	 * <https://github.com/badoo/soft-mocks/blob/06fe26a2c9ab4cae17b88648439952ab0586438f/src/QA/SoftMocks.php#L1572>
78
	 * Thank you.
79
	 * 
80
	 * The MIT License (MIT)
81
	 * Copyright (c) 2016 Badoo Development
82
	 */
83
	// Cannot rewrite constants that are used as default values in function arguments
84
	public function beforeParam()
85
	{
86
		$this->disable_const_rewrite_level++;
87
	}
88
89
	public function rewriteParam()
90
	{
91
		$this->disable_const_rewrite_level--;
92
	}
93
94
	// Cannot rewrite constants that are used as default values in constant declarations
95
	public function beforeConst()
96
	{
97
		$this->disable_const_rewrite_level++;
98
	}
99
100
	public function rewriteConst()
101
	{
102
		$this->disable_const_rewrite_level--;
103
	}
104
105
	// Cannot rewrite constants that are used as default values in property declarations
106
	public function beforeStmt_PropertyProperty()
107
	{
108
		$this->disable_const_rewrite_level++;
109
	}
110
111
	public function rewriteStmt_PropertyProperty()
112
	{
113
		$this->disable_const_rewrite_level--;
114
	}
115
116
	// Cannot rewrite constants that are used as default values in static variable declarations
117
	public function beforeStmt_StaticVar()
118
	{
119
		$this->disable_const_rewrite_level++;
120
	}
121
122
	public function rewriteStmt_StaticVar()
123
	{
124
		$this->disable_const_rewrite_level--;
125
	}
126
}
127