Complex classes like Path 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 Path, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | class Path { |
||
8 | |||
9 | /** @var ArrayObject */ |
||
10 | private $segments; |
||
11 | |||
12 | /** @var string */ |
||
13 | private $stream; |
||
14 | |||
15 | /** @var Text */ |
||
16 | private $pathname; |
||
17 | |||
18 | /** @var string */ |
||
19 | private $dirname; |
||
20 | |||
21 | /** @var string */ |
||
22 | private $filename; |
||
23 | |||
24 | /** @var string */ |
||
25 | private $extension; |
||
26 | |||
27 | |||
28 | public function __construct($pathname) { |
||
31 | |||
32 | private function init($pathname) { |
||
45 | |||
46 | /** |
||
47 | * Returns the extension |
||
48 | * |
||
49 | * @return string the extension |
||
50 | */ |
||
51 | public function getExtension() { |
||
54 | |||
55 | /** |
||
56 | * Returns the filename |
||
57 | * |
||
58 | * @return string the filename |
||
59 | */ |
||
60 | public function getFilename() { |
||
63 | |||
64 | /** |
||
65 | * Gets the path without filename |
||
66 | * |
||
67 | * @return string |
||
68 | */ |
||
69 | public function getDirname() { |
||
72 | |||
73 | /** |
||
74 | * Gets the full pathname |
||
75 | * |
||
76 | * @return Text |
||
77 | */ |
||
78 | public function getPathname() { |
||
81 | |||
82 | /** |
||
83 | * @return bool |
||
84 | */ |
||
85 | public function isStream() { |
||
88 | |||
89 | /** |
||
90 | * Changes the extension of this path |
||
91 | * |
||
92 | * @param string $extension the new extension |
||
93 | * @return $this |
||
94 | */ |
||
95 | public function setExtension($extension) { |
||
111 | |||
112 | /** |
||
113 | * Returns a path with the same segments as this path but with a |
||
114 | * trailing separator added (if not already existent). |
||
115 | * |
||
116 | * @return $this |
||
117 | */ |
||
118 | public function addTrailingSeparator() { |
||
124 | |||
125 | /** |
||
126 | * Returns the path obtained from the concatenation of the given path's |
||
127 | * segments/string to the end of this path. |
||
128 | * |
||
129 | * @param string|Text|Path $path |
||
130 | * @return Path |
||
131 | */ |
||
132 | public function append($path) { |
||
143 | |||
144 | /** |
||
145 | * Returns whether this path has a trailing separator. |
||
146 | * |
||
147 | * @return boolean |
||
148 | */ |
||
149 | public function hasTrailingSeparator() { |
||
152 | |||
153 | /** |
||
154 | * Returns whether this path is empty |
||
155 | * |
||
156 | * @return boolean |
||
157 | */ |
||
158 | public function isEmpty() { |
||
161 | |||
162 | /** |
||
163 | * Returns whether this path is an absolute path. |
||
164 | * |
||
165 | * @return boolean |
||
166 | */ |
||
167 | public function isAbsolute() { |
||
189 | |||
190 | /** |
||
191 | * Checks whether this path is the prefix of another path |
||
192 | * |
||
193 | * @param Path $anotherPath |
||
194 | * @return boolean |
||
195 | */ |
||
196 | public function isPrefixOf(Path $anotherPath) { |
||
199 | |||
200 | /** |
||
201 | * Returns the last segment of this path, or null if it does not have any segments. |
||
202 | * |
||
203 | * @return Text |
||
204 | */ |
||
205 | public function lastSegment() { |
||
208 | |||
209 | /** |
||
210 | * Makes the path relative to another given path |
||
211 | * |
||
212 | * @param Path $base |
||
213 | * @return Path the new relative path |
||
214 | */ |
||
215 | public function makeRelativeTo(Path $base) { |
||
219 | |||
220 | /** |
||
221 | * Returns a count of the number of segments which match in this |
||
222 | * path and the given path, comparing in increasing segment number order. |
||
223 | * |
||
224 | * @param Path $anotherPath |
||
225 | * @return int |
||
226 | */ |
||
227 | public function matchingFirstSegments(Path $anotherPath) { |
||
239 | |||
240 | /** |
||
241 | * Returns a new path which is the same as this path but with the file extension removed. |
||
242 | * |
||
243 | * @return Path |
||
244 | */ |
||
245 | public function removeExtension() { |
||
248 | |||
249 | /** |
||
250 | * Returns a copy of this path with the given number of segments removed from the beginning. |
||
251 | * |
||
252 | * @param int $count |
||
253 | * @return Path |
||
254 | */ |
||
255 | public function removeFirstSegments($count) { |
||
262 | |||
263 | /** |
||
264 | * Returns a copy of this path with the given number of segments removed from the end. |
||
265 | * |
||
266 | * @param int $count |
||
267 | * @return Path |
||
268 | */ |
||
269 | public function removeLastSegments($count) { |
||
276 | |||
277 | /** |
||
278 | * Returns a copy of this path with the same segments as this path but with a trailing separator removed. |
||
279 | * |
||
280 | * @return $this |
||
281 | */ |
||
282 | public function removeTrailingSeparator() { |
||
288 | |||
289 | /** |
||
290 | * Returns the specified segment of this path, or null if the path does not have such a segment. |
||
291 | * |
||
292 | * @param int $index |
||
293 | * @return string |
||
294 | */ |
||
295 | public function segment($index) { |
||
302 | |||
303 | /** |
||
304 | * Returns the number of segments in this path. |
||
305 | * |
||
306 | * @return int |
||
307 | */ |
||
308 | public function segmentCount() { |
||
311 | |||
312 | /** |
||
313 | * Returns the segments in this path in order. |
||
314 | * |
||
315 | * @return ArrayObject<string> |
||
316 | */ |
||
317 | public function segments() { |
||
320 | |||
321 | /** |
||
322 | * Returns a FileDescriptor corresponding to this path. |
||
323 | * |
||
324 | * @return FileDescriptor |
||
325 | */ |
||
326 | public function toFileDescriptor() { |
||
329 | |||
330 | /** |
||
331 | * Returns a string representation of this path |
||
332 | * |
||
333 | * @return string A string representation of this path |
||
334 | */ |
||
335 | public function toString() { |
||
338 | |||
339 | /** |
||
340 | * String representation as pathname |
||
341 | */ |
||
342 | public function __toString() { |
||
345 | |||
346 | /** |
||
347 | * Returns a copy of this path truncated after the given number of segments. |
||
348 | * |
||
349 | * @param int $count |
||
350 | * @return Path |
||
351 | */ |
||
352 | public function upToSegment($count) { |
||
360 | |||
361 | /** |
||
362 | * Checks whether both paths point to the same location |
||
363 | * |
||
364 | * @param Path|string $anotherPath |
||
365 | * @return boolean true if the do, false if they don't |
||
366 | */ |
||
367 | public function equals($anotherPath) { |
||
380 | } |
||
381 |