Completed
Push — master ( e3ecd3...4a966e )
by Kenji
02:47
created

MethodPatcher::generateNewSource()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 42
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 6
eloc 22
c 1
b 1
f 0
nc 7
nop 1
dl 0
loc 42
rs 8.439
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 20 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;
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
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
26
	public static $replacement;
27
28
	public function __construct()
29
	{
30
		$this->node_visitor = new NodeVisitor();
31
	}
32
33
	protected static function generateNewSource($source)
34
	{
35
		$tokens = token_get_all($source);
36
		$new_source = '';
37
		$i = -1;
38
39
		ksort(self::$replacement);
40
		reset(self::$replacement);
41
		$replacement = each(self::$replacement);
42
43
		$start_method = false;
44
45
		foreach ($tokens as $token)
46
		{
47
			$i++;
48
49
			if ($i == $replacement['key'])
50
			{
51
				$start_method = true;
52
			}
53
54
			if (is_string($token))
55
			{
56
				if ($start_method && $token === '{')
57
				{
58
					$new_source .= '{ ' . self::CODE;
59
					$start_method = false;
60
					$replacement = each(self::$replacement);
61
				}
62
				else
63
				{
64
					$new_source .= $token;
65
				}
66
			}
67
			else
68
			{
69
				$new_source .= $token[1];
70
			}
71
		}
72
73
		return $new_source;
74
	}
75
}
76