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 | 101 | public function __construct(array $config = []) |
|
118 | |||
119 | |||
120 | /** |
||
121 | * Initialize an action instance |
||
122 | * |
||
123 | * @param array $config Configuration options passed to the constructor |
||
124 | * @return void |
||
125 | */ |
||
126 | 101 | public function initialize(array $config) |
|
130 | |||
131 | |||
132 | /** |
||
133 | * Api method for action name. |
||
134 | * |
||
135 | * @param string $name Action name. |
||
136 | * @return string |
||
137 | */ |
||
138 | 81 | public function name($name = null) |
|
147 | |||
148 | /** |
||
149 | * Api method for activated route. |
||
150 | * |
||
151 | * @param array $route Activated route. |
||
152 | * @return array |
||
153 | */ |
||
154 | 13 | public function route($route = null) |
|
163 | |||
164 | /** |
||
165 | * Set or get service. |
||
166 | * |
||
167 | * @param Service $service An Service instance. |
||
168 | * @return Service |
||
169 | */ |
||
170 | 101 | public function service($service = null) |
|
179 | |||
180 | /** |
||
181 | * Apply validation process. |
||
182 | * |
||
183 | * @return bool |
||
184 | */ |
||
185 | 47 | public function validates() |
|
189 | |||
190 | /** |
||
191 | * Execute action. |
||
192 | * |
||
193 | * @return mixed |
||
194 | */ |
||
195 | abstract public function execute(); |
||
196 | |||
197 | /** |
||
198 | * Action execution life cycle. |
||
199 | * |
||
200 | * @return mixed |
||
201 | */ |
||
202 | 59 | public function process() |
|
242 | |||
243 | /** |
||
244 | * Execute action call to the method. |
||
245 | * |
||
246 | * This method pass action params as method params. |
||
247 | * |
||
248 | * @param string $methodName Method name. |
||
249 | * @return mixed |
||
250 | * @throws Exception |
||
251 | */ |
||
252 | 1 | protected function _executeAction($methodName = 'action') |
|
279 | |||
280 | /** |
||
281 | * @return array |
||
282 | */ |
||
283 | 101 | public function implementedEvents() |
|
302 | |||
303 | /** |
||
304 | * Returns action input params |
||
305 | * |
||
306 | * @return mixed |
||
307 | */ |
||
308 | 51 | public function data() |
|
312 | |||
313 | /** |
||
314 | * Get the extension registry for this action. |
||
315 | * |
||
316 | * If called with the first parameter, it will be set as the action $this->_extensions property |
||
317 | * |
||
318 | * @param \CakeDC\Api\Service\Action\ExtensionRegistry|null $extensions Extension registry. |
||
319 | * |
||
320 | * @return \CakeDC\Api\Service\Action\ExtensionRegistry |
||
321 | */ |
||
322 | 101 | public function extensions($extensions = null) |
|
333 | |||
334 | /** |
||
335 | * Loads the defined extensions using the Extension factory. |
||
336 | * |
||
337 | * @return void |
||
338 | */ |
||
339 | 101 | protected function _loadExtensions() |
|
351 | |||
352 | /** |
||
353 | * Initialize auth. |
||
354 | * |
||
355 | * @return Auth |
||
356 | */ |
||
357 | 101 | protected function _initializeAuth() |
|
369 | |||
370 | /** |
||
371 | * Prepare Auth configuration. |
||
372 | * |
||
373 | * @return array |
||
374 | */ |
||
375 | 101 | protected function _authConfig() |
|
386 | } |
||
387 |