Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like ZillowAPI 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 ZillowAPI, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class ZillowAPI { |
||
25 | /** |
||
26 | * Zillow API Key (aka ZWSID). |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | static private $zws_id; |
||
31 | /** |
||
32 | * Return format. XML or JSON. |
||
33 | * |
||
34 | * @var [string |
||
35 | */ |
||
36 | static private $output; |
||
37 | /** |
||
38 | * Zillow BaseAPI Endpoint |
||
39 | * |
||
40 | * @var string |
||
41 | * @access protected |
||
42 | */ |
||
43 | protected $base_uri = 'https://www.zillow.com/webservice/'; |
||
44 | /** |
||
45 | * Construct. |
||
46 | * |
||
47 | * @access public |
||
48 | * @param mixed $zws_id ZWSID. |
||
49 | * @param mixed $output Output. |
||
50 | * @return void |
||
51 | */ |
||
52 | public function __construct( $zws_id, $output = 'json' ) { |
||
56 | /** |
||
57 | * Fetch the request from the API. |
||
58 | * |
||
59 | * @access private |
||
60 | * @param mixed $request Request URL. |
||
61 | * @return $body Body. |
||
62 | */ |
||
63 | View Code Duplication | private function fetch( $request ) { |
|
72 | /** |
||
73 | * Get Zillow Reviews (https://www.zillow.com/howto/api/ReviewsAPI.htm) |
||
74 | * |
||
75 | * @access public |
||
76 | * @param mixed $screenname Screenname. |
||
77 | * @param mixed $email (default: null) Email. |
||
78 | * @param string $count (default: '3') Count. |
||
79 | * @param mixed $returnTeamMemberReviews (default: null) Return Team Member Reviews. |
||
80 | * @return Request. |
||
81 | */ |
||
82 | function get_reviews( $screenname, $email = null, $count = '3', $returnTeamMemberReviews = null ) { |
||
89 | /** |
||
90 | * Get Mortage Rate Summary (https://www.zillow.com/howto/api/GetRateSummary.htm) |
||
91 | * |
||
92 | * @access public |
||
93 | * @param mixed $state (default: null) State. |
||
94 | * @param string $callback (default: '') Callback. |
||
95 | * @return Request. |
||
96 | */ |
||
97 | function get_rate_summary( $state = null, $callback = '' ) { |
||
101 | /** |
||
102 | * Get Monthly Payments (https://www.zillow.com/howto/api/GetMonthlyPayments.htm) |
||
103 | * |
||
104 | * @access public |
||
105 | * @param mixed $price Price. |
||
106 | * @param mixed $down (default: null) Down. |
||
107 | * @param mixed $dollarsdown (default: null) DollarsDown. |
||
108 | * @param mixed $zip (default: null) Zip. |
||
109 | * @param mixed $output (default: null) Output. |
||
110 | * @param mixed $callback (default: null) Callback. |
||
111 | * @return Request. |
||
112 | */ |
||
113 | function get_monthly_payments( $price, $down = null, $dollarsdown = null, $zip = null, $output = null, $callback = null ) { |
||
120 | /** |
||
121 | * Get Deep Search Results. |
||
122 | * |
||
123 | * @access public |
||
124 | * @param mixed $address Address. |
||
125 | * @param mixed $citystatezip City/State/Zip. |
||
126 | * @param mixed $rentzestimate (default: null) Rent Zestimate. |
||
127 | * @return Request. |
||
128 | */ |
||
129 | function get_deep_search_results( $address, $citystatezip, $rentzestimate = null ) { |
||
139 | /** |
||
140 | * Get Deep Comps. |
||
141 | * |
||
142 | * @access public |
||
143 | * @param mixed $zpid ZPID. |
||
144 | * @param string $count (default: '5') Count. |
||
145 | * @param bool $rentzestimate (default: false) Rent Zestimate. |
||
146 | * @return Request. |
||
147 | */ |
||
148 | View Code Duplication | function get_deep_comps( $zpid, $count = '5', $rentzestimate = false ) { |
|
158 | /** |
||
159 | * Get Updated Property Details. |
||
160 | * |
||
161 | * @access public |
||
162 | * @param mixed $zpid ZPID. |
||
163 | * @return Request. |
||
164 | */ |
||
165 | function get_updated_property_details( $zpid ) { |
||
175 | /** |
||
176 | * Get Search Results. |
||
177 | * |
||
178 | * @access public |
||
179 | * @param mixed $address Address. |
||
180 | * @param mixed $citystatezip City/State/Zip. |
||
181 | * @param bool $rentzestimate (default: false) Rent Zestimate. |
||
182 | * @return Request. |
||
183 | */ |
||
184 | function get_search_results( $address, $citystatezip, $rentzestimate = false ) { |
||
194 | /** |
||
195 | * Get Zillow Zestimate. |
||
196 | * http://wern-ancheta.com/blog/2014/03/20/getting-started-with-zillow-api/ |
||
197 | * https://github.com/letsgetrandy/wp-zestimate |
||
198 | * |
||
199 | * @access public |
||
200 | * @param mixed $zpid ZPID. |
||
201 | * @return Request. |
||
202 | */ |
||
203 | function get_zestimate( $zpid ) { |
||
213 | /** |
||
214 | * Get Chart. |
||
215 | * |
||
216 | * @access public |
||
217 | * @param mixed $zpid ZPID. |
||
218 | * @param mixed $unit_type Unit Type. |
||
219 | * @param string $width (default: '600') Width. |
||
220 | * @param string $height (default: '300') Height. |
||
221 | * @param string $chart_duration (default: '1year') Chart Duration. |
||
222 | * @return Request. |
||
223 | */ |
||
224 | View Code Duplication | function get_chart( $zpid, $unit_type, $width = '600', $height = '300', $chart_duration = '1year' ) { |
|
234 | /** |
||
235 | * Get Comps. |
||
236 | * |
||
237 | * @access public |
||
238 | * @param mixed $zpid ZPID. |
||
239 | * @param mixed $count Count. |
||
240 | * @param bool $rentzestimate (default: false) Rent Zestimate. |
||
241 | * @return Request. |
||
242 | */ |
||
243 | function get_comps( $zpid, $count, $rentzestimate = false ) { |
||
253 | /** |
||
254 | * Get the ZPID from a Zillow Property Url. |
||
255 | * |
||
256 | * @access public |
||
257 | * @param mixed $url URL. |
||
258 | * @return Request. |
||
259 | */ |
||
260 | View Code Duplication | function get_zpid_from_url( $url ) { |
|
268 | /** |
||
269 | * Get Agent/Team Screenname from URL. |
||
270 | * |
||
271 | * @access public |
||
272 | * @param mixed $url URL. |
||
273 | * @return void |
||
274 | */ |
||
275 | View Code Duplication | function get_agent_screenname_from_url( $url ) { |
|
282 | /** |
||
283 | * Response code message for GetSearchResults. |
||
284 | * |
||
285 | * @param [String] $code : Response code to get message from. |
||
286 | * @return [String] : Message corresponding to response code sent in. |
||
287 | */ |
||
288 | public function response_code_msg( $code = '' ) { |
||
335 | } |
||
336 | } |
||
337 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.