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 string request handler implementation to be specified in concrete implementation |
||
22 | */ |
||
23 | protected $handlerClass; |
||
24 | |||
25 | /** |
||
26 | * @var QueryBuilderInterface |
||
27 | */ |
||
28 | protected $builder; |
||
29 | |||
30 | /** |
||
31 | * @var Query |
||
32 | */ |
||
33 | protected $query; |
||
34 | |||
35 | /** |
||
36 | * @var string Connection name |
||
37 | */ |
||
38 | protected $dbname; |
||
39 | |||
40 | /** |
||
41 | * @var array request method |
||
42 | */ |
||
43 | protected $method; |
||
44 | protected $uri; |
||
45 | protected $headers = []; |
||
46 | protected $body; |
||
47 | protected $version; |
||
48 | |||
49 | protected $isBuilt; |
||
50 | protected $parts = []; |
||
51 | protected $fullUri; |
||
52 | |||
53 | abstract public function send($options = []); |
||
54 | |||
55 | 2 | public function __construct(QueryBuilderInterface $builder, Query $query) |
|
60 | |||
61 | public function getDbname() |
||
65 | |||
66 | 2 | public function getMethod() |
|
67 | { |
||
68 | 2 | return $this->method; |
|
|
|||
69 | } |
||
70 | |||
71 | public function getUri() |
||
75 | |||
76 | 2 | public function getFullUri() |
|
77 | { |
||
78 | 2 | if ($this->fullUri === null) { |
|
79 | 2 | $this->fullUri = $this->createFullUri(); |
|
80 | } |
||
81 | |||
82 | 2 | return $this->fullUri; |
|
83 | } |
||
84 | |||
85 | 2 | public function createFullUri() |
|
89 | |||
90 | 2 | public function isFullUri($uri) |
|
94 | |||
95 | 2 | public function getHeaders() |
|
96 | { |
||
97 | 2 | return $this->headers; |
|
98 | } |
||
99 | |||
100 | public function getBody() |
||
104 | |||
105 | 2 | public function getVersion() |
|
106 | { |
||
107 | 2 | return $this->version; |
|
108 | } |
||
109 | |||
110 | /** |
||
111 | * @return Query |
||
112 | */ |
||
113 | 2 | public function getQuery() |
|
117 | |||
118 | 2 | protected function build() |
|
119 | { |
||
120 | 2 | if ($this->isBuilt === null) { |
|
121 | 2 | if (!empty($this->query)) { |
|
122 | 2 | $this->updateFromQuery(); |
|
123 | } |
||
124 | 2 | $this->isBuilt = true; |
|
125 | } |
||
126 | 2 | } |
|
127 | |||
128 | 2 | protected function updateFromQuery() |
|
142 | |||
143 | 2 | protected function buildDbname() |
|
147 | |||
148 | 2 | protected function buildAuth() |
|
152 | |||
153 | 2 | protected function buildMethod() |
|
157 | |||
158 | 2 | protected function buildUri() |
|
162 | |||
163 | 2 | protected function buildQueryParams() |
|
164 | { |
||
165 | 2 | $params = $this->builder->buildQueryParams($this->query); |
|
166 | 2 | if (is_array($params)) { |
|
167 | 2 | $params = http_build_query($params, '', '&'); |
|
168 | } |
||
169 | 2 | if (!empty($params)) { |
|
170 | $this->uri .= '?' . $params; |
||
171 | } |
||
172 | 2 | } |
|
173 | |||
174 | 2 | protected function buildHeaders() |
|
175 | { |
||
176 | 2 | $this->headers = $this->builder->buildHeaders($this->query); |
|
177 | 2 | if (empty($this->headers['User-Agent'])) { |
|
178 | 2 | $this->headers['User-Agent'] = $this->prepareUserAgent(); |
|
179 | } |
||
180 | 2 | } |
|
181 | |||
182 | 2 | protected function buildBody() |
|
186 | |||
187 | 2 | protected function buildFormParams() |
|
191 | |||
192 | 2 | protected function setFormParams($params) |
|
193 | { |
||
194 | 2 | if (!empty($params)) { |
|
195 | $this->body = is_array($params) ? http_build_query($params, '', '&') : $params; |
||
196 | $this->headers['Content-Type'] = 'application/x-www-form-urlencoded'; |
||
197 | } |
||
198 | 2 | } |
|
199 | |||
200 | 2 | protected function buildProtocolVersion() |
|
204 | |||
205 | 2 | public function serialize() |
|
209 | |||
210 | public function unserialize($string) |
||
211 | { |
||
212 | foreach (unserialize($string) as $key => $value) { |
||
213 | $this->{$key} = $value; |
||
214 | } |
||
215 | } |
||
216 | |||
217 | 2 | public function getParts() |
|
218 | { |
||
219 | 2 | if (empty($this->parts)) { |
|
220 | 2 | $this->build(); |
|
221 | 2 | foreach (['dbname', 'method', 'uri', 'headers', 'body', 'version'] as $key) { |
|
222 | 2 | $this->parts[$key] = $this->{$key}; |
|
223 | } |
||
224 | } |
||
225 | |||
226 | 2 | return $this->parts; |
|
227 | } |
||
228 | |||
229 | 2 | public function isRaw() |
|
233 | |||
234 | protected function getHandler() |
||
235 | { |
||
236 | $handler = $this->getDb()->getHandler(); |
||
237 | if ($handler === null) { |
||
238 | $handler = $this->createHandler(); |
||
239 | } |
||
240 | |||
241 | return $handler; |
||
242 | } |
||
243 | |||
244 | protected function createHandler() |
||
250 | |||
251 | protected function prepareHandlerConfig($config) |
||
255 | |||
256 | 2 | protected function prepareUserAgent() |
|
260 | |||
261 | /** |
||
262 | * @return AbstractConnection|ConnectionInterface |
||
263 | */ |
||
264 | 2 | public function getDb() |
|
265 | { |
||
266 | 2 | return isset($this->builder) ? $this->builder->db : AbstractConnection::getDb($this->dbname); |
|
267 | } |
||
268 | |||
269 | /** |
||
270 | * {@inheritdoc} |
||
271 | * Should be declared abstract, but it is not supported in PHP5. |
||
272 | */ |
||
273 | public static function isSupported() |
||
277 | } |
||
278 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.