Completed
Push — master ( 6d813b...5a9478 )
by Kenji
11s
created

MethodPatcher::generateNewSource()   B

Complexity

Conditions 9
Paths 26

Size

Total Lines 59

Duplication

Lines 59
Ratio 100 %

Importance

Changes 0
Metric Value
cc 9
nc 26
nop 1
dl 59
loc 59
rs 7.3389
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 View Code Duplication
class MethodPatcher 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
	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);
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...
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