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 |
||
42 | abstract class Service implements EventListenerInterface, EventDispatcherInterface |
||
43 | { |
||
44 | use EventDispatcherTrait; |
||
45 | |||
46 | /** |
||
47 | * Extensions to load and attach to listener |
||
48 | * |
||
49 | * @var array |
||
50 | */ |
||
51 | public $extensions = []; |
||
52 | |||
53 | /** |
||
54 | * Actions routes description map, indexed by action name. |
||
55 | * |
||
56 | * @var array |
||
57 | */ |
||
58 | protected $_actions = []; |
||
59 | |||
60 | /** |
||
61 | * Actions classes map, indexed by action name. |
||
62 | * |
||
63 | * @var array |
||
64 | */ |
||
65 | protected $_actionsClassMap = []; |
||
66 | |||
67 | /** |
||
68 | * Service url acceptable extensions list. |
||
69 | * |
||
70 | * @var array |
||
71 | */ |
||
72 | protected $_routeExtensions = ['json']; |
||
73 | |||
74 | /** |
||
75 | * |
||
76 | * |
||
77 | * @var string |
||
78 | */ |
||
79 | protected $_routePrefix = ''; |
||
80 | |||
81 | /** |
||
82 | * Service name |
||
83 | * |
||
84 | * @var string |
||
85 | */ |
||
86 | protected $_name = null; |
||
87 | |||
88 | /** |
||
89 | * Service version. |
||
90 | * |
||
91 | * @var int |
||
92 | */ |
||
93 | protected $_version; |
||
94 | |||
95 | /** |
||
96 | * Parser class to process the HTTP request. |
||
97 | * |
||
98 | * @var BaseParser |
||
99 | */ |
||
100 | protected $_parser; |
||
101 | |||
102 | /** |
||
103 | * Renderer class to build the HTTP response. |
||
104 | * |
||
105 | * @var BaseRenderer |
||
106 | */ |
||
107 | protected $_renderer; |
||
108 | |||
109 | /** |
||
110 | * The parser class. |
||
111 | * |
||
112 | * @var string |
||
113 | */ |
||
114 | protected $_parserClass = null; |
||
115 | |||
116 | /** |
||
117 | * The Renderer class. |
||
118 | * |
||
119 | * @var string |
||
120 | */ |
||
121 | protected $_rendererClass = null; |
||
122 | |||
123 | /** |
||
124 | * Dependent services names list |
||
125 | * |
||
126 | * @var array<string> |
||
127 | */ |
||
128 | protected $_innerServices = []; |
||
129 | |||
130 | /** |
||
131 | * Parent service instance. |
||
132 | * |
||
133 | * @var Service |
||
134 | */ |
||
135 | protected $_parentService; |
||
136 | |||
137 | /** |
||
138 | * Service Action Result object. |
||
139 | * |
||
140 | * @var Result |
||
141 | */ |
||
142 | protected $_result; |
||
143 | |||
144 | /** |
||
145 | * Base url for service. |
||
146 | * |
||
147 | * @var string |
||
148 | */ |
||
149 | protected $_baseUrl; |
||
150 | |||
151 | /** |
||
152 | * Request |
||
153 | * |
||
154 | * @var \Cake\Http\ServerRequest |
||
155 | */ |
||
156 | protected $_request; |
||
157 | |||
158 | /** |
||
159 | * Request |
||
160 | * |
||
161 | * @var \Cake\Http\Response |
||
162 | */ |
||
163 | protected $_response; |
||
164 | |||
165 | /** |
||
166 | * @var string |
||
167 | */ |
||
168 | protected $_corsSuffix = '_cors'; |
||
169 | |||
170 | /** |
||
171 | * Extension registry. |
||
172 | * |
||
173 | * @var \CakeDC\Api\Service\ExtensionRegistry |
||
174 | */ |
||
175 | protected $_extensions; |
||
176 | |||
177 | /** |
||
178 | * Service constructor. |
||
179 | * |
||
180 | * @param array $config Service configuration. |
||
181 | */ |
||
182 | 140 | public function __construct(array $config = []) |
|
219 | |||
220 | /** |
||
221 | * Initialize method |
||
222 | * |
||
223 | * @return void |
||
224 | */ |
||
225 | 140 | public function initialize() |
|
232 | |||
233 | /** |
||
234 | * Gets service name. |
||
235 | * |
||
236 | * @return string |
||
237 | */ |
||
238 | 139 | public function getName() |
|
242 | |||
243 | /** |
||
244 | * Sets service name. |
||
245 | * |
||
246 | * @param string $name Service name. |
||
247 | * @return $this |
||
248 | */ |
||
249 | 140 | public function setName($name) |
|
255 | |||
256 | /** |
||
257 | * Get and set service name. |
||
258 | * |
||
259 | * @param string $name Service name. |
||
260 | * @deprecated 3.4.0 Use setName()/getName() instead. |
||
261 | * @return string |
||
262 | */ |
||
263 | public function name($name = null) |
||
271 | |||
272 | /** |
||
273 | * Gets service version number. |
||
274 | * |
||
275 | * @return int |
||
276 | */ |
||
277 | 81 | public function getVersion() |
|
281 | |||
282 | /** |
||
283 | * Sets service version. |
||
284 | * |
||
285 | * @param int $version Version number. |
||
286 | * @return void |
||
287 | */ |
||
288 | public function setVersion($version) |
||
292 | |||
293 | /** |
||
294 | * Get and set service version. |
||
295 | * |
||
296 | * @param int $version Version number. |
||
297 | * @deprecated 3.4.0 Use setVersion()/getVersion() instead. |
||
298 | * @return int|$this |
||
299 | */ |
||
300 | public function version($version = null) |
||
308 | |||
309 | /** |
||
310 | * Gets the service parser. |
||
311 | * |
||
312 | * @return BaseParser |
||
313 | */ |
||
314 | 66 | public function getParser() |
|
318 | |||
319 | /** |
||
320 | * Sets the service parser. |
||
321 | * |
||
322 | * @param BaseParser $parser A Parser instance. |
||
323 | * @return $this |
||
324 | */ |
||
325 | public function setParser(BaseParser $parser) |
||
331 | |||
332 | /** |
||
333 | * Service parser configuration method. |
||
334 | * |
||
335 | * @param BaseParser $parser A Parser instance. |
||
336 | * @deprecated 3.4.0 Use getParser()/setParser() instead. |
||
337 | * @return BaseParser|$this |
||
338 | */ |
||
339 | public function parser(BaseParser $parser = null) |
||
347 | |||
348 | /** |
||
349 | * Gets the Request. |
||
350 | * |
||
351 | * @return \Cake\Http\ServerRequest |
||
352 | */ |
||
353 | 123 | public function getRequest() |
|
357 | |||
358 | /** |
||
359 | * Sets the Request. |
||
360 | * |
||
361 | * @param \Cake\Http\ServerRequest $request A Request object. |
||
362 | * @return void |
||
363 | */ |
||
364 | 140 | public function setRequest(ServerRequest $request) |
|
368 | |||
369 | /** |
||
370 | * Get and set request. |
||
371 | * |
||
372 | * @param \Cake\Http\ServerRequest $request A Request object. |
||
373 | * @deprecated 3.4.0 Use getRequest()/setRequest() instead. |
||
374 | * @return \Cake\Http\ServerRequest|$this |
||
375 | */ |
||
376 | 1 | public function request($request = null) |
|
384 | |||
385 | /** |
||
386 | * Get the service route scopes and their connected routes. |
||
387 | * |
||
388 | * @return array |
||
389 | */ |
||
390 | 3 | public function routes() |
|
396 | |||
397 | /** |
||
398 | * @param callable $callable Wrapped router instance. |
||
399 | * @return mixed |
||
400 | */ |
||
401 | 65 | protected function _routesWrapper(callable $callable) |
|
411 | |||
412 | /** |
||
413 | * Reset to default application routes. |
||
414 | * |
||
415 | * @return void |
||
416 | */ |
||
417 | 65 | public function resetRoutes() |
|
421 | |||
422 | /** |
||
423 | * Initialize service level routes |
||
424 | * |
||
425 | * @return void |
||
426 | */ |
||
427 | 8 | public function loadRoutes() |
|
439 | |||
440 | /** |
||
441 | * Build router settings. |
||
442 | * This implementation build action map for resource routes based on Service actions. |
||
443 | * |
||
444 | * @return array |
||
445 | */ |
||
446 | 64 | public function routerDefaultOptions() |
|
472 | |||
473 | /** |
||
474 | * Finds URL for specified action. |
||
475 | * |
||
476 | * Returns an URL pointing to a combination of controller and action. |
||
477 | * |
||
478 | * @param string|array|null $route An array specifying any of the following: |
||
479 | * 'controller', 'action', 'plugin' additionally, you can provide routed |
||
480 | * elements or query string parameters. If string it can be name any valid url |
||
481 | * string. |
||
482 | * @return string Full translated URL with base path. |
||
483 | * @throws \Cake\Core\Exception\Exception When the route name is not found |
||
484 | */ |
||
485 | public function routeUrl($route) |
||
491 | |||
492 | /** |
||
493 | * Reverses a parsed parameter array into a string. |
||
494 | * |
||
495 | * @param \Cake\Http\ServerRequest|array $params The params array or |
||
496 | * Cake\Http\ServerRequest object that needs to be reversed. |
||
497 | * @return string The string that is the reversed result of the array |
||
498 | */ |
||
499 | 13 | public function routeReverse($params) |
|
509 | |||
510 | /** |
||
511 | * Dispatch service call. |
||
512 | * |
||
513 | * @return \CakeDC\Api\Service\Action\Result |
||
514 | */ |
||
515 | 56 | public function dispatch() |
|
543 | |||
544 | /** |
||
545 | * Dispatch service call through callbacks and action. |
||
546 | * |
||
547 | * @return Result|mixed |
||
548 | */ |
||
549 | 56 | protected function _dispatch() |
|
564 | |||
565 | /** |
||
566 | * Build action instance |
||
567 | * |
||
568 | * @return \CakeDC\Api\Service\Action\Action |
||
569 | * @throws Exception |
||
570 | */ |
||
571 | 64 | public function buildAction() |
|
605 | |||
606 | /** |
||
607 | * Parses given URL string. Returns 'routing' parameters for that URL. |
||
608 | * |
||
609 | * @param string $url URL to be parsed |
||
610 | * @return array Parsed elements from URL |
||
611 | * @throws \Cake\Routing\Exception\MissingRouteException When a route cannot be handled |
||
612 | */ |
||
613 | public function parseRoute($url) |
||
624 | |||
625 | /** |
||
626 | * Returns action class map. |
||
627 | * |
||
628 | * @return array |
||
629 | */ |
||
630 | 61 | public function getActionsClassMap() |
|
634 | |||
635 | /** |
||
636 | * Build base url |
||
637 | * |
||
638 | * @return string |
||
639 | */ |
||
640 | 65 | public function getBaseUrl() |
|
650 | |||
651 | /** |
||
652 | * Gets the parent service method. |
||
653 | * |
||
654 | * @return Service |
||
655 | */ |
||
656 | 55 | public function getParentService() |
|
660 | |||
661 | /** |
||
662 | * Sets the parent service method. |
||
663 | * |
||
664 | * @param Service $parentService Parent Service |
||
665 | * @return $this |
||
666 | */ |
||
667 | 9 | public function setParentService(Service $parentService) |
|
673 | |||
674 | /** |
||
675 | * Parent service get and set methods. |
||
676 | * |
||
677 | * @param Service $service Parent Service instance. |
||
678 | * @deprecated 3.4.0 Use getParentService()/setParentService() instead. |
||
679 | * @return Service|$this |
||
680 | */ |
||
681 | 4 | public function parent(Service $service = null) |
|
689 | |||
690 | /** |
||
691 | * Build action class |
||
692 | * |
||
693 | * @param string $class Class name. |
||
694 | * @param array $route Activated route. |
||
695 | * @return mixed |
||
696 | */ |
||
697 | 64 | public function buildActionClass($class, $route) |
|
703 | |||
704 | /** |
||
705 | * Action constructor options. |
||
706 | * |
||
707 | * @param array $route Activated route. |
||
708 | * @return array |
||
709 | */ |
||
710 | 64 | protected function _actionOptions($route) |
|
723 | |||
724 | /** |
||
725 | * Gets the result for service. |
||
726 | * |
||
727 | * @return Result |
||
728 | */ |
||
729 | 56 | public function getResult() |
|
740 | |||
741 | /** |
||
742 | * Sets the result for service. |
||
743 | * |
||
744 | * @param Result $result A Result object. |
||
745 | * @return $this |
||
746 | */ |
||
747 | public function setResult(Result $result) |
||
758 | |||
759 | /** |
||
760 | * @param null $value value |
||
761 | * @deprecated 3.4.0 Use getResult()/setResult() instead. |
||
762 | * @return Result |
||
763 | */ |
||
764 | 30 | public function result($value = null) |
|
772 | |||
773 | /** |
||
774 | * Fill up response and stop execution. |
||
775 | * |
||
776 | * @param Result $result A Result instance. |
||
777 | * @return Response |
||
778 | */ |
||
779 | 56 | public function respond($result = null) |
|
795 | |||
796 | /** |
||
797 | * Gets the response. |
||
798 | * |
||
799 | * @return \Cake\Http\Response |
||
800 | */ |
||
801 | 131 | public function getResponse() |
|
805 | |||
806 | /** |
||
807 | * Sets the response. |
||
808 | * |
||
809 | * @param \Cake\Http\Response $response Response |
||
810 | * @return $this |
||
811 | */ |
||
812 | 140 | public function setResponse(Response $response) |
|
818 | |||
819 | /** |
||
820 | * Get and set response. |
||
821 | * |
||
822 | * @param \Cake\Http\Response $response A Response object. |
||
823 | * @deprecated 3.4.0 Use getResponse()/setResponse() instead. |
||
824 | * @return \Cake\Http\Response |
||
825 | */ |
||
826 | 1 | public function response(Response $response = null) |
|
834 | |||
835 | /** |
||
836 | * Gets the service renderer. |
||
837 | * |
||
838 | * @return BaseRenderer |
||
839 | */ |
||
840 | 68 | public function getRenderer() |
|
844 | |||
845 | /** |
||
846 | * Sets the service renderer. |
||
847 | * |
||
848 | * @param BaseRenderer $renderer Rendered |
||
849 | * @return $this |
||
850 | */ |
||
851 | 140 | public function setRenderer(BaseRenderer $renderer) |
|
857 | |||
858 | /** |
||
859 | * Service renderer configuration method. |
||
860 | * |
||
861 | * @param BaseRenderer $renderer A Renderer instance. |
||
862 | * @deprecated 3.4.0 Use getRenderer()/setRenderer() instead. |
||
863 | * @return BaseRenderer|$this |
||
864 | */ |
||
865 | public function renderer(BaseRenderer $renderer = null) |
||
873 | |||
874 | /** |
||
875 | * Define action config. |
||
876 | * |
||
877 | * @param string $actionName Action name. |
||
878 | * @param string $className Class name. |
||
879 | * @param array $route Route config. |
||
880 | * @return void |
||
881 | */ |
||
882 | 23 | public function mapAction($actionName, $className, $route) |
|
894 | |||
895 | /** |
||
896 | * Lists supported events. |
||
897 | * |
||
898 | * @return array |
||
899 | */ |
||
900 | 140 | public function implementedEvents() |
|
918 | |||
919 | /** |
||
920 | * Gets the extension registry instance. |
||
921 | * |
||
922 | * @return \CakeDC\Api\Service\ExtensionRegistry |
||
923 | */ |
||
924 | public function getExtensions() |
||
932 | |||
933 | /** |
||
934 | * Sets the extension registry for this service. |
||
935 | * |
||
936 | * @param \CakeDC\Api\Service\ExtensionRegistry $extensions The extension registry instance. |
||
937 | * @return $this |
||
938 | */ |
||
939 | 140 | public function setExtensions($extensions) |
|
948 | |||
949 | /** |
||
950 | * Get the extension registry for this service. |
||
951 | * |
||
952 | * If called with the first parameter, it will be set as the action $this->_extensions property |
||
953 | * |
||
954 | * @param \CakeDC\Api\Service\ExtensionRegistry|null $extensions Extension registry. |
||
955 | * @deprecated 3.4.0 Use getExtensions()/setExtensions() instead. |
||
956 | * @return \CakeDC\Api\Service\ExtensionRegistry|$this |
||
957 | */ |
||
958 | public function extensions($extensions = null) |
||
966 | |||
967 | /** |
||
968 | * Loads the defined extensions using the Extension factory. |
||
969 | * |
||
970 | * @return void |
||
971 | */ |
||
972 | 140 | protected function _loadExtensions() |
|
984 | |||
985 | /** |
||
986 | * Initialize parser. |
||
987 | * |
||
988 | * @param array $config Service options |
||
989 | * @return void |
||
990 | */ |
||
991 | 140 | protected function _initializeParser(array $config) |
|
1007 | |||
1008 | /** |
||
1009 | * Initialize renderer. |
||
1010 | * |
||
1011 | * @param array $config Service options. |
||
1012 | * @return void |
||
1013 | */ |
||
1014 | 140 | protected function _initializeRenderer(array $config) |
|
1030 | } |
||
1031 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: