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 |
||
| 31 | class UserNetAPI extends UserAPI { |
||
| 32 | |||
| 33 | /** |
||
| 34 | * nothing special to be done here. |
||
| 35 | */ |
||
| 36 | public function __construct() { |
||
| 39 | |||
| 40 | /** |
||
| 41 | * wrapper JSON function |
||
| 42 | * |
||
| 43 | * @param array|bool|null $data the core data to be converted to JSON |
||
| 44 | * @param int $status extra status information, defaults to 1 |
||
| 45 | * @return string JSON encoded data |
||
| 46 | */ |
||
| 47 | public function returnJSON($data, $status = 1) { |
||
| 54 | |||
| 55 | /** |
||
| 56 | * outputs the list of supported languages. |
||
| 57 | */ |
||
| 58 | public function JSON_listLanguages() { |
||
| 65 | |||
| 66 | /** |
||
| 67 | * outputs the list of countries with configured IdPs |
||
| 68 | * |
||
| 69 | */ |
||
| 70 | public function JSON_listCountries() { |
||
| 78 | |||
| 79 | /** |
||
| 80 | * outputs the list of IdPs in a given country |
||
| 81 | * |
||
| 82 | * @param string $country the country we are interested in |
||
| 83 | */ |
||
| 84 | public function JSON_listIdentityProviders($country) { |
||
| 92 | |||
| 93 | /** |
||
| 94 | * outputs the list of all active IdPs |
||
| 95 | * |
||
| 96 | * The IdP list is formatted for DiscoJuice consumption |
||
| 97 | */ |
||
| 98 | public function JSON_listIdentityProvidersForDisco() { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * outputs the list of IdPs in a given country ordered with respect to their distance to the user's location |
||
| 110 | * |
||
| 111 | * @param string $country the country in question |
||
| 112 | * @param array $location the coordinates of the approximate user location |
||
| 113 | * |
||
| 114 | */ |
||
| 115 | public function JSON_orderIdentityProviders($country, $location = NULL) { |
||
| 123 | |||
| 124 | /** |
||
| 125 | * outputs a list of profiles available for a given IdP |
||
| 126 | * |
||
| 127 | * @param int $idpIdentifier the IdP identifier |
||
| 128 | * @param int $sort should the result set be sorted? 0 = no, 1 = yes |
||
| 129 | */ |
||
| 130 | public function JSON_listProfiles($idpIdentifier, $sort = 0) { |
||
| 153 | |||
| 154 | /** |
||
| 155 | * outputs the list of devices available for the given profile |
||
| 156 | * |
||
| 157 | * @param int $profileId the Profile identifier |
||
| 158 | */ |
||
| 159 | public function JSON_listDevices($profileId) { |
||
| 177 | |||
| 178 | /** |
||
| 179 | * outputs the link to the installers (additionally, actually generates it or takes it from cache) |
||
| 180 | * |
||
| 181 | * @param string $device identifier as in {@link devices.php} |
||
| 182 | * @param int $prof_id profile identifier |
||
| 183 | */ |
||
| 184 | public function JSON_generateInstaller($device, $prof_id) { |
||
| 193 | |||
| 194 | /** |
||
| 195 | * outputs OS guess in JSON |
||
| 196 | */ |
||
| 197 | public function JSON_detectOS() { |
||
| 202 | |||
| 203 | /** |
||
| 204 | * outputs user certificates pertaining to a given token in JSON |
||
| 205 | * @param string $token |
||
| 206 | */ |
||
| 207 | public function JSON_getUserCerts($token) { |
||
| 212 | |||
| 213 | /** outputs the user location as JSON |
||
| 214 | * @throws Exception |
||
| 215 | */ |
||
| 216 | public function JSON_locateUser() { |
||
| 220 | |||
| 221 | /** |
||
| 222 | * outputs support data prepared within {@link GUI::profileAttributes()} |
||
| 223 | */ |
||
| 224 | public function JSON_profileAttributes($prof_id) { |
||
| 228 | |||
| 229 | /** |
||
| 230 | * outputs a logo |
||
| 231 | * |
||
| 232 | * @param string|int $identifier |
||
| 233 | * @param string $type "federation" or "idp" |
||
| 234 | * @param int $width |
||
| 235 | * @param int $height |
||
| 236 | */ |
||
| 237 | View Code Duplication | public function sendLogo($identifier, $type, $width = 0, $height = 0) { |
|
| 244 | |||
| 245 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.