Complex classes like FileOperationTrait 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 FileOperationTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | trait FileOperationTrait { |
||
8 | |||
9 | /** @var string */ |
||
10 | protected $pathname; |
||
11 | |||
12 | /** |
||
13 | * Static instantiator |
||
14 | * |
||
15 | * @param string $pathname |
||
16 | * @return static |
||
17 | */ |
||
18 | public static function create($pathname) { |
||
21 | |||
22 | protected function init($pathname) { |
||
25 | |||
26 | /** |
||
27 | * Returns the file extensions |
||
28 | * |
||
29 | * @return string the file extension |
||
30 | */ |
||
31 | public function getExtension() { |
||
34 | |||
35 | /** |
||
36 | * Returns the filename |
||
37 | * |
||
38 | * @return string the filename |
||
39 | */ |
||
40 | public function getFilename() { |
||
43 | |||
44 | /** |
||
45 | * Gets the path without filename |
||
46 | * |
||
47 | * @return string |
||
48 | */ |
||
49 | public function getDirname() { |
||
52 | |||
53 | /** |
||
54 | * Gets the path to the file |
||
55 | * |
||
56 | * @return string |
||
57 | */ |
||
58 | public function getPathname() { |
||
61 | |||
62 | /** |
||
63 | * Converts the path into a path object |
||
64 | * |
||
65 | * @return Path |
||
66 | */ |
||
67 | public function toPath() { |
||
70 | |||
71 | /** |
||
72 | * Gets last access time. |
||
73 | * |
||
74 | * @return DateTime |
||
75 | */ |
||
76 | public function getLastAccessedAt() { |
||
82 | |||
83 | /** |
||
84 | * Gets the created time. |
||
85 | * |
||
86 | * @return DateTime |
||
87 | */ |
||
88 | public function getCreatedAt() { |
||
94 | |||
95 | /** |
||
96 | * Gets last modified time. |
||
97 | * |
||
98 | * @return DateTime |
||
99 | */ |
||
100 | public function getModifiedAt() { |
||
106 | |||
107 | /** |
||
108 | * Gets file inode |
||
109 | * |
||
110 | * @return int Returns the inode number of the file, or FALSE on failure. |
||
111 | */ |
||
112 | public function getInode() { |
||
115 | |||
116 | /** |
||
117 | * Gets file group |
||
118 | * |
||
119 | * @return int Returns the group ID, or FALSE if an error occurs. |
||
120 | */ |
||
121 | public function getGroup() { |
||
124 | |||
125 | /** |
||
126 | * Gets file owner |
||
127 | * |
||
128 | * @return int Returns the user ID of the owner, or FALSE on failure. |
||
129 | */ |
||
130 | public function getOwner() { |
||
133 | |||
134 | /** |
||
135 | * Gets file permissions |
||
136 | * |
||
137 | * @return int Returns the file's permissions as a numeric mode. Lower bits of this |
||
138 | * mode are the same as the permissions expected by chmod(), however on most platforms |
||
139 | * the return value will also include information on the type of file given as filename. |
||
140 | * The examples below demonstrate how to test the return value for specific permissions |
||
141 | * and file types on POSIX systems, including Linux and Mac OS X. |
||
142 | */ |
||
143 | public function getPermissions() { |
||
146 | |||
147 | /** |
||
148 | * Checks its existance |
||
149 | * |
||
150 | * @return boolean Returns TRUE if exists; FALSE otherwise. Will return FALSE for symlinks |
||
151 | * pointing to non-existing files. |
||
152 | */ |
||
153 | public function exists() { |
||
156 | |||
157 | /** |
||
158 | * Tells whether is executable |
||
159 | * |
||
160 | * @return boolean Returns TRUE if exists and is executable. |
||
161 | */ |
||
162 | public function isExecutable() { |
||
165 | |||
166 | /** |
||
167 | * Tells whether is readable |
||
168 | * |
||
169 | * @return boolean Returns TRUE if exists and is readable. |
||
170 | */ |
||
171 | public function isReadable() { |
||
174 | |||
175 | /** |
||
176 | * Tells whether is writable |
||
177 | * |
||
178 | * @return boolean Returns TRUE if exists and is writable. |
||
179 | */ |
||
180 | public function isWritable() { |
||
183 | |||
184 | /** |
||
185 | * Tells whether the filename is a symbolic link |
||
186 | * |
||
187 | * @return boolean Returns TRUE if the filename exists and is a symbolic link, FALSE otherwise. |
||
188 | */ |
||
189 | public function isLink() { |
||
192 | |||
193 | /** |
||
194 | * Returns the target if this is a symbolic link |
||
195 | * |
||
196 | * @see #isLink |
||
197 | * @return Path|null The target path or null if this isn't a link |
||
198 | */ |
||
199 | public function getLinkTarget() { |
||
205 | |||
206 | /** |
||
207 | * Attempts to change the group. |
||
208 | * |
||
209 | * Only the superuser may change the group arbitrarily; other users may |
||
210 | * change the group of a file to any group of which that user is a member. |
||
211 | * |
||
212 | * @param mixed $group A group name or number. |
||
213 | * @return boolean Returns TRUE on success or FALSE on failure. |
||
214 | */ |
||
215 | public function setGroup($group) { |
||
222 | |||
223 | /** |
||
224 | * Attempts to change the mode. |
||
225 | * |
||
226 | * @see #setGroup |
||
227 | * @see #setOwner |
||
228 | * |
||
229 | * @param int $mode |
||
230 | * Note that mode is not automatically assumed to be an octal value, so strings |
||
231 | * (such as "g+w") will not work properly. To ensure the expected operation, you |
||
232 | * need to prefix mode with a zero (0). |
||
233 | * |
||
234 | * @return boolean Returns TRUE on success or FALSE on failure. |
||
235 | */ |
||
236 | public function setMode($mode) { |
||
243 | |||
244 | /** |
||
245 | * Changes file owner |
||
246 | * |
||
247 | * Attempts to change the owner. Only the superuser may change the owner of a file. |
||
248 | * |
||
249 | * @param mixed $user A user name or number. |
||
250 | * @return boolean Returns TRUE on success or FALSE on failure. |
||
251 | */ |
||
252 | public function setOwner($user) { |
||
259 | |||
260 | /** |
||
261 | * Copies the file |
||
262 | * |
||
263 | * If the destination file already exists, it will be overwritten. |
||
264 | * |
||
265 | * @throws FileException When an error appeared. |
||
266 | * @param string|Path $destination The destination path. |
||
267 | */ |
||
268 | public function copy($destination) { |
||
275 | |||
276 | /** |
||
277 | * Moves the file |
||
278 | * |
||
279 | * @throws FileException When an error appeared. |
||
280 | * @param string|Path $destination |
||
281 | */ |
||
282 | public function move($destination) { |
||
291 | |||
292 | /** |
||
293 | * Transforms destination into path and ensures, parent directory exists |
||
294 | * |
||
295 | * @param string $destination |
||
296 | * @return Path |
||
297 | */ |
||
298 | private function getDestination($destination) { |
||
304 | |||
305 | /** |
||
306 | * Creates a symlink to the given destination |
||
307 | * |
||
308 | * @param string|Path $destination |
||
309 | */ |
||
310 | public function linkTo($destination) { |
||
332 | |||
333 | public abstract function delete(); |
||
334 | |||
335 | } |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.