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 | * @access public |
||
36 | * @param string $name |
||
37 | * @param ConverterInterface $converter |
||
38 | * @param array $types |
||
39 | * @param bool $strict |
||
40 | * @return ConverterHolder $this |
||
41 | */ |
||
42 | public function registerConverter($name, ConverterInterface $converter, array $types, $strict = null) |
||
43 | { |
||
44 | $this->addConverter($name, $converter, $strict); |
||
45 | |||
46 | foreach ($types as $type) { |
||
47 | $this->addTypeToConverter($name, $type); |
||
48 | } |
||
49 | |||
50 | return $this; |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * addConverter |
||
55 | * |
||
56 | * Add a converter with a new name. If strict is set to true and the |
||
57 | * converter for this type has already been registered, then it throws and |
||
58 | * exception. |
||
59 | * |
||
60 | * @access protected |
||
61 | * @param string $name |
||
62 | * @param ConverterInterface $converter |
||
63 | * @param bool $strict (default true) |
||
64 | * @throws ConverterException if $name already exists and strict. |
||
65 | * @return ConverterHolder $this |
||
66 | */ |
||
67 | protected function addConverter($name, ConverterInterface $converter, $strict = null) |
||
85 | |||
86 | /** |
||
87 | * hasConverterName |
||
88 | * |
||
89 | * Tell if the converter exists or not. |
||
90 | * |
||
91 | * @access public |
||
92 | * @param string $name |
||
93 | * @return bool |
||
94 | */ |
||
95 | public function hasConverterName($name) |
||
96 | { |
||
97 | return (bool) isset($this->converters[$name]); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * getConverter |
||
102 | * |
||
103 | * Return the converter associated with this name. If no converters found, |
||
104 | * NULL is returned. |
||
105 | * |
||
106 | * @access public |
||
107 | * @param string $name |
||
108 | * @return ConverterInterface |
||
109 | */ |
||
110 | public function getConverter($name) |
||
111 | { |
||
112 | return $this->hasConverterName($name) ? $this->converters[$name] : null; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * getConverterNames |
||
117 | * |
||
118 | * Returns an array with the names of the registered converters. |
||
119 | * |
||
120 | * @access public |
||
121 | * @return array |
||
122 | */ |
||
123 | public function getConverterNames() |
||
124 | { |
||
125 | return array_keys($this->converters); |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * addTypeToConverter |
||
130 | * |
||
131 | * Make the given converter to support a new PostgreSQL type. If the given |
||
132 | * type is already defined, it is overrided with the new converter. |
||
133 | * |
||
134 | * @access public |
||
135 | * @param string $name |
||
136 | * @param string $type |
||
137 | * @throws ConverterException if $name does not exist. |
||
138 | * @return ConverterHolder $this |
||
139 | */ |
||
140 | public function addTypeToConverter($name, $type) |
||
156 | |||
157 | /** |
||
158 | * getConverterForType |
||
159 | * |
||
160 | * Returns the converter instance for the given type. |
||
161 | * |
||
162 | * @access public |
||
163 | * @param string $type |
||
164 | * @throws ConverterException if there are no converters associated. |
||
165 | * @return ConverterInterface |
||
166 | */ |
||
167 | public function getConverterForType($type) |
||
181 | |||
182 | /** |
||
183 | * hasType |
||
184 | * |
||
185 | * Does the type exist ? |
||
186 | * |
||
187 | * @access public |
||
188 | * @param string $type |
||
189 | * @return bool |
||
190 | */ |
||
191 | public function hasType($type) |
||
195 | |||
196 | /** |
||
197 | * getTypes |
||
198 | * |
||
199 | * Return the list of handled types. |
||
200 | * |
||
201 | * @access public |
||
202 | * @return array |
||
203 | */ |
||
204 | public function getTypes() |
||
208 | |||
209 | /** |
||
210 | * getTypesWithConverterName |
||
211 | * |
||
212 | * Return the list of types with the related converter name. |
||
213 | * |
||
214 | * @access public |
||
215 | * @return array |
||
216 | */ |
||
217 | public function getTypesWithConverterName() |
||
221 | } |
||
222 |