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; |
12
|
|
|
|
13
|
|
|
require __DIR__ . '/ConstantPatcher/NodeVisitor.php'; |
14
|
|
|
require __DIR__ . '/ConstantPatcher/Proxy.php'; |
15
|
|
|
|
16
|
|
|
use LogicException; |
17
|
|
|
|
18
|
|
|
use Kenjis\MonkeyPatch\Patcher\ConstantPatcher\NodeVisitor; |
19
|
|
|
|
20
|
|
View Code Duplication |
class ConstantPatcher extends AbstractPatcher |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var special constant names which we don't patch |
24
|
|
|
*/ |
25
|
|
|
private static $blacklist = [ |
26
|
|
|
'true', |
27
|
|
|
'false', |
28
|
|
|
'null', |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
public static $replacement; |
32
|
|
|
|
33
|
|
|
public function __construct() |
34
|
|
|
{ |
35
|
|
|
$this->node_visitor = new NodeVisitor(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $name constant name |
40
|
|
|
* @return boolean |
41
|
|
|
*/ |
42
|
|
|
public static function isBlacklisted($name) |
43
|
|
|
{ |
44
|
|
|
if (in_array(strtolower($name), self::$blacklist)) |
45
|
|
|
{ |
46
|
|
|
return true; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return false; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
protected static function generateNewSource($source) |
53
|
|
|
{ |
54
|
|
|
$tokens = token_get_all($source); |
55
|
|
|
$new_source = ''; |
56
|
|
|
$i = -1; |
57
|
|
|
|
58
|
|
|
ksort(self::$replacement); |
59
|
|
|
reset(self::$replacement); |
60
|
|
|
$replacement['key'] = key(self::$replacement); |
|
|
|
|
61
|
|
|
$replacement['value'] = current(self::$replacement); |
62
|
|
|
next(self::$replacement); |
63
|
|
|
if ($replacement['key'] === null) |
64
|
|
|
{ |
65
|
|
|
$replacement = false; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
foreach ($tokens as $token) |
69
|
|
|
{ |
70
|
|
|
$i++; |
71
|
|
|
|
72
|
|
|
if (is_string($token)) |
73
|
|
|
{ |
74
|
|
|
$new_source .= $token; |
75
|
|
|
} |
76
|
|
|
elseif ($i == $replacement['key']) |
77
|
|
|
{ |
78
|
|
|
$new_source .= $replacement['value']; |
79
|
|
|
$replacement['key'] = key(self::$replacement); |
80
|
|
|
$replacement['value'] = current(self::$replacement); |
81
|
|
|
next(self::$replacement); |
82
|
|
|
if ($replacement['key'] === null) |
83
|
|
|
{ |
84
|
|
|
$replacement = false; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
else |
88
|
|
|
{ |
89
|
|
|
$new_source .= $token[1]; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if ($replacement !== false) |
94
|
|
|
{ |
95
|
|
|
throw new LogicException('Replacement data still remain'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $new_source; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
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.