Complex classes like Files 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 Files, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class Files extends Nodes |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * @api {get} /api/v2/files/:id/history Get history |
||
| 25 | * @apiVersion 2.0.0 |
||
| 26 | * @apiName getHistory |
||
| 27 | * @apiGroup Node\File |
||
| 28 | * @apiPermission none |
||
| 29 | * @apiDescription Get a full change history of a file |
||
| 30 | * @apiUse _getNode |
||
| 31 | * |
||
| 32 | * @apiExample (cURL) example: |
||
| 33 | * curl -XGET "https://SERVER/api/v2/files/history?id=544627ed3c58891f058b4686&pretty" |
||
| 34 | * curl -XGET "https://SERVER/api/v2/files/544627ed3c58891f058b4686/history?pretty" |
||
| 35 | * curl -XGET "https://SERVER/api/v2/files/history?p=/absolute/path/to/my/file&pretty" |
||
| 36 | * |
||
| 37 | * @apiSuccess (200 OK) {object[]} - History |
||
| 38 | * @apiSuccess (200 OK) {number} -.version Version |
||
| 39 | * @apiSuccess (200 OK) {string} -.changed ISO806 timestamp |
||
| 40 | * @apiSuccess (200 OK) {object} -.user User object |
||
| 41 | * @apiSuccess (200 OK) {number} -.type Change type, there are five different change types including:</br> |
||
| 42 | * 0 - Initially added</br> |
||
| 43 | * 1 - Content modified</br> |
||
| 44 | * 2 - Version rollback</br> |
||
| 45 | * 3 - Deleted</br> |
||
| 46 | * 4 - Undeleted |
||
| 47 | * @apiSuccess (200 OK) {number} -.size Content size in bytes |
||
| 48 | * @apiSuccessExample {json} Success-Response: |
||
| 49 | * HTTP/1.1 200 OK |
||
| 50 | * [ |
||
| 51 | * { |
||
| 52 | * "version": 1, |
||
| 53 | * "changed": "" |
||
| 54 | * "user": { |
||
| 55 | * "id": "544627ed3c58891f058b4611", |
||
| 56 | * "name": "peter.meier" |
||
| 57 | * }, |
||
| 58 | * "type": 0, |
||
| 59 | * "size": 178, |
||
| 60 | * } |
||
| 61 | * ] |
||
| 62 | * |
||
| 63 | * @param RoleAttributeDecorator $role_decorator |
||
| 64 | * @param string $id |
||
| 65 | * @param string $p |
||
| 66 | * |
||
| 67 | * @return Response |
||
| 68 | */ |
||
| 69 | public function getHistory(RoleAttributeDecorator $role_decorator, ?string $id = null, ?string $p = null): Response |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @api {post} /api/v2/files/:id/restore Rollback version |
||
| 95 | * @apiVersion 2.0.0 |
||
| 96 | * @apiName postRestore |
||
| 97 | * @apiGroup Node\File |
||
| 98 | * @apiPermission none |
||
| 99 | * @apiDescription Rollback to a recent version from history. Use the version number from history. |
||
| 100 | * @apiUse _getNode |
||
| 101 | * |
||
| 102 | * @apiExample (cURL) example: |
||
| 103 | * curl -XPOST "https://SERVER/api/v2/files/restore?id=544627ed3c58891f058b4686&pretty&vesion=11" |
||
| 104 | * curl -XPOST "https://SERVER/api/v2/files/544627ed3c58891f058b4686/restore?pretty&version=1" |
||
| 105 | * curl -XPOST "https://SERVER/api/v2/files/restore?p=/absolute/path/to/my/file&pretty&version=3" |
||
| 106 | * |
||
| 107 | * @apiParam (GET Parameter) {number} version The version from history to rollback to |
||
| 108 | * |
||
| 109 | * @apiSuccessExample {json} Success-Response: |
||
| 110 | * HTTP/1.1 200 OK |
||
| 111 | * { |
||
| 112 | * "id": "544627ed3c58891f058b4686" |
||
| 113 | * } |
||
| 114 | * |
||
| 115 | * @param string $id |
||
| 116 | * @param string $p |
||
| 117 | * @param string $version |
||
| 118 | * |
||
| 119 | * @return Response |
||
| 120 | */ |
||
| 121 | public function postRestore(int $version, ?string $id = null, ?string $p = null): Response |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @api {put} /api/v2/files/chunk Upload file chunk |
||
| 132 | * @apiVersion 2.0.0 |
||
| 133 | * @apiName putChunk |
||
| 134 | * @apiGroup Node\File |
||
| 135 | * @apiPermission none |
||
| 136 | * @apiUse _getNode |
||
| 137 | * @apuUse _conflictNode |
||
| 138 | * @apiUse _writeAction |
||
| 139 | * @apiDescription Upload a file chunk. Use this method if you have possible big files! |
||
| 140 | * You have to manually splitt the binary data into |
||
| 141 | * multiple chunks and upload them successively using this method. Once uploading the last chunk, |
||
| 142 | * the server will automatically create or update the file node. |
||
| 143 | * You may set the parent collection, name and or custom attributes only with the last request to save traffic. |
||
| 144 | * |
||
| 145 | * @apiExample (cURL) example: |
||
| 146 | * # Upload a new file myfile.jpg into the collection 544627ed3c58891f058b4686. |
||
| 147 | * 1. First splitt the file into multiple 8M (For example, you could also use a smaller or bigger size) chunks |
||
| 148 | * 2. Create a unique name for the session (Could also be the filename), best thing is to create a UUIDv4 |
||
| 149 | * 3. Upload each chunk successively (follow the binary order of your file!) using the chunk PUT method |
||
| 150 | * (The server identifies each chunk with the index parameter, beginning with #1). |
||
| 151 | * 4. If chunk number 3 will be reached, the server automatically place all chunks to the new file node |
||
| 152 | * |
||
| 153 | * curl -XPUT "https://SERVER/api/v2/files/chunk?collection=544627ed3c58891f058b4686&name=myfile.jpg&index=1&chunks=3&session=myuniquesession&size=12342442&pretty" --data-binary @chunk1.bin |
||
| 154 | * curl -XPUT "https://SERVER/api/v2/files/chunk?collection=544627ed3c58891f058b4686&name=myfile.jpg&index=2&chunks=3&session=myuniquesession&size=12342442&pretty" --data-binary @chunk2.bin |
||
| 155 | * curl -XPUT "https://SERVER/api/v2/files/chunk?collection=544627ed3c58891f058b4686&name=myfile.jpg&index=3&chunks=3&session=myuniquesession&size=12342442&pretty" --data-binary @chunk3.bin |
||
| 156 | * |
||
| 157 | * @apiParam (GET Parameter) {string} [id] Either id, p (path) of a file node or a parent collection id must be given |
||
| 158 | * @apiParam (GET Parameter) {string} [p] Either id, p (path) of a file node or a parent collection id must be given |
||
| 159 | * @apiParam (GET Parameter) {string} [collection] Either id, p (path) of a file node or a parent collection id must be given |
||
| 160 | * (If none of them are given, the file will be placed to the root) |
||
| 161 | * @apiParam (GET Parameter) {string} [name] Needs to be set if the chunk belongs to a new file |
||
| 162 | * @apiParam (GET Parameter) {number} index Chunk ID (consider chunk order!) |
||
| 163 | * @apiParam (GET Parameter) {number} chunks Total number of chunks |
||
| 164 | * @apiParam (GET Parameter) {string} session A unique name which identifes a group of chunks (One file) |
||
| 165 | * @apiParam (GET Parameter) {number} size The total file size in bytes |
||
| 166 | * @apiParam (GET Parameter) {object} [attributes] Overwrite some attributes which are usually generated on the server |
||
| 167 | * @apiParam (GET Parameter) {number} [attributes.created] Set specific created timestamp (UNIX timestamp format) |
||
| 168 | * @apiParam (GET Parameter) {number} [attributes.changed] Set specific changed timestamp (UNIX timestamp format) |
||
| 169 | * |
||
| 170 | * |
||
| 171 | * @apiSuccess (200 OK) {number} status Status Code |
||
| 172 | * @apiSuccess (200 OK) {number} data Increased version number if the last chunk was uploaded and existing node was updated. |
||
| 173 | * It will return the old version if the submited file content was equal to the existing one. |
||
| 174 | * |
||
| 175 | * @apiSuccess (201 Created) {number} status Status Code |
||
| 176 | * @apiSuccess (201 Created) {string} data Node ID if the last chunk was uploaded and a new node was added |
||
| 177 | * |
||
| 178 | * @apiSuccess (206 Partial Content) {number} status Status Code |
||
| 179 | * @apiSuccess (206 Partial Content) {string} data Chunk ID if it was not the last chunk |
||
| 180 | * @apiSuccessExample {json} Success-Response (Not the last chunk yet): |
||
| 181 | * HTTP/1.1 206 Partial Content |
||
| 182 | * { |
||
| 183 | * "session": "78297329329389e332234341", |
||
| 184 | * "size": 12323244224, |
||
| 185 | * "chunks_left": 4 |
||
| 186 | * } |
||
| 187 | * |
||
| 188 | * @apiSuccessExample {json} Success-Response (New file created, Last chunk): |
||
| 189 | * HTTP/1.1 201 Created |
||
| 190 | * { |
||
| 191 | * "id": "78297329329389e332234342", |
||
| 192 | * "version": 1 |
||
| 193 | * } |
||
| 194 | * |
||
| 195 | * @apiSuccessExample {json} Success-Response (File updated, Last chunk): |
||
| 196 | * HTTP/1.1 200 OK |
||
| 197 | * { |
||
| 198 | * "id": "78297329329389e332234342", |
||
| 199 | * "version": 2 |
||
| 200 | * } |
||
| 201 | * |
||
| 202 | * @apiErrorExample {json} Error-Response (quota full): |
||
| 203 | * HTTP/1.1 507 Insufficient Storage |
||
| 204 | * { |
||
| 205 | * "status": 507 |
||
| 206 | * "data": { |
||
| 207 | * "error": "Balloon\Exception\InsufficientStorage", |
||
| 208 | * "message": "user quota is full", |
||
| 209 | * "code": 66 |
||
| 210 | * } |
||
| 211 | * } |
||
| 212 | * |
||
| 213 | * @apiErrorExample {json} Error-Response (Size limit exceeded): |
||
| 214 | * HTTP/1.1 400 Bad Request |
||
| 215 | * { |
||
| 216 | * "status": 400, |
||
| 217 | * "data": { |
||
| 218 | * "error": "Balloon\\Exception\\Conflict", |
||
| 219 | * "message": "file size exceeded limit", |
||
| 220 | * "code": 17 |
||
| 221 | * } |
||
| 222 | * } |
||
| 223 | * |
||
| 224 | * @apiErrorExample {json} Error-Response (Chunks lost): |
||
| 225 | * HTTP/1.1 400 Bad Request |
||
| 226 | * { |
||
| 227 | * "status": 400, |
||
| 228 | * "data": { |
||
| 229 | * "error": "Balloon\\Exception\\Conflict", |
||
| 230 | * "message": "chunks lost, reupload all chunks", |
||
| 231 | * "code": 275 |
||
| 232 | * } |
||
| 233 | * } |
||
| 234 | * |
||
| 235 | * @apiErrorExample {json} Error-Response (Chunks invalid size): |
||
| 236 | * HTTP/1.1 400 Bad Request |
||
| 237 | * { |
||
| 238 | * "status": 400, |
||
| 239 | * "data": { |
||
| 240 | * "error": "Balloon\\Exception\\Conflict", |
||
| 241 | * "message": "merged chunks temp file size is not as expected", |
||
| 242 | * "code": 276 |
||
| 243 | * } |
||
| 244 | * } |
||
| 245 | * |
||
| 246 | * @param string $id |
||
| 247 | * @param string $p |
||
| 248 | * @param string $collection |
||
| 249 | * @param string $name |
||
| 250 | * @param int $index |
||
| 251 | * @param int $chunks |
||
| 252 | * @param string $session |
||
| 253 | * @param int $size |
||
| 254 | * @param array $attributes |
||
| 255 | * @param int $conflict |
||
| 256 | * |
||
| 257 | * @return Response |
||
| 258 | */ |
||
| 259 | public function putChunk( |
||
| 353 | |||
| 354 | /** |
||
| 355 | * @api {put} /api/v2/files Upload file |
||
| 356 | * @apiVersion 2.0.0 |
||
| 357 | * @apiName put |
||
| 358 | * @apiGroup Node\File |
||
| 359 | * @apiPermission none |
||
| 360 | * @apiUse _getNode |
||
| 361 | * @apiUse _conflictNode |
||
| 362 | * @apiUse _writeAction |
||
| 363 | * |
||
| 364 | * @apiDescription Upload an entire file in one-shot. Attention, there is file size limit, |
||
| 365 | * if you have possible big files use the method PUT chunk! |
||
| 366 | * |
||
| 367 | * @apiExample (cURL) example: |
||
| 368 | * #Update content of file 544627ed3c58891f058b4686 |
||
| 369 | * curl -XPUT "https://SERVER/api/v2/files?id=544627ed3c58891f058b4686" --data-binary myfile.txt |
||
| 370 | * curl -XPUT "https://SERVER/api/v2/files/544627ed3c58891f058b4686" --data-binary myfile.txt |
||
| 371 | * |
||
| 372 | * #Upload new file under collection 544627ed3c58891f058b3333 |
||
| 373 | * curl -XPUT "https://SERVER/api/v2/files?collection=544627ed3c58891f058b3333&name=myfile.txt" --data-binary myfile.txt |
||
| 374 | * |
||
| 375 | * @apiParam (GET Parameter) {string} [id] Either id, p (path) of a file node or a parent collection id must be given |
||
| 376 | * |
||
| 377 | * @apiParam (GET Parameter) {string} [id] Either id, p (path) of a file node or a parent collection id must be given |
||
| 378 | * @apiParam (GET Parameter) {string} [p] Either id, p (path) of a file node or a parent collection id must be given |
||
| 379 | * @apiParam (GET Parameter) {string} [collection] Either id, p (path) of a file node or a parent collection id must be given |
||
| 380 | * (If none of them are given, the file will be placed to the root) |
||
| 381 | * @apiParam (GET Parameter) {string} [name] Needs to be set if the chunk belongs to a new file |
||
| 382 | * or to identify an existing child file if a collection id was set |
||
| 383 | * @apiParam (GET Parameter) {object} attributes Overwrite some attributes which are usually generated on the server |
||
| 384 | * @apiParam (GET Parameter) {number} attributes.created Set specific created timestamp (UNIX timestamp format) |
||
| 385 | * @apiParam (GET Parameter) {number} attributes.changed Set specific changed timestamp (UNIX timestamp format) |
||
| 386 | * |
||
| 387 | * @apiSuccess (200 OK) {number} status Status Code |
||
| 388 | * @apiSuccess (200 OK) {number} data Increased version number if an existing file was updated. It will return |
||
| 389 | * the old version if the submited file content was equal to the existing one. |
||
| 390 | * |
||
| 391 | * @apiSuccess (201 Created) {number} status Status Code |
||
| 392 | * @apiSuccess (201 Created) {string} data Node ID |
||
| 393 | * @apiSuccessExample {json} Success-Response (New file created): |
||
| 394 | * HTTP/1.1 201 Created |
||
| 395 | * { |
||
| 396 | * "status": 201, |
||
| 397 | * "data": "78297329329389e332234342" |
||
| 398 | * } |
||
| 399 | * |
||
| 400 | * @apiSuccessExample {json} Success-Response (File updated): |
||
| 401 | * HTTP/1.1 200 OK |
||
| 402 | * { |
||
| 403 | * "status": 200, |
||
| 404 | * "data": 2 |
||
| 405 | * } |
||
| 406 | * |
||
| 407 | * @apiErrorExample {json} Error-Response (quota full): |
||
| 408 | * HTTP/1.1 507 Insufficient Storage |
||
| 409 | * { |
||
| 410 | * "status": 507 |
||
| 411 | * "data": { |
||
| 412 | * "error": "Balloon\Exception\InsufficientStorage", |
||
| 413 | * "message": "user quota is full", |
||
| 414 | * "code": 65 |
||
| 415 | * } |
||
| 416 | * } |
||
| 417 | * |
||
| 418 | * @apiErrorExample {json} Error-Response (Size limit exceeded): |
||
| 419 | * HTTP/1.1 400 Bad Request |
||
| 420 | * { |
||
| 421 | * "status": 400, |
||
| 422 | * "data": { |
||
| 423 | * "error": "Balloon\\Exception\\Conflict", |
||
| 424 | * "message": "file size exceeded limit", |
||
| 425 | * "code": 17 |
||
| 426 | * } |
||
| 427 | * } |
||
| 428 | * |
||
| 429 | * @param string $id |
||
| 430 | * @param string $p |
||
| 431 | * @param string $collection |
||
| 432 | * @param string $name |
||
| 433 | * @param array $attributes |
||
| 434 | * @param int $conflict |
||
| 435 | * |
||
| 436 | * @return Response |
||
| 437 | */ |
||
| 438 | public function put( |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Add or update file. |
||
| 456 | * |
||
| 457 | * @param resource|string $content |
||
| 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 | * |
||
| 465 | * @return Response |
||
| 466 | */ |
||
| 467 | protected function _put( |
||
| 551 | } |
||
| 552 |
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: