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() |
|
74 | { |
||
75 | 2 | $schema = []; |
|
76 | 2 | foreach ($this->from as $path) { |
|
77 | 1 | if (is_dir($path)) { |
|
78 | 1 | $files = glob($path . '/*.yml'); |
|
79 | 1 | foreach ($files as $file) { |
|
80 | 1 | $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 | 2 | $metadatas = []; |
|
88 | 2 | foreach ($schema as $className => $mappingInformation) { |
|
89 | 1 | $metadatas[] = $this->convertToClassMetadataInfo($className, $mappingInformation); |
|
90 | } |
||
91 | |||
92 | 2 | return $metadatas; |
|
93 | } |
||
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) |
|
142 | { |
||
143 | 1 | $id = false; |
|
144 | |||
145 | 1 | if (isset($model['columns']) && $model['columns']) { |
|
146 | 1 | foreach ($model['columns'] as $name => $column) { |
|
147 | 1 | $fieldMapping = $this->convertColumn($className, $name, $column, $metadata); |
|
148 | |||
149 | 1 | if (isset($fieldMapping['id']) && $fieldMapping['id']) { |
|
150 | $id = true; |
||
151 | } |
||
152 | } |
||
153 | } |
||
154 | |||
155 | 1 | if ( ! $id) { |
|
156 | $fieldMapping = [ |
||
157 | 1 | 'fieldName' => 'id', |
|
158 | 'columnName' => 'id', |
||
159 | 'type' => 'integer', |
||
160 | 'id' => true |
||
161 | ]; |
||
162 | 1 | $metadata->mapField($fieldMapping); |
|
163 | 1 | $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO); |
|
164 | } |
||
165 | 1 | } |
|
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) |
|
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.