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 PMF_Configuration 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 PMF_Configuration, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class PMF_Configuration |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | protected $_tableName = 'faqconfig'; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | public $config = []; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Constructor. |
||
| 48 | * |
||
| 49 | * @param PMF_DB_Driver $database |
||
| 50 | * |
||
| 51 | * @return PMF_Configuration |
||
| 52 | */ |
||
| 53 | public function __construct(PMF_DB_Driver $database) |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Fetches all configuration items into an array. |
||
| 60 | */ |
||
| 61 | public function getAll() |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Returns a configuration item. |
||
| 87 | * |
||
| 88 | * @param string $item Configuration item |
||
| 89 | * |
||
| 90 | * @return mixed |
||
| 91 | */ |
||
| 92 | public function get($item) |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Sets one single configuration item. |
||
| 117 | * |
||
| 118 | * @param string $key |
||
| 119 | * @param mixed $value |
||
| 120 | * |
||
| 121 | * @return bool |
||
| 122 | */ |
||
| 123 | View Code Duplication | public function set($key, $value) |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Sets the PMF_DB_Driver object. |
||
| 137 | * |
||
| 138 | * @param PMF_DB_Driver $database |
||
| 139 | */ |
||
| 140 | public function setDb(PMF_DB_Driver $database) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Returns the PMF_DB_Driver object. |
||
| 147 | * |
||
| 148 | * @return PMF_DB_Driver |
||
| 149 | */ |
||
| 150 | public function getDb() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Sets the PMF_Instance object. |
||
| 157 | * |
||
| 158 | * @param PMF_Instance $instance |
||
| 159 | */ |
||
| 160 | public function setInstance(PMF_Instance $instance) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Returns the PMF_Instance object. |
||
| 167 | * |
||
| 168 | * @return PMF_Instance |
||
| 169 | */ |
||
| 170 | public function getInstance() |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Sets the Language object. |
||
| 177 | * |
||
| 178 | * @param PMF_Language $language |
||
| 179 | */ |
||
| 180 | public function setLanguage(PMF_Language $language) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Returns the Language object. |
||
| 187 | * |
||
| 188 | * @return PMF_Language |
||
| 189 | */ |
||
| 190 | public function getLanguage() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Returns the default language. |
||
| 197 | * |
||
| 198 | * @return string |
||
| 199 | */ |
||
| 200 | public function getDefaultLanguage() |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Returns the default URL of the phpMyFAQ installation. |
||
| 207 | * |
||
| 208 | * @return string |
||
| 209 | */ |
||
| 210 | public function getDefaultUrl() |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Sets the PMF_Ldap object. |
||
| 223 | * |
||
| 224 | * @param PMF_Ldap $ldap |
||
| 225 | */ |
||
| 226 | public function setLdap(PMF_Ldap $ldap) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Returns the PMF_Ldap object. |
||
| 233 | * |
||
| 234 | * @return PMF_Ldap |
||
| 235 | */ |
||
| 236 | public function getLdap() |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Sets the LDAP configuration. |
||
| 243 | * |
||
| 244 | * @param Array $ldapConfig |
||
| 245 | */ |
||
| 246 | public function setLdapConfig(Array $ldapConfig) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Returns the LDAP configuration. |
||
| 284 | * |
||
| 285 | * @return array |
||
| 286 | */ |
||
| 287 | public function getLdapConfig() |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Returns the LDAP server(s). |
||
| 294 | * |
||
| 295 | * @return array |
||
| 296 | */ |
||
| 297 | public function getLdapServer() |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Sets the Elasticsearch client instance. |
||
| 304 | * |
||
| 305 | * @param Elasticsearch\Client $esClient |
||
| 306 | */ |
||
| 307 | public function setElasticsearch(Elasticsearch\Client $esClient) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Returns the Elasticsearch client instance. |
||
| 314 | * |
||
| 315 | * @return Elasticsearch\Client |
||
| 316 | */ |
||
| 317 | public function getElasticsearch() |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Sets the Elasticsearch configuration. |
||
| 324 | * |
||
| 325 | * @param array $data |
||
| 326 | */ |
||
| 327 | public function setElasticsearchConfig(Array $data) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Returns the Elasticsearch configuration. |
||
| 334 | * |
||
| 335 | * @return array |
||
| 336 | */ |
||
| 337 | public function getElasticsearchConfig() |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Adds a configuration item for the database. |
||
| 344 | * |
||
| 345 | * @param string $name |
||
| 346 | * @param mixed $value |
||
| 347 | * |
||
| 348 | * @return bool |
||
| 349 | */ |
||
| 350 | View Code Duplication | public function add($name, $value) |
|
| 365 | |||
| 366 | /** |
||
| 367 | * Deletes a configuration item for the database. |
||
| 368 | * |
||
| 369 | * @param string $name |
||
| 370 | * |
||
| 371 | * @return bool |
||
| 372 | */ |
||
| 373 | View Code Duplication | public function delete($name) |
|
| 387 | |||
| 388 | /** |
||
| 389 | * Updates all configuration items. |
||
| 390 | * |
||
| 391 | * @param array $newConfigs Array with new configuration values |
||
| 392 | * |
||
| 393 | * @return bool |
||
| 394 | */ |
||
| 395 | public function update(Array $newConfigs) |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Returns all sorting possibilities for FAQ records. |
||
| 440 | * |
||
| 441 | * @param string $current |
||
| 442 | * |
||
| 443 | * @return string |
||
| 444 | */ |
||
| 445 | public static function sortingOptions($current) |
||
| 461 | } |
||
| 462 |
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.