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 |
||
| 11 | class File implements Storage |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * $path. |
||
| 15 | * |
||
| 16 | * @var string |
||
| 17 | */ |
||
| 18 | public $path; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * $suffix. |
||
| 22 | * |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | public $suffix = '.rules'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * cached. |
||
| 29 | * |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | public static $cached = [ |
||
| 33 | 'zip3' => null, |
||
| 34 | 'zip5' => null, |
||
| 35 | ]; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * __construct. |
||
| 39 | * |
||
| 40 | * @param string $path |
||
| 41 | */ |
||
| 42 | 13 | public function __construct($path = null) |
|
| 46 | |||
| 47 | /** |
||
| 48 | * zip3. |
||
| 49 | * |
||
| 50 | * @param \Recca0120\Twzipcode\Address $address |
||
| 51 | * @return string |
||
| 52 | */ |
||
| 53 | 9 | public function zip3(Address $address) |
|
| 60 | |||
| 61 | /** |
||
| 62 | * rules. |
||
| 63 | * |
||
| 64 | * @param string $zip3 |
||
| 65 | * @return \Recca0120\Lodash\JArray |
||
| 66 | */ |
||
| 67 | 8 | public function rules($zip3) |
|
| 75 | |||
| 76 | /** |
||
| 77 | * load. |
||
| 78 | * |
||
| 79 | * @param string $source |
||
| 80 | * @return $this |
||
| 81 | */ |
||
| 82 | 13 | public function load($source) |
|
| 107 | |||
| 108 | /** |
||
| 109 | * loadFile. |
||
| 110 | * |
||
| 111 | * @param string $file |
||
| 112 | * @return $this |
||
| 113 | */ |
||
| 114 | 1 | public function loadFile($file = null) |
|
| 115 | { |
||
| 116 | 1 | $file = $file ?: $this->path.'../Zip32_utf8_10501_1.csv'; |
|
| 117 | 1 | $this->load($this->getSource($file)); |
|
| 118 | |||
| 119 | 1 | return $this; |
|
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * flush. |
||
| 124 | * |
||
| 125 | * @return $this |
||
| 126 | */ |
||
| 127 | 13 | public function flush() |
|
| 136 | |||
| 137 | /** |
||
| 138 | * getSource. |
||
| 139 | * |
||
| 140 | * @param string $file |
||
| 141 | * @return string |
||
| 142 | */ |
||
| 143 | 1 | protected function getSource($file) |
|
| 161 | |||
| 162 | /** |
||
| 163 | * prepareSource. |
||
| 164 | * |
||
| 165 | * @param string $source |
||
| 166 | * @return array |
||
| 167 | */ |
||
| 168 | 13 | protected function prepareSource($source) |
|
| 189 | |||
| 190 | /** |
||
| 191 | * each. |
||
| 192 | * |
||
| 193 | * @param array $rules |
||
| 194 | * @param \Closure $callback |
||
| 195 | */ |
||
| 196 | 13 | protected function each($rules, $callback) |
|
| 206 | |||
| 207 | /** |
||
| 208 | * compress. |
||
| 209 | * |
||
| 210 | * @param \Recca0120\Lodash\JArray $plainText |
||
| 211 | * @return string |
||
| 212 | */ |
||
| 213 | 13 | protected function compress($plainText) |
|
| 217 | |||
| 218 | /** |
||
| 219 | * decompress. |
||
| 220 | * |
||
| 221 | * @param string $compressed |
||
| 222 | * @return mixed |
||
| 223 | */ |
||
| 224 | 10 | protected function decompress($compressed) |
|
| 228 | |||
| 229 | /** |
||
| 230 | * store. |
||
| 231 | * |
||
| 232 | * @param string $filename |
||
| 233 | * @param \Recca0120\Lodash\JArray $data |
||
| 234 | * @return $this |
||
| 235 | */ |
||
| 236 | 13 | protected function store($filename, $data) |
|
| 245 | |||
| 246 | /** |
||
| 247 | * restore. |
||
| 248 | * |
||
| 249 | * @param string $filename |
||
| 250 | * @return mixed |
||
| 251 | */ |
||
| 252 | 10 | protected function restore($filename) |
|
| 266 | } |
||
| 267 |