Complex classes like CommonFileHandlingTrait 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 CommonFileHandlingTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
44 | trait CommonFileHandlingTrait |
||
45 | { |
||
46 | use FilePathNormalizerTrait; |
||
47 | /** |
||
48 | * @param string $fileName |
||
49 | * |
||
50 | * @param MediatorInterface $yem |
||
51 | * |
||
52 | * @return false|string |
||
53 | * @throws \DomainException |
||
54 | * @throws \InvalidArgumentException |
||
55 | * @throws \LogicException |
||
56 | */ |
||
57 | protected function safeFileRead(string $fileName, MediatorInterface $yem) |
||
68 | /** |
||
69 | * Safely write file using lock and temp file. |
||
70 | * |
||
71 | * @param string $data |
||
72 | * @param string $pathFile |
||
73 | * @param MediatorInterface $yem |
||
74 | * |
||
75 | * @return bool |
||
76 | * @throws \DomainException |
||
77 | * @throws \InvalidArgumentException |
||
78 | * @throws \LogicException |
||
79 | */ |
||
80 | protected function safeFileWrite(string $data, string $pathFile, MediatorInterface $yem): bool |
||
104 | /** |
||
105 | * @param string $fileName |
||
106 | * @param MediatorInterface $yem |
||
107 | * @param string $mode |
||
108 | * |
||
109 | * @return bool|resource |
||
110 | * @throws \DomainException |
||
111 | * @throws \InvalidArgumentException |
||
112 | * @throws \LogicException |
||
113 | */ |
||
114 | private function acquireLockedHandle(string $fileName, MediatorInterface $yem, string $mode = 'cb+') |
||
129 | /** |
||
130 | * @param resource $handle |
||
131 | * |
||
132 | * @return bool |
||
133 | */ |
||
134 | private function acquiredLock($handle): bool |
||
148 | /** |
||
149 | * Used to delete a file when unlink might fail and it needs to be retried. |
||
150 | * |
||
151 | * @param string $fileName |
||
152 | * @param MediatorInterface $yem |
||
153 | * |
||
154 | * @return bool |
||
155 | * @throws \DomainException |
||
156 | * @throws \InvalidArgumentException |
||
157 | * @throws \LogicException |
||
158 | */ |
||
159 | private function deleteWithRetry(string $fileName, MediatorInterface $yem): bool |
||
186 | /** |
||
187 | * @param string $path |
||
188 | * |
||
189 | * @param MediatorInterface $yem |
||
190 | * |
||
191 | * @return bool |
||
192 | * @throws \DomainException |
||
193 | * @throws \InvalidArgumentException |
||
194 | */ |
||
195 | private function isWritablePath(string $path, MediatorInterface $yem): bool |
||
214 | /** |
||
215 | * @param resource $handle |
||
216 | * |
||
217 | * @return self Fluent interface. |
||
218 | */ |
||
219 | private function releaseHandle($handle) |
||
227 | /** |
||
228 | * @param string $fileName |
||
229 | * @param MediatorInterface $yem |
||
230 | * |
||
231 | * @return bool|string |
||
232 | * @throws \DomainException |
||
233 | * @throws \InvalidArgumentException |
||
234 | * @throws \LogicException |
||
235 | */ |
||
236 | private function safeDataRead(string $fileName, MediatorInterface $yem) |
||
264 | /** |
||
265 | * @param string $data |
||
266 | * @param string $fileName |
||
267 | * @param MediatorInterface $yem |
||
268 | * |
||
269 | * @return bool |
||
270 | * @throws \DomainException |
||
271 | * @throws \InvalidArgumentException |
||
272 | * @throws \LogicException |
||
273 | */ |
||
274 | private function safeDataWrite(string $data, string $fileName, MediatorInterface $yem): bool |
||
301 | } |
||
302 |