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

Proxy::checkCalledMethod()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 27
Code Lines 13

Duplication

Lines 27
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 27
loc 27
rs 8.5806
cc 4
eloc 13
nc 4
nop 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 25 and the first side effect is on line 13.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Part of ci-phpunit-test
4
 *
5
 * @author     Kenji Suzuki <https://github.com/kenjis>
6
 * @license    MIT License
7
 * @copyright  2015 Kenji Suzuki
8
 * @link       https://github.com/kenjis/ci-phpunit-test
9
 */
10
11
namespace Kenjis\MonkeyPatch\Patcher\ConstantPatcher;
12
13
class_alias('Kenjis\MonkeyPatch\Patcher\ConstantPatcher\Proxy', '__ConstProxy__');
14
15
use LogicException;
16
use ReflectionConstant;
17
use ReflectionException;
18
19
use Kenjis\MonkeyPatch\Patcher\ConstantPatcher;
20
use Kenjis\MonkeyPatch\Patcher\Backtrace;
21
use Kenjis\MonkeyPatch\MonkeyPatchManager;
22
use Kenjis\MonkeyPatch\Cache;
23
use Kenjis\MonkeyPatch\InvocationVerifier;
24
25
class Proxy
26
{
27
	private static $patches = [];
28
	private static $patches_to_apply = [];
29
	private static $expected_invocations = [];
0 ignored issues
show
Unused Code introduced by
The property $expected_invocations is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
30
	private static $invocations = [];
31
32
	/**
33
	 * Set a constant patch
34
	 * 
35
	 * @param string $constant	 constant name
36
	 * @param mixed  $value		value
37
	 * @param string $class_name   class::method to apply this patch
0 ignored issues
show
Bug introduced by
There is no parameter named $class_name. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
38
	 * 
39
	 * @throws LogicException
40
	 */
41
	public static function patch($constant, $value, $class_method = null)
42
	{
43
		self::$patches[$constant] = $value;
44
		self::$patches_to_apply[$constant] = strtolower($class_method);
45
	}
46
47
	/**
48
	 * Clear all patches and invocation data
49
	 */
50
	public static function reset()
51
	{
52
		self::$patches = [];
53
		self::$patches_to_apply = [];
54
	}
55
56
	protected static function logInvocation($constant)
57
	{
58
		if (MonkeyPatchManager::$debug)
59
		{
60
			$trace = debug_backtrace();
61
			$info = Backtrace::getInfo('ConstantPatcher', $trace);
62
63
			$file = $info['file'];
64
			$line = $info['line'];
65
			$method = isset($info['class_method']) ? $info['class_method'] : $info['function'];
66
67
			MonkeyPatchManager::log(
68
				'invoke_const: ' . $constant . ') on line ' . $line . ' in ' . $file . ' by ' . $method . '()'
69
			);
70
		}
71
	}
72
73 View Code Duplication
	protected static function checkCalledMethod($constant)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74
	{
75
		$trace = debug_backtrace();
76
		$info = Backtrace::getInfo('ConstantPatcher', $trace);
77
		
78
		$class = strtolower($info['class']);
79
		$class_method = strtolower($info['class_method']);
80
81
		// Patches the constants only in the class
82
		if (strpos(self::$patches_to_apply[$constant], '::') === false)
83
		{
84
			if (self::$patches_to_apply[$constant] !== $class)
85
			{
86
				return false;
87
			}
88
			return true;
89
		}
90
		//Patches the constants only in the class method
91
		else
92
		{
93
			if (self::$patches_to_apply[$constant] !== $class_method)
94
			{
95
				return false;
96
			}
97
			return true;
98
		}
99
	}
100
101
	/**
102
	 * Get patched constant value
103
	 * 
104
	 * @param string $constant
105
	 * @return mixed
106
	 */
107
	public static function get($constant)
108
	{
109
		self::logInvocation($constant);
110
		self::$invocations[$constant][] = null;
111
112 View Code Duplication
		if (isset(self::$patches_to_apply[$constant]))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
113
		{
114
			if (! self::checkCalledMethod($constant))
115
			{
116
				MonkeyPatchManager::log(
117
					'invoke_const: ' . $constant . ' not patched (out of scope)'
118
				);
119
				return constant($constant);
120
			}
121
		}
122
123
		if (array_key_exists($constant, self::$patches))
124
		{
125
			MonkeyPatchManager::log('invoke_const: ' . $constant . ' patched');
126
			return self::$patches[$constant];
127
		}
128
129
		MonkeyPatchManager::log(
130
			'invoke_const: ' . $constant . ' not patched (no patch)'
131
		);
132
		return constant($constant);
133
	}
134
}
135