Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
41 | class Path extends ObjectAbstract implements PathInterface, ErrorAwareInterface, FilesystemAwareInterface, ExtensionAwareInterface |
||
42 | { |
||
43 | use FilesystemAwareTrait, ErrorAwareTrait, ExtensionAwareTrait; |
||
44 | |||
45 | /** |
||
46 | * Full path with mount point |
||
47 | * |
||
48 | * @var string |
||
49 | * @access protected |
||
50 | */ |
||
51 | protected $full; |
||
52 | |||
53 | /** |
||
54 | * relative path without mount point prefix |
||
55 | * |
||
56 | * @var string |
||
57 | * @access protected |
||
58 | */ |
||
59 | protected $path; |
||
60 | |||
61 | /** |
||
62 | * Instantiate the path object |
||
63 | * |
||
64 | * @param string $full full path |
||
65 | * @param string $path relative path without mount point |
||
66 | * @param FilesystemInterface $filesystem |
||
67 | * @access public |
||
68 | * @api |
||
69 | */ |
||
70 | public function __construct( |
||
71 | /*# string */ $full, |
||
72 | /*# string */ $path, |
||
73 | FilesystemInterface $filesystem |
||
74 | ) { |
||
75 | $this->setFilesystem($filesystem); |
||
76 | $this->full = $full; |
||
77 | $this->path = $path; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * {@inheritDoc} |
||
82 | */ |
||
83 | public function exists()/*# : bool */ |
||
84 | { |
||
85 | if (!$this->getDriver()->exists($this->path)) { |
||
86 | return $this->setError( |
||
87 | Message::get(Message::MSG_PATH_NOTFOUND, $this->full), |
||
88 | Message::MSG_PATH_NOTFOUND |
||
89 | ); |
||
90 | } |
||
91 | return true; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * {@inheritDoc} |
||
96 | */ |
||
97 | View Code Duplication | public function getContent(/*# bool */ $stream = false) |
|
98 | { |
||
99 | // not exists or filesystem not readable |
||
100 | if (!$this->exists() || !$this->isFilesystemReadable()) { |
||
101 | return null; |
||
102 | } |
||
103 | |||
104 | $res = $this->getDriver()->getContent($this->path, $stream); |
||
105 | $this->resetError(); |
||
106 | return $res; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * {@inheritDoc} |
||
111 | */ |
||
112 | public function getMeta()/*# : array */ |
||
113 | { |
||
114 | if (!$this->exists()) { |
||
115 | return []; |
||
116 | } |
||
117 | |||
118 | $res = $this->getDriver()->getMeta($this->path); |
||
119 | $this->resetError(); |
||
120 | return $res; |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * {@inheritDoc} |
||
125 | */ |
||
126 | public function getPath()/*# : string */ |
||
130 | |||
131 | /** |
||
132 | * {@inheritDoc} |
||
133 | */ |
||
134 | public function getFullPath()/*# : string */ |
||
138 | |||
139 | /** |
||
140 | * {@inheritDoc} |
||
141 | */ |
||
142 | public function setContent($content)/*# : bool */ |
||
153 | |||
154 | /** |
||
155 | * {@inheritDoc} |
||
156 | */ |
||
157 | View Code Duplication | public function setMeta(array $meta)/*# : bool */ |
|
170 | |||
171 | /** |
||
172 | * {@inheritDoc} |
||
173 | */ |
||
174 | public function isDir()/*# : bool */ |
||
183 | |||
184 | /** |
||
185 | * {@inheritDoc} |
||
186 | */ |
||
187 | public function rename(/*# string */ $destination)/*# : bool */ |
||
191 | |||
192 | /** |
||
193 | * {@inheritDoc} |
||
194 | */ |
||
195 | public function copy(/*# string */ $destination)/*# : bool */ |
||
199 | |||
200 | /** |
||
201 | * {@inheritDoc} |
||
202 | */ |
||
203 | View Code Duplication | public function delete()/*# : bool */ |
|
216 | |||
217 | /** |
||
218 | * Reset error to driver's error |
||
219 | * |
||
220 | * @access protected |
||
221 | */ |
||
222 | protected function resetError() |
||
226 | |||
227 | /** |
||
228 | * Get the driver |
||
229 | * |
||
230 | * @return DriverInterface |
||
231 | * @access protected |
||
232 | */ |
||
233 | protected function getDriver()/*# : DriverInterface */ |
||
237 | |||
238 | /** |
||
239 | * Do copy or rename in same filesystem |
||
240 | * |
||
241 | * @param string $destination |
||
242 | * @param string $action 'copy' or 'rename' |
||
243 | * @return bool |
||
244 | * @access protected |
||
245 | */ |
||
246 | protected function alterAction( |
||
265 | |||
266 | /** |
||
267 | * Is current path has trailing '/' |
||
268 | * |
||
269 | * @param string $path |
||
270 | * @return bool |
||
271 | * @access protected |
||
272 | */ |
||
273 | protected function hasTrailingSlash(/*# string */ $path)/*# : bool */ |
||
281 | |||
282 | /** |
||
283 | * Check filesystem readable or not |
||
284 | * |
||
285 | * @return bool |
||
286 | * @access protected |
||
287 | */ |
||
288 | protected function isFilesystemReadable()/*# : bool */ |
||
299 | |||
300 | /** |
||
301 | * Check filesystem writable or not |
||
302 | * |
||
303 | * @return bool |
||
304 | * @access protected |
||
305 | */ |
||
306 | protected function isFilesystemWritable()/*# : bool */ |
||
317 | |||
318 | /** |
||
319 | * Check filesystem file deletable or not |
||
320 | * |
||
321 | * @return bool |
||
322 | * @access protected |
||
323 | */ |
||
324 | protected function isFilesystemDeletable()/*# : bool */ |
||
335 | } |
||
336 |