Complex classes like Context 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 Context, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class Context implements TokenVisitorInterface { |
||
14 | |||
15 | // context constants |
||
16 | const CONTEXT_FILE = 'file'; |
||
17 | const CONTEXT_STRUCT = 'struct'; |
||
18 | const CONTEXT_ROUTINE = 'routine'; |
||
19 | const CONTEXT_BLOCK = 'block'; |
||
20 | const CONTEXT_GROUP = 'group'; |
||
21 | |||
22 | // event constants |
||
23 | const EVENT_BLOCK_ENTER = 'context.block_enter'; |
||
24 | const EVENT_BLOCK_LEAVE = 'context.block_leave'; |
||
25 | const EVENT_STRUCT_ENTER = 'context.struct_enter'; |
||
26 | const EVENT_STRUCT_LEAVE = 'context.struct_leave'; |
||
27 | const EVENT_ROUTINE_ENTER = 'context.routine_enter'; |
||
28 | const EVENT_ROUTINE_LEAVE = 'context.routine_leave'; |
||
29 | const EVENT_GROUP_ENTER = 'context.group_enter'; |
||
30 | const EVENT_GROUP_LEAVE = 'context.group_leave'; |
||
31 | |||
32 | // current contexts |
||
33 | private $block; |
||
34 | private $group; |
||
35 | private $inStructBody = false; |
||
36 | private $inRoutineBody = false; |
||
37 | |||
38 | // stacks |
||
39 | private $blockStack; |
||
40 | private $groupStack; |
||
41 | private $contextStack; |
||
42 | private $line; |
||
43 | |||
44 | // helpers |
||
45 | private $blockDetected; |
||
46 | private $tracker; |
||
47 | private $dispatcher; |
||
48 | private $parser; |
||
49 | private $matcher; |
||
50 | private $events = [ |
||
51 | self::EVENT_BLOCK_ENTER, self::EVENT_BLOCK_LEAVE, |
||
52 | self::EVENT_GROUP_ENTER, self::EVENT_GROUP_LEAVE, |
||
53 | self::EVENT_ROUTINE_ENTER, self::EVENT_ROUTINE_LEAVE, |
||
54 | self::EVENT_STRUCT_ENTER, self::EVENT_STRUCT_LEAVE |
||
55 | ]; |
||
56 | |||
57 | private static $PROPERTIES = [T_PRIVATE, T_PUBLIC, T_PROTECTED, T_STATIC, T_VAR]; |
||
|
|||
58 | |||
59 | 10 | public function __construct(Parser $parser) { |
|
67 | |||
68 | 10 | public function reset() { |
|
82 | |||
83 | 10 | public function setTracker(TokenTracker $tracker) { |
|
86 | |||
87 | 10 | public function addListener($name, $listener) { |
|
90 | |||
91 | public function removeListener($name, $listener) { |
||
94 | |||
95 | 10 | public function visitToken(Token $token) { |
|
105 | |||
106 | 10 | private function detectBlockContext(Token $token) { |
|
121 | |||
122 | 10 | private function enterBlockContext(Token $token) { |
|
158 | |||
159 | 10 | private function findBlockStart(Token $token) { |
|
170 | |||
171 | 10 | private function leaveBlockContext(Token $token) { |
|
203 | |||
204 | public function isBlockContextDetected() { |
||
207 | |||
208 | /** |
||
209 | * Tells you, whenever being in a struct, this is also true when inside |
||
210 | * a method or inside a function, which is inside a method |
||
211 | * |
||
212 | * @return bool |
||
213 | */ |
||
214 | public function inStructBody() { |
||
217 | |||
218 | /** |
||
219 | * Tells you, whenever being in a function or method |
||
220 | * |
||
221 | * @return bool |
||
222 | */ |
||
223 | public function inRoutineBody() { |
||
226 | |||
227 | 10 | private function detectGroupContext(Token $token) { |
|
258 | |||
259 | 10 | private function isFunctionInvocation($token) { |
|
263 | |||
264 | 10 | private function detectLineContext(Token $token) { |
|
269 | |||
270 | 2 | public function resetLineContext() { |
|
273 | |||
274 | /** |
||
275 | * Returns the line context or null if not present |
||
276 | * |
||
277 | * @return string|null |
||
278 | */ |
||
279 | 2 | public function getLineContext() { |
|
282 | |||
283 | /** |
||
284 | * Returns the current context. Context is one of the Context::CONTEXT_* constants. |
||
285 | * |
||
286 | * @return string |
||
287 | */ |
||
288 | 5 | public function getCurrentContext() { |
|
295 | |||
296 | /** |
||
297 | * Returns the current block context |
||
298 | * |
||
299 | * @return Block |
||
300 | */ |
||
301 | public function getBlockContext() { |
||
304 | |||
305 | /** |
||
306 | * Returns the current group context |
||
307 | * |
||
308 | * @return Group|null |
||
309 | */ |
||
310 | 2 | public function getGroupContext() { |
|
313 | |||
314 | 10 | private function peekBlock() { |
|
320 | |||
321 | 10 | private function peekGroup() { |
|
327 | } |
||
328 |
This check marks private properties in classes that are never used. Those properties can be removed.