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 | class XdtParser |
||
|
|
|||
| 6 | { |
||
| 7 | /** |
||
| 8 | * Array to hold mappings to different xdt keys |
||
| 9 | * Eg: ['first_name' => '2031', 'last_name' => '2031']; |
||
| 10 | * @var array |
||
| 11 | */ |
||
| 12 | private $fieldsMap; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Holds the content unparsed rows |
||
| 16 | * @var array |
||
| 17 | */ |
||
| 18 | private $xdtRows = []; |
||
| 19 | |||
| 20 | /** @var array */ |
||
| 21 | private $parsedRows = []; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @param string $content |
||
| 25 | * @param array $fieldsMap |
||
| 26 | * @return XdtParser |
||
| 27 | */ |
||
| 28 | public static function make(string $content, array $fieldsMap = []) |
||
| 32 | |||
| 33 | 42 | /** |
|
| 34 | * XdtParser constructor. |
||
| 35 | * @param string $content |
||
| 36 | * @param array $fieldsMap |
||
| 37 | */ |
||
| 38 | private function __construct(string $content, array $fieldsMap = []) |
||
| 44 | 42 | ||
| 45 | 42 | private function parseXdtRows() |
|
| 54 | 42 | ||
| 55 | /** |
||
| 56 | 42 | * @param string $string |
|
| 57 | * @return array |
||
| 58 | */ |
||
| 59 | public function parseSingle(string $string) |
||
| 73 | 42 | ||
| 74 | /** |
||
| 75 | * @param string $field |
||
| 76 | * @return null |
||
| 77 | */ |
||
| 78 | public function first(string $field) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @param string $field |
||
| 91 | * @return array|mixed|null |
||
| 92 | */ |
||
| 93 | public function find(string $field) |
||
| 112 | 21 | ||
| 113 | /** |
||
| 114 | * @param string $field |
||
| 115 | * @return string |
||
| 116 | */ |
||
| 117 | public function getKey(string $field) |
||
| 121 | |||
| 122 | 30 | /** |
|
| 123 | * @param string $key |
||
| 124 | * @return string |
||
| 125 | */ |
||
| 126 | public function getFieldName(string $key) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @return array |
||
| 138 | */ |
||
| 139 | public function getMapped() |
||
| 149 | |||
| 150 | 12 | /** |
|
| 151 | * @return array |
||
| 152 | 12 | */ |
|
| 153 | public function all() |
||
| 164 | 3 | ||
| 165 | /** |
||
| 166 | 3 | * @param array $fieldsMap |
|
| 167 | * @return XdtParser |
||
| 168 | 3 | */ |
|
| 169 | 3 | public function setFieldsMap(array $fieldsMap) |
|
| 174 | |||
| 175 | /** |
||
| 176 | * @return array |
||
| 177 | */ |
||
| 178 | public function getFieldsMap() |
||
| 182 | 3 | ||
| 183 | 3 | /** |
|
| 184 | * @param array $fields |
||
| 185 | */ |
||
| 186 | public function addFieldsMap(array $fields) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @param array $fields |
||
| 195 | */ |
||
| 196 | public function removeFields(array $fields) |
||
| 202 | 3 | ||
| 203 | /** |
||
| 204 | * @return array |
||
| 205 | */ |
||
| 206 | public function getXdtRows(): array |
||
| 210 | } |