Total Complexity | 40 |
Total Lines | 242 |
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) |
||
64 | { |
||
65 | $destination = $input->getOption('output'); |
||
66 | $metaDataEntries = $this->entityManager->getMetadataFactory()->getAllMetadata(); |
||
67 | |||
68 | if (!is_dir($destination)) { |
||
|
|||
69 | mkdir($destination, 0777, true); |
||
70 | } |
||
71 | |||
72 | /** @var ClassMetadata $metaData */ |
||
73 | foreach ($metaDataEntries as $metaData) { |
||
74 | if ($filter = $input->getOption('filter')) { |
||
75 | if (strpos($metaData->getName(), $filter) === false) { |
||
76 | $output->writeln( |
||
77 | sprintf( |
||
78 | 'Filtering out %s...', |
||
79 | $metaData->getName() |
||
80 | ), |
||
81 | OutputInterface::VERBOSITY_VERY_VERBOSE |
||
82 | ); |
||
83 | continue; |
||
84 | } |
||
85 | } |
||
86 | |||
87 | $fileName = $destination . '/' . $metaData->reflClass->getShortName() . 'DataFactory.php'; |
||
88 | // TODO handle duplicate names |
||
89 | |||
90 | $file = FileGenerator::fromArray( |
||
91 | [ |
||
92 | 'docblock' => DocBlockGenerator::fromArray( |
||
93 | [ |
||
94 | 'shortDescription' => '', |
||
95 | 'longDescription' => '', |
||
96 | 'tags' => [ |
||
97 | [ |
||
98 | 'name' => 'var', |
||
99 | 'description' => '\\Codeception\\Module\\DataFactory $factory' |
||
100 | ], |
||
101 | [ |
||
102 | 'name' => 'var', |
||
103 | 'description' => '\\Doctrine\\ORM\\EntityManager $em' |
||
104 | ] |
||
105 | ] |
||
106 | ] |
||
107 | ), |
||
108 | 'body' => $this->buildBody($metaData), |
||
109 | ] |
||
110 | ); |
||
111 | |||
112 | if (file_exists($fileName)) { |
||
113 | $output->writeln( |
||
114 | sprintf( |
||
115 | '%s already exists. Skipping...', |
||
116 | $fileName |
||
117 | ), |
||
118 | OutputInterface::VERBOSITY_VERBOSE |
||
119 | ); |
||
120 | |||
121 | continue; |
||
122 | } |
||
123 | |||
124 | file_put_contents($fileName, $file->generate()); |
||
125 | } |
||
126 | |||
127 | $output->writeln( |
||
128 | sprintf( |
||
129 | 'Data factories written to "%s"', |
||
130 | $destination |
||
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 | $data[] = sprintf( |
||
220 | " '%s' => 'entity|' . \\%s::class", |
||
221 | $associationMapping['fieldName'], |
||
222 | $associationMapping['targetEntity'] |
||
223 | ); |
||
224 | break; |
||
225 | case 2: |
||
226 | // TODO many to one? |
||
227 | break; |
||
228 | default: |
||
229 | break; |
||
230 | } |
||
231 | } |
||
232 | |||
233 | return $data; |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * @param ClassMetadata $metaData |
||
238 | * @return string |
||
239 | */ |
||
240 | private function buildBody(ClassMetadata $metaData): string |
||
261 | } |
||
262 | } |