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 |
||
26 | class PgTimestamp implements ConverterInterface |
||
27 | { |
||
28 | const TS_FORMAT = 'Y-m-d H:i:s.uP'; |
||
29 | |||
30 | /** |
||
31 | * fromPg |
||
32 | * |
||
33 | * @see ConverterInterface |
||
34 | */ |
||
35 | public function fromPg($data, $type, Session $session) |
||
36 | { |
||
37 | $data = trim($data); |
||
38 | |||
39 | return $data !== '' ? new \DateTime($data) : null; |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * toPg |
||
44 | * |
||
45 | * @see ConverterInterface |
||
46 | */ |
||
47 | View Code Duplication | public function toPg($data, $type, Session $session) |
|
55 | |||
56 | /** |
||
57 | * toPgStandardFormat |
||
58 | * |
||
59 | * @see ConverterInterface |
||
60 | */ |
||
61 | public function toPgStandardFormat($data, $type, Session $session) |
||
62 | { |
||
63 | return |
||
64 | $data !== null |
||
65 | ? $this->checkData($data)->format(static::TS_FORMAT) |
||
66 | : null |
||
67 | ; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * checkData |
||
72 | * |
||
73 | * Ensure a DateTime instance. |
||
74 | * |
||
75 | * @access protected |
||
76 | * @param mixed $data |
||
77 | * @throws ConverterException |
||
78 | * @return \DateTime |
||
79 | */ |
||
80 | View Code Duplication | protected function checkData($data) |
|
99 | } |
||
100 |