Complex classes like ApiRoute 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 ApiRoute, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class ApiRoute extends ApiRouteSpec implements IRouter |
||
| 25 | { |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var callable[] |
||
| 29 | */ |
||
| 30 | public $onMatch; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | private $presenter; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var array |
||
| 39 | */ |
||
| 40 | private $actions = [ |
||
| 41 | 'POST' => false, |
||
| 42 | 'GET' => false, |
||
| 43 | 'PUT' => false, |
||
| 44 | 'DELETE' => false, |
||
| 45 | 'OPTIONS' => false, |
||
| 46 | 'PATCH' => false, |
||
| 47 | ]; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var array |
||
| 51 | */ |
||
| 52 | private $default_actions = [ |
||
| 53 | 'POST' => 'create', |
||
| 54 | 'GET' => 'read', |
||
| 55 | 'PUT' => 'update', |
||
| 56 | 'DELETE' => 'delete', |
||
| 57 | 'OPTIONS' => 'options', |
||
| 58 | 'PATCH' => 'patch', |
||
| 59 | ]; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | private $formats = [ |
||
| 65 | 'json' => 'application/json', |
||
| 66 | 'xml' => 'application/xml', |
||
| 67 | ]; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var array |
||
| 71 | */ |
||
| 72 | private $placeholder_order = []; |
||
| 73 | |||
| 74 | |||
| 75 | /** |
||
| 76 | * @param mixed $data |
||
| 77 | * @param string $presenter |
||
| 78 | * @param array $data |
||
| 79 | */ |
||
| 80 | public function __construct($path, $presenter = null, array $data = []) |
||
| 118 | |||
| 119 | |||
| 120 | /** |
||
| 121 | * @param string $presenter |
||
| 122 | * @return void |
||
| 123 | */ |
||
| 124 | public function setPresenter($presenter) |
||
| 128 | |||
| 129 | |||
| 130 | /** |
||
| 131 | * @return string |
||
| 132 | */ |
||
| 133 | public function getPresenter() |
||
| 137 | |||
| 138 | |||
| 139 | /** |
||
| 140 | * @param string $action |
||
| 141 | * @param string $method |
||
| 142 | * @return void |
||
| 143 | */ |
||
| 144 | public function setAction($action, $method = null) |
||
| 156 | |||
| 157 | |||
| 158 | /** |
||
| 159 | * @param string $string |
||
| 160 | * @return string |
||
| 161 | */ |
||
| 162 | private function prepareForMatch($string) |
||
| 166 | |||
| 167 | |||
| 168 | /** |
||
| 169 | * Get all parameters from url mask |
||
| 170 | * @return array |
||
| 171 | */ |
||
| 172 | public function getPlacehodlerParameters() |
||
| 186 | |||
| 187 | |||
| 188 | /** |
||
| 189 | * Get required parameters from url mask |
||
| 190 | * @return array |
||
| 191 | */ |
||
| 192 | public function getRequiredParams() |
||
| 209 | |||
| 210 | |||
| 211 | /** |
||
| 212 | * @param Nette\Http\IRequest $httpRequest |
||
| 213 | * @return void |
||
| 214 | */ |
||
| 215 | public function resolveFormat(Nette\Http\IRequest $httpRequest) |
||
| 233 | |||
| 234 | |||
| 235 | /** |
||
| 236 | * @return string |
||
| 237 | */ |
||
| 238 | public function getFormatFull() |
||
| 242 | |||
| 243 | |||
| 244 | /** |
||
| 245 | * @param array $methods |
||
| 246 | */ |
||
| 247 | public function setMethods(array $methods) |
||
| 261 | |||
| 262 | |||
| 263 | /** |
||
| 264 | * @return array |
||
| 265 | */ |
||
| 266 | public function getMethods() |
||
| 270 | |||
| 271 | |||
| 272 | /** |
||
| 273 | * @param Nette\Http\IRequest $request |
||
| 274 | * @return string |
||
| 275 | */ |
||
| 276 | public function resolveMethod(Nette\Http\IRequest $request) |
||
| 290 | |||
| 291 | |||
| 292 | /******************************************************************************** |
||
| 293 | * Interface IRouter * |
||
| 294 | ********************************************************************************/ |
||
| 295 | |||
| 296 | |||
| 297 | /** |
||
| 298 | * Maps HTTP request to a Request object. |
||
| 299 | * @return Request|NULL |
||
| 300 | */ |
||
| 301 | public function match(Nette\Http\IRequest $httpRequest) |
||
| 409 | |||
| 410 | |||
| 411 | /** |
||
| 412 | * Constructs absolute URL from Request object. |
||
| 413 | * @return string|NULL |
||
| 414 | */ |
||
| 415 | public function constructUrl(Request $request, Nette\Http\Url $url) |
||
| 461 | } |
||
| 462 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.