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) |
||
173 | |||
174 | /** |
||
175 | * Returns activated route. |
||
176 | * |
||
177 | * @return array |
||
178 | */ |
||
179 | 13 | public function getRoute() |
|
183 | |||
184 | /** |
||
185 | * Sets activated route. |
||
186 | * |
||
187 | * @param array $route Route config. |
||
188 | * @return $this |
||
189 | */ |
||
190 | 64 | public function setRoute(array $route) |
|
196 | |||
197 | /** |
||
198 | * Api method for activated route. |
||
199 | * |
||
200 | * @param array $route Activated route. |
||
201 | * @deprecated 3.4.0 Use setRoute()/getRoute() instead. |
||
202 | * @return array |
||
203 | */ |
||
204 | public function route($route = null) |
||
217 | |||
218 | /** |
||
219 | * @return Service |
||
220 | */ |
||
221 | 9 | public function getService() |
|
225 | |||
226 | /** |
||
227 | * Set a service |
||
228 | * |
||
229 | * @param Service $service service |
||
230 | */ |
||
231 | 123 | public function setService(Service $service) |
|
235 | |||
236 | /** |
||
237 | * Set or get service. |
||
238 | * |
||
239 | * @param Service $service An Service instance. |
||
240 | * @return Service |
||
241 | * @deprecated 3.4.0 Use setService()/getService() instead. |
||
242 | */ |
||
243 | public function service($service = null) |
||
256 | |||
257 | /** |
||
258 | * Apply validation process. |
||
259 | * |
||
260 | * @return bool |
||
261 | */ |
||
262 | 47 | public function validates() |
|
266 | |||
267 | /** |
||
268 | * Execute action. |
||
269 | * |
||
270 | * @return mixed |
||
271 | */ |
||
272 | abstract public function execute(); |
||
273 | |||
274 | /** |
||
275 | * Action execution life cycle. |
||
276 | * |
||
277 | * @return mixed |
||
278 | */ |
||
279 | 60 | public function process() |
|
321 | |||
322 | /** |
||
323 | * Execute action call to the method. |
||
324 | * |
||
325 | * This method pass action params as method params. |
||
326 | * |
||
327 | * @param string $methodName Method name. |
||
328 | * @return mixed |
||
329 | * @throws Exception |
||
330 | */ |
||
331 | 1 | protected function _executeAction($methodName = 'action') |
|
358 | |||
359 | /** |
||
360 | * @return array |
||
361 | */ |
||
362 | 123 | public function implementedEvents() |
|
381 | |||
382 | /** |
||
383 | * Returns action input params |
||
384 | * |
||
385 | * @return mixed |
||
386 | */ |
||
387 | 66 | public function getData() |
|
391 | |||
392 | /** |
||
393 | * Returns action input params |
||
394 | * |
||
395 | * @return mixed |
||
396 | * @deprecated 3.6.0 Use getData() instead. |
||
397 | */ |
||
398 | public function data() |
||
407 | |||
408 | /** |
||
409 | * @return \CakeDC\Api\Service\Action\ExtensionRegistry |
||
410 | */ |
||
411 | 49 | public function getExtensions() |
|
415 | |||
416 | /** |
||
417 | * Set a service |
||
418 | * |
||
419 | * @param \CakeDC\Api\Service\Action\ExtensionRegistry|null $extensions Extension registry. |
||
420 | */ |
||
421 | 123 | public function setExtensions($extensions = null) |
|
431 | |||
432 | /** |
||
433 | * Get the extension registry for this action. |
||
434 | * |
||
435 | * If called with the first parameter, it will be set as the action $this->_extensions property |
||
436 | * |
||
437 | * @param \CakeDC\Api\Service\Action\ExtensionRegistry|null $extensions Extension registry. |
||
438 | * |
||
439 | * @return \CakeDC\Api\Service\Action\ExtensionRegistry |
||
440 | * @deprecated 3.6.0 Use setExtensions()/getExtensions() instead. |
||
441 | */ |
||
442 | public function extensions($extensions = null) |
||
459 | |||
460 | /** |
||
461 | * Loads the defined extensions using the Extension factory. |
||
462 | * |
||
463 | * @return void |
||
464 | */ |
||
465 | 123 | protected function _loadExtensions() |
|
477 | |||
478 | /** |
||
479 | * Initialize auth. |
||
480 | * |
||
481 | * @return Auth |
||
482 | */ |
||
483 | 123 | protected function _initializeAuth() |
|
495 | |||
496 | /** |
||
497 | * Prepare Auth configuration. |
||
498 | * |
||
499 | * @return array |
||
500 | */ |
||
501 | 123 | protected function _authConfig() |
|
512 | } |
||
513 |
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.