Completed
Push — master ( c0bce4...52e213 )
by Kenji
10s
created

ConstantPatcher   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 81
Duplicated Lines 100 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
dl 81
loc 81
rs 10
c 0
b 0
f 0
wmc 10
lcom 2
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isBlacklisted() 9 9 2
A __construct() 4 4 1
C generateNewSource() 48 48 7

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 View Code Duplication
class ConstantPatcher extends AbstractPatcher
0 ignored issues
show
Duplication introduced by
This class 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...
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);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$replacement was never initialized. Although not strictly required by PHP, it is generally a good practice to add $replacement = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
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