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 |
||
| 28 | abstract class TypeConverter implements ConverterInterface |
||
| 29 | { |
||
| 30 | protected $class_name; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * getTypeClassName |
||
| 34 | * |
||
| 35 | * Return the type class name |
||
| 36 | * |
||
| 37 | * @access protected |
||
| 38 | * @return string |
||
| 39 | */ |
||
| 40 | abstract protected function getTypeClassName(); |
||
| 41 | |||
| 42 | /** |
||
| 43 | * __construct |
||
| 44 | * |
||
| 45 | * Set the type class name. |
||
| 46 | * |
||
| 47 | * @access public |
||
| 48 | * @param string $class_name |
||
| 49 | */ |
||
| 50 | public function __construct($class_name = null) |
||
| 51 | { |
||
| 52 | $this->class_name = |
||
| 53 | $class_name === null |
||
| 54 | ? $this->getTypeClassName() |
||
| 55 | : $class_name |
||
| 56 | ; |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * fromPg |
||
| 61 | * |
||
| 62 | * @see ConverterInterface |
||
| 63 | */ |
||
| 64 | public function fromPg($data, $type, Session $session) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * toPg |
||
| 77 | * |
||
| 78 | * @see ConverterInterface |
||
| 79 | */ |
||
| 80 | View Code Duplication | public function toPg($data, $type, Session $session) |
|
| 88 | |||
| 89 | /** |
||
| 90 | * toPgStandardFormat |
||
| 91 | * |
||
| 92 | * @see ConverterInterface |
||
| 93 | */ |
||
| 94 | View Code Duplication | public function toPgStandardFormat($data, $type, Session $session) |
|
| 95 | { |
||
| 96 | return |
||
| 97 | $data !== null |
||
| 98 | ? sprintf("%s", str_replace('"', '""', (string) $this->checkData($data))) |
||
| 99 | : null |
||
| 100 | ; |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * checkData |
||
| 105 | * |
||
| 106 | * Check if data is suitable for Pg conversion. If not an attempt is made |
||
| 107 | * to build the object from the given definition. |
||
| 108 | * |
||
| 109 | * @access public |
||
| 110 | * @param mixed $data |
||
| 111 | * @return object |
||
| 112 | */ |
||
| 113 | public function checkData($data) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * createObjectFrom |
||
| 126 | * |
||
| 127 | * Create a range object from a given definition. If the object creation |
||
| 128 | * fails, an exception is thrown. |
||
| 129 | * |
||
| 130 | * @access protected |
||
| 131 | * @param mixed $data |
||
| 132 | * @return BaseRange |
||
| 133 | * @throws ConverterException |
||
| 134 | */ |
||
| 135 | protected function createObjectFrom($data) |
||
| 149 | } |
||
| 150 |