Complex classes like Monitor 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 Monitor, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class Monitor extends EventEmitter |
||
19 | { |
||
20 | use MonitorTrait; |
||
21 | |||
22 | public const EV_CREATE = 'EV_CREATE'; |
||
23 | public const EV_MODIFY = 'EV_MODIFY'; |
||
24 | public const EV_DELETE = 'EV_DELETE'; |
||
25 | |||
26 | protected const MASK_FILE = 0 |
||
27 | | IN_MODIFY; |
||
28 | |||
29 | protected const MASK_FILE_WITH_ATTRIB = 0 |
||
30 | | IN_MODIFY |
||
31 | | IN_ATTRIB; |
||
32 | |||
33 | /** |
||
34 | * the flag |
||
35 | * IN_MODIFY |
||
36 | * makes no sense for directories, as they will report the modify for the first child |
||
37 | * of the directory, and that MODIFY flag is set for the inner files in self::MASK_FILE |
||
38 | * For directories, the modify event is related to mtime and permissions, which is |
||
39 | * included in the IN_ATTRIB flag. |
||
40 | */ |
||
41 | protected const MASK_DIRECTORY = 0 |
||
42 | // | IN_MODIFY |
||
43 | | IN_MOVED_TO |
||
44 | | IN_MOVED_FROM |
||
45 | | IN_CREATE |
||
46 | | IN_DELETE |
||
47 | | IN_DELETE_SELF |
||
48 | | IN_ISDIR; |
||
49 | |||
50 | protected const MASK_DIRECTORY_WITH_MODIFY = 0 |
||
51 | // | IN_MODIFY |
||
52 | | IN_ATTRIB |
||
53 | | IN_MOVED_TO |
||
54 | | IN_MOVED_FROM |
||
55 | | IN_CREATE |
||
56 | | IN_DELETE |
||
57 | | IN_DELETE_SELF |
||
58 | | IN_ISDIR; |
||
59 | |||
60 | protected const MASK_DIRECTORY_CREATED_ONLY = 0 |
||
61 | | IN_MOVED_FROM |
||
62 | | IN_CREATE |
||
63 | | IN_ISDIR; |
||
64 | |||
65 | |||
66 | /** |
||
67 | * @var \MKraemer\ReactInotify\Inotify |
||
68 | */ |
||
69 | protected $inotify; |
||
70 | |||
71 | /** |
||
72 | * @var \Dimsh\React\Filesystem\Monitor\MonitorConfigurator |
||
73 | */ |
||
74 | protected $configurator; |
||
75 | |||
76 | /** |
||
77 | * @var [] |
||
78 | */ |
||
79 | protected $descriptors; |
||
80 | |||
81 | /** |
||
82 | * @var \React\EventLoop\LoopInterface|null |
||
83 | */ |
||
84 | protected $external_event_loop = null; |
||
85 | |||
86 | /** |
||
87 | * @var \React\EventLoop\LoopInterface|null |
||
88 | */ |
||
89 | protected $internal_loop; |
||
90 | |||
91 | /** |
||
92 | * @internal |
||
93 | * @var \Dimsh\React\Filesystem\Monitor\Monitor|null |
||
94 | */ |
||
95 | protected $base_dir_monitor = null; |
||
96 | |||
97 | /** |
||
98 | * Tell if $this->setupListeners() is called. |
||
99 | * |
||
100 | * @internal |
||
101 | * @var bool |
||
102 | */ |
||
103 | protected $is_inotify_event_listener_attached = false; |
||
104 | |||
105 | |||
106 | /** |
||
107 | * MonitorMultiManual constructor. |
||
108 | * |
||
109 | * @param \Dimsh\React\Filesystem\Monitor\MonitorConfigurator $configurator |
||
110 | * @param \React\EventLoop\LoopInterface|null $event_loop if passed then this event loop will be |
||
111 | * used to construct the Inotify object and |
||
112 | * should be "run" externally. When it is |
||
113 | * null an internal loop is created and |
||
114 | * method $this->run() must be called. |
||
115 | */ |
||
116 | 3 | public function __construct(MonitorConfigurator $configurator, ?LoopInterface $event_loop = null) |
|
175 | |||
176 | /** |
||
177 | * Get the event loop used by this instance. |
||
178 | * |
||
179 | * It is either the internal loop created in the constructor if no external one is |
||
180 | * passed to it, or the external. |
||
181 | * |
||
182 | * @return \React\EventLoop\LoopInterface |
||
183 | */ |
||
184 | 3 | public function getEventLoop(): LoopInterface |
|
188 | |||
189 | /** |
||
190 | * Fire the created event. |
||
191 | * |
||
192 | * If $path has a trailing slash, then a directory is created, else a file. |
||
193 | * This will only be fired for files/dirs that matches the patterns defined |
||
194 | * in the configurator. |
||
195 | * |
||
196 | * @param string $path |
||
197 | */ |
||
198 | protected function fireCreated($path): void |
||
202 | |||
203 | /** |
||
204 | * Fire the modified event. |
||
205 | * |
||
206 | * If $path has a trailing slash, then a directory is modified, else a file. |
||
207 | * This will only be fired for files/dirs that matches the patterns defined |
||
208 | * in the configurator. |
||
209 | * |
||
210 | * @param string $path |
||
211 | */ |
||
212 | protected function fireModified($path): void |
||
216 | |||
217 | /** |
||
218 | * Fire the deleted event. |
||
219 | * |
||
220 | * If $path has a trailing slash, then a directory is deleted, else a file. |
||
221 | * This will only be fired for files/dirs that matches the patterns defined |
||
222 | * in the configurator. |
||
223 | * |
||
224 | * @param string $path |
||
225 | */ |
||
226 | protected function fireDeleted($path): void |
||
230 | |||
231 | /** |
||
232 | * Setup event listeners on the inotify. |
||
233 | * |
||
234 | * @param bool $detach set to true to detach event listeners |
||
235 | * |
||
236 | * @return void |
||
237 | */ |
||
238 | 3 | protected function setupListeners($detach = false): void |
|
308 | |||
309 | /** |
||
310 | * Add a file/directory to the monitored list. |
||
311 | * |
||
312 | * if $path has a trailing slash then it is a directory, else a file. |
||
313 | * |
||
314 | * @param string $path |
||
315 | * |
||
316 | * @return void |
||
317 | */ |
||
318 | 3 | protected function add($path): void |
|
355 | |||
356 | /** |
||
357 | * If the path matches a pattern we want to monitor, then add the path to |
||
358 | * the watched list and fire the created event. |
||
359 | * |
||
360 | * @param string $path |
||
361 | * @param int $mask |
||
362 | * |
||
363 | * @return void |
||
364 | */ |
||
365 | 3 | protected function conditionallyAddAndFire($path, $mask): void |
|
376 | |||
377 | /** |
||
378 | * add the passed path to watched list and if the path match a pattern |
||
379 | * configured then fire the created event |
||
380 | * |
||
381 | * @param string $path |
||
382 | * @param int $mask |
||
383 | * |
||
384 | * @return void |
||
385 | */ |
||
386 | 3 | protected function addAndConditionallyFire($path, $mask): void |
|
397 | |||
398 | /** |
||
399 | * Remove file/directory from the monitored list. |
||
400 | * |
||
401 | * if $path has a trailing slash then it is a directory, else a file. |
||
402 | * When removing a directory, all items below it are removed from the list. |
||
403 | * |
||
404 | * @param string $path |
||
405 | * @param bool $skip_fire set to true to not fire the deleted event |
||
406 | * |
||
407 | * @return void |
||
408 | */ |
||
409 | protected function remove($path, $skip_fire = false): void |
||
440 | |||
441 | /** |
||
442 | * Run the event loop, this will block. |
||
443 | * |
||
444 | * If the event loop is passed externally to the constructor, then a user warning is triggered. |
||
445 | */ |
||
446 | 3 | public function run() |
|
456 | |||
457 | /** |
||
458 | * Stop the monitor using the traditional way of removing the listeners. |
||
459 | * |
||
460 | * @return void |
||
461 | */ |
||
462 | public function stop(): void |
||
473 | |||
474 | /** |
||
475 | * Stop the monitor using the traditional way and call the stop on the event loop. |
||
476 | * |
||
477 | * @return void |
||
478 | */ |
||
479 | public function stopAll(): void |
||
484 | |||
485 | /** |
||
486 | * Stop the monitor using a quick call to inotify close. |
||
487 | * |
||
488 | * This should be used carefully, this is not the correct way of stopping the monitor. |
||
489 | * |
||
490 | * If called too many times, the following warning may be generated: |
||
491 | * |
||
492 | * PHP Warning: inotify_add_watch(): The user limit on the total number of inotify |
||
493 | * watches was reached or the kernel failed to allocate a needed resource in |
||
494 | * vendor/mkraemer/react-inotify/src/MKraemer/ReactInotify/Inotify.php on line 77 |
||
495 | * |
||
496 | * @return void |
||
497 | */ |
||
498 | public function stopQuick(): void |
||
506 | |||
507 | /** |
||
508 | * Perform a quick stop and stop the event loop also. |
||
509 | * |
||
510 | */ |
||
511 | public function stopQuickAll(): void |
||
516 | |||
517 | } |
||
518 |