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 |
||
| 8 | trait FileOperationTrait { |
||
| 9 | |||
| 10 | /** @var string */ |
||
| 11 | protected $pathname; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Static instantiator |
||
| 15 | * |
||
| 16 | * @param string $pathname |
||
| 17 | * @return static |
||
| 18 | */ |
||
| 19 | 1 | public static function create($pathname) { |
|
| 22 | |||
| 23 | 19 | protected function init($pathname) { |
|
| 26 | |||
| 27 | /** |
||
| 28 | * Returns the file extensions |
||
| 29 | * |
||
| 30 | * @return string the file extension |
||
| 31 | */ |
||
| 32 | 1 | public function getExtension() { |
|
| 35 | |||
| 36 | /** |
||
| 37 | * Returns the filename |
||
| 38 | * |
||
| 39 | * @return string the filename |
||
| 40 | */ |
||
| 41 | 6 | public function getFilename() { |
|
| 44 | |||
| 45 | /** |
||
| 46 | * Gets the path without filename |
||
| 47 | * |
||
| 48 | * @return string |
||
| 49 | */ |
||
| 50 | 8 | public function getDirname() { |
|
| 53 | |||
| 54 | /** |
||
| 55 | * Gets the path to the file |
||
| 56 | * |
||
| 57 | * @return string |
||
| 58 | */ |
||
| 59 | 7 | public function getPathname() { |
|
| 62 | |||
| 63 | /** |
||
| 64 | * Converts the path into a path object |
||
| 65 | * |
||
| 66 | * @return Path |
||
| 67 | */ |
||
| 68 | 1 | public function toPath() { |
|
| 71 | |||
| 72 | /** |
||
| 73 | * Gets last access time. |
||
| 74 | * |
||
| 75 | * @return DateTime |
||
| 76 | */ |
||
| 77 | 1 | public function getLastAccessedAt() { |
|
| 78 | 1 | $timestamp = fileatime($this->pathname); |
|
| 79 | 1 | $time = new DateTime(); |
|
| 80 | 1 | $time->setTimestamp($timestamp); |
|
| 81 | 1 | return $time; |
|
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Gets the created time. |
||
| 86 | * |
||
| 87 | * @return DateTime |
||
| 88 | */ |
||
| 89 | 1 | public function getCreatedAt() { |
|
| 90 | 1 | $timestamp = filemtime($this->pathname); |
|
| 91 | 1 | $time = new DateTime(); |
|
| 92 | 1 | $time->setTimestamp($timestamp); |
|
| 93 | 1 | return $time; |
|
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Gets last modified time. |
||
| 98 | * |
||
| 99 | * @return DateTime |
||
| 100 | */ |
||
| 101 | public function getModifiedAt() { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Gets file inode |
||
| 110 | * |
||
| 111 | * @return int Returns the inode number of the file, or FALSE on failure. |
||
| 112 | */ |
||
| 113 | public function getInode() { |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Gets file group |
||
| 119 | * |
||
| 120 | * @return int Returns the group ID, or FALSE if an error occurs. |
||
| 121 | */ |
||
| 122 | public function getGroup() { |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Gets file owner |
||
| 128 | * |
||
| 129 | * @return int Returns the user ID of the owner, or FALSE on failure. |
||
| 130 | */ |
||
| 131 | public function getOwner() { |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Gets file permissions |
||
| 137 | * |
||
| 138 | * @return int Returns the file's permissions as a numeric mode. Lower bits of this |
||
| 139 | * mode are the same as the permissions expected by chmod(), however on most platforms |
||
| 140 | * the return value will also include information on the type of file given as filename. |
||
| 141 | * The examples below demonstrate how to test the return value for specific permissions |
||
| 142 | * and file types on POSIX systems, including Linux and Mac OS X. |
||
| 143 | */ |
||
| 144 | public function getPermissions() { |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Checks its existance |
||
| 150 | * |
||
| 151 | * @return boolean Returns TRUE if exists; FALSE otherwise. Will return FALSE for symlinks |
||
| 152 | * pointing to non-existing files. |
||
| 153 | */ |
||
| 154 | 16 | public function exists() { |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Tells whether is executable |
||
| 160 | * |
||
| 161 | * @return boolean Returns TRUE if exists and is executable. |
||
| 162 | */ |
||
| 163 | public function isExecutable() { |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Tells whether is readable |
||
| 169 | * |
||
| 170 | * @return boolean Returns TRUE if exists and is readable. |
||
| 171 | */ |
||
| 172 | 3 | public function isReadable() { |
|
| 173 | 3 | return is_readable($this->pathname); |
|
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Tells whether is writable |
||
| 178 | * |
||
| 179 | * @return boolean Returns TRUE if exists and is writable. |
||
| 180 | */ |
||
| 181 | public function isWritable() { |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Tells whether the filename is a symbolic link |
||
| 187 | * |
||
| 188 | * @return boolean Returns TRUE if the filename exists and is a symbolic link, FALSE otherwise. |
||
| 189 | */ |
||
| 190 | 4 | public function isLink() { |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Returns the target if this is a symbolic link |
||
| 196 | * |
||
| 197 | * @see #isLink |
||
| 198 | * @return Path|null The target path or null if this isn't a link |
||
| 199 | */ |
||
| 200 | 1 | public function getLinkTarget() { |
|
| 206 | |||
| 207 | /** |
||
| 208 | * Attempts to change the group. |
||
| 209 | * |
||
| 210 | * Only the superuser may change the group arbitrarily; other users may |
||
| 211 | * change the group of a file to any group of which that user is a member. |
||
| 212 | * |
||
| 213 | * @param mixed $group A group name or number. |
||
| 214 | * @return boolean Returns TRUE on success or FALSE on failure. |
||
| 215 | */ |
||
| 216 | public function setGroup($group) { |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Attempts to change the mode. |
||
| 226 | * |
||
| 227 | * @see #setGroup |
||
| 228 | * @see #setOwner |
||
| 229 | * |
||
| 230 | * @param int $mode |
||
| 231 | * Note that mode is not automatically assumed to be an octal value, so strings |
||
| 232 | * (such as "g+w") will not work properly. To ensure the expected operation, you |
||
| 233 | * need to prefix mode with a zero (0). |
||
| 234 | * |
||
| 235 | * @return boolean Returns TRUE on success or FALSE on failure. |
||
| 236 | */ |
||
| 237 | 3 | public function setMode($mode) { |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Changes file owner |
||
| 247 | * |
||
| 248 | * Attempts to change the owner. Only the superuser may change the owner of a file. |
||
| 249 | * |
||
| 250 | * @param mixed $user A user name or number. |
||
| 251 | * @return boolean Returns TRUE on success or FALSE on failure. |
||
| 252 | */ |
||
| 253 | public function setOwner($user) { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Copies the file |
||
| 263 | * |
||
| 264 | * If the destination file already exists, it will be overwritten. |
||
| 265 | * |
||
| 266 | * @throws FileException When an error appeared. |
||
| 267 | * @param string|Path $destination The destination path. |
||
| 268 | */ |
||
| 269 | 2 | public function copy($destination) { |
|
| 276 | |||
| 277 | /** |
||
| 278 | * Moves the file |
||
| 279 | * |
||
| 280 | * @throws FileException When an error appeared. |
||
| 281 | * @param string|Path $destination |
||
| 282 | */ |
||
| 283 | 2 | public function move($destination) { |
|
| 292 | |||
| 293 | /** |
||
| 294 | * Transforms destination into path and ensures, parent directory exists |
||
| 295 | * |
||
| 296 | * @param string $destination |
||
| 297 | * @return Path |
||
| 298 | */ |
||
| 299 | 4 | private function getDestination($destination) { |
|
| 305 | |||
| 306 | /** |
||
| 307 | * Creates a symlink to the given destination |
||
| 308 | * |
||
| 309 | * @param string|Path $destination |
||
| 310 | */ |
||
| 311 | 1 | public function linkTo($destination) { |
|
| 333 | |||
| 334 | public abstract function delete(); |
||
| 335 | |||
| 336 | } |
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.