Complex classes like MethodExtractor often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MethodExtractor, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class MethodExtractor implements ExtractorInterface { |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var Searcher |
||
| 31 | */ |
||
| 32 | private $searcher; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Constructor |
||
| 36 | * |
||
| 37 | * @param Searcher $searcher |
||
| 38 | */ |
||
| 39 | public function __construct(Searcher $searcher) |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Extract method from position |
||
| 46 | * |
||
| 47 | * @param int $n |
||
| 48 | * @param TokenCollection$tokens |
||
| 49 | * @return ReflectedMethod |
||
| 50 | * @param ReflectedClass |
||
| 51 | * @throws \Exception |
||
| 52 | */ |
||
| 53 | public function extract(&$n, TokenCollection $tokens, $currentClass = null) |
||
| 54 | { |
||
| 55 | $start = $n; |
||
| 56 | |||
| 57 | $declaration = $this->searcher->getUnder(array(')'), $n, $tokens); |
||
| 58 | if(!preg_match('!function\s+(.*)\(\s*(.*)!is', $declaration, $matches)) { |
||
| 59 | throw new \Exception(sprintf("Closure detected instead of method\nDetails:\n%s", $declaration)); |
||
| 60 | } |
||
| 61 | list(, $name, $args) = $matches; |
||
| 62 | $method = new ReflectedMethod($name); |
||
| 63 | |||
| 64 | // visibility |
||
| 65 | $this->extractVisibility($method, $p = $start, $tokens); // please keep "p = start" |
||
| 66 | |||
| 67 | // state |
||
| 68 | $this->extractState($method, $p = $start, $tokens); // please keep "p = start" |
||
| 69 | |||
| 70 | $arguments = preg_split('!\s*,\s*!m', $args); |
||
| 71 | foreach($arguments as $argDecl) { |
||
| 72 | |||
| 73 | if(0 == strlen($argDecl)) { |
||
| 74 | continue; |
||
| 75 | } |
||
| 76 | |||
| 77 | $elems = preg_split('!([\s=]+)!', $argDecl); |
||
| 78 | $isRequired = 2 == sizeof($elems, COUNT_NORMAL); |
||
| 79 | |||
| 80 | if(sizeof($elems, COUNT_NORMAL) == 1) { |
||
| 81 | list($name, $type) = array_pad($elems, 2, null); |
||
| 82 | } else { |
||
| 83 | if('$' == $elems[0][0]) { |
||
| 84 | $name = $elems[0]; |
||
| 85 | $type = null; |
||
| 86 | $isRequired = false; |
||
| 87 | } else { |
||
| 88 | list($type, $name) = array_pad($elems, 2, null); |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | $argument = new ReflectedArgument($name, $type, $isRequired); |
||
| 93 | $method->pushArgument($argument); |
||
| 94 | } |
||
| 95 | |||
| 96 | |||
| 97 | |||
| 98 | // does method has body ? (example: interface ; abstract classes) |
||
| 99 | $p = $n + 1; |
||
| 100 | $underComma = trim($this->searcher->getUnder(array(';'), $p, $tokens)); |
||
| 101 | if(strlen($underComma) > 0) { |
||
| 102 | // |
||
| 103 | // Body |
||
| 104 | $this->extractContent($method, $n, $tokens); |
||
| 105 | |||
| 106 | // Calls |
||
| 107 | $this->extractCalls($method, $n, $tokens); |
||
| 108 | |||
| 109 | // Tokens |
||
| 110 | $end = $this->searcher->getPositionOfClosingBrace($n, $tokens); |
||
| 111 | if($end > 0) { |
||
| 112 | $method->setTokens($tokens->extract($n, $end)); |
||
| 113 | } |
||
| 114 | } else { |
||
| 115 | $method->setTokens($tokens->extract(0, $n)); |
||
| 116 | } |
||
| 117 | |||
| 118 | // |
||
| 119 | // Dependencies |
||
| 120 | $this->extractDependencies($method, 0, $method->getTokens(), $currentClass); |
||
|
|
|||
| 121 | |||
| 122 | // returns |
||
| 123 | $p = $start; |
||
| 124 | $this->extractReturns($method, $p, $tokens); |
||
| 125 | |||
| 126 | // usage |
||
| 127 | $this->extractUsage($method); |
||
| 128 | |||
| 129 | return $method; |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Extracts visibility |
||
| 134 | * |
||
| 135 | * @param ReflectedMethod $method |
||
| 136 | * @param $n |
||
| 137 | * @param TokenCollection $tokens |
||
| 138 | * @return $this |
||
| 139 | */ |
||
| 140 | public function extractVisibility(ReflectedMethod $method, $n, TokenCollection $tokens) { |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Extracts state |
||
| 159 | * |
||
| 160 | * @param ReflectedMethod $method |
||
| 161 | * @param $n |
||
| 162 | * @param TokenCollection $tokens |
||
| 163 | * @return $this |
||
| 164 | */ |
||
| 165 | public function extractState(ReflectedMethod $method, $n, TokenCollection $tokens) { |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Extracts content of method |
||
| 174 | * |
||
| 175 | * @param ReflectedMethod $method |
||
| 176 | * @param integer $n |
||
| 177 | * @param TokenCollection $tokens |
||
| 178 | * @return $this |
||
| 179 | */ |
||
| 180 | private function extractContent(ReflectedMethod $method, $n, TokenCollection $tokens) { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Extracts content of method |
||
| 191 | * |
||
| 192 | * @param ReflectedMethod $method |
||
| 193 | * @param integer $n |
||
| 194 | * @param TokenCollection $tokens |
||
| 195 | * @param ReflectedClass $currentClass |
||
| 196 | * @return $this |
||
| 197 | */ |
||
| 198 | private function extractDependencies(ReflectedMethod $method, $n, TokenCollection $tokens, ReflectedClass $currentClass = null) { |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Extracts calls of method |
||
| 234 | * |
||
| 235 | * @param ReflectedMethod $method |
||
| 236 | * @param integer $n |
||
| 237 | * @param TokenCollection $tokens |
||
| 238 | * @return $this |
||
| 239 | */ |
||
| 240 | private function extractCalls(ReflectedMethod $method, $n, TokenCollection $tokens) { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Extract the list of returned values |
||
| 263 | * |
||
| 264 | * @param ReflectedMethod $method |
||
| 265 | * @return $this |
||
| 266 | */ |
||
| 267 | private function extractReturns(ReflectedMethod $method, $n, TokenCollection $tokens) { |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Extracts usage of method |
||
| 298 | * |
||
| 299 | * @param ReflectedMethod $method |
||
| 300 | * @return $this |
||
| 301 | */ |
||
| 302 | private function extractUsage(ReflectedMethod $method) { |
||
| 328 | } |
||
| 329 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: