Complex classes like Controller 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 Controller, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | abstract class Controller |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * Filesystem. |
||
| 30 | * |
||
| 31 | * @var Filesystem |
||
| 32 | */ |
||
| 33 | protected $fs; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @apiDefine _getNode |
||
| 37 | * |
||
| 38 | * @apiParam (GET Parameter) {string} id Either id or p (path) of a node must be given. |
||
| 39 | * @apiParam (GET Parameter) {string} p Either id or p (path) of a node must be given. |
||
| 40 | * @apiError (General Error Response) {number} status Status Code |
||
| 41 | * @apiError (General Error Response) {object[]} data Error body |
||
| 42 | * @apiError (General Error Response) {string} data.error Exception |
||
| 43 | * @apiError (General Error Response) {string} data.message Message |
||
| 44 | * @apiError (General Error Response) {number} data.code Error code |
||
| 45 | * |
||
| 46 | * @apiErrorExample {json} Error-Response (Invalid Parameter): |
||
| 47 | * HTTP/1.1 400 Bad Request |
||
| 48 | * { |
||
| 49 | * "status": 400, |
||
| 50 | * "data": { |
||
| 51 | * "error": "Balloon\\Exception\\InvalidArgument", |
||
| 52 | * "message": "invalid node id specified", |
||
| 53 | * "code": 0 |
||
| 54 | * } |
||
| 55 | * } |
||
| 56 | * |
||
| 57 | * @apiErrorExample {json} Error-Response (Insufficient Access): |
||
| 58 | * HTTP/1.1 403 Forbidden |
||
| 59 | * { |
||
| 60 | * "status": 403, |
||
| 61 | * "data": { |
||
| 62 | * "error": "Balloon\\Exception\\Forbidden", |
||
| 63 | * "message": "not allowed to read node 51354d073c58891f058b4580", |
||
| 64 | * "code": 40 |
||
| 65 | * } |
||
| 66 | * } |
||
| 67 | * |
||
| 68 | * @apiErrorExample {json} Error-Response (Not found): |
||
| 69 | * HTTP/1.1 404 Not Found |
||
| 70 | * { |
||
| 71 | * "status": 404, |
||
| 72 | * "data": { |
||
| 73 | * "error": "Balloon\\Exception\\NotFound", |
||
| 74 | * "message": "node 51354d073c58891f058b4580 not found", |
||
| 75 | * "code": 49 |
||
| 76 | * } |
||
| 77 | * } |
||
| 78 | */ |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @apiDefine _multiError |
||
| 82 | * |
||
| 83 | * @apiErrorExample {json} Error-Response (Multi node error): |
||
| 84 | * HTTP/1.1 400 Bad Request |
||
| 85 | * { |
||
| 86 | * "status": 400, |
||
| 87 | * "data": [ |
||
| 88 | * { |
||
| 89 | * id: "51354d073c58891f058b4580", |
||
| 90 | * name: "file.zip", |
||
| 91 | * error: "Balloon\\Exception\\Conflict", |
||
| 92 | * message: "node already exists", |
||
| 93 | * code: 30 |
||
| 94 | * } |
||
| 95 | * ] |
||
| 96 | * } |
||
| 97 | */ |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @apiDefine _writeAction |
||
| 101 | * |
||
| 102 | * @apiErrorExample {json} Error-Response (Conflict): |
||
| 103 | * HTTP/1.1 400 Bad Request |
||
| 104 | * { |
||
| 105 | * "status": 400, |
||
| 106 | * "data": { |
||
| 107 | * "error": "Balloon\\Exception\\Conflict", |
||
| 108 | * "message": "a node called myname does already exists", |
||
| 109 | * "code": 17 |
||
| 110 | * } |
||
| 111 | * } |
||
| 112 | */ |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @apiDefine _conflictNode |
||
| 116 | * @apiParam (GET Parameter) {number} [conflict=0] Decides how to handle a conflict if a node with the same name already exists at the destination. |
||
| 117 | * Possible values are:</br> |
||
| 118 | * - 0 No action</br> |
||
| 119 | * - 1 Automatically rename the node</br> |
||
| 120 | * - 2 Overwrite the destination (merge)</br> |
||
| 121 | */ |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @apiDefine _getNodes |
||
| 125 | * |
||
| 126 | * @apiParam (GET Parameter) {string[]} id Either a single id as string or multiple as an array or a single p (path) as string or multiple paths as array must be given. |
||
| 127 | * @apiParam (GET Parameter) {string[]} p Either a single id as string or multiple as an array or a single p (path) as string or multiple paths as array must be given. |
||
| 128 | * @apiError (General Error Response) {number} status Status Code |
||
| 129 | * @apiError (General Error Response) {object[]} data Error body |
||
| 130 | * @apiError (General Error Response) {string} data.error Exception |
||
| 131 | * @apiError (General Error Response) {string} data.message Message |
||
| 132 | * @apiError (General Error Response) {number} data.code General error messages of type Balloon\\Exception do not usually have an error code |
||
| 133 | * |
||
| 134 | * @apiErrorExample {json} Error-Response (Invalid Parameter): |
||
| 135 | * HTTP/1.1 400 Bad Request |
||
| 136 | * { |
||
| 137 | * "status": 400, |
||
| 138 | * "data": { |
||
| 139 | * "error": "Balloon\\Exception\\InvalidArgument", |
||
| 140 | * "message": "invalid node id specified", |
||
| 141 | * "code": 0 |
||
| 142 | * } |
||
| 143 | * } |
||
| 144 | * |
||
| 145 | * @apiErrorExample {json} Error-Response (Insufficient Access): |
||
| 146 | * HTTP/1.1 403 Forbidden |
||
| 147 | * { |
||
| 148 | * "status": 403, |
||
| 149 | * "data": { |
||
| 150 | * "error": "Balloon\\Exception\\Forbidden", |
||
| 151 | * "message": "not allowed to read node 51354d073c58891f058b4580", |
||
| 152 | * "code": 40 |
||
| 153 | * } |
||
| 154 | * } |
||
| 155 | * |
||
| 156 | * @apiErrorExample {json} Error-Response (Not found): |
||
| 157 | * HTTP/1.1 404 Not Found |
||
| 158 | * { |
||
| 159 | * "status": 404, |
||
| 160 | * "data": { |
||
| 161 | * "error": "Balloon\\Exception\\NotFound", |
||
| 162 | * "message": "node 51354d073c58891f058b4580 not found", |
||
| 163 | * "code": 49 |
||
| 164 | * } |
||
| 165 | * } |
||
| 166 | */ |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Load node with path. |
||
| 170 | */ |
||
| 171 | public function findNodeByPath(string $path = '', ?string $class = null): NodeInterface |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Load nodes by id. |
||
| 219 | */ |
||
| 220 | public function findNodesByPath(array $path = [], ?string $class = null): Generator |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Load nodes by id. |
||
| 250 | * |
||
| 251 | * @param null|mixed $class |
||
| 252 | */ |
||
| 253 | public function getNodes(?array $id = null, ?array $path = null, $class = null, int $deleted = NodeInterface::DELETED_EXCLUDE): Generator |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Load node. |
||
| 279 | * |
||
| 280 | * @param null|mixed $id |
||
| 281 | * @param null|mixed $path |
||
| 282 | * @param null|mixed $class |
||
| 283 | */ |
||
| 284 | public function getNode($id = null, $path = null, $class = null, bool $multiple = false, bool $allow_root = false, ?int $deleted = null): NodeInterface |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Do bulk operations. |
||
| 318 | * |
||
| 319 | * @param array|string $id |
||
| 320 | * @param array|string $p |
||
| 321 | */ |
||
| 322 | protected function bulk($id, $p, Closure $action): Response |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Get node. |
||
| 356 | * |
||
| 357 | * @param string $id |
||
| 358 | * @param string $path |
||
| 359 | * @param string $class Force set node type |
||
| 360 | * @param bool $multiple Allow $id to be an array |
||
| 361 | * @param bool $allow_root Allow instance of root collection |
||
| 362 | * @param bool $deleted How to handle deleted node |
||
| 363 | */ |
||
| 364 | protected function _getNode( |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Get nodes. |
||
| 390 | * |
||
| 391 | * @param string $id |
||
| 392 | * @param string $path |
||
| 393 | * @param string $class Force set node type |
||
| 394 | * @param bool $deleted How to handle deleted node |
||
| 395 | */ |
||
| 396 | protected function _getNodes( |
||
| 417 | } |
||
| 418 |
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: