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() |
|
| 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() |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Method PUT |
||
| 207 | * |
||
| 208 | * @return array|false |
||
| 209 | * @throws BadRequestException |
||
| 210 | */ |
||
| 211 | 6 | public function methodPut() |
|
| 237 | |||
| 238 | /** |
||
| 239 | * Method DELETE |
||
| 240 | * |
||
| 241 | * @return false |
||
| 242 | * @throws BadRequestException |
||
| 243 | */ |
||
| 244 | 4 | public function methodDelete() |
|
| 262 | |||
| 263 | /** |
||
| 264 | * Method OPTIONS |
||
| 265 | * |
||
| 266 | * @return false |
||
| 267 | */ |
||
| 268 | 2 | public function methodOptions() |
|
| 274 | |||
| 275 | /** |
||
| 276 | * Get allowed methods by CRUD |
||
| 277 | * |
||
| 278 | * @param bool $primary |
||
| 279 | * @return array |
||
| 280 | */ |
||
| 281 | 2 | protected function getMethods($primary = false) |
|
| 316 | } |
||
| 317 |