Total Complexity | 40 |
Total Lines | 345 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like GeneratePhpModel 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 GeneratePhpModel, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
37 | class GeneratePhpModel 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 |
||
81 | { |
||
82 | if (!\DB_CONNECTION) { |
||
83 | echo 'A database connection is needed. Do you set your config.php file?'; |
||
84 | } |
||
85 | |||
86 | $status = $this->checkOptions($params); |
||
87 | if ($status !== 0) { |
||
88 | return $status; |
||
89 | } |
||
90 | |||
91 | $status = $this->checkParams($params); |
||
92 | if ($status !== 0) { |
||
93 | return $status; |
||
94 | } |
||
95 | |||
96 | echo 'Generating Model class file' . \PHP_EOL . \PHP_EOL; |
||
97 | echo ' Options setted:' . \PHP_EOL; |
||
98 | echo ' Table name: ' . $this->tableName . \PHP_EOL; |
||
99 | echo ' Model name: ' . $this->modelName . \PHP_EOL; |
||
100 | echo ' Destiny path: ' . $this->folderDstPath . \PHP_EOL; |
||
101 | |||
102 | if (!$this->areYouSure()) { |
||
103 | echo ' Options [TABLE NAME] [MODEL NAME] [DST]' . \PHP_EOL; |
||
104 | return self::RETURN_SUCCESS; |
||
105 | } |
||
106 | |||
107 | $status = $this->check(); |
||
108 | if ($status !== 0) { |
||
109 | return $status; |
||
110 | } |
||
111 | |||
112 | return $this->generateModel(); |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Return description about this class. |
||
117 | * |
||
118 | * @return string |
||
119 | */ |
||
120 | public function getDescription(): string |
||
121 | { |
||
122 | return 'Generate a 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 |
||
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 |
||
323 | { |
||
324 | if ($this->tableName === null) { |
||
325 | echo 'ERROR: Table name not setted.' . \PHP_EOL . \PHP_EOL; |
||
326 | return self::RETURN_TABLE_NAME_NOT_SET; |
||
327 | } |
||
328 | if ($this->modelName === null) { |
||
329 | echo 'ERROR: Model name not setted.' . \PHP_EOL . \PHP_EOL; |
||
330 | return self::RETURN_MODEL_NAME_NOT_SET; |
||
331 | } |
||
332 | if ($this->folderDstPath === null) { |
||
333 | echo 'ERROR: Destiny folder not setted.' . \PHP_EOL . \PHP_EOL; |
||
334 | return self::RETURN_DST_FOLDER_NOT_SET; |
||
335 | } |
||
336 | if (!is_file($this->folderDstPath) && !@mkdir($this->folderDstPath) && !is_dir($this->folderDstPath)) { |
||
337 | echo "ERROR: Can't create folder " . $this->folderDstPath . '.' . \PHP_EOL . \PHP_EOL; |
||
338 | return self::RETURN_CANT_CREATE_FOLDER; |
||
339 | } |
||
340 | if (!\in_array($this->tableName, $this->dataBase->getTables(), false)) { |
||
341 | echo 'ERROR: Table not exists.' . \PHP_EOL . \PHP_EOL; |
||
342 | return self::RETURN_TABLE_NOT_EXISTS; |
||
343 | } |
||
344 | return self::RETURN_SUCCESS; |
||
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 | 'Model.php.twig', |
||
359 | ['fsc' => $this] |
||
360 | ); |
||
361 | |||
362 | $status = $this->saveFile($this->folderDstPath . $this->modelName . '.php', $txt); |
||
363 | if (\is_bool($status)) { |
||
364 | echo "Can't save " . $this->folderDstPath . $this->modelName . '.php"' . \PHP_EOL; |
||
365 | return $status; |
||
366 | } |
||
367 | echo 'Finished! Look at "' . $this->folderDstPath . '"' . \PHP_EOL; |
||
368 | return self::RETURN_SUCCESS; |
||
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 |
||
382 | } |
||
383 | } |
||
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