Total Complexity | 43 |
Total Lines | 247 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like GenerateDataFactoryCommand 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 GenerateDataFactoryCommand, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class GenerateDataFactoryCommand extends Command |
||
20 | { |
||
21 | /** @var EntityManagerInterface */ |
||
22 | protected $entityManager; |
||
23 | |||
24 | /** |
||
25 | * GenerateTraitCommand constructor. |
||
26 | * @param EntityManagerInterface $entityManager |
||
27 | */ |
||
28 | public function __construct(EntityManagerInterface $entityManager) |
||
32 | } |
||
33 | |||
34 | protected function configure() |
||
60 | ); |
||
61 | } |
||
62 | |||
63 | public function execute(InputInterface $input, OutputInterface $output) |
||
131 | ) |
||
132 | ); |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * @param ClassMetadata $metaData |
||
137 | * @return string[] |
||
138 | */ |
||
139 | private function buildFactoryData(ClassMetadata $metaData): array |
||
140 | { |
||
141 | $data = []; |
||
142 | |||
143 | foreach ($metaData->fieldMappings as $fieldMapping) { |
||
144 | switch ($fieldMapping['type']) { |
||
145 | case 'smallint': |
||
146 | case 'integer': |
||
147 | case 'bigint': |
||
148 | $data[] = sprintf( |
||
149 | " '%s' => random_int(0, 65000),\n", |
||
150 | $fieldMapping['fieldName'] |
||
151 | ); |
||
152 | break; |
||
153 | case 'decimal': |
||
154 | case 'float': |
||
155 | $data[] = sprintf( |
||
156 | " '%s' => Faker::randomFloat(2),\n", |
||
157 | $fieldMapping['fieldName'] |
||
158 | ); |
||
159 | break; |
||
160 | case 'string': |
||
161 | $data[] = sprintf( |
||
162 | " '%s' => Faker::sentence(),\n", |
||
163 | $fieldMapping['fieldName'] |
||
164 | ); |
||
165 | break; |
||
166 | case 'text': |
||
167 | $data[] = sprintf( |
||
168 | " '%s' => Faker::paragraph(),\n", |
||
169 | $fieldMapping['fieldName'] |
||
170 | ); |
||
171 | break; |
||
172 | case 'guid': |
||
173 | $data[] = sprintf( |
||
174 | " '%s' => uniqid('', true)", |
||
175 | $fieldMapping['fieldName'] |
||
176 | ); |
||
177 | break; |
||
178 | case 'binary': |
||
179 | case 'blob': |
||
180 | break; |
||
181 | case 'boolean': |
||
182 | $data[] = sprintf( |
||
183 | " '%s' => (bool)random_int(0, 1),\n", |
||
184 | $fieldMapping['fieldName'] |
||
185 | ); |
||
186 | break; |
||
187 | case 'date': |
||
188 | case 'date_immutable': |
||
189 | case 'datetime': |
||
190 | case 'datetime_immutable': |
||
191 | case 'datetimetz': |
||
192 | case 'datetimetz_immutable': |
||
193 | case 'time': |
||
194 | case 'time_immutable': |
||
195 | $data[] = sprintf( |
||
196 | " '%s' => Faker::dateTime(),\n", |
||
197 | $fieldMapping['fieldName'] |
||
198 | ); |
||
199 | break; |
||
200 | case 'dateinterval': |
||
201 | case 'array': |
||
202 | case 'simple_array': |
||
203 | case 'json': |
||
204 | case 'json_array': |
||
205 | case 'object': |
||
206 | break; |
||
207 | default: |
||
208 | $data[] = sprintf( |
||
209 | " '%s' => Faker::word(),\n", |
||
210 | $fieldMapping['fieldName'] |
||
211 | ); |
||
212 | break; |
||
213 | } |
||
214 | } |
||
215 | |||
216 | foreach ($metaData->associationMappings as $associationMapping) { |
||
217 | switch ($associationMapping['type']) { |
||
218 | case 1: |
||
219 | case 2: |
||
220 | case 3: |
||
221 | $data[] = sprintf( |
||
222 | " '%s' => 'entity|' . \\%s::class,\n", |
||
223 | $associationMapping['fieldName'], |
||
224 | $associationMapping['targetEntity'] |
||
225 | ); |
||
226 | break; |
||
227 | case 4: |
||
228 | // TODO one to many |
||
229 | break; |
||
230 | case 8: |
||
231 | // TODO many to many |
||
232 | break; |
||
233 | default: |
||
234 | break; |
||
235 | } |
||
236 | } |
||
237 | |||
238 | return $data; |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * @param ClassMetadata $metaData |
||
243 | * @return string |
||
244 | */ |
||
245 | private function buildBody(ClassMetadata $metaData): string |
||
266 | } |
||
267 | } |