Complex classes like PhpFileParser 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 PhpFileParser, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
6 | class PhpFileParser implements Iterator, ArrayAccess { |
||
|
|||
7 | |||
8 | /** |
||
9 | * @var string original file name |
||
10 | */ |
||
11 | private $fileName = null; |
||
12 | |||
13 | /** |
||
14 | * @var array |
||
15 | */ |
||
16 | private $tokens = null; |
||
17 | |||
18 | /** |
||
19 | * @var string |
||
20 | */ |
||
21 | private $sha1hash = null; |
||
22 | |||
23 | /** |
||
24 | * @param $fileName |
||
25 | * @throws CodeReview_IOException |
||
26 | * @throws Exception |
||
27 | */ |
||
28 | 12 | public function __construct($fileName) { |
|
48 | |||
49 | /** |
||
50 | * Return fileds to serialize. |
||
51 | * |
||
52 | * @return array |
||
53 | */ |
||
54 | 6 | public function __sleep() { |
|
57 | |||
58 | /** |
||
59 | * Verify class contents against original file to detect changes. |
||
60 | */ |
||
61 | 6 | public function __wakeup() { |
|
64 | |||
65 | /** |
||
66 | * Uses SHA1 hash to determine if file contents has changed since analysis. |
||
67 | * |
||
68 | * @return bool |
||
69 | * @throws CodeReview_IOException |
||
70 | * @throws LogicException |
||
71 | */ |
||
72 | 6 | protected function validateFileContents() { |
|
85 | |||
86 | /** |
||
87 | * Checks if file exists and is readable. |
||
88 | * |
||
89 | * @param $fileName |
||
90 | * @return bool |
||
91 | * @throws CodeReview_IOException |
||
92 | */ |
||
93 | 12 | protected function validateFilePath($fileName) { |
|
105 | |||
106 | /** |
||
107 | * Compute parents of the tokens to easily determine containing methods and classes. |
||
108 | * |
||
109 | * @param bool $debug |
||
110 | */ |
||
111 | 10 | private function computeNestingParentTokens($debug = false) { |
|
156 | |||
157 | /** |
||
158 | * @param array $tokens |
||
159 | * @param int $offset |
||
160 | * @return bool |
||
161 | */ |
||
162 | 10 | public function isEqualToAnyToken($tokens, $offset = null) { |
|
170 | |||
171 | /** |
||
172 | * @param $token string|int individual token identifier or predefined T_* constant value for complex tokens |
||
173 | * @param int $offset optional offset when checking other than current |
||
174 | * @return bool |
||
175 | */ |
||
176 | 10 | public function isEqualToToken($token, $offset = null) { |
|
190 | |||
191 | /** |
||
192 | * @param int $offset optional offset when checking other than current |
||
193 | * @return mixed |
||
194 | */ |
||
195 | public function getDefiningFunctionName($offset = null) { |
||
213 | |||
214 | /** |
||
215 | * @param int $offset optional offset when checking other than current |
||
216 | * @return mixed |
||
217 | */ |
||
218 | public function getDefiningClassName($offset = null) { |
||
231 | |||
232 | /** |
||
233 | * @param string $fileName |
||
234 | * @return bool|string |
||
235 | * @throws CodeReview_IOException |
||
236 | */ |
||
237 | 1 | public function exportPhp($fileName = null) { |
|
258 | |||
259 | /** |
||
260 | * (PHP 5 >= 5.0.0)<br/> |
||
261 | * Return the current element |
||
262 | * |
||
263 | * @link http://php.net/manual/en/iterator.current.php |
||
264 | * @return mixed Can return any type. |
||
265 | */ |
||
266 | 3 | public function current() { |
|
269 | |||
270 | /** |
||
271 | * (PHP 5 >= 5.0.0)<br/> |
||
272 | * Move forward to next element |
||
273 | * @link http://php.net/manual/en/iterator.next.php |
||
274 | * @return void Any returned value is ignored. |
||
275 | */ |
||
276 | 3 | public function next() { |
|
279 | |||
280 | /** |
||
281 | * (PHP 5 >= 5.0.0)<br/> |
||
282 | * Return the key of the current element |
||
283 | * @link http://php.net/manual/en/iterator.key.php |
||
284 | * @return mixed scalar on success, or null on failure. |
||
285 | */ |
||
286 | 3 | public function key() { |
|
289 | |||
290 | /** |
||
291 | * (PHP 5 >= 5.0.0)<br/> |
||
292 | * Checks if current position is valid |
||
293 | * @link http://php.net/manual/en/iterator.valid.php |
||
294 | * @return boolean The return value will be casted to boolean and then evaluated. |
||
295 | * Returns true on success or false on failure. |
||
296 | */ |
||
297 | 3 | public function valid() { |
|
302 | |||
303 | /** |
||
304 | * (PHP 5 >= 5.0.0)<br/> |
||
305 | * Rewind the Iterator to the first element |
||
306 | * @link http://php.net/manual/en/iterator.rewind.php |
||
307 | * @return void Any returned value is ignored. |
||
308 | */ |
||
309 | 3 | public function rewind() { |
|
312 | |||
313 | /** |
||
314 | * (PHP 5 >= 5.0.0)<br/> |
||
315 | * Whether a offset exists |
||
316 | * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
||
317 | * @param mixed $offset <p> |
||
318 | * An offset to check for. |
||
319 | * </p> |
||
320 | * @return boolean true on success or false on failure. |
||
321 | * </p> |
||
322 | * <p> |
||
323 | * The return value will be casted to boolean if non-boolean was returned. |
||
324 | */ |
||
325 | 10 | public function offsetExists($offset) { |
|
328 | |||
329 | /** |
||
330 | * (PHP 5 >= 5.0.0)<br/> |
||
331 | * Offset to retrieve |
||
332 | * @link http://php.net/manual/en/arrayaccess.offsetget.php |
||
333 | * @param mixed $offset <p> |
||
334 | * The offset to retrieve. |
||
335 | * </p> |
||
336 | * @return mixed Can return all value types. |
||
337 | */ |
||
338 | 10 | public function offsetGet($offset) { |
|
341 | |||
342 | /** |
||
343 | * (PHP 5 >= 5.0.0)<br/> |
||
344 | * Offset to set |
||
345 | * @link http://php.net/manual/en/arrayaccess.offsetset.php |
||
346 | * @param mixed $offset <p> |
||
347 | * The offset to assign the value to. |
||
348 | * </p> |
||
349 | * @param mixed $value <p> |
||
350 | * The value to set. |
||
351 | * </p> |
||
352 | * @return void |
||
353 | */ |
||
354 | public function offsetSet($offset, $value) { |
||
357 | |||
358 | /** |
||
359 | * (PHP 5 >= 5.0.0)<br/> |
||
360 | * Offset to unset |
||
361 | * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
||
362 | * @param mixed $offset <p> |
||
363 | * The offset to unset. |
||
364 | * </p> |
||
365 | * @return void |
||
366 | */ |
||
367 | public function offsetUnset($offset) { |
||
370 | } |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.