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 |
||
| 24 | class PgBytea implements ConverterInterface |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @see Pomm\Converter\ConverterInterface |
||
| 28 | */ |
||
| 29 | View Code Duplication | public function toPg($data, $type, Session $session) |
|
| 30 | { |
||
| 31 | return $data !== null |
||
| 32 | ? sprintf( |
||
| 33 | "%s '%s'", |
||
| 34 | $type, |
||
| 35 | $this->escapeByteString($session, $data) |
||
| 36 | ) |
||
| 37 | : sprintf("NULL::%s", $type) |
||
| 38 | ; |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @see Pomm\Converter\ConverterInterface |
||
| 43 | */ |
||
| 44 | public function fromPg($data, $type, Session $session) |
||
| 45 | { |
||
| 46 | return $data !== null |
||
| 47 | ? $session->getConnection()->unescapeBytea($data) |
||
| 48 | : null |
||
| 49 | ; |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @see Pomm\Converter\ConverterInterface |
||
| 54 | */ |
||
| 55 | View Code Duplication | public function toPgStandardFormat($data, $type, Session $session) |
|
| 56 | { |
||
| 57 | return $data !== null |
||
| 58 | ? sprintf( |
||
| 59 | '%s', |
||
| 60 | $this->escapeByteString($session, $data) |
||
| 61 | ) |
||
| 62 | : null |
||
| 63 | ; |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * escapeByteString |
||
| 68 | * |
||
| 69 | * Escape a binary string to postgres. |
||
| 70 | * |
||
| 71 | * @access protected |
||
| 72 | * @param mixed $string |
||
| 73 | * @param Session $session |
||
| 74 | * @return string |
||
| 75 | */ |
||
| 76 | protected function escapeByteString(Session $session, $string) |
||
| 80 | } |
||
| 81 |