Completed
Push — master ( d71c4b...e086ee )
by Kenji
8s
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
30
	/**
31
	 * Set a constant patch
32
	 * 
33
	 * @param string $constant      constant name
34
	 * @param mixed  $value         value
35
	 * @param string $class_method  class::method to apply this patch
36
	 * 
37
	 * @throws LogicException
38
	 */
39
	public static function patch($constant, $value, $class_method = null)
40
	{
41
		self::$patches[$constant] = $value;
42
		self::$patches_to_apply[$constant] = strtolower($class_method);
43
	}
44
45
	/**
46
	 * Clear all patches and invocation data
47
	 */
48
	public static function reset()
49
	{
50
		self::$patches = [];
51
		self::$patches_to_apply = [];
52
	}
53
54
	protected static function logInvocation($constant)
55
	{
56
		if (MonkeyPatchManager::$debug)
57
		{
58
			$trace = debug_backtrace();
59
			$info = Backtrace::getInfo('ConstantPatcher', $trace);
60
61
			$file = $info['file'];
62
			$line = $info['line'];
63
			$method = isset($info['class_method']) ? $info['class_method'] : $info['function'];
64
65
			MonkeyPatchManager::log(
66
				'invoke_const: ' . $constant . ') on line ' . $line . ' in ' . $file . ' by ' . $method . '()'
67
			);
68
		}
69
	}
70
71 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...
72
	{
73
		$trace = debug_backtrace();
74
		$info = Backtrace::getInfo('ConstantPatcher', $trace);
75
		
76
		$class = strtolower($info['class']);
77
		$class_method = strtolower($info['class_method']);
78
79
		// Patches the constants only in the class
80
		if (strpos(self::$patches_to_apply[$constant], '::') === false)
81
		{
82
			if (self::$patches_to_apply[$constant] !== $class)
83
			{
84
				return false;
85
			}
86
			return true;
87
		}
88
		//Patches the constants only in the class method
89
		else
90
		{
91
			if (self::$patches_to_apply[$constant] !== $class_method)
92
			{
93
				return false;
94
			}
95
			return true;
96
		}
97
	}
98
99
	/**
100
	 * Get patched constant value
101
	 * 
102
	 * @param string $constant
103
	 * @return mixed
104
	 */
105
	public static function get($constant)
106
	{
107
		self::logInvocation($constant);
108
109 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...
110
		{
111
			if (! self::checkCalledMethod($constant))
112
			{
113
				MonkeyPatchManager::log(
114
					'invoke_const: ' . $constant . ' not patched (out of scope)'
115
				);
116
				return constant($constant);
117
			}
118
		}
119
120
		if (array_key_exists($constant, self::$patches))
121
		{
122
			MonkeyPatchManager::log('invoke_const: ' . $constant . ' patched');
123
			return self::$patches[$constant];
124
		}
125
126
		MonkeyPatchManager::log(
127
			'invoke_const: ' . $constant . ' not patched (no patch)'
128
		);
129
		return constant($constant);
130
	}
131
}
132