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 ConverterHolder |
||
| 26 | { |
||
| 27 | protected $converters = []; |
||
| 28 | protected $types = []; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * registerConverter |
||
| 32 | * |
||
| 33 | * Declare a converter and assign types to it. |
||
| 34 | * |
||
| 35 | * @param string $name |
||
| 36 | * @param ConverterInterface $converter |
||
| 37 | * @param array $types |
||
| 38 | * @param bool $strict |
||
| 39 | * @return ConverterHolder $this |
||
| 40 | */ |
||
| 41 | public function registerConverter($name, ConverterInterface $converter, array $types, $strict = null) |
||
| 51 | |||
| 52 | /** |
||
| 53 | * addConverter |
||
| 54 | * |
||
| 55 | * Add a converter with a new name. If strict is set to true and the |
||
| 56 | * converter for this type has already been registered, then it throws and |
||
| 57 | * exception. |
||
| 58 | * |
||
| 59 | * @param string $name |
||
| 60 | * @param ConverterInterface $converter |
||
| 61 | * @param bool $strict (default true) |
||
| 62 | * @throws ConverterException if $name already exists and strict. |
||
| 63 | * @return ConverterHolder $this |
||
| 64 | */ |
||
| 65 | protected function addConverter($name, ConverterInterface $converter, $strict = null) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * hasConverterName |
||
| 86 | * |
||
| 87 | * Tell if the converter exists or not. |
||
| 88 | * |
||
| 89 | * @param string $name |
||
| 90 | * @return bool |
||
| 91 | */ |
||
| 92 | public function hasConverterName($name) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * getConverter |
||
| 99 | * |
||
| 100 | * Return the converter associated with this name. If no converters found, |
||
| 101 | * NULL is returned. |
||
| 102 | * |
||
| 103 | * @param string $name |
||
| 104 | * @return ConverterInterface |
||
| 105 | */ |
||
| 106 | public function getConverter($name) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * getConverterNames |
||
| 113 | * |
||
| 114 | * Returns an array with the names of the registered converters. |
||
| 115 | * |
||
| 116 | * @return array |
||
| 117 | */ |
||
| 118 | public function getConverterNames() |
||
| 122 | |||
| 123 | /** |
||
| 124 | * addTypeToConverter |
||
| 125 | * |
||
| 126 | * Make the given converter to support a new PostgreSQL type. If the given |
||
| 127 | * type is already defined, it is overrided with the new converter. |
||
| 128 | * |
||
| 129 | * @param string $name |
||
| 130 | * @param string $type |
||
| 131 | * @throws ConverterException if $name does not exist. |
||
| 132 | * @return ConverterHolder $this |
||
| 133 | */ |
||
| 134 | public function addTypeToConverter($name, $type) |
||
| 150 | |||
| 151 | /** |
||
| 152 | * getConverterForType |
||
| 153 | * |
||
| 154 | * Returns the converter instance for the given type. |
||
| 155 | * |
||
| 156 | * @param string $type |
||
| 157 | * @throws ConverterException if there are no converters associated. |
||
| 158 | * @return ConverterInterface |
||
| 159 | */ |
||
| 160 | public function getConverterForType($type) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * hasType |
||
| 177 | * |
||
| 178 | * Does the type exist ? |
||
| 179 | * |
||
| 180 | * @param string $type |
||
| 181 | * @return bool |
||
| 182 | */ |
||
| 183 | public function hasType($type) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * getTypes |
||
| 190 | * |
||
| 191 | * Return the list of handled types. |
||
| 192 | * |
||
| 193 | * @return array |
||
| 194 | */ |
||
| 195 | public function getTypes() |
||
| 199 | |||
| 200 | /** |
||
| 201 | * getTypesWithConverterName |
||
| 202 | * |
||
| 203 | * Return the list of types with the related converter name. |
||
| 204 | * |
||
| 205 | * @return array |
||
| 206 | */ |
||
| 207 | public function getTypesWithConverterName() |
||
| 211 | } |
||
| 212 |