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 | /** @var bool */ |
||
| 15 | private $corrupted = false; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Holds the content unparsed rows |
||
| 19 | * @var array |
||
| 20 | */ |
||
| 21 | private $xdtRows = []; |
||
| 22 | |||
| 23 | /** @var array */ |
||
| 24 | private $parsedRows = []; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @param string $content |
||
| 28 | * @param array $fieldsMap |
||
| 29 | * @return XdtParser |
||
| 30 | */ |
||
| 31 | 42 | public static function make(string $content, array $fieldsMap = []) |
|
| 35 | |||
| 36 | /** |
||
| 37 | * XdtParser constructor. |
||
| 38 | * @param string $content |
||
| 39 | * @param array $fieldsMap |
||
| 40 | */ |
||
| 41 | 42 | private function __construct(string $content, array $fieldsMap = []) |
|
| 47 | |||
| 48 | 42 | private function parseXdtRows() |
|
| 57 | |||
| 58 | /** |
||
| 59 | * @param string $string |
||
| 60 | * @return array |
||
| 61 | */ |
||
| 62 | 42 | public function parseSingle(string $string) |
|
| 63 | { |
||
| 64 | 42 | $matched = preg_match('/^\\r?\\n?(\\d{3})(\\d{4})(.*?)\\r?\\n?$/', $string, $matches); |
|
| 65 | |||
| 66 | 42 | if (!$matched) { |
|
| 67 | $this->corrupted = true; |
||
| 68 | } |
||
| 69 | |||
| 70 | return [ |
||
| 71 | 42 | 'length' => $matches[1] ? intval($matches[1]) : null, |
|
| 72 | 42 | 'key' => $matches[2] ?? null, |
|
| 73 | 42 | 'value' => $matches[3] ?? null |
|
| 74 | ]; |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @param string $field |
||
| 79 | * @return null |
||
| 80 | */ |
||
| 81 | 6 | public function first(string $field) |
|
| 91 | |||
| 92 | /** |
||
| 93 | * @param string $field |
||
| 94 | * @return array|mixed|null |
||
| 95 | */ |
||
| 96 | 21 | public function find(string $field) |
|
| 115 | |||
| 116 | /** |
||
| 117 | * @param string $field |
||
| 118 | * @return string |
||
| 119 | */ |
||
| 120 | 30 | public function getKey(string $field) |
|
| 124 | |||
| 125 | /** |
||
| 126 | * @param string $key |
||
| 127 | * @return string |
||
| 128 | */ |
||
| 129 | 3 | public function getFieldName(string $key) |
|
| 138 | |||
| 139 | /** |
||
| 140 | * @return bool |
||
| 141 | */ |
||
| 142 | 3 | public function isCorrupt() |
|
| 146 | |||
| 147 | /** |
||
| 148 | * @return array |
||
| 149 | */ |
||
| 150 | 12 | public function getMapped() |
|
| 160 | |||
| 161 | /** |
||
| 162 | * @return array |
||
| 163 | */ |
||
| 164 | 3 | public function all() |
|
| 175 | |||
| 176 | /** |
||
| 177 | * @param array $fieldsMap |
||
| 178 | * @return XdtParser |
||
| 179 | */ |
||
| 180 | 3 | public function setFieldsMap(array $fieldsMap) |
|
| 185 | |||
| 186 | /** |
||
| 187 | * @return array |
||
| 188 | */ |
||
| 189 | public function getFieldsMap() |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @param array $fields |
||
| 196 | */ |
||
| 197 | 3 | public function addFieldsMap(array $fields) |
|
| 203 | |||
| 204 | /** |
||
| 205 | * @param array $fields |
||
| 206 | */ |
||
| 207 | 3 | public function removeFields(array $fields) |
|
| 213 | |||
| 214 | /** |
||
| 215 | * @return array |
||
| 216 | */ |
||
| 217 | 3 | public function getXdtRows(): array |
|
| 221 | } |