Complex classes like AbstractRequest 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 AbstractRequest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | abstract class AbstractRequest implements RequestInterface |
||
14 | { |
||
15 | /** |
||
16 | * @var string response implementation to be specified in concrete implementation |
||
17 | */ |
||
18 | protected $responseClass; |
||
19 | |||
20 | /** |
||
21 | * @var QueryBuilderInterface |
||
22 | */ |
||
23 | protected $builder; |
||
24 | |||
25 | /** |
||
26 | * @var Query |
||
27 | */ |
||
28 | protected $query; |
||
29 | |||
30 | /** |
||
31 | * @var string Connection name |
||
32 | */ |
||
33 | protected $dbname; |
||
34 | |||
35 | /** |
||
36 | * @var array request method |
||
37 | */ |
||
38 | protected $method; |
||
39 | protected $uri; |
||
40 | protected $headers = []; |
||
41 | protected $body; |
||
42 | protected $version; |
||
43 | |||
44 | protected $isBuilt; |
||
45 | protected $parts = []; |
||
46 | protected $fullUri; |
||
47 | |||
48 | abstract public function send($options = []); |
||
49 | |||
50 | public function __construct(QueryBuilderInterface $builder, Query $query) |
||
55 | |||
56 | public function getDbname() |
||
60 | |||
61 | public function getMethod() |
||
65 | |||
66 | public function getUri() |
||
70 | |||
71 | public function getFullUri() |
||
79 | |||
80 | public function createFullUri() |
||
84 | |||
85 | public function isFullUri($uri) |
||
89 | |||
90 | public function getHeaders() |
||
94 | |||
95 | public function getBody() |
||
99 | |||
100 | public function getVersion() |
||
104 | |||
105 | /** |
||
106 | * @return Query |
||
107 | */ |
||
108 | public function getQuery() |
||
112 | |||
113 | protected function build() |
||
122 | |||
123 | protected function updateFromQuery() |
||
137 | |||
138 | protected function buildDbname() |
||
142 | |||
143 | protected function buildAuth() |
||
147 | |||
148 | protected function buildMethod() |
||
152 | |||
153 | protected function buildUri() |
||
157 | |||
158 | protected function buildQueryParams() |
||
168 | |||
169 | protected function buildHeaders() |
||
176 | |||
177 | protected function buildBody() |
||
181 | |||
182 | protected function buildFormParams() |
||
186 | |||
187 | protected function setFormParams($params) |
||
194 | |||
195 | protected function buildProtocolVersion() |
||
199 | |||
200 | public function serialize() |
||
204 | |||
205 | public function unserialize($string) |
||
211 | |||
212 | public function getParts() |
||
223 | |||
224 | public function isRaw() |
||
228 | |||
229 | protected function getHandler() |
||
238 | |||
239 | protected function createHandler() |
||
245 | |||
246 | protected function prepareHandlerConfig($config) |
||
250 | |||
251 | protected function prepareUserAgent() |
||
255 | |||
256 | public function getDb() |
||
260 | } |
||
261 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: