Complex classes like ResourceAbstract 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 ResourceAbstract, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | class ResourceAbstract |
||
8 | { |
||
9 | /** |
||
10 | * @var array |
||
11 | */ |
||
12 | private $methods; |
||
13 | |||
14 | /** |
||
15 | * @var Twitch\Client |
||
16 | */ |
||
17 | private $client; |
||
18 | |||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | private $resource_name; |
||
23 | |||
24 | /** |
||
25 | * @var array |
||
26 | */ |
||
27 | private $url_parts = array(); |
||
28 | |||
29 | |||
30 | /** |
||
31 | * ResourceAbstract constructor. |
||
32 | * |
||
33 | * @param Twitch\Client $client |
||
34 | * @param string $resource_name |
||
35 | * @param array $declaration |
||
36 | */ |
||
37 | public function __construct(Twitch\Client $client, $resource_name = "", $declaration = []) |
||
38 | { |
||
39 | $this->client = $client; |
||
40 | $this->resource_name = $resource_name; |
||
41 | $this->methods = isset($declaration['methods']) ? $declaration['methods'] : []; |
||
42 | |||
43 | // If this line gives errros .. Comment it out because somewhere in the resource it self is an error. |
||
44 | // $client->registerResource($this->resource_name, $this); |
||
45 | // Todo validate resources |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Validate arguments given for a 'virtual' method. |
||
50 | * |
||
51 | * @param string $method_name |
||
52 | * @param array $args |
||
53 | * @return bool |
||
54 | * @throws Exception\RuntimeException |
||
55 | */ |
||
56 | private function validate_arguments($method_name = '', $args = array()) |
||
102 | |||
103 | /** |
||
104 | * Call and process the 'virtual' method as defined in Client.php |
||
105 | * |
||
106 | * @param string $method |
||
107 | * @param array $arguments |
||
108 | * @param array $body |
||
109 | * @return mixed |
||
110 | * @throws Exception\AuthorizationRequiredException |
||
111 | * @throws Exception\RuntimeException |
||
112 | */ |
||
113 | public function call($method, $arguments = array(), $body = array()) |
||
178 | |||
179 | /** |
||
180 | * @return Client |
||
181 | */ |
||
182 | public function getClient() { |
||
185 | |||
186 | /** |
||
187 | * @return string |
||
188 | */ |
||
189 | public function getResourceName() { |
||
192 | |||
193 | /** |
||
194 | * @return array |
||
195 | */ |
||
196 | public function getMethods() { |
||
199 | |||
200 | } |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.