Complex classes like Rest 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 Rest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class Rest extends AbstractController |
||
| 30 | { |
||
| 31 | /** |
||
| 32 | * @var string relation list |
||
| 33 | */ |
||
| 34 | protected $relation; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var string relation Id |
||
| 38 | */ |
||
| 39 | protected $relationId; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var array params of query |
||
| 43 | */ |
||
| 44 | protected $params = array(); |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var array query data |
||
| 48 | */ |
||
| 49 | protected $data = array(); |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Prepare request for processing |
||
| 53 | */ |
||
| 54 | 25 | public function __construct() |
|
| 80 | |||
| 81 | /** |
||
| 82 | * {@inheritdoc} |
||
| 83 | * |
||
| 84 | * Everyone method can return: |
||
| 85 | * 401 Unauthorized - if authorization is required |
||
| 86 | * 403 Forbidden - if user don't have permissions |
||
| 87 | * 501 Not Implemented - if something not exists |
||
| 88 | * |
||
| 89 | * Methods can return: |
||
| 90 | * HEAD /module/rest/ -> 200 // return overview of collection |
||
| 91 | * HEAD /module/rest/id -> 200 // return overview of item |
||
| 92 | * -> 404 // not found |
||
| 93 | * GET /module/rest/ -> 200 // return collection or |
||
| 94 | * -> 206 // return part of collection |
||
| 95 | * GET /module/rest/id -> 200 // return one item or |
||
| 96 | * -> 404 // not found |
||
| 97 | * POST /module/rest/ -> 201 // item created or |
||
| 98 | * -> 400 // bad request, validation error |
||
| 99 | * POST /module/rest/id -> 501 // error, not used in REST |
||
| 100 | * PATCH /module/rest/ |
||
| 101 | * PUT /module/rest/ -> 200 // all items was updated or |
||
| 102 | * -> 207 // multi-status ? |
||
| 103 | * PATCH /module/rest/id |
||
| 104 | * PUT /module/rest/id -> 200 // item was updated or |
||
| 105 | * -> 304 // item not modified or |
||
| 106 | * -> 400 // bad request, validation error or |
||
| 107 | * -> 404 // not found |
||
| 108 | * DELETE /module/rest/ -> 204 // all items was deleted or |
||
| 109 | * -> 207 // multi-status ? |
||
| 110 | * DELETE /module/rest/id -> 204 // item was deleted |
||
| 111 | * -> 404 // not found |
||
| 112 | * |
||
| 113 | * @return mixed |
||
| 114 | * @throws NotImplementedException |
||
| 115 | * @throws NotFoundException |
||
| 116 | * @throws BadRequestException |
||
| 117 | */ |
||
| 118 | 25 | public function __invoke() |
|
| 119 | { |
||
| 120 | 25 | switch ($this->method) { |
|
| 121 | 25 | case Request::METHOD_HEAD: |
|
| 122 | 25 | case Request::METHOD_GET: |
|
| 123 | 8 | return $this->methodGet(); |
|
| 124 | // break |
||
| 125 | 17 | case Request::METHOD_POST: |
|
| 126 | 4 | return $this->methodPost(); |
|
| 127 | // break |
||
| 128 | 13 | case Request::METHOD_PATCH: |
|
| 129 | 13 | case Request::METHOD_PUT: |
|
| 130 | 6 | return $this->methodPut(); |
|
| 131 | // break |
||
| 132 | 7 | case Request::METHOD_DELETE: |
|
| 133 | 4 | return $this->methodDelete(); |
|
| 134 | // break |
||
| 135 | 3 | case Request::METHOD_OPTIONS: |
|
| 136 | 2 | return $this->methodOptions(); |
|
| 137 | // break |
||
| 138 | 1 | default: |
|
| 139 | 1 | throw new NotImplementedException(); |
|
| 140 | 1 | } |
|
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Method HEAD and GET |
||
| 145 | * |
||
| 146 | * @return mixed |
||
| 147 | */ |
||
| 148 | 8 | public function methodGet() |
|
| 167 | |||
| 168 | /** |
||
| 169 | * Method POST |
||
| 170 | * |
||
| 171 | * @return array|false |
||
| 172 | * @throws BadRequestException |
||
| 173 | * @throws NotImplementedException |
||
| 174 | */ |
||
| 175 | 4 | public function methodPost() |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Method PUT |
||
| 208 | * |
||
| 209 | * @return array|false |
||
| 210 | * @throws BadRequestException |
||
| 211 | */ |
||
| 212 | 6 | public function methodPut() |
|
| 238 | |||
| 239 | /** |
||
| 240 | * Method DELETE |
||
| 241 | * |
||
| 242 | * @return false |
||
| 243 | * @throws BadRequestException |
||
| 244 | */ |
||
| 245 | 4 | public function methodDelete() |
|
| 263 | |||
| 264 | /** |
||
| 265 | * Method OPTIONS |
||
| 266 | * |
||
| 267 | * @return false |
||
| 268 | */ |
||
| 269 | 2 | public function methodOptions() |
|
| 275 | |||
| 276 | /** |
||
| 277 | * Get allowed methods by CRUD |
||
| 278 | * |
||
| 279 | * @param bool $primary |
||
| 280 | * @return array |
||
| 281 | */ |
||
| 282 | 2 | protected function getMethods($primary = false) |
|
| 317 | } |
||
| 318 |