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 |
||
24 | class WalkscoreAPI { |
||
25 | /** |
||
26 | * API Key. |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | static private $wsapikey; |
||
31 | /** |
||
32 | * Return format. XML or JSON. |
||
33 | * |
||
34 | * @var [string |
||
35 | */ |
||
36 | static private $output; |
||
37 | /** |
||
38 | * URL to the API. |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | private $base_uri = 'http://api.walkscore.com'; |
||
43 | /** |
||
44 | * __construct function. |
||
45 | * |
||
46 | * @access public |
||
47 | * @param mixed $wsapikey API Key. |
||
48 | * @return void |
||
49 | */ |
||
50 | public function __construct( $wsapikey, $output = 'json' ) { |
||
54 | /** |
||
55 | * Fetch the request from the API. |
||
56 | * |
||
57 | * @access private |
||
58 | * @param mixed $request Request URL. |
||
59 | * @return $body Body. |
||
60 | */ |
||
61 | View Code Duplication | private function fetch( $request ) { |
|
70 | /** |
||
71 | * Get Walkscore (https://www.walkscore.com/professional/api.php) |
||
72 | * |
||
73 | * @access public |
||
74 | * @param mixed $wsapikey Your Walk Score API Key. Contact us to get one. Required. |
||
75 | * @param mixed $latitude The latitude of the requested location. Required. |
||
76 | * @param mixed $longitude The longitude of the requested location. Required. |
||
77 | * @param mixed $address The URL encoded address. Required. |
||
78 | * @param mixed $format Return results in XML or JSON (defaults to XML). |
||
79 | * @return void |
||
80 | */ |
||
81 | function get_walkscore( $latitude, $longitude, $address ) { |
||
88 | /** |
||
89 | * get_transit_score function. |
||
90 | * |
||
91 | * @access public |
||
92 | * @param mixed $latitude |
||
93 | * @param mixed $longitude |
||
94 | * @param mixed $city |
||
95 | * @param mixed $state |
||
96 | * @param mixed $country |
||
97 | * @param mixed $research |
||
98 | * @return void |
||
99 | */ |
||
100 | function get_transit_score( $latitude, $longitude, $city, $state, $country, $research ) { |
||
117 | /** |
||
118 | * Response code message |
||
119 | * |
||
120 | * @param [String] $code : Response code to get message from. |
||
121 | * @return [String] : Message corresponding to response code sent in. |
||
122 | */ |
||
123 | public function response_code_msg( $code = '' ) { |
||
152 | } |
||
153 | } |
||
154 |
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.