| Conditions | 14 |
| Paths | 2 |
| Total Lines | 72 |
| Code Lines | 36 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 52 | public function filterChunk($chunk, IntroductionList $introductions) |
||
| 53 | { |
||
| 54 | // Get our buckets from the stream |
||
| 55 | $interfaceHook = ''; |
||
| 56 | $keywordNeeded = true; |
||
| 57 | // Has to be done only once at the beginning of the definition |
||
| 58 | if (empty($interfaceHook) && $introductions->count() > 0) { |
||
| 59 | // Get the tokens |
||
| 60 | $tokens = token_get_all($chunk); |
||
| 61 | |||
| 62 | // Go through the tokens and check what we found |
||
| 63 | $tokensCount = count($tokens); |
||
| 64 | for ($i = 0; $i < $tokensCount; $i++) { |
||
| 65 | // We need something to hook into, right after class header seems fine |
||
| 66 | if (is_array($tokens[$i]) && $tokens[$i][0] === T_CLASS && $tokens[$i - 1][0] !== T_PAAMAYIM_NEKUDOTAYIM) { |
||
| 67 | for ($j = $i; $j < $tokensCount; $j++) { |
||
| 68 | // If we got the opening bracket we can break |
||
| 69 | if ($tokens[$j] === '{' || $tokens[$j][0] === T_CURLY_OPEN) { |
||
| 70 | break; |
||
| 71 | } |
||
| 72 | |||
| 73 | if (is_array($tokens[$j])) { |
||
| 74 | // we have to check if there already are interfaces |
||
| 75 | if ($tokens[$j][0] === T_IMPLEMENTS) { |
||
| 76 | $keywordNeeded = false; |
||
| 77 | } |
||
| 78 | |||
| 79 | $interfaceHook .= $tokens[$j][1]; |
||
| 80 | |||
|
|
|||
| 81 | } else { |
||
| 82 | $interfaceHook .= $tokens[$j]; |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | // build up the injected code and make the injection |
||
| 87 | if ($keywordNeeded) { |
||
| 88 | $implementsCode = ' implements '; |
||
| 89 | |||
| 90 | } else { |
||
| 91 | $implementsCode = ', '; |
||
| 92 | } |
||
| 93 | $useCode = ''; |
||
| 94 | $interfaces = array(); |
||
| 95 | foreach ($introductions as $introduction) { |
||
| 96 | $interfaces[] = $introduction->getInterface(); |
||
| 97 | |||
| 98 | // build up code for the trait usage |
||
| 99 | $useCode .= 'use ' . $introduction->getImplementation() . '; |
||
| 100 | '; |
||
| 101 | } |
||
| 102 | $implementsCode .= implode(', ', $interfaces); |
||
| 103 | |||
| 104 | // add the "use" code |
||
| 105 | $chunk = str_replace( |
||
| 106 | $interfaceHook . '{', |
||
| 107 | $interfaceHook . '{' . $useCode, |
||
| 108 | $chunk |
||
| 109 | ); |
||
| 110 | |||
| 111 | // add the "implements" code |
||
| 112 | $chunk = str_replace( |
||
| 113 | $interfaceHook, |
||
| 114 | $interfaceHook . $implementsCode, |
||
| 115 | $chunk |
||
| 116 | ); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | } |
||
| 121 | |||
| 122 | return $chunk; |
||
| 123 | } |
||
| 124 | |||
| 152 |