Complex classes like Service 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 Service, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
38 | abstract class Service implements EventListenerInterface, EventDispatcherInterface |
||
39 | { |
||
40 | use EventDispatcherTrait; |
||
41 | |||
42 | /** |
||
43 | * Extensions to load and attach to listener |
||
44 | * |
||
45 | * @var array |
||
46 | */ |
||
47 | public $extensions = []; |
||
48 | |||
49 | /** |
||
50 | * Actions routes description map, indexed by action name. |
||
51 | * |
||
52 | * @var array |
||
53 | */ |
||
54 | protected $_actions = []; |
||
55 | |||
56 | /** |
||
57 | * Actions classes map, indexed by action name. |
||
58 | * |
||
59 | * @var array |
||
60 | */ |
||
61 | protected $_actionsClassMap = []; |
||
62 | |||
63 | /** |
||
64 | * Service url acceptable extensions list. |
||
65 | * |
||
66 | * @var array |
||
67 | */ |
||
68 | protected $_routeExtensions = ['json']; |
||
69 | |||
70 | /** |
||
71 | * |
||
72 | * |
||
73 | * @var string |
||
74 | */ |
||
75 | protected $_routePrefix = ''; |
||
76 | |||
77 | /** |
||
78 | * Service name |
||
79 | * |
||
80 | * @var string |
||
81 | */ |
||
82 | protected $_name = null; |
||
83 | |||
84 | /** |
||
85 | * Service version. |
||
86 | * |
||
87 | * @var int |
||
88 | */ |
||
89 | protected $_version; |
||
90 | |||
91 | /** |
||
92 | * Parser class to process the HTTP request. |
||
93 | * |
||
94 | * @var BaseParser |
||
95 | */ |
||
96 | protected $_parser; |
||
97 | |||
98 | /** |
||
99 | * Renderer class to build the HTTP response. |
||
100 | * |
||
101 | * @var BaseRenderer |
||
102 | */ |
||
103 | protected $_renderer; |
||
104 | |||
105 | /** |
||
106 | * The parser class. |
||
107 | * |
||
108 | * @var string |
||
109 | */ |
||
110 | protected $_parserClass = null; |
||
111 | |||
112 | /** |
||
113 | * The Renderer class. |
||
114 | * |
||
115 | * @var string |
||
116 | */ |
||
117 | protected $_rendererClass = null; |
||
118 | |||
119 | /** |
||
120 | * Dependent services names list |
||
121 | * |
||
122 | * @var array<string> |
||
123 | */ |
||
124 | protected $_innerServices = []; |
||
125 | |||
126 | /** |
||
127 | * Parent service instance. |
||
128 | * |
||
129 | * @var Service |
||
130 | */ |
||
131 | protected $_parentService; |
||
132 | |||
133 | /** |
||
134 | * Service Action Result object. |
||
135 | * |
||
136 | * @var Result |
||
137 | */ |
||
138 | protected $_result; |
||
139 | |||
140 | /** |
||
141 | * Base url for service. |
||
142 | * |
||
143 | * @var string |
||
144 | */ |
||
145 | protected $_baseUrl; |
||
146 | |||
147 | /** |
||
148 | * Request |
||
149 | * |
||
150 | * @var \Cake\Network\Request |
||
151 | */ |
||
152 | protected $_request; |
||
153 | |||
154 | /** |
||
155 | * Request |
||
156 | * |
||
157 | * @var \Cake\Network\Response |
||
158 | */ |
||
159 | protected $_response; |
||
160 | 118 | ||
161 | /** |
||
162 | 118 | * @var string |
|
163 | 118 | */ |
|
164 | 118 | protected $_corsSuffix = '_cors'; |
|
165 | 118 | ||
166 | 118 | /** |
|
167 | 118 | * Extension registry. |
|
168 | 118 | * |
|
169 | 78 | * @var \CakeDC\Api\Service\ExtensionRegistry |
|
170 | 78 | */ |
|
171 | 118 | protected $_extensions; |
|
172 | 96 | ||
173 | 96 | /** |
|
174 | 118 | * Service constructor. |
|
175 | * |
||
176 | * @param array $config Service configuration. |
||
177 | 118 | */ |
|
178 | 1 | public function __construct(array $config = []) |
|
216 | |||
217 | /** |
||
218 | * Get and set service name. |
||
219 | * |
||
220 | * @param string $name Service name. |
||
221 | * @return string |
||
222 | 118 | */ |
|
223 | public function name($name = null) |
||
232 | |||
233 | /** |
||
234 | * Get and set service version. |
||
235 | * |
||
236 | 51 | * @param int $version Version number. |
|
237 | * @return int |
||
238 | 51 | */ |
|
239 | 51 | public function version($version = null) |
|
248 | |||
249 | /** |
||
250 | * Initialize method |
||
251 | * |
||
252 | 118 | * @return void |
|
253 | */ |
||
254 | 118 | public function initialize() |
|
261 | |||
262 | /** |
||
263 | * Service parser configuration method. |
||
264 | * |
||
265 | * @param BaseParser $parser A Parser instance. |
||
266 | * @return BaseParser |
||
267 | */ |
||
268 | 3 | public function parser(BaseParser $parser = null) |
|
277 | |||
278 | /** |
||
279 | 64 | * Get and set request. |
|
280 | * |
||
281 | 64 | * @param \Cake\Network\Request $request A request object. |
|
282 | 64 | * @return \Cake\Network\Request |
|
283 | 64 | */ |
|
284 | 64 | public function request($request = null) |
|
294 | |||
295 | 64 | /** |
|
296 | * Get the service route scopes and their connected routes. |
||
297 | 64 | * |
|
298 | 64 | * @return array |
|
299 | */ |
||
300 | public function routes() |
||
306 | |||
307 | 8 | /** |
|
308 | * @param callable $callable Wrapped router instance. |
||
309 | 8 | * @return mixed |
|
310 | 8 | */ |
|
311 | 8 | protected function _routesWrapper(callable $callable) |
|
321 | |||
322 | /** |
||
323 | * Reset to default application routes. |
||
324 | 63 | * |
|
325 | * @return void |
||
326 | 63 | */ |
|
327 | 63 | public function resetRoutes() |
|
331 | |||
332 | 42 | /** |
|
333 | 42 | * Initialize service level routes |
|
334 | 8 | * |
|
335 | 8 | * @return void |
|
336 | 8 | */ |
|
337 | 42 | public function loadRoutes() |
|
349 | |||
350 | /** |
||
351 | * Build router settings. |
||
352 | * This implementation build action map for resource routes based on Service actions. |
||
353 | * |
||
354 | * @return array |
||
355 | */ |
||
356 | public function routerDefaultOptions() |
||
382 | 1 | ||
383 | 1 | /** |
|
384 | * Finds URL for specified action. |
||
385 | 13 | * |
|
386 | * Returns an URL pointing to a combination of controller and action. |
||
387 | * |
||
388 | * @param string|array|null $route An array specifying any of the following: |
||
389 | * 'controller', 'action', 'plugin' additionally, you can provide routed |
||
390 | * elements or query string parameters. If string it can be name any valid url |
||
391 | * string. |
||
392 | * @return string Full translated URL with base path. |
||
393 | 56 | * @throws \Cake\Core\Exception\Exception When the route name is not found |
|
394 | */ |
||
395 | public function routeUrl($route) |
||
401 | |||
402 | 52 | /** |
|
403 | 52 | * Reverses a parsed parameter array into a string. |
|
404 | * |
||
405 | 56 | * @param \Cake\Network\Request|array $params The params array or |
|
406 | 6 | * Cake\Network\Request object that needs to be reversed. |
|
407 | 6 | * @return string The string that is the reversed result of the array |
|
408 | 8 | */ |
|
409 | 2 | public function routeReverse($params) |
|
419 | |||
420 | /** |
||
421 | * Dispatch service call. |
||
422 | * |
||
423 | * @return \CakeDC\Api\Service\Action\Result |
||
424 | */ |
||
425 | 63 | public function dispatch() |
|
453 | 2 | ||
454 | /** |
||
455 | 60 | * Build action instance |
|
456 | 60 | * |
|
457 | * @return \CakeDC\Api\Service\Action\Action |
||
458 | 60 | * @throws Exception |
|
459 | */ |
||
460 | public function buildAction() |
||
495 | |||
496 | /** |
||
497 | * Parses given URL string. Returns 'routing' parameters for that URL. |
||
498 | * |
||
499 | 54 | * @param string $url URL to be parsed |
|
500 | * @return array Parsed elements from URL |
||
501 | 54 | * @throws \Cake\Routing\Exception\MissingRouteException When a route cannot be handled |
|
502 | 54 | */ |
|
503 | public function parseRoute($url) |
||
509 | |||
510 | /** |
||
511 | * Build base url |
||
512 | * |
||
513 | * @return string |
||
514 | */ |
||
515 | public function baseUrl() |
||
525 | |||
526 | /** |
||
527 | * Parent service get and set methods |
||
528 | * |
||
529 | 63 | * @param Service $service Parent Service instance. |
|
530 | * @return Service |
||
531 | 63 | */ |
|
532 | public function parent(Service $service = null) |
||
541 | |||
542 | /** |
||
543 | * Build action class |
||
544 | * |
||
545 | * @param string $class Class name. |
||
546 | 56 | * @param array $route Activated route. |
|
547 | * @return mixed |
||
548 | 56 | */ |
|
549 | 2 | public function buildActionClass($class, $route) |
|
555 | 56 | ||
556 | 56 | /** |
|
557 | * Action constructor options. |
||
558 | 56 | * |
|
559 | * @param array $route Activated route. |
||
560 | * @return array |
||
561 | */ |
||
562 | protected function _actionOptions($route) |
||
575 | 8 | ||
576 | 8 | /** |
|
577 | 8 | * @return \CakeDC\Api\Service\Action\Result |
|
578 | 52 | */ |
|
579 | 52 | public function result($value = null) |
|
593 | 118 | ||
594 | 109 | /** |
|
595 | * Fill up response and stop execution. |
||
596 | * |
||
597 | 118 | * @param Result $result A Result instance. |
|
598 | * @return Response |
||
599 | 118 | */ |
|
600 | public function respond($result = null) |
||
617 | |||
618 | /** |
||
619 | * Get and set response. |
||
620 | * |
||
621 | * @param \Cake\Network\Response $response A Response object. |
||
622 | * @return \Cake\Network\Response |
||
623 | */ |
||
624 | public function response($response = null) |
||
634 | 8 | ||
635 | 8 | /** |
|
636 | 8 | * Service renderer configuration method. |
|
637 | 8 | * |
|
638 | * @param BaseRenderer $renderer A Renderer instance. |
||
639 | * @return BaseRenderer |
||
640 | */ |
||
641 | public function renderer(BaseRenderer $renderer = null) |
||
650 | 118 | ||
651 | 118 | /** |
|
652 | 118 | * Define action config. |
|
653 | 118 | * |
|
654 | * @param string $actionName Action name. |
||
655 | 118 | * @param string $className Class name. |
|
656 | 118 | * @param array $route Route config. |
|
657 | * @return void |
||
658 | */ |
||
659 | 118 | public function mapAction($actionName, $className, $route) |
|
671 | 13 | ||
672 | 13 | /** |
|
673 | 118 | * @return array |
|
674 | 118 | */ |
|
675 | 105 | public function implementedEvents() |
|
693 | |||
694 | /** |
||
695 | * Get the extension registry for this service. |
||
696 | * |
||
697 | * If called with the first parameter, it will be set as the action $this->_extensions property |
||
698 | * |
||
699 | * @param \CakeDC\Api\Service\ExtensionRegistry|null $extensions Extension registry. |
||
700 | * |
||
701 | * @return \CakeDC\Api\Service\ExtensionRegistry |
||
702 | */ |
||
703 | public function extensions($extensions = null) |
||
714 | |||
715 | /** |
||
716 | * Loads the defined extensions using the Extension factory. |
||
717 | * |
||
718 | * @return void |
||
719 | */ |
||
720 | protected function _loadExtensions() |
||
732 | |||
733 | /** |
||
734 | * Initialize parser. |
||
735 | * |
||
736 | * @param array $config Service options |
||
737 | * @return void |
||
738 | */ |
||
739 | protected function _initializeParser(array $config) |
||
755 | |||
756 | /** |
||
757 | * Initialize renderer. |
||
758 | * |
||
759 | * @param array $config Service options. |
||
760 | * @return void |
||
761 | */ |
||
762 | protected function _initializeRenderer(array $config) |
||
778 | } |
||
779 |
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.