Total Complexity | 52 |
Total Lines | 305 |
Duplicated Lines | 2.95 % |
Coverage | 7.03% |
Changes | 0 |
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.
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 | 1 | public function __construct($from) |
|
65 | 1 | } |
|
66 | |||
67 | /** |
||
68 | * Gets an array of ClassMetadata instances from the passed |
||
69 | * Doctrine 1 schema. |
||
70 | * |
||
71 | * @return array An array of ClassMetadata instances |
||
72 | */ |
||
73 | 1 | public function getMetadata() |
|
74 | { |
||
75 | 1 | $schema = []; |
|
76 | 1 | foreach ($this->from as $path) { |
|
77 | if (is_dir($path)) { |
||
78 | $files = glob($path . '/*.yml'); |
||
79 | foreach ($files as $file) { |
||
80 | $schema = array_merge($schema, (array) Yaml::parse(file_get_contents($file))); |
||
|
|||
81 | } |
||
82 | } else { |
||
83 | $schema = array_merge($schema, (array) Yaml::parse(file_get_contents($path))); |
||
84 | } |
||
85 | } |
||
86 | |||
87 | 1 | $metadatas = []; |
|
88 | 1 | foreach ($schema as $className => $mapping) { |
|
89 | $metadatas[] = $this->convertToClassMetadata($className, $mappingInformation); |
||
90 | } |
||
91 | |||
92 | 1 | return $metadatas; |
|
93 | } |
||
94 | |||
95 | /** |
||
96 | * @param string $className |
||
97 | * @param array $mappingInformation |
||
98 | * |
||
99 | * @return \Doctrine\ORM\Mapping\ClassMetadata |
||
100 | */ |
||
101 | private function convertToClassMetadata($className, $mappingInformation) |
||
102 | { |
||
103 | $metadata = new ClassMetadata($className); |
||
104 | |||
105 | $this->convertTableName($className, $mappingInformation, $metadata); |
||
106 | $this->convertColumns($className, $mappingInformation, $metadata); |
||
107 | $this->convertIndexes($className, $mappingInformation, $metadata); |
||
108 | $this->convertRelations($className, $mappingInformation, $metadata); |
||
109 | |||
110 | return $metadata; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * @param string $className |
||
115 | * @param array $model |
||
116 | * @param ClassMetadata $metadata |
||
117 | * |
||
118 | * @return void |
||
119 | */ |
||
120 | private function convertTableName($className, array $model, ClassMetadata $metadata) |
||
121 | { |
||
122 | if (isset($model['tableName']) && $model['tableName']) { |
||
123 | $e = explode('.', $model['tableName']); |
||
124 | |||
125 | if (count($e) > 1) { |
||
126 | $metadata->table['schema'] = $e[0]; |
||
127 | $metadata->table['name'] = $e[1]; |
||
128 | } else { |
||
129 | $metadata->table['name'] = $e[0]; |
||
130 | } |
||
131 | } |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * @param string $className |
||
136 | * @param array $model |
||
137 | * @param ClassMetadata $metadata |
||
138 | * |
||
139 | * @return void |
||
140 | */ |
||
141 | private function convertColumns($className, array $model, ClassMetadata $metadata) |
||
142 | { |
||
143 | $id = false; |
||
144 | |||
145 | if (isset($model['columns']) && $model['columns']) { |
||
146 | foreach ($model['columns'] as $name => $column) { |
||
147 | $fieldMapping = $this->convertColumn($className, $name, $column, $metadata); |
||
148 | |||
149 | if (isset($fieldMapping['id']) && $fieldMapping['id']) { |
||
150 | $id = true; |
||
151 | } |
||
152 | } |
||
153 | } |
||
154 | |||
155 | if ( ! $id) { |
||
156 | $fieldMapping = [ |
||
157 | 'fieldName' => 'id', |
||
158 | 'columnName' => 'id', |
||
159 | 'type' => 'integer', |
||
160 | 'id' => true |
||
161 | ]; |
||
162 | $metadata->mapField($fieldMapping); |
||
163 | $metadata->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_AUTO); |
||
164 | } |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * @param string $className |
||
169 | * @param string $name |
||
170 | * @param string|array $column |
||
171 | * @param ClassMetadata $metadata |
||
172 | * |
||
173 | * @return array |
||
174 | * |
||
175 | * @throws ToolsException |
||
176 | */ |
||
177 | private function convertColumn($className, $name, $column, ClassMetadata $metadata) |
||
178 | { |
||
179 | if (is_string($column)) { |
||
180 | $string = $column; |
||
181 | $column = []; |
||
182 | $column['type'] = $string; |
||
183 | } |
||
184 | |||
185 | if ( ! isset($column['name'])) { |
||
186 | $column['name'] = $name; |
||
187 | } |
||
188 | |||
189 | // check if a column alias was used (column_name as field_name) |
||
190 | View Code Duplication | if (preg_match("/(\w+)\sas\s(\w+)/i", $column['name'], $matches)) { |
|
191 | $name = $matches[1]; |
||
192 | $column['name'] = $name; |
||
193 | $column['alias'] = $matches[2]; |
||
194 | } |
||
195 | |||
196 | View Code Duplication | if (preg_match("/([a-zA-Z]+)\(([0-9]+)\)/", $column['type'], $matches)) { |
|
197 | $column['type'] = $matches[1]; |
||
198 | $column['length'] = $matches[2]; |
||
199 | } |
||
200 | |||
201 | $column['type'] = strtolower($column['type']); |
||
202 | // check if legacy column type (1.x) needs to be mapped to a 2.0 one |
||
203 | if (isset($this->legacyTypeMap[$column['type']])) { |
||
204 | $column['type'] = $this->legacyTypeMap[$column['type']]; |
||
205 | } |
||
206 | |||
207 | if ( ! Type::hasType($column['type'])) { |
||
208 | throw ToolsException::couldNotMapDoctrine1Type($column['type']); |
||
209 | } |
||
210 | |||
211 | $fieldMapping = []; |
||
212 | |||
213 | if (isset($column['primary'])) { |
||
214 | $fieldMapping['id'] = true; |
||
215 | } |
||
216 | |||
217 | $fieldMapping['fieldName'] = $column['alias'] ?? $name; |
||
218 | $fieldMapping['columnName'] = $column['name']; |
||
219 | $fieldMapping['type'] = $column['type']; |
||
220 | |||
221 | if (isset($column['length'])) { |
||
222 | $fieldMapping['length'] = $column['length']; |
||
223 | } |
||
224 | |||
225 | $allowed = ['precision', 'scale', 'unique', 'options', 'notnull', 'version']; |
||
226 | |||
227 | foreach ($column as $key => $value) { |
||
228 | if (in_array($key, $allowed)) { |
||
229 | $fieldMapping[$key] = $value; |
||
230 | } |
||
231 | } |
||
232 | |||
233 | $metadata->mapField($fieldMapping); |
||
234 | |||
235 | if (isset($column['autoincrement'])) { |
||
236 | $metadata->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_AUTO); |
||
237 | } elseif (isset($column['sequence'])) { |
||
238 | $metadata->setIdGeneratorType(ClassMetadata::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 | return $fieldMapping; |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * @param string $className |
||
260 | * @param array $model |
||
261 | * @param ClassMetadata $metadata |
||
262 | * |
||
263 | * @return void |
||
264 | */ |
||
265 | private function convertIndexes($className, array $model, ClassMetadata $metadata) |
||
266 | { |
||
267 | if (empty($model['indexes'])) { |
||
268 | return; |
||
269 | } |
||
270 | |||
271 | foreach ($model['indexes'] as $name => $index) { |
||
272 | $type = (isset($index['type']) && $index['type'] == 'unique') |
||
273 | ? 'uniqueConstraints' : 'indexes'; |
||
274 | |||
275 | $metadata->table[$type][$name] = [ |
||
276 | 'columns' => $index['fields'] |
||
277 | ]; |
||
278 | } |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * @param string $className |
||
283 | * @param array $model |
||
284 | * @param ClassMetadata $metadata |
||
285 | * |
||
286 | * @return void |
||
287 | */ |
||
288 | private function convertRelations($className, array $model, ClassMetadata $metadata) |
||
342 | } |
||
343 | } |
||
344 | } |
||
345 |