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 |
||
| 36 | class ShiConverter extends LanguageConverter { |
||
| 37 | protected $mDoContentConvert; |
||
| 38 | |||
| 39 | public $mToLatin = [ |
||
| 40 | 'ⴰ' => 'a', 'ⴱ' => 'b', 'ⴳ' => 'g', 'ⴷ' => 'd', 'ⴹ' => 'ḍ', 'ⴻ' => 'e', |
||
| 41 | 'ⴼ' => 'f', 'ⴽ' => 'k', 'ⵀ' => 'h', 'ⵃ' => 'ḥ', 'ⵄ' => 'ε', 'ⵅ' => 'x', |
||
| 42 | 'ⵇ' => 'q', 'ⵉ' => 'i', 'ⵊ' => 'j', 'ⵍ' => 'l', 'ⵎ' => 'm', 'ⵏ' => 'n', |
||
| 43 | 'ⵓ' => 'u', 'ⵔ' => 'r', 'ⵕ' => 'ṛ', 'ⵖ' => 'γ', 'ⵙ' => 's', 'ⵚ' => 'ṣ', |
||
| 44 | 'ⵛ' => 'š', 'ⵜ' => 't', 'ⵟ' => 'ṭ', 'ⵡ' => 'w', 'ⵢ' => 'y', 'ⵣ' => 'z', |
||
| 45 | 'ⵥ' => 'ẓ', 'ⵯ' => 'ʷ', 'ⵖ' => 'ɣ', 'ⵠ' => 'v', 'ⵒ' => 'p', |
||
| 46 | ]; |
||
| 47 | |||
| 48 | public $mUpperToLowerCaseLatin = [ |
||
| 49 | 'A' => 'a', 'B' => 'b', 'C' => 'c', 'D' => 'd', 'E' => 'e', |
||
| 50 | 'F' => 'f', 'G' => 'g', 'H' => 'h', 'I' => 'i', 'J' => 'j', |
||
| 51 | 'K' => 'k', 'L' => 'l', 'M' => 'm', 'N' => 'n', 'O' => 'o', |
||
| 52 | 'P' => 'p', 'Q' => 'q', 'R' => 'r', 'S' => 's', 'T' => 't', |
||
| 53 | 'U' => 'u', 'V' => 'v', 'W' => 'w', 'X' => 'x', 'Y' => 'y', |
||
| 54 | 'Z' => 'z', 'Ɣ' => 'ɣ', |
||
| 55 | ]; |
||
| 56 | |||
| 57 | public $mToTifinagh = [ |
||
| 58 | 'a' => 'ⴰ', 'b' => 'ⴱ', 'g' => 'ⴳ', 'd' => 'ⴷ', 'ḍ' => 'ⴹ', 'e' => 'ⴻ', |
||
| 59 | 'f' => 'ⴼ', 'k' => 'ⴽ', 'h' => 'ⵀ', 'ḥ' => 'ⵃ', 'ε' => 'ⵄ', 'x' => 'ⵅ', |
||
| 60 | 'q' => 'ⵇ', 'i' => 'ⵉ', 'j' => 'ⵊ', 'l' => 'ⵍ', 'm' => 'ⵎ', 'n' => 'ⵏ', |
||
| 61 | 'u' => 'ⵓ', 'r' => 'ⵔ', 'ṛ' => 'ⵕ', 'γ' => 'ⵖ', 's' => 'ⵙ', 'ṣ' => 'ⵚ', |
||
| 62 | 'š' => 'ⵛ', 't' => 'ⵜ', 'ṭ' => 'ⵟ', 'w' => 'ⵡ', 'y' => 'ⵢ', 'z' => 'ⵣ', |
||
| 63 | 'ẓ' => 'ⵥ', 'ʷ' => 'ⵯ', 'ɣ' => 'ⵖ', 'v' => 'ⵠ', 'p' => 'ⵒ', |
||
| 64 | ]; |
||
| 65 | |||
| 66 | View Code Duplication | function loadDefaultTables() { |
|
| 74 | |||
| 75 | /** |
||
| 76 | * rules should be defined as -{Tifinagh | Latin-} -or- |
||
| 77 | * -{code:text | code:text | ...}- |
||
| 78 | * update: delete all rule parsing because it's not used |
||
| 79 | * currently, and just produces a couple of bugs |
||
| 80 | * |
||
| 81 | * @param string $rule |
||
| 82 | * @param array $flags |
||
| 83 | * @return array |
||
| 84 | */ |
||
| 85 | View Code Duplication | function parseManualRule( $rule, $flags = [] ) { |
|
| 98 | |||
| 99 | /** |
||
| 100 | * Do not convert content on talk pages |
||
| 101 | * |
||
| 102 | * @param string $text |
||
| 103 | * @param Parser $parser |
||
| 104 | * @return string |
||
| 105 | */ |
||
| 106 | function parserConvert( $text, &$parser ) { |
||
| 112 | |||
| 113 | /** |
||
| 114 | * A function wrapper: |
||
| 115 | * - if there is no selected variant, leave the link |
||
| 116 | * names as they were |
||
| 117 | * - do not try to find variants for usernames |
||
| 118 | * |
||
| 119 | * @param string &$link |
||
| 120 | * @param Title &$nt |
||
| 121 | * @param bool $ignoreOtherCond |
||
| 122 | */ |
||
| 123 | View Code Duplication | function findVariantLink( &$link, &$nt, $ignoreOtherCond = false ) { |
|
| 138 | |||
| 139 | /** |
||
| 140 | * It translates text into variant |
||
| 141 | * |
||
| 142 | * @param string $text |
||
| 143 | * @param string $toVariant |
||
| 144 | * |
||
| 145 | * @return string |
||
| 146 | */ |
||
| 147 | View Code Duplication | function translate( $text, $toVariant ) { |
|
| 160 | } |
||
| 161 | |||
| 182 |
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: