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:
Complex classes like ConvertDoctrine1Schema often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ConvertDoctrine1Schema, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
37 | class ConvertDoctrine1Schema |
||
38 | { |
||
39 | /** |
||
40 | * @var array |
||
41 | */ |
||
42 | private $from; |
||
43 | |||
44 | /** |
||
45 | * @var array |
||
46 | */ |
||
47 | private $legacyTypeMap = [ |
||
48 | // TODO: This list may need to be updated |
||
49 | 'clob' => 'text', |
||
50 | 'timestamp' => 'datetime', |
||
51 | 'enum' => 'string' |
||
52 | ]; |
||
53 | |||
54 | /** |
||
55 | * Constructor passes the directory or array of directories |
||
56 | * to convert the Doctrine 1 schema files from. |
||
57 | * |
||
58 | * @param array $from |
||
59 | * |
||
60 | * @author Jonathan Wage |
||
61 | */ |
||
62 | 2 | public function __construct($from) |
|
66 | |||
67 | /** |
||
68 | * Gets an array of ClassMetadataInfo instances from the passed |
||
69 | * Doctrine 1 schema. |
||
70 | * |
||
71 | * @return array An array of ClassMetadataInfo instances |
||
72 | */ |
||
73 | 2 | public function getMetadata() |
|
94 | |||
95 | /** |
||
96 | * @param string $className |
||
97 | * @param array $mappingInformation |
||
98 | * |
||
99 | * @return \Doctrine\ORM\Mapping\ClassMetadataInfo |
||
100 | */ |
||
101 | 1 | private function convertToClassMetadataInfo($className, $mappingInformation) |
|
112 | |||
113 | /** |
||
114 | * @param string $className |
||
115 | * @param array $model |
||
116 | * @param ClassMetadataInfo $metadata |
||
117 | * |
||
118 | * @return void |
||
119 | */ |
||
120 | 1 | private function convertTableName($className, array $model, ClassMetadataInfo $metadata) |
|
133 | |||
134 | /** |
||
135 | * @param string $className |
||
136 | * @param array $model |
||
137 | * @param ClassMetadataInfo $metadata |
||
138 | * |
||
139 | * @return void |
||
140 | */ |
||
141 | 1 | private function convertColumns($className, array $model, ClassMetadataInfo $metadata) |
|
166 | |||
167 | /** |
||
168 | * @param string $className |
||
169 | * @param string $name |
||
170 | * @param string|array $column |
||
171 | * @param ClassMetadataInfo $metadata |
||
172 | * |
||
173 | * @return array |
||
174 | * |
||
175 | * @throws ToolsException |
||
176 | */ |
||
177 | 1 | private function convertColumn($className, $name, $column, ClassMetadataInfo $metadata) |
|
178 | { |
||
179 | 1 | if (is_string($column)) { |
|
180 | 1 | $string = $column; |
|
181 | 1 | $column = []; |
|
182 | 1 | $column['type'] = $string; |
|
183 | } |
||
184 | |||
185 | 1 | if ( ! isset($column['name'])) { |
|
186 | 1 | $column['name'] = $name; |
|
187 | } |
||
188 | |||
189 | // check if a column alias was used (column_name as field_name) |
||
190 | 1 | View Code Duplication | if (preg_match("/(\w+)\sas\s(\w+)/i", $column['name'], $matches)) { |
191 | 1 | $name = $matches[1]; |
|
192 | 1 | $column['name'] = $name; |
|
193 | 1 | $column['alias'] = $matches[2]; |
|
194 | } |
||
195 | |||
196 | 1 | View Code Duplication | if (preg_match("/([a-zA-Z]+)\(([0-9]+)\)/", $column['type'], $matches)) { |
197 | 1 | $column['type'] = $matches[1]; |
|
198 | 1 | $column['length'] = $matches[2]; |
|
199 | } |
||
200 | |||
201 | 1 | $column['type'] = strtolower($column['type']); |
|
202 | // check if legacy column type (1.x) needs to be mapped to a 2.0 one |
||
203 | 1 | if (isset($this->legacyTypeMap[$column['type']])) { |
|
204 | 1 | $column['type'] = $this->legacyTypeMap[$column['type']]; |
|
205 | } |
||
206 | |||
207 | 1 | if ( ! Type::hasType($column['type'])) { |
|
208 | throw ToolsException::couldNotMapDoctrine1Type($column['type']); |
||
209 | } |
||
210 | |||
211 | 1 | $fieldMapping = []; |
|
212 | |||
213 | 1 | if (isset($column['primary'])) { |
|
214 | $fieldMapping['id'] = true; |
||
215 | } |
||
216 | |||
217 | 1 | $fieldMapping['fieldName'] = isset($column['alias']) ? $column['alias'] : $name; |
|
218 | 1 | $fieldMapping['columnName'] = $column['name']; |
|
219 | 1 | $fieldMapping['type'] = $column['type']; |
|
220 | |||
221 | 1 | if (isset($column['length'])) { |
|
222 | 1 | $fieldMapping['length'] = $column['length']; |
|
223 | } |
||
224 | |||
225 | 1 | $allowed = ['precision', 'scale', 'unique', 'options', 'notnull', 'version']; |
|
226 | |||
227 | 1 | foreach ($column as $key => $value) { |
|
228 | 1 | if (in_array($key, $allowed)) { |
|
229 | 1 | $fieldMapping[$key] = $value; |
|
230 | } |
||
231 | } |
||
232 | |||
233 | 1 | $metadata->mapField($fieldMapping); |
|
234 | |||
235 | 1 | if (isset($column['autoincrement'])) { |
|
236 | $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO); |
||
237 | 1 | } elseif (isset($column['sequence'])) { |
|
238 | $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_SEQUENCE); |
||
239 | |||
240 | $definition = [ |
||
241 | 'sequenceName' => is_array($column['sequence']) ? $column['sequence']['name']:$column['sequence'] |
||
242 | ]; |
||
243 | |||
244 | if (isset($column['sequence']['size'])) { |
||
245 | $definition['allocationSize'] = $column['sequence']['size']; |
||
246 | } |
||
247 | |||
248 | if (isset($column['sequence']['value'])) { |
||
249 | $definition['initialValue'] = $column['sequence']['value']; |
||
250 | } |
||
251 | |||
252 | $metadata->setSequenceGeneratorDefinition($definition); |
||
253 | } |
||
254 | |||
255 | 1 | return $fieldMapping; |
|
256 | } |
||
257 | |||
258 | /** |
||
259 | * @param string $className |
||
260 | * @param array $model |
||
261 | * @param ClassMetadataInfo $metadata |
||
262 | * |
||
263 | * @return void |
||
264 | */ |
||
265 | 1 | private function convertIndexes($className, array $model, ClassMetadataInfo $metadata) |
|
280 | |||
281 | /** |
||
282 | * @param string $className |
||
283 | * @param array $model |
||
284 | * @param ClassMetadataInfo $metadata |
||
285 | * |
||
286 | * @return void |
||
287 | */ |
||
288 | 1 | private function convertRelations($className, array $model, ClassMetadataInfo $metadata) |
|
344 | } |
||
345 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.