Complex classes like File 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 File, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 19 | class File extends Node | ||
| 20 | { | ||
| 21 | /** | ||
| 22 |      * @api {get} /api/v1/file/preview?id=:id Get Preview | ||
| 23 | * @apiVersion 1.0.6 | ||
| 24 | * @apiName getPreview | ||
| 25 | * @apiGroup Node\File | ||
| 26 | * @apiPermission none | ||
| 27 | * @apiDescription Get a preview of the files content. The body either contains an encoded string or a jpeg binary | ||
| 28 | * @apiUse _getNode | ||
| 29 | * | ||
| 30 | * @apiExample (cURL) exmaple: | ||
| 31 | * curl -XGET "https://SERVER/api/v1/file/preview?id=544627ed3c58891f058b4686 > preview.jpg" | ||
| 32 | * curl -XGET "https://SERVER/api/v1/file/544627ed3c58891f058b4686/preview > preview.jpg" | ||
| 33 | * curl -XGET "https://SERVER/api/v1/file/preview?p=/absolute/path/to/my/file > preview.jpg" | ||
| 34 | * | ||
| 35 |      * @apiParam (GET Parameter) {string} [encode=false] Set to base64 to return a jpeg encoded preview as base64, else return it as jpeg binary | ||
| 36 | * | ||
| 37 |      * @apiSuccessExample {string} Success-Response: | ||
| 38 | * HTTP/1.1 200 OK | ||
| 39 | * | ||
| 40 |      * @apiSuccessExample {binary} Success-Response: | ||
| 41 | * HTTP/1.1 200 OK | ||
| 42 | * | ||
| 43 |      * @apiErrorExample {json} Error-Response (thumbnail not found): | ||
| 44 | * HTTP/1.1 404 Not Found | ||
| 45 |      * { | ||
| 46 | * "status": 404, | ||
| 47 |      *      "data": { | ||
| 48 | * "error": "Balloon\\Exception\\NotFound", | ||
| 49 | * "message": "no preview exists" | ||
| 50 | * } | ||
| 51 | * } | ||
| 52 | * | ||
| 53 | * @param string $id | ||
| 54 | * @param string $p | ||
| 55 | * @param string $encode | ||
| 56 | * @return void | ||
| 57 | */ | ||
| 58 | public function getPreview(?string $id=null, ?string $p=null, ?string $encode=null): void | ||
| 72 | |||
| 73 | |||
| 74 | /** | ||
| 75 |      * @api {get} /api/v1/file/history?id=:id Get history | ||
| 76 | * @apiVersion 1.0.6 | ||
| 77 | * @apiName getHistory | ||
| 78 | * @apiGroup Node\File | ||
| 79 | * @apiPermission none | ||
| 80 | * @apiDescription Get a full change history of a file | ||
| 81 | * @apiUse _getNode | ||
| 82 | * | ||
| 83 | * @apiExample (cURL) example: | ||
| 84 | * curl -XGET "https://SERVER/api/v1/file/history?id=544627ed3c58891f058b4686&pretty" | ||
| 85 | * curl -XGET "https://SERVER/api/v1/file/544627ed3c58891f058b4686/history?pretty" | ||
| 86 | * curl -XGET "https://SERVER/api/v1/file/history?p=/absolute/path/to/my/file&pretty" | ||
| 87 | * | ||
| 88 |      * @apiSuccess (200 OK) {number} status Status Code | ||
| 89 |      * @apiSuccess (200 OK) {object[]} data History | ||
| 90 |      * @apiSuccess (200 OK) {number} data.version Version | ||
| 91 |      * @apiSuccess (200 OK) {object} data.changed Changed timestamp | ||
| 92 |      * @apiSuccess (200 OK) {number} data.changed.sec Changed timestamp in Unix time | ||
| 93 |      * @apiSuccess (200 OK) {number} data.changed.usec Additional microseconds to changed Unix timestamp | ||
| 94 |      * @apiSuccess (200 OK) {string} data.user User which changed the version | ||
| 95 |      * @apiSuccess (200 OK) {number} data.type Change type, there are five different change types including:</br> | ||
| 96 | * 0 - Initially added</br> | ||
| 97 | * 1 - Content modified</br> | ||
| 98 | * 2 - Version rollback</br> | ||
| 99 | * 3 - Deleted</br> | ||
| 100 | * 4 - Undeleted | ||
| 101 |      * @apiSuccess (200 OK) {object} data.file Reference to the content | ||
| 102 |      * @apiSuccess (200 OK) {string} data.file.id Content reference ID | ||
| 103 |      * @apiSuccess (200 OK) {number} data.size Content size in bytes | ||
| 104 |      * @apiSuccess (200 OK) {string} data.mime Content mime type | ||
| 105 |      * @apiSuccessExample {json} Success-Response: | ||
| 106 | * HTTP/1.1 200 OK | ||
| 107 |      * { | ||
| 108 | * "status": 200, | ||
| 109 | * "data": [ | ||
| 110 |      *          { | ||
| 111 | * "version": 1, | ||
| 112 |      *              "changed": { | ||
| 113 | * "sec": 1413883885, | ||
| 114 | * "usec": 876000 | ||
| 115 | * }, | ||
| 116 | * "user": "peter.meier", | ||
| 117 | * "type": 0, | ||
| 118 |      *              "file": { | ||
| 119 | * "$id": "544627ed3c58891f058b4688" | ||
| 120 | * }, | ||
| 121 | * "size": 178, | ||
| 122 | * "mime": "text\/plain" | ||
| 123 | * } | ||
| 124 | * ] | ||
| 125 | * } | ||
| 126 | * | ||
| 127 | * @param string $id | ||
| 128 | * @param string $p | ||
| 129 | * @return Response | ||
| 130 | */ | ||
| 131 | public function getHistory(?string $id=null, ?string $p=null): Response | ||
| 138 | |||
| 139 | |||
| 140 | /** | ||
| 141 |      * @api {post} /api/v1/file/restore?id=:id Rollback version | ||
| 142 | * @apiVersion 1.0.6 | ||
| 143 | * @apiName postRestore | ||
| 144 | * @apiGroup Node\File | ||
| 145 | * @apiPermission none | ||
| 146 | * @apiDescription Rollback to a recent version from history. Use the version number from history. | ||
| 147 | * @apiUse _getNode | ||
| 148 | * | ||
| 149 | * @apiExample (cURL) example: | ||
| 150 | * curl -XPOST "https://SERVER/api/v1/file/restore?id=544627ed3c58891f058b4686&pretty&vesion=11" | ||
| 151 | * curl -XPOST "https://SERVER/api/v1/file/544627ed3c58891f058b4686/restore?pretty&version=1" | ||
| 152 | * curl -XPOST "https://SERVER/api/v1/file/restore?p=/absolute/path/to/my/file&pretty&version=3" | ||
| 153 | * | ||
| 154 |      * @apiParam (GET Parameter) {number} version The version from history to rollback to | ||
| 155 | * | ||
| 156 |      * @apiSuccessExample {json} Success-Response: | ||
| 157 | * HTTP/1.1 204 No Content | ||
| 158 | * | ||
| 159 | * @param string $id | ||
| 160 | * @param string $p | ||
| 161 | * @param string $version | ||
| 162 | * @return Response | ||
| 163 | */ | ||
| 164 | public function postRestore(int $version, ?string $id=null, ?string $p=null): Response | ||
| 169 | |||
| 170 | |||
| 171 | /** | ||
| 172 |      * @api {put} /api/v1/file/chunk Upload file chunk | ||
| 173 | * @apiVersion 1.0.6 | ||
| 174 | * @apiName putChunk | ||
| 175 | * @apiGroup Node\File | ||
| 176 | * @apiPermission none | ||
| 177 | * @apiUse _getNode | ||
| 178 | * @apuUse _conflictNode | ||
| 179 | * @apiUse _writeAction | ||
| 180 | * @apiDescription Upload a file chunk. Use this method if you have possible big files! | ||
| 181 | * You have to manually splitt the binary data into | ||
| 182 | * multiple chunks and upload them successively using this method. Once uploading the last chunk, | ||
| 183 | * the server will automatically create or update the file node. | ||
| 184 | * You may set the parent collection, name and or custom attributes only with the last request to save traffic. | ||
| 185 | * | ||
| 186 | * @apiExample (cURL) example: | ||
| 187 | * # Upload a new file myfile.jpg into the collection 544627ed3c58891f058b4686. | ||
| 188 | * 1. First splitt the file into multiple 8M (For example, you could also use a smaller or bigger size) chunks | ||
| 189 | * 2. Create a unique name for the chunkgroup (Could also be the filename), best thing is to create a UUIDv4 | ||
| 190 | * 3. Upload each chunk successively (follow the binary order of your file!) using the chunk PUT method | ||
| 191 | * (The server identifies each chunk with the index parameter, beginning with #1). | ||
| 192 | * 4. If chunk number 3 will be reached, the server automatically place all chunks to the new file node | ||
| 193 | * | ||
| 194 | * curl -XPUT "https://SERVER/api/v1/file/chunk?collection=544627ed3c58891f058b4686&name=myfile.jpg&index=1&chunks=3&chunkgroup=myuniquechunkgroup&size=12342442&pretty" --data-binary @chunk1.bin | ||
| 195 | * curl -XPUT "https://SERVER/api/v1/file/chunk?collection=544627ed3c58891f058b4686&name=myfile.jpg&index=2&chunks=3&chunkgroup=myuniquechunkgroup&size=12342442&pretty" --data-binary @chunk2.bin | ||
| 196 | * curl -XPUT "https://SERVER/api/v1/file/chunk?collection=544627ed3c58891f058b4686&name=myfile.jpg&index=3&chunks=3&chunkgroup=myuniquechunkgroup&size=12342442&pretty" --data-binary @chunk3.bin | ||
| 197 | * | ||
| 198 |      * @apiParam (GET Parameter) {string} [id] Either id, p (path) of a file node or a parent collection id must be given | ||
| 199 |      * @apiParam (GET Parameter) {string} [p] Either id, p (path) of a file node or a parent collection id must be given | ||
| 200 |      * @apiParam (GET Parameter) {string} [collection] Either id, p (path) of a file node or a parent collection id must be given | ||
| 201 | * (If none of them are given, the file will be placed to the root) | ||
| 202 |      * @apiParam (GET Parameter) {string} [name] Needs to be set if the chunk belongs to a new file | ||
| 203 |      * @apiParam (GET Parameter) {number} index Chunk ID (consider chunk order!) | ||
| 204 |      * @apiParam (GET Parameter) {number} chunks Total number of chunks | ||
| 205 |      * @apiParam (GET Parameter) {string} chunkgroup A unique name which identifes a group of chunks (One file) | ||
| 206 |      * @apiParam (GET Parameter) {number} size The total file size in bytes | ||
| 207 |      * @apiParam (GET Parameter) {object} [attributes] Overwrite some attributes which are usually generated on the server | ||
| 208 |      * @apiParam (GET Parameter) {number} [attributes.created] Set specific created timestamp (UNIX timestamp format) | ||
| 209 |      * @apiParam (GET Parameter) {number} [attributes.changed] Set specific changed timestamp (UNIX timestamp format) | ||
| 210 | * | ||
| 211 | * | ||
| 212 |      * @apiSuccess (200 OK) {number} status Status Code | ||
| 213 |      * @apiSuccess (200 OK) {number} data Increased version number if the last chunk was uploaded and existing node was updated. | ||
| 214 | * It will return the old version if the submited file content was equal to the existing one. | ||
| 215 | * | ||
| 216 |      * @apiSuccess (201 Created) {number} status Status Code | ||
| 217 |      * @apiSuccess (201 Created) {string} data Node ID if the last chunk was uploaded and a new node was added | ||
| 218 | * | ||
| 219 |      * @apiSuccess (206 Partial Content) {number} status Status Code | ||
| 220 |      * @apiSuccess (206 Partial Content) {string} data Chunk ID if it was not the last chunk | ||
| 221 |      * @apiSuccessExample {json} Success-Response (Not the last chunk yet): | ||
| 222 | * HTTP/1.1 206 Partial Content | ||
| 223 |      * { | ||
| 224 | * "status": 206, | ||
| 225 | * "data": "1" | ||
| 226 | * } | ||
| 227 | * | ||
| 228 |      * @apiSuccessExample {json} Success-Response (New file created, Last chunk): | ||
| 229 | * HTTP/1.1 201 Created | ||
| 230 |      * { | ||
| 231 | * "status": 201, | ||
| 232 | * "data": "78297329329389e332234342" | ||
| 233 | * } | ||
| 234 | * | ||
| 235 |      * @apiSuccessExample {json} Success-Response (File updated, Last chunk): | ||
| 236 | * HTTP/1.1 200 OK | ||
| 237 |      * { | ||
| 238 | * "status": 200, | ||
| 239 | * "data": 2 | ||
| 240 | * } | ||
| 241 | * | ||
| 242 |      * @apiErrorExample {json} Error-Response (quota full): | ||
| 243 | * HTTP/1.1 507 Insufficient Storage | ||
| 244 |      * { | ||
| 245 | * "status": 507 | ||
| 246 |      *      "data": { | ||
| 247 | * "error": "Balloon\Exception\InsufficientStorage", | ||
| 248 | * "message": "user quota is full", | ||
| 249 | * "code": 66 | ||
| 250 | * } | ||
| 251 | * } | ||
| 252 | * | ||
| 253 |      * @apiErrorExample {json} Error-Response (Size limit exceeded): | ||
| 254 | * HTTP/1.1 400 Bad Request | ||
| 255 |      * { | ||
| 256 | * "status": 400, | ||
| 257 |      *      "data": { | ||
| 258 | * "error": "Balloon\\Exception\\Conflict", | ||
| 259 | * "message": "file size exceeded limit", | ||
| 260 | * "code": 17 | ||
| 261 | * } | ||
| 262 | * } | ||
| 263 | * | ||
| 264 |      * @apiErrorExample {json} Error-Response (Chunks lost): | ||
| 265 | * HTTP/1.1 400 Bad Request | ||
| 266 |      * { | ||
| 267 | * "status": 400, | ||
| 268 |      *      "data": { | ||
| 269 | * "error": "Balloon\\Exception\\Conflict", | ||
| 270 | * "message": "chunks lost, reupload all chunks", | ||
| 271 | * "code": 275 | ||
| 272 | * } | ||
| 273 | * } | ||
| 274 | * | ||
| 275 |      * @apiErrorExample {json} Error-Response (Chunks invalid size): | ||
| 276 | * HTTP/1.1 400 Bad Request | ||
| 277 |      * { | ||
| 278 | * "status": 400, | ||
| 279 |      *      "data": { | ||
| 280 | * "error": "Balloon\\Exception\\Conflict", | ||
| 281 | * "message": "merged chunks temp file size is not as expected", | ||
| 282 | * "code": 276 | ||
| 283 | * } | ||
| 284 | * } | ||
| 285 | * | ||
| 286 | * @param string $id | ||
| 287 | * @param string $p | ||
| 288 | * @param string $collection | ||
| 289 | * @param string $name | ||
| 290 | * @param int $index | ||
| 291 | * @param int $chunks | ||
| 292 | * @param string $chunkgroup | ||
| 293 | * @param int $size | ||
| 294 | * @param array $attributes | ||
| 295 | * @param int $conflict | ||
| 296 | * @return Response | ||
| 297 | */ | ||
| 298 | public function putChunk( | ||
| 381 | |||
| 382 | |||
| 383 | /** | ||
| 384 |      * @api {put} /api/v1/file Upload file | ||
| 385 | * @apiVersion 1.0.6 | ||
| 386 | * @apiName put | ||
| 387 | * @apiGroup Node\File | ||
| 388 | * @apiPermission none | ||
| 389 | * @apiUse _getNode | ||
| 390 | * @apiUse _conflictNode | ||
| 391 | * @apiUse _writeAction | ||
| 392 | * | ||
| 393 | * @apiDescription Upload an entire file in one-shot. Attention, there is file size limit, | ||
| 394 | * if you have possible big files use the method PUT chunk! | ||
| 395 | * | ||
| 396 | * @apiExample (cURL) example: | ||
| 397 | * #Update content of file 544627ed3c58891f058b4686 | ||
| 398 | * curl -XPUT "https://SERVER/api/v1/file?id=544627ed3c58891f058b4686" --data-binary myfile.txt | ||
| 399 | * curl -XPUT "https://SERVER/api/v1/file/544627ed3c58891f058b4686" --data-binary myfile.txt | ||
| 400 | * | ||
| 401 | * #Upload new file under collection 544627ed3c58891f058b3333 | ||
| 402 | * curl -XPUT "https://SERVER/api/v1/file?collection=544627ed3c58891f058b3333&name=myfile.txt" --data-binary myfile.txt | ||
| 403 | * | ||
| 404 |      * @apiParam (GET Parameter) {string} [id] Either id, p (path) of a file node or a parent collection id must be given | ||
| 405 | * | ||
| 406 |      * @apiParam (GET Parameter) {string} [id] Either id, p (path) of a file node or a parent collection id must be given | ||
| 407 |      * @apiParam (GET Parameter) {string} [p] Either id, p (path) of a file node or a parent collection id must be given | ||
| 408 |      * @apiParam (GET Parameter) {string} [collection] Either id, p (path) of a file node or a parent collection id must be given | ||
| 409 | * (If none of them are given, the file will be placed to the root) | ||
| 410 |      * @apiParam (GET Parameter) {string} [name] Needs to be set if the chunk belongs to a new file | ||
| 411 | * or to identify an existing child file if a collection id was set | ||
| 412 |      * @apiParam (GET Parameter) {object} attributes Overwrite some attributes which are usually generated on the server | ||
| 413 |      * @apiParam (GET Parameter) {number} attributes.created Set specific created timestamp (UNIX timestamp format) | ||
| 414 |      * @apiParam (GET Parameter) {number} attributes.changed Set specific changed timestamp (UNIX timestamp format) | ||
| 415 | * | ||
| 416 |      * @apiSuccess (200 OK) {number} status Status Code | ||
| 417 |      * @apiSuccess (200 OK) {number} data Increased version number if an existing file was updated. It will return | ||
| 418 | * the old version if the submited file content was equal to the existing one. | ||
| 419 | * | ||
| 420 |      * @apiSuccess (201 Created) {number} status Status Code | ||
| 421 |      * @apiSuccess (201 Created) {string} data Node ID | ||
| 422 |      * @apiSuccessExample {json} Success-Response (New file created): | ||
| 423 | * HTTP/1.1 201 Created | ||
| 424 |      * { | ||
| 425 | * "status": 201, | ||
| 426 | * "data": "78297329329389e332234342" | ||
| 427 | * } | ||
| 428 | * | ||
| 429 |      * @apiSuccessExample {json} Success-Response (File updated): | ||
| 430 | * HTTP/1.1 200 OK | ||
| 431 |      * { | ||
| 432 | * "status": 200, | ||
| 433 | * "data": 2 | ||
| 434 | * } | ||
| 435 | * | ||
| 436 |      * @apiErrorExample {json} Error-Response (quota full): | ||
| 437 | * HTTP/1.1 507 Insufficient Storage | ||
| 438 |      * { | ||
| 439 | * "status": 507 | ||
| 440 |      *      "data": { | ||
| 441 | * "error": "Balloon\Exception\InsufficientStorage", | ||
| 442 | * "message": "user quota is full", | ||
| 443 | * "code": 65 | ||
| 444 | * } | ||
| 445 | * } | ||
| 446 | * | ||
| 447 |      * @apiErrorExample {json} Error-Response (Size limit exceeded): | ||
| 448 | * HTTP/1.1 400 Bad Request | ||
| 449 |      * { | ||
| 450 | * "status": 400, | ||
| 451 |      *      "data": { | ||
| 452 | * "error": "Balloon\\Exception\\Conflict", | ||
| 453 | * "message": "file size exceeded limit", | ||
| 454 | * "code": 17 | ||
| 455 | * } | ||
| 456 | * } | ||
| 457 | * | ||
| 458 | * @param string $id | ||
| 459 | * @param string $p | ||
| 460 | * @param string $collection | ||
| 461 | * @param string $name | ||
| 462 | * @param array $attributes | ||
| 463 | * @param int $conflict | ||
| 464 | * @return Response | ||
| 465 | */ | ||
| 466 | public function put( | ||
| 480 | |||
| 481 | |||
| 482 | /** | ||
| 483 | * Add or update file | ||
| 484 | * | ||
| 485 | * @param string|resource $content | ||
| 486 | * @param string $id | ||
| 487 | * @param string $p | ||
| 488 | * @param string $collection | ||
| 489 | * @param string $name | ||
| 490 | * @param array $attributes | ||
| 491 | * @param int $conflict | ||
| 492 | * @return Response | ||
| 493 | */ | ||
| 494 | protected function _put( | ||
| 567 | } | ||
| 568 | 
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: