Complex classes like AbstractEntryPoint 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 AbstractEntryPoint, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | abstract class AbstractEntryPoint implements EPInterface { |
||
14 | |||
15 | /** |
||
16 | * Whether or not Authentication is Required |
||
17 | * @var bool |
||
18 | */ |
||
19 | protected $_AUTH_REQUIRED = true; |
||
20 | |||
21 | /** |
||
22 | * The URL for the EntryPoint |
||
23 | * - When configuring URL you define URL Parameters with $variables |
||
24 | * Examples: |
||
25 | * - Forecasts/$record_id |
||
26 | * - $module Variable is a keyword to place the Module property into the URL |
||
27 | * Examples: |
||
28 | * - $module/$record |
||
29 | * - Options property is used to replace variables in the order in which they are passed |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $_URL; |
||
34 | |||
35 | /** |
||
36 | * An array of Required Data properties that should be passed in the Request |
||
37 | * @var array |
||
38 | */ |
||
39 | protected $_REQUIRED_DATA = array(); |
||
40 | |||
41 | /** |
||
42 | * The required type of Data to be given to the EntryPoint. If none, different types can be passed in. |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $_DATA_TYPE; |
||
46 | |||
47 | /** |
||
48 | * The configured URL for the EntryPoint |
||
49 | * @var string |
||
50 | */ |
||
51 | protected $Url; |
||
52 | |||
53 | /** |
||
54 | * The initial URL passed into the EntryPoint |
||
55 | * @var |
||
56 | */ |
||
57 | protected $baseUrl; |
||
58 | |||
59 | /** |
||
60 | * The configured Module for the EntryPoint |
||
61 | * @var string |
||
62 | */ |
||
63 | protected $Module; |
||
64 | |||
65 | /** |
||
66 | * The passed in Options for the EntryPoint |
||
67 | * - If $module variable is used in $_URL static property, then 1st option will be used as Module |
||
68 | * @var array |
||
69 | */ |
||
70 | protected $Options = array(); |
||
71 | |||
72 | /** |
||
73 | * The data being passed to the API EntryPoint |
||
74 | * @var mixed - array or Std Object |
||
75 | */ |
||
76 | protected $Data; |
||
77 | |||
78 | /** |
||
79 | * The Request Object, used by the EntryPoint to submit the data |
||
80 | * @var Object |
||
81 | */ |
||
82 | protected $Request; |
||
83 | |||
84 | /** |
||
85 | * The Response Object, returned by the Request Object |
||
86 | * @var Object |
||
87 | */ |
||
88 | protected $Response; |
||
89 | |||
90 | /** |
||
91 | * Access Token for authentication |
||
92 | * @var string |
||
93 | */ |
||
94 | protected $accessToken; |
||
95 | |||
96 | |||
97 | public function __construct($url,$options = array()){ |
||
106 | |||
107 | /** |
||
108 | * @inheritdoc |
||
109 | */ |
||
110 | public function setOptions(array $options){ |
||
117 | |||
118 | /** |
||
119 | * @inheritdoc |
||
120 | * @throws RequiredDataException - When passed in data contains issues |
||
121 | */ |
||
122 | public function setData($data){ |
||
129 | |||
130 | /** |
||
131 | * @inheritdoc |
||
132 | */ |
||
133 | public function setAuth($accessToken) { |
||
140 | |||
141 | /** |
||
142 | * @inheritdoc |
||
143 | * @throws InvalidURLException - When passed in URL contains $variables |
||
144 | */ |
||
145 | public function setUrl($url) { |
||
152 | |||
153 | /** |
||
154 | * @inheritdoc |
||
155 | */ |
||
156 | public function setRequest(RequestInterface $Request) { |
||
160 | |||
161 | /** |
||
162 | * @inheritdoc |
||
163 | */ |
||
164 | public function setResponse(ResponseInterface $Response) { |
||
168 | |||
169 | /** |
||
170 | * @inheritdoc |
||
171 | */ |
||
172 | public function getModule() { |
||
175 | |||
176 | /** |
||
177 | * @inheritdoc |
||
178 | */ |
||
179 | public function getData(){ |
||
182 | |||
183 | /** |
||
184 | * @inheritdoc |
||
185 | */ |
||
186 | public function getUrl(){ |
||
189 | |||
190 | /** |
||
191 | * @inheritdoc |
||
192 | */ |
||
193 | public function getResponse(){ |
||
196 | |||
197 | /** |
||
198 | * @inheritdoc |
||
199 | */ |
||
200 | public function getRequest(){ |
||
203 | |||
204 | /** |
||
205 | * @inheritdoc |
||
206 | */ |
||
207 | public function execute($data = null){ |
||
214 | |||
215 | /** |
||
216 | * @inheritdoc |
||
217 | */ |
||
218 | public function authRequired() { |
||
221 | |||
222 | /** |
||
223 | * Override function for configuring Default Values on some EntryPoints to allow for short hand |
||
224 | */ |
||
225 | protected function configureData($data){ |
||
235 | |||
236 | /** |
||
237 | * Configures the URL, by updating any variable placeholders in the URL property on the EntryPoint |
||
238 | * - Replaces $module with $this->Module |
||
239 | * - Replaces all other variables starting with $, with options in the order they were given |
||
240 | */ |
||
241 | protected function configureURL(){ |
||
259 | |||
260 | /** |
||
261 | * Verify if URL is configured properly |
||
262 | * @return bool |
||
263 | * @throws InvalidURLException |
||
264 | */ |
||
265 | protected function verifyUrl(){ |
||
272 | |||
273 | /** |
||
274 | * Verify URL variables have been removed, and that valid number of options were passed. |
||
275 | * @return bool |
||
276 | * @throws RequiredOptionsException |
||
277 | */ |
||
278 | protected function verifyOptions(){ |
||
287 | |||
288 | /** |
||
289 | * Validate Required Data for the Request |
||
290 | * @return bool |
||
291 | * @throws RequiredDataException |
||
292 | */ |
||
293 | protected function verifyData(){ |
||
312 | |||
313 | /** |
||
314 | * Checks if EntryPoint URL contains requires Options |
||
315 | * @return bool |
||
316 | */ |
||
317 | protected function requiresOptions(){ |
||
320 | |||
321 | } |