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 |
||
| 21 | class External_Util extends \eoxia\Singleton_Util {
|
||
| 22 | /** |
||
| 23 | * Le constructeur obligatoirement pour utiliser la classe \eoxia\Singleton_Util |
||
| 24 | * |
||
| 25 | * @return void nothing |
||
| 26 | */ |
||
| 27 | protected function construct() {}
|
||
| 28 | |||
| 29 | /** |
||
| 30 | * Parcours le fichier digirisk.config.json pour récupérer les chemins vers tous les modules. |
||
| 31 | * Initialise ensuite un par un, tous ses modules. |
||
| 32 | * |
||
| 33 | * @return mixed WP_Error si les configs de DigiRisk ne sont pas initialisés, ou si aucun module n'est présent. |
||
| 34 | */ |
||
| 35 | public function exec( $path, $plugin_slug ) {
|
||
| 47 | |||
| 48 | /** |
||
| 49 | * Appelle la méthode init_config de \eoxia\Config_Util pour initialiser les configs du module |
||
| 50 | * |
||
| 51 | * @param string $module_json_path Le chemin vers le dossier du module. |
||
| 52 | * @return void nothing |
||
| 53 | */ |
||
| 54 | public function inc_config( $plugin_slug, $module_json_path ) {
|
||
| 57 | |||
| 58 | /** |
||
| 59 | * Inclus les dépendences du module (qui sont défini dans le config.json du module en question) |
||
| 60 | * |
||
| 61 | * @param string $module_json_path Le chemin vers le module. |
||
| 62 | * @return void nothing |
||
| 63 | */ |
||
| 64 | public function inc( $plugin_slug, $module_json_path ) {
|
||
| 84 | |||
| 85 | /** |
||
| 86 | * Inclus les fichiers prioritaires qui se trouvent dans la clé "priority" dans le .config.json du module |
||
| 87 | * |
||
| 88 | * @param string $path_to_module_and_dependence_folder Le chemin vers le module. |
||
| 89 | * @param string $dependence_folder le chemin vers le dossier à inclure. |
||
| 90 | * @param array $list_priority_file La liste des chemins des fichiers à inclure en priorité. |
||
| 91 | * @return void nothing |
||
| 92 | */ |
||
| 93 | View Code Duplication | public function inc_priority_file( $path_to_module_and_dependence_folder, $dependence_folder, $list_priority_file ) {
|
|
| 102 | } |
||
| 103 | } // End if(). |
||
| 104 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: