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:
1 | <?php |
||
23 | class GreatSchoolsAPI { |
||
24 | /** |
||
25 | * API Key. |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | static private $api_key; |
||
30 | /** |
||
31 | * Output Type. |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | static private $output; |
||
36 | /** |
||
37 | * BaseAPI Endpoint |
||
38 | * |
||
39 | * @var string |
||
40 | * @access public |
||
41 | */ |
||
42 | public $base_uri = 'http://api.greatschools.org/schools/'; |
||
43 | /** |
||
44 | * __construct function. |
||
45 | * |
||
46 | * @access public |
||
47 | * @param mixed $api_key API Key |
||
48 | * @param string $output (default: 'json') Output. |
||
49 | * @return void |
||
50 | */ |
||
51 | public function __construct( $api_key, $output = 'json' ) { |
||
55 | /** |
||
56 | * Fetch the request from the API. |
||
57 | * |
||
58 | * @access private |
||
59 | * @param mixed $request Request URL. |
||
60 | * @return $body Body. |
||
61 | */ |
||
62 | View Code Duplication | private function fetch( $request ) { |
|
71 | /** |
||
72 | * get_schools function. |
||
73 | * |
||
74 | * @access public |
||
75 | * @param mixed $state State. |
||
76 | * @param mixed $city City. |
||
77 | * @return void |
||
78 | */ |
||
79 | View Code Duplication | public function get_schools( $state, $city ) { |
|
86 | /** |
||
87 | * get_nearby_schools function. |
||
88 | * |
||
89 | * @access public |
||
90 | * @param mixed $state State. |
||
91 | * @param mixed $zip Zip. |
||
92 | * @param mixed $city City. |
||
93 | * @param mixed $address Address. |
||
94 | * @param mixed $latitude Latitude. |
||
95 | * @param mixed $longitude Longitude. |
||
96 | * @param mixed $school_type School Type. |
||
97 | * @param mixed $level_code Level Code. |
||
98 | * @param mixed $radius Radius. |
||
99 | * @param mixed $limit Limit. |
||
100 | * @return void |
||
101 | */ |
||
102 | public function get_nearby_schools( $state, $zip, $city, $address, $latitude, $longitude, $school_type, $level_code, $radius, $limit ) { |
||
104 | /** |
||
105 | * Returns a profile data for a specific school. |
||
106 | * |
||
107 | * @access public |
||
108 | * @param mixed $state State. |
||
109 | * @param mixed $school_id School ID. |
||
110 | * @return void |
||
111 | */ |
||
112 | View Code Duplication | public function get_school( $state, $school_id ) { |
|
119 | /** |
||
120 | * Returns a list of schools based on a search string. |
||
121 | * |
||
122 | * @access public |
||
123 | * @param mixed $state State. |
||
124 | * @param mixed $query_string Query String. |
||
125 | * @return void |
||
126 | */ |
||
127 | View Code Duplication | public function search_for_schools( $state, $query_string, $level_code, $sort, $limit ) { |
|
134 | /** |
||
135 | * Returns a list of the most recent reviews for a school or for any schools in a city. |
||
136 | * |
||
137 | * @access public |
||
138 | * @param mixed $state State. |
||
139 | * @param mixed $city City. |
||
140 | * @param mixed $school_id School ID. |
||
141 | * @param mixed $cutoffage Cut Off Age. |
||
142 | * @param mixed $limit Limit. |
||
143 | * @param mixed $topic_id Topic ID. |
||
144 | * @return void |
||
145 | */ |
||
146 | View Code Duplication | public function get_school_reviews( $state, $city, $school_id, $cutoffage, $limit, $topic_id ) { |
|
153 | /** |
||
154 | * Returns a list of topics available for topical reviews. |
||
155 | * |
||
156 | * @access public |
||
157 | * @return void |
||
158 | */ |
||
159 | public function get_review_topics() { |
||
166 | /** |
||
167 | * Returns test and rank data for a specific school. |
||
168 | * |
||
169 | * @access public |
||
170 | * @param mixed $state State. |
||
171 | * @param mixed $school_id School ID. |
||
172 | * @return void |
||
173 | */ |
||
174 | View Code Duplication | public function get_school_test_scores( $state, $school_id ) { |
|
181 | /** |
||
182 | * Returns census and profile data for a school. |
||
183 | * |
||
184 | * @access public |
||
185 | * @param mixed $state State. |
||
186 | * @param mixed $school_id School ID. |
||
187 | * @return void |
||
188 | */ |
||
189 | View Code Duplication | public function get_school_census_data( $state, $school_id ) { |
|
196 | /** |
||
197 | * Returns information about a city. |
||
198 | * |
||
199 | * @access public |
||
200 | * @param mixed $state State. |
||
201 | * @param mixed $city City. |
||
202 | * @return void |
||
203 | */ |
||
204 | View Code Duplication | public function get_city_schools( $state, $city ) { |
|
211 | /** |
||
212 | * Returns a list of cities near another city. |
||
213 | * |
||
214 | * @access public |
||
215 | * @param mixed $state State. |
||
216 | * @param mixed $city City. |
||
217 | * @param mixed $radius Radius. |
||
218 | * @param mixed $sort Sort. |
||
219 | * @return void |
||
220 | */ |
||
221 | View Code Duplication | public function get_nearby_cities( $state, $city, $radius, $sort = 'rating' ) { |
|
228 | /** |
||
229 | * get_districts function. |
||
230 | * |
||
231 | * @access public |
||
232 | * @param mixed $state State. |
||
233 | * @param mixed $city City. |
||
234 | * @return void |
||
235 | */ |
||
236 | View Code Duplication | public function get_districts( $state, $city ) { |
|
243 | } |
||
244 | } |
||
245 |
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.