| Conditions | 15 |
| Paths | 3 |
| Total Lines | 81 |
| 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 |
||
| 58 | public function filter($in, $out, & $consumed, $closing) |
||
| 59 | { |
||
| 60 | // get all the introductions of a structure definition |
||
| 61 | $introductions = $this->params; |
||
| 62 | |||
| 63 | // Get our buckets from the stream |
||
| 64 | $interfaceHook = ''; |
||
| 65 | $keywordNeeded = true; |
||
| 66 | while ($bucket = stream_bucket_make_writeable($in)) { |
||
| 67 | // Has to be done only once at the beginning of the definition |
||
| 68 | if (empty($interfaceHook) && $introductions->count() > 0) { |
||
| 69 | // Get the tokens |
||
| 70 | $tokens = token_get_all($bucket->data); |
||
| 71 | |||
| 72 | // Go through the tokens and check what we found |
||
| 73 | $tokensCount = count($tokens); |
||
| 74 | for ($i = 0; $i < $tokensCount; $i++) { |
||
| 75 | // We need something to hook into, right after class header seems fine |
||
| 76 | if (is_array($tokens[$i]) && $tokens[$i][0] === T_CLASS && $tokens[$i - 1][0] !== T_PAAMAYIM_NEKUDOTAYIM) { |
||
| 77 | for ($j = $i; $j < $tokensCount; $j++) { |
||
| 78 | // If we got the opening bracket we can break |
||
| 79 | if ($tokens[$j] === '{' || $tokens[$j][0] === T_CURLY_OPEN) { |
||
| 80 | break; |
||
| 81 | } |
||
| 82 | |||
| 83 | if (is_array($tokens[$j])) { |
||
| 84 | // we have to check if there already are interfaces |
||
| 85 | if ($tokens[$j][0] === T_IMPLEMENTS) { |
||
| 86 | $keywordNeeded = false; |
||
| 87 | } |
||
| 88 | |||
| 89 | $interfaceHook .= $tokens[$j][1]; |
||
| 90 | |||
|
|
|||
| 91 | } else { |
||
| 92 | $interfaceHook .= $tokens[$j]; |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | // build up the injected code and make the injection |
||
| 97 | if ($keywordNeeded) { |
||
| 98 | $implementsCode = ' implements '; |
||
| 99 | |||
| 100 | } else { |
||
| 101 | $implementsCode = ', '; |
||
| 102 | } |
||
| 103 | $useCode = ''; |
||
| 104 | $interfaces = array(); |
||
| 105 | foreach ($introductions as $introduction) { |
||
| 106 | $interfaces[] = $introduction->getInterface(); |
||
| 107 | |||
| 108 | // build up code for the trait usage |
||
| 109 | $useCode .= 'use ' . $introduction->getImplementation() . '; |
||
| 110 | '; |
||
| 111 | } |
||
| 112 | $implementsCode .= implode(', ', $interfaces); |
||
| 113 | |||
| 114 | // add the "use" code |
||
| 115 | $bucket->data = str_replace( |
||
| 116 | $interfaceHook . '{', |
||
| 117 | $interfaceHook . '{' . $useCode, |
||
| 118 | $bucket->data |
||
| 119 | ); |
||
| 120 | |||
| 121 | // add the "implements" code |
||
| 122 | $bucket->data = str_replace( |
||
| 123 | $interfaceHook, |
||
| 124 | $interfaceHook . $implementsCode, |
||
| 125 | $bucket->data |
||
| 126 | ); |
||
| 127 | } |
||
| 128 | } |
||
| 129 | |||
| 130 | } |
||
| 131 | |||
| 132 | // Tell them how much we already processed, and stuff it back into the output |
||
| 133 | $consumed += $bucket->datalen; |
||
| 134 | stream_bucket_append($out, $bucket); |
||
| 135 | } |
||
| 136 | |||
| 137 | return PSFS_PASS_ON; |
||
| 138 | } |
||
| 139 | |||
| 167 |