Total Complexity | 40 |
Total Lines | 345 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like GeneratePhpEditController 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 GeneratePhpEditController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
37 | class GeneratePhpEditController extends ConsoleAbstract |
||
38 | { |
||
39 | use Common\TableInformation; |
||
40 | |||
41 | /** |
||
42 | * Constant values for return, to easy know how execution ends. |
||
43 | */ |
||
44 | const RETURN_SUCCESS = 0; |
||
45 | const RETURN_TABLE_NAME_NOT_SET = 1; |
||
46 | const RETURN_MODEL_NAME_NOT_SET = 2; |
||
47 | const RETURN_DST_FOLDER_NOT_SET = 3; |
||
48 | const RETURN_CANT_CREATE_FOLDER = 4; |
||
49 | const RETURN_TABLE_NOT_EXISTS = 5; |
||
50 | const RETURN_FAIL_SAVING_FILE = 6; |
||
51 | |||
52 | /** |
||
53 | * Name of the table. |
||
54 | * |
||
55 | * @var string |
||
56 | */ |
||
57 | private $tableName; |
||
58 | |||
59 | /** |
||
60 | * Name of the model. |
||
61 | * |
||
62 | * @var string |
||
63 | */ |
||
64 | private $modelName; |
||
65 | |||
66 | /** |
||
67 | * Folder destiny path. |
||
68 | * |
||
69 | * @var string |
||
70 | */ |
||
71 | private $folderDstPath; |
||
72 | |||
73 | /** |
||
74 | * Start point to run the command. |
||
75 | * |
||
76 | * @param array $params |
||
77 | * |
||
78 | * @return int |
||
79 | */ |
||
80 | public function run(...$params): int |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Return description about this class. |
||
117 | * |
||
118 | * @return string |
||
119 | */ |
||
120 | public function getDescription(): string |
||
121 | { |
||
122 | return 'Generate a Edit Controller for model class from database table.'; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Print help information to the user. |
||
127 | * |
||
128 | * @return string |
||
129 | */ |
||
130 | public function getHelpMsg(): string |
||
131 | { |
||
132 | $array = \explode('\\', __CLASS__); |
||
133 | $class = array_pop($array); |
||
134 | return 'Use as: php vendor/bin/console ' . $class . ' [OPTIONS]' . \PHP_EOL |
||
135 | . 'Available options:' . \PHP_EOL |
||
136 | . ' -h, --help Show this help.' . \PHP_EOL |
||
137 | . ' -t, --tables Show tables.' . \PHP_EOL |
||
138 | . ' -g, --gen Generate model.' . \PHP_EOL |
||
139 | . \PHP_EOL |
||
140 | . ' OPTION1 Table name' . \PHP_EOL |
||
141 | . ' OPTION2 Model name' . \PHP_EOL |
||
142 | . ' OPTION3 Destiny path' . \PHP_EOL |
||
143 | . \PHP_EOL; |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Returns an associative array of available methods for the user. |
||
148 | * Add more options if you want to add support for custom methods. |
||
149 | * [ |
||
150 | * '-h' => 'getHelpMsg', |
||
151 | * '--help' => 'getHelpMsg', |
||
152 | * ] |
||
153 | * |
||
154 | * @return array |
||
155 | */ |
||
156 | public function getUserMethods(): array |
||
157 | { |
||
158 | // Adding extra method |
||
159 | $methods = parent::getUserMethods(); |
||
160 | $methods['-t'] = 'getTablesMsg'; |
||
161 | $methods['--tables'] = 'getTablesMsg'; |
||
162 | $methods['-g'] = 'generateModel'; |
||
163 | $methods['--gen'] = 'generateModel'; |
||
164 | return $methods; |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * Return a list of tables. |
||
169 | * |
||
170 | * @return string |
||
171 | */ |
||
172 | public function getTablesMsg(): string |
||
173 | { |
||
174 | return \implode(', ', $this->getTables($this->dataBase)); |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Set table name to use. |
||
179 | * |
||
180 | * @param string $tableName |
||
181 | * |
||
182 | * @return $this |
||
183 | */ |
||
184 | public function setTableName(string $tableName): self |
||
185 | { |
||
186 | $this->tableName = $tableName; |
||
187 | return $this; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * Set the model name to use. |
||
192 | * |
||
193 | * @param string $modelName |
||
194 | * |
||
195 | * @return $this |
||
196 | */ |
||
197 | public function setModelName(string $modelName): self |
||
198 | { |
||
199 | $this->modelName = $modelName; |
||
200 | return $this; |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * Set destiny folder. |
||
205 | * |
||
206 | * @param string $folderDstPath |
||
207 | * |
||
208 | * @return $this |
||
209 | */ |
||
210 | public function setFolderDstPath(string $folderDstPath): self |
||
211 | { |
||
212 | $this->folderDstPath = $folderDstPath; |
||
213 | return $this; |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * Set database. |
||
218 | * |
||
219 | * @param DataBase $dataBase |
||
220 | */ |
||
221 | public function setDataBase(DataBase $dataBase) |
||
222 | { |
||
223 | $this->dataBase = $dataBase; |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * Return the table name. |
||
228 | * |
||
229 | * @return string |
||
230 | */ |
||
231 | public function getTableName(): string |
||
232 | { |
||
233 | return $this->tableName; |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * Return the model name. |
||
238 | * |
||
239 | * @return string |
||
240 | */ |
||
241 | public function getModelName(): string |
||
242 | { |
||
243 | return $this->modelName; |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * Return the folder destiny path. |
||
248 | * |
||
249 | * @return string |
||
250 | */ |
||
251 | public function getFolderDstPath(): string |
||
252 | { |
||
253 | return $this->folderDstPath; |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * Return the DataBase object. |
||
258 | * |
||
259 | * @return DataBase |
||
260 | */ |
||
261 | public function getDataBase() |
||
262 | { |
||
263 | return $this->dataBase; |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * Check if options are looking for help. |
||
268 | * |
||
269 | * @param array $params |
||
270 | * |
||
271 | * @return int |
||
272 | */ |
||
273 | private function checkOptions(array $params = []): int |
||
274 | { |
||
275 | if (isset($params[0])) { |
||
276 | switch ($params[0]) { |
||
277 | case '-h': |
||
278 | case '--help': |
||
279 | echo $this->getHelpMsg(); |
||
280 | return -1; |
||
281 | case '-t': |
||
282 | case '--tables': |
||
283 | $this->setDataBase($params[1] ?? null); |
||
284 | echo $this->getTablesMsg(); |
||
285 | return -1; |
||
286 | case '-g': |
||
287 | case '--gen': |
||
288 | $this->setTableName($params[1] ?? ''); |
||
289 | $this->setModelName($params[2] ?? ''); |
||
290 | $this->setFolderDstPath(\FS_FOLDER . $params[3] ?? ''); |
||
291 | $this->setDataBase($params[4] ?? null); |
||
292 | } |
||
293 | } |
||
294 | return 0; |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * Check if options are looking for help. |
||
299 | * |
||
300 | * @param array $params |
||
301 | * |
||
302 | * @return int |
||
303 | */ |
||
304 | private function checkParams(array $params = []): int |
||
305 | { |
||
306 | if (!isset($params[0])) { |
||
307 | echo 'No table name setted.' . \PHP_EOL; |
||
308 | return -1; |
||
309 | } |
||
310 | if (!isset($params[1])) { |
||
311 | echo 'No model name setted.' . \PHP_EOL; |
||
312 | return -1; |
||
313 | } |
||
314 | return 0; |
||
315 | } |
||
316 | |||
317 | /** |
||
318 | * Launch basic checks. |
||
319 | * |
||
320 | * @return int |
||
321 | */ |
||
322 | private function check(): int |
||
345 | } |
||
346 | |||
347 | /** |
||
348 | * Generate model file. |
||
349 | * |
||
350 | * @return int |
||
351 | */ |
||
352 | private function generateModel(): int |
||
353 | { |
||
354 | $loader = new Twig_Loader_Filesystem([__DIR__ . '/../Template']); |
||
355 | $twig = new Twig_Environment($loader, ['debug' => \FS_DEBUG,]); |
||
356 | $twig->addExtension(new Twig_Extension_Debug()); |
||
357 | $txt = $twig->render( |
||
358 | 'EditController.php.twig', |
||
359 | ['fsc' => $this] |
||
369 | } |
||
370 | |||
371 | /** |
||
372 | * Save file. |
||
373 | * |
||
374 | * @param string $pathName |
||
375 | * @param string $content |
||
376 | * |
||
377 | * @return int |
||
378 | */ |
||
379 | private function saveFile(string $pathName, string $content): int |
||
384 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths