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) | ||
| 276 | |||
| 277 | /** | ||
| 278 | * Gets service version number. | ||
| 279 | * | ||
| 280 | * @return int | ||
| 281 | */ | ||
| 282 | 81 | public function getVersion() | |
| 286 | |||
| 287 | /** | ||
| 288 | * Sets service version. | ||
| 289 | * | ||
| 290 | * @param int $version Version number. | ||
| 291 | * @return void | ||
| 292 | */ | ||
| 293 | public function setVersion($version) | ||
| 297 | |||
| 298 | /** | ||
| 299 | * Get and set service version. | ||
| 300 | * | ||
| 301 | * @param int $version Version number. | ||
| 302 | * @deprecated 3.4.0 Use setVersion()/getVersion() instead. | ||
| 303 | * @return int|$this | ||
| 304 | */ | ||
| 305 | public function version($version = null) | ||
| 318 | |||
| 319 | /** | ||
| 320 | * Gets the service parser. | ||
| 321 | * | ||
| 322 | * @return BaseParser | ||
| 323 | */ | ||
| 324 | 66 | public function getParser() | |
| 328 | |||
| 329 | /** | ||
| 330 | * Sets the service parser. | ||
| 331 | * | ||
| 332 | * @param BaseParser $parser A Parser instance. | ||
| 333 | * @return $this | ||
| 334 | */ | ||
| 335 | public function setParser(BaseParser $parser) | ||
| 341 | |||
| 342 | /** | ||
| 343 | * Service parser configuration method. | ||
| 344 | * | ||
| 345 | * @param BaseParser $parser A Parser instance. | ||
| 346 | * @deprecated 3.4.0 Use getParser()/setParser() instead. | ||
| 347 | * @return BaseParser|$this | ||
| 348 | */ | ||
| 349 | public function parser(BaseParser $parser = null) | ||
| 362 | |||
| 363 | /** | ||
| 364 | * Gets the Request. | ||
| 365 | * | ||
| 366 | * @return \Cake\Http\ServerRequest | ||
| 367 | */ | ||
| 368 | 123 | public function getRequest() | |
| 372 | |||
| 373 | /** | ||
| 374 | * Sets the Request. | ||
| 375 | * | ||
| 376 | * @param \Cake\Http\ServerRequest $request A Request object. | ||
| 377 | * @return void | ||
| 378 | */ | ||
| 379 | 140 | public function setRequest(ServerRequest $request) | |
| 383 | |||
| 384 | /** | ||
| 385 | * Get and set request. | ||
| 386 | * | ||
| 387 | * @param \Cake\Http\ServerRequest $request A Request object. | ||
| 388 | * @deprecated 3.4.0 Use getRequest()/setRequest() instead. | ||
| 389 | * @return \Cake\Http\ServerRequest|$this | ||
| 390 | */ | ||
| 391 | public function request($request = null) | ||
| 404 | |||
| 405 | /** | ||
| 406 | * Get the service route scopes and their connected routes. | ||
| 407 | * | ||
| 408 | * @return array | ||
| 409 | */ | ||
| 410 | 3 | public function routes() | |
| 416 | |||
| 417 | /** | ||
| 418 | * @param callable $callable Wrapped router instance. | ||
| 419 | * @return mixed | ||
| 420 | */ | ||
| 421 | 65 | protected function _routesWrapper(callable $callable) | |
| 431 | |||
| 432 | /** | ||
| 433 | * Reset to default application routes. | ||
| 434 | * | ||
| 435 | * @return void | ||
| 436 | */ | ||
| 437 | 65 | public function resetRoutes() | |
| 441 | |||
| 442 | /** | ||
| 443 | * Initialize service level routes | ||
| 444 | * | ||
| 445 | * @return void | ||
| 446 | */ | ||
| 447 | 8 | public function loadRoutes() | |
| 459 | |||
| 460 | /** | ||
| 461 | * Build router settings. | ||
| 462 | * This implementation build action map for resource routes based on Service actions. | ||
| 463 | * | ||
| 464 | * @return array | ||
| 465 | */ | ||
| 466 | 64 | public function routerDefaultOptions() | |
| 492 | |||
| 493 | /** | ||
| 494 | * Finds URL for specified action. | ||
| 495 | * | ||
| 496 | * Returns an URL pointing to a combination of controller and action. | ||
| 497 | * | ||
| 498 | * @param string|array|null $route An array specifying any of the following: | ||
| 499 | * 'controller', 'action', 'plugin' additionally, you can provide routed | ||
| 500 | * elements or query string parameters. If string it can be name any valid url | ||
| 501 | * string. | ||
| 502 | * @return string Full translated URL with base path. | ||
| 503 | * @throws \Cake\Core\Exception\Exception When the route name is not found | ||
| 504 | */ | ||
| 505 | public function routeUrl($route) | ||
| 511 | |||
| 512 | /** | ||
| 513 | * Reverses a parsed parameter array into a string. | ||
| 514 | * | ||
| 515 | * @param \Cake\Http\ServerRequest|array $params The params array or | ||
| 516 | * Cake\Http\ServerRequest object that needs to be reversed. | ||
| 517 | * @return string The string that is the reversed result of the array | ||
| 518 | */ | ||
| 519 | 13 | public function routeReverse($params) | |
| 529 | |||
| 530 | /** | ||
| 531 | * Dispatch service call. | ||
| 532 | * | ||
| 533 | * @return \CakeDC\Api\Service\Action\Result | ||
| 534 | */ | ||
| 535 | 56 | public function dispatch() | |
| 563 | |||
| 564 | /** | ||
| 565 | * Dispatch service call through callbacks and action. | ||
| 566 | * | ||
| 567 | * @return Result|mixed | ||
| 568 | */ | ||
| 569 | 56 | protected function _dispatch() | |
| 584 | |||
| 585 | /** | ||
| 586 | * Build action instance | ||
| 587 | * | ||
| 588 | * @return \CakeDC\Api\Service\Action\Action | ||
| 589 | * @throws Exception | ||
| 590 | */ | ||
| 591 | 64 | public function buildAction() | |
| 625 | |||
| 626 | /** | ||
| 627 | * Parses given URL string. Returns 'routing' parameters for that URL. | ||
| 628 | * | ||
| 629 | * @param string $url URL to be parsed | ||
| 630 | * @return array Parsed elements from URL | ||
| 631 | * @throws \Cake\Routing\Exception\MissingRouteException When a route cannot be handled | ||
| 632 | */ | ||
| 633 | public function parseRoute($url) | ||
| 644 | |||
| 645 | /** | ||
| 646 | * Returns action class map. | ||
| 647 | * | ||
| 648 | * @return array | ||
| 649 | */ | ||
| 650 | 61 | public function getActionsClassMap() | |
| 654 | |||
| 655 | /** | ||
| 656 | * Build base url | ||
| 657 | * | ||
| 658 | * @return string | ||
| 659 | */ | ||
| 660 | 65 | public function getBaseUrl() | |
| 670 | |||
| 671 | /** | ||
| 672 | * Gets the parent service method. | ||
| 673 | * | ||
| 674 | * @return Service | ||
| 675 | */ | ||
| 676 | 55 | public function getParentService() | |
| 680 | |||
| 681 | /** | ||
| 682 | * Sets the parent service method. | ||
| 683 | * | ||
| 684 | * @param Service $parentService Parent Service | ||
| 685 | * @return $this | ||
| 686 | */ | ||
| 687 | 9 | public function setParentService(Service $parentService) | |
| 693 | |||
| 694 | /** | ||
| 695 | * Parent service get and set methods. | ||
| 696 | * | ||
| 697 | * @param Service $service Parent Service instance. | ||
| 698 | * @deprecated 3.4.0 Use getParentService()/setParentService() instead. | ||
| 699 | * @return Service|$this | ||
| 700 | */ | ||
| 701 | public function parent(Service $service = null) | ||
| 714 | |||
| 715 | /** | ||
| 716 | * Build action class | ||
| 717 | * | ||
| 718 | * @param string $class Class name. | ||
| 719 | * @param array $route Activated route. | ||
| 720 | * @return mixed | ||
| 721 | */ | ||
| 722 | 64 | public function buildActionClass($class, $route) | |
| 728 | |||
| 729 | /** | ||
| 730 | * Action constructor options. | ||
| 731 | * | ||
| 732 | * @param array $route Activated route. | ||
| 733 | * @return array | ||
| 734 | */ | ||
| 735 | 64 | protected function _actionOptions($route) | |
| 748 | |||
| 749 | /** | ||
| 750 | * Gets the result for service. | ||
| 751 | * | ||
| 752 | * @return Result | ||
| 753 | */ | ||
| 754 | 56 | public function getResult() | |
| 765 | |||
| 766 | /** | ||
| 767 | * Sets the result for service. | ||
| 768 | * | ||
| 769 | * @param Result $result A Result object. | ||
| 770 | * @return $this | ||
| 771 | */ | ||
| 772 | public function setResult(Result $result) | ||
| 783 | |||
| 784 | /** | ||
| 785 | * @param null $value value | ||
| 786 | * @deprecated 3.4.0 Use getResult()/setResult() instead. | ||
| 787 | * @return Result | ||
| 788 | */ | ||
| 789 | public function result($value = null) | ||
| 802 | |||
| 803 | /** | ||
| 804 | * Fill up response and stop execution. | ||
| 805 | * | ||
| 806 | * @param Result $result A Result instance. | ||
| 807 | * @return Response | ||
| 808 | */ | ||
| 809 | 56 | public function respond($result = null) | |
| 825 | |||
| 826 | /** | ||
| 827 | * Gets the response. | ||
| 828 | * | ||
| 829 | * @return \Cake\Http\Response | ||
| 830 | */ | ||
| 831 | 131 | public function getResponse() | |
| 835 | |||
| 836 | /** | ||
| 837 | * Sets the response. | ||
| 838 | * | ||
| 839 | * @param \Cake\Http\Response $response Response | ||
| 840 | * @return $this | ||
| 841 | */ | ||
| 842 | 140 | public function setResponse(Response $response) | |
| 848 | |||
| 849 | /** | ||
| 850 | * Get and set response. | ||
| 851 | * | ||
| 852 | * @param \Cake\Http\Response $response A Response object. | ||
| 853 | * @deprecated 3.4.0 Use getResponse()/setResponse() instead. | ||
| 854 | * @return \Cake\Http\Response | ||
| 855 | */ | ||
| 856 | public function response(Response $response = null) | ||
| 869 | |||
| 870 | /** | ||
| 871 | * Gets the service renderer. | ||
| 872 | * | ||
| 873 | * @return BaseRenderer | ||
| 874 | */ | ||
| 875 | 68 | public function getRenderer() | |
| 879 | |||
| 880 | /** | ||
| 881 | * Sets the service renderer. | ||
| 882 | * | ||
| 883 | * @param BaseRenderer $renderer Rendered | ||
| 884 | * @return $this | ||
| 885 | */ | ||
| 886 | 140 | public function setRenderer(BaseRenderer $renderer) | |
| 892 | |||
| 893 | /** | ||
| 894 | * Service renderer configuration method. | ||
| 895 | * | ||
| 896 | * @param BaseRenderer $renderer A Renderer instance. | ||
| 897 | * @deprecated 3.4.0 Use getRenderer()/setRenderer() instead. | ||
| 898 | * @return BaseRenderer|$this | ||
| 899 | */ | ||
| 900 | public function renderer(BaseRenderer $renderer = null) | ||
| 913 | |||
| 914 | /** | ||
| 915 | * Define action config. | ||
| 916 | * | ||
| 917 | * @param string $actionName Action name. | ||
| 918 | * @param string $className Class name. | ||
| 919 | * @param array $route Route config. | ||
| 920 | * @return void | ||
| 921 | */ | ||
| 922 | 23 | public function mapAction($actionName, $className, $route) | |
| 934 | |||
| 935 | /** | ||
| 936 | * Lists supported events. | ||
| 937 | * | ||
| 938 | * @return array | ||
| 939 | */ | ||
| 940 | 140 | public function implementedEvents() | |
| 958 | |||
| 959 | /** | ||
| 960 | * Gets the extension registry instance. | ||
| 961 | * | ||
| 962 | * @return \CakeDC\Api\Service\ExtensionRegistry | ||
| 963 | */ | ||
| 964 | public function getExtensions() | ||
| 972 | |||
| 973 | /** | ||
| 974 | * Sets the extension registry for this service. | ||
| 975 | * | ||
| 976 | * @param \CakeDC\Api\Service\ExtensionRegistry $extensions The extension registry instance. | ||
| 977 | * @return $this | ||
| 978 | */ | ||
| 979 | 140 | public function setExtensions($extensions) | |
| 988 | |||
| 989 | /** | ||
| 990 | * Get the extension registry for this service. | ||
| 991 | * | ||
| 992 | * If called with the first parameter, it will be set as the action $this->_extensions property | ||
| 993 | * | ||
| 994 | * @param \CakeDC\Api\Service\ExtensionRegistry|null $extensions Extension registry. | ||
| 995 | * @deprecated 3.4.0 Use getExtensions()/setExtensions() instead. | ||
| 996 | * @return \CakeDC\Api\Service\ExtensionRegistry|$this | ||
| 997 | */ | ||
| 998 | public function extensions($extensions = null) | ||
| 1011 | |||
| 1012 | /** | ||
| 1013 | * Loads the defined extensions using the Extension factory. | ||
| 1014 | * | ||
| 1015 | * @return void | ||
| 1016 | */ | ||
| 1017 | 140 | protected function _loadExtensions() | |
| 1029 | |||
| 1030 | /** | ||
| 1031 | * Initialize parser. | ||
| 1032 | * | ||
| 1033 | * @param array $config Service options | ||
| 1034 | * @return void | ||
| 1035 | */ | ||
| 1036 | 140 | protected function _initializeParser(array $config) | |
| 1052 | |||
| 1053 | /** | ||
| 1054 | * Initialize renderer. | ||
| 1055 | * | ||
| 1056 | * @param array $config Service options. | ||
| 1057 | * @return void | ||
| 1058 | */ | ||
| 1059 | 140 | protected function _initializeRenderer(array $config) | |
| 1075 | } | ||
| 1076 | 
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: