1
|
|
|
<?php |
|
|
|
|
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; |
12
|
|
|
|
13
|
|
|
require __DIR__ . '/MethodPatcher/NodeVisitor.php'; |
14
|
|
|
require __DIR__ . '/MethodPatcher/PatchManager.php'; |
15
|
|
|
|
16
|
|
|
use LogicException; |
17
|
|
|
|
18
|
|
|
use Kenjis\MonkeyPatch\Patcher\MethodPatcher\NodeVisitor; |
19
|
|
|
|
20
|
|
View Code Duplication |
class MethodPatcher extends AbstractPatcher |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
const CODE = <<<'EOL' |
23
|
|
|
if (($__ret__ = \__PatchManager__::getReturn(__CLASS__, __FUNCTION__, func_get_args())) !== __GO_TO_ORIG__) return $__ret__; |
24
|
|
|
EOL; |
25
|
|
|
const CODENORET = <<<'EOL' |
26
|
|
|
if (($__ret__ = \__PatchManager__::getReturn(__CLASS__, __FUNCTION__, func_get_args())) !== __GO_TO_ORIG__) return; |
27
|
|
|
EOL; |
28
|
|
|
|
29
|
|
|
public static $replacement; |
30
|
|
|
|
31
|
|
|
public function __construct() |
32
|
|
|
{ |
33
|
|
|
$this->node_visitor = new NodeVisitor(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
protected static function generateNewSource($source) |
37
|
|
|
{ |
38
|
|
|
$tokens = token_get_all($source); |
39
|
|
|
$new_source = ''; |
40
|
|
|
$i = -1; |
41
|
|
|
|
42
|
|
|
ksort(self::$replacement); |
43
|
|
|
reset(self::$replacement); |
44
|
|
|
$replacement['key'] = key(self::$replacement); |
|
|
|
|
45
|
|
|
$replacement['value'] = current(self::$replacement); |
46
|
|
|
next(self::$replacement); |
47
|
|
|
if ($replacement['key'] === null) |
48
|
|
|
{ |
49
|
|
|
$replacement = false; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$start_method = false; |
53
|
|
|
|
54
|
|
|
foreach ($tokens as $key => $token) |
55
|
|
|
{ |
56
|
|
|
$i++; |
57
|
|
|
|
58
|
|
|
if ($i == $replacement['key']) |
59
|
|
|
{ |
60
|
|
|
$start_method = true; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if (is_string($token)) |
64
|
|
|
{ |
65
|
|
|
if ($start_method && $token === '{') |
66
|
|
|
{ |
67
|
|
|
if(self::isVoidFunction($tokens, $key)){ |
68
|
|
|
$new_source .= '{ ' . self::CODENORET; |
69
|
|
|
} |
70
|
|
|
else{ |
71
|
|
|
$new_source .= '{ ' . self::CODE; |
72
|
|
|
} |
73
|
|
|
$start_method = false; |
74
|
|
|
$replacement['key'] = key(self::$replacement); |
75
|
|
|
$replacement['value'] = current(self::$replacement); |
76
|
|
|
next(self::$replacement); |
77
|
|
|
if ($replacement['key'] === null) |
78
|
|
|
{ |
79
|
|
|
$replacement = false; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
else |
83
|
|
|
{ |
84
|
|
|
$new_source .= $token; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
else |
88
|
|
|
{ |
89
|
|
|
$new_source .= $token[1]; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $new_source; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Checks if a function has a void return type |
98
|
|
|
* |
99
|
|
|
* @param $tokens |
100
|
|
|
* @param $key |
101
|
|
|
* @return bool |
102
|
|
|
*/ |
103
|
|
|
protected static function isVoidFunction($tokens, $key){ |
104
|
|
|
if($key - 1 <= 0){ |
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
$token = $tokens[$key - 1]; |
108
|
|
|
if(is_array($token)){ |
109
|
|
|
$token = $token[1]; |
110
|
|
|
} |
111
|
|
|
//Loop backwards though the start of the function block till you either find "void" or the end of the |
112
|
|
|
//parameters declaration. |
113
|
|
|
while($token !== ")"){ |
114
|
|
|
if(strpos($token, "void") !== false){ |
115
|
|
|
return true; |
116
|
|
|
} |
117
|
|
|
$token = $tokens[$key - 1]; |
118
|
|
|
if(is_array($token)){ |
119
|
|
|
$token = $token[1]; |
120
|
|
|
} |
121
|
|
|
$key--; |
122
|
|
|
} |
123
|
|
|
return false; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
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.