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 |
||
| 25 | class PgInterval implements ConverterInterface |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * @see ConverterInterface |
||
| 29 | */ |
||
| 30 | public function fromPg($data, $type, Session $session) |
||
| 31 | { |
||
| 32 | if (trim($data) === '') { |
||
| 33 | return null; |
||
| 34 | } |
||
| 35 | |||
| 36 | try { |
||
| 37 | return new \DateInterval(preg_replace('/\.[0-9]+S/', 'S', $data)); |
||
| 38 | } catch (\Exception $e) { |
||
| 39 | throw new ConverterException(sprintf("Data '%s' is not an ISO8601 interval representation.", $data), null, $e); |
||
| 40 | } |
||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @see ConverterInterface |
||
| 45 | */ |
||
| 46 | View Code Duplication | public function toPg($data, $type, Session $session) |
|
| 53 | |||
| 54 | |||
| 55 | /** |
||
| 56 | * @see ConverterInterface |
||
| 57 | */ |
||
| 58 | View Code Duplication | public function toPgStandardFormat($data, $type, Session $session) |
|
| 59 | { |
||
| 60 | return $data !== null |
||
| 61 | ? sprintf('"%s"', $this->checkData($data)->format('%Y years %M months %D days %H:%i:%S')) |
||
| 62 | : null |
||
| 63 | ; |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * checkData |
||
| 68 | * |
||
| 69 | * Check if Data is a DateInterval. If not, it tries to instantiate a |
||
| 70 | * DateInterval with the given data. |
||
| 71 | * |
||
| 72 | * @access protected |
||
| 73 | * @param mixed $data |
||
| 74 | * @throws ConverterException |
||
| 75 | * @return \DateInterval $data |
||
| 76 | */ |
||
| 77 | View Code Duplication | protected function checkData($data) |
|
| 89 | } |
||
| 90 |