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

ConstantPatcher::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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  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
class ConstantPatcher extends AbstractPatcher
21
{
22
	public static $replacement;
23
24
	public function __construct()
25
	{
26
		$this->node_visitor = new NodeVisitor();
27
	}
28
29 View Code Duplication
	protected static function generateNewSource($source)
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...
30
	{
31
		$tokens = token_get_all($source);
32
		$new_source = '';
33
		$i = -1;
34
35
		ksort(self::$replacement);
36
		reset(self::$replacement);
37
		$replacement = each(self::$replacement);
38
39
		foreach ($tokens as $token)
40
		{
41
			$i++;
42
43
			if (is_string($token))
44
			{
45
				$new_source .= $token;
46
			}
47
			elseif ($i == $replacement['key'])
48
			{
49
				$new_source .= $replacement['value'];
50
				$replacement = each(self::$replacement);
51
			}
52
			else
53
			{
54
				$new_source .= $token[1];
55
			}
56
		}
57
58
		if ($replacement !== false)
59
		{
60
			throw new LogicException('Replacement data still remain');
61
		}
62
63
		return $new_source;
64
	}
65
}
66