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 |
||
| 5 | abstract class Input |
||
| 6 | { |
||
| 7 | /** |
||
| 8 | * Input. |
||
| 9 | * |
||
| 10 | * @var string |
||
| 11 | */ |
||
| 12 | protected $input; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Original input string. |
||
| 16 | * |
||
| 17 | * @var string |
||
| 18 | */ |
||
| 19 | protected $original; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Input name. |
||
| 23 | * |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | protected $name; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Input description. |
||
| 30 | * |
||
| 31 | * @var null|string |
||
| 32 | */ |
||
| 33 | protected $description; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Array of input modes to apply. |
||
| 37 | * |
||
| 38 | * @var array |
||
| 39 | */ |
||
| 40 | protected $modeArray = []; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Argument mode. |
||
| 44 | * |
||
| 45 | * @var int |
||
| 46 | */ |
||
| 47 | protected $mode = 0; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Construct. |
||
| 51 | * |
||
| 52 | * @param string $input |
||
| 53 | */ |
||
| 54 | public function __construct($input) |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Parse the set input string. |
||
| 62 | * |
||
| 63 | * @return $this |
||
| 64 | */ |
||
| 65 | abstract public function parse(); |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Get the input attributes. |
||
| 69 | * |
||
| 70 | * @return array |
||
| 71 | */ |
||
| 72 | abstract public function getAttributes(); |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Set array modeArray value if value contains *. |
||
| 76 | * |
||
| 77 | * @param string $constant |
||
| 78 | * |
||
| 79 | * @return $this |
||
| 80 | */ |
||
| 81 | View Code Duplication | protected function setArray($constant) |
|
| 91 | |||
| 92 | /** |
||
| 93 | * Parse an argument/option description. |
||
| 94 | * |
||
| 95 | * @return $this |
||
| 96 | */ |
||
| 97 | protected function setDescription() |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Calculate the mode score. |
||
| 112 | * |
||
| 113 | * @param string $class |
||
| 114 | * |
||
| 115 | * @return $this |
||
| 116 | */ |
||
| 117 | protected function calculateMode($class) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Set the input name as the input value. |
||
| 128 | * |
||
| 129 | * @return $this |
||
| 130 | */ |
||
| 131 | protected function setName() |
||
| 137 | } |
||
| 138 |