Complex classes like Action 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 Action, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | abstract class Action implements EventListenerInterface, EventDispatcherInterface |
||
| 34 | { |
||
| 35 | use EventDispatcherTrait; |
||
| 36 | use InstanceConfigTrait; |
||
| 37 | use ValidatorAwareTrait; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Extensions to load and attach to listener |
||
| 41 | * |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | public $extensions = []; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * An Auth instance. |
||
| 48 | * |
||
| 49 | * @var Auth |
||
| 50 | */ |
||
| 51 | public $Auth; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Default Action options. |
||
| 55 | * |
||
| 56 | * @var array |
||
| 57 | */ |
||
| 58 | protected $_defaultConfig = []; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * A Service reference. |
||
| 62 | * |
||
| 63 | * @var Service |
||
| 64 | */ |
||
| 65 | protected $_service; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Activated route. |
||
| 69 | * |
||
| 70 | * @var array |
||
| 71 | */ |
||
| 72 | protected $_route = null; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Extension registry. |
||
| 76 | * |
||
| 77 | * @var \CakeDC\Api\Service\Action\ExtensionRegistry |
||
| 78 | */ |
||
| 79 | protected $_extensions; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Action name. |
||
| 83 | * |
||
| 84 | * @var string |
||
| 85 | */ |
||
| 86 | protected $_name; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Action constructor. |
||
| 90 | * |
||
| 91 | * @param array $config Configuration options passed to the constructor |
||
| 92 | */ |
||
| 93 | 123 | public function __construct(array $config = []) |
|
| 118 | |||
| 119 | /** |
||
| 120 | * Initialize an action instance |
||
| 121 | * |
||
| 122 | * @param array $config Configuration options passed to the constructor |
||
| 123 | * @return void |
||
| 124 | */ |
||
| 125 | 123 | public function initialize(array $config) |
|
| 129 | |||
| 130 | /** |
||
| 131 | * Gets an action name. |
||
| 132 | * |
||
| 133 | * @return string |
||
| 134 | */ |
||
| 135 | 79 | public function getName() |
|
| 139 | |||
| 140 | /** |
||
| 141 | * Sets an action name. |
||
| 142 | * |
||
| 143 | * @param string $name An action name. |
||
| 144 | * @return $this |
||
| 145 | */ |
||
| 146 | 81 | public function setName($name) |
|
| 152 | |||
| 153 | /** |
||
| 154 | * Get and set service name. |
||
| 155 | * |
||
| 156 | * @param string $name Action name. |
||
| 157 | * @deprecated 3.4.0 Use setName()/getName() instead. |
||
| 158 | * @return string |
||
| 159 | */ |
||
| 160 | public function name($name = null) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Returns activated route. |
||
| 171 | * |
||
| 172 | * @return array |
||
| 173 | */ |
||
| 174 | 13 | public function getRoute() |
|
| 178 | |||
| 179 | /** |
||
| 180 | * Sets activated route. |
||
| 181 | * |
||
| 182 | * @param array $route Route config. |
||
| 183 | * @return $this |
||
| 184 | */ |
||
| 185 | 64 | public function setRoute(array $route) |
|
| 191 | |||
| 192 | /** |
||
| 193 | * Api method for activated route. |
||
| 194 | * |
||
| 195 | * @param array $route Activated route. |
||
| 196 | * @return array |
||
| 197 | */ |
||
| 198 | 8 | public function route($route = null) |
|
| 206 | |||
| 207 | /** |
||
| 208 | * @return Service |
||
| 209 | */ |
||
| 210 | 9 | public function getService() |
|
| 214 | |||
| 215 | /** |
||
| 216 | * Set a service |
||
| 217 | * |
||
| 218 | * @param Service $service service |
||
| 219 | */ |
||
| 220 | 123 | public function setService(Service $service) |
|
| 224 | |||
| 225 | /** |
||
| 226 | * Set or get service. |
||
| 227 | * |
||
| 228 | * @param Service $service An Service instance. |
||
| 229 | * @return Service |
||
| 230 | */ |
||
| 231 | 28 | public function service($service = null) |
|
| 239 | |||
| 240 | /** |
||
| 241 | * Apply validation process. |
||
| 242 | * |
||
| 243 | * @return bool |
||
| 244 | */ |
||
| 245 | 47 | public function validates() |
|
| 249 | |||
| 250 | /** |
||
| 251 | * Execute action. |
||
| 252 | * |
||
| 253 | * @return mixed |
||
| 254 | */ |
||
| 255 | abstract public function execute(); |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Action execution life cycle. |
||
| 259 | * |
||
| 260 | * @return mixed |
||
| 261 | */ |
||
| 262 | 60 | public function process() |
|
| 304 | |||
| 305 | /** |
||
| 306 | * Execute action call to the method. |
||
| 307 | * |
||
| 308 | * This method pass action params as method params. |
||
| 309 | * |
||
| 310 | * @param string $methodName Method name. |
||
| 311 | * @return mixed |
||
| 312 | * @throws Exception |
||
| 313 | */ |
||
| 314 | 1 | protected function _executeAction($methodName = 'action') |
|
| 341 | |||
| 342 | /** |
||
| 343 | * @return array |
||
| 344 | */ |
||
| 345 | 123 | public function implementedEvents() |
|
| 364 | |||
| 365 | /** |
||
| 366 | * Returns action input params |
||
| 367 | * |
||
| 368 | * @return mixed |
||
| 369 | */ |
||
| 370 | 66 | public function data() |
|
| 374 | |||
| 375 | /** |
||
| 376 | * Get the extension registry for this action. |
||
| 377 | * |
||
| 378 | * If called with the first parameter, it will be set as the action $this->_extensions property |
||
| 379 | * |
||
| 380 | * @param \CakeDC\Api\Service\Action\ExtensionRegistry|null $extensions Extension registry. |
||
| 381 | * |
||
| 382 | * @return \CakeDC\Api\Service\Action\ExtensionRegistry |
||
| 383 | */ |
||
| 384 | 123 | public function extensions($extensions = null) |
|
| 395 | |||
| 396 | /** |
||
| 397 | * Loads the defined extensions using the Extension factory. |
||
| 398 | * |
||
| 399 | * @return void |
||
| 400 | */ |
||
| 401 | 123 | protected function _loadExtensions() |
|
| 413 | |||
| 414 | /** |
||
| 415 | * Initialize auth. |
||
| 416 | * |
||
| 417 | * @return Auth |
||
| 418 | */ |
||
| 419 | 123 | protected function _initializeAuth() |
|
| 431 | |||
| 432 | /** |
||
| 433 | * Prepare Auth configuration. |
||
| 434 | * |
||
| 435 | * @return array |
||
| 436 | */ |
||
| 437 | 123 | protected function _authConfig() |
|
| 448 | } |
||
| 449 |