| Total Complexity | 46 |
| Total Lines | 383 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like GeneratePhpListController 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 GeneratePhpListController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | class GeneratePhpListController 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 List 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 | * Return items from table by type. |
||
| 268 | * |
||
| 269 | * @param string $type |
||
| 270 | * |
||
| 271 | * @return array |
||
| 272 | */ |
||
| 273 | public function getItemsFromTableBy(string $type = '') |
||
| 274 | { |
||
| 275 | $items = []; |
||
| 276 | foreach ($this->getItemsFromTable($this->getDataBase(), $this->getTableName()) as $item) { |
||
| 277 | $colType = $this->parseType($item['type'], 'view'); |
||
| 278 | if (0 === \strpos($colType, $type)) { |
||
| 279 | $items[] = $item; |
||
| 280 | } |
||
| 281 | } |
||
| 282 | return $items; |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Return constraints grouped by name. |
||
| 287 | * |
||
| 288 | * @return array |
||
| 289 | */ |
||
| 290 | public function getConstraintsGroupByName() |
||
| 291 | { |
||
| 292 | $constraint = []; |
||
| 293 | foreach ($this->getConstraintFromTable($this->getDataBase(), $this->getTableName()) as $cons) { |
||
| 294 | if (isset($constraint[$cons['name']])) { |
||
| 295 | $constraint[$cons['name']]['column_name'] .= ', ' . $cons['column_name']; |
||
| 296 | } else { |
||
| 297 | $constraint[$cons['name']] = $cons; |
||
| 298 | } |
||
| 299 | } |
||
| 300 | \ksort($constraint); |
||
| 301 | return $constraint; |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Check if options are looking for help. |
||
| 306 | * |
||
| 307 | * @param array $params |
||
| 308 | * |
||
| 309 | * @return int |
||
| 310 | */ |
||
| 311 | private function checkOptions(array $params = []): int |
||
| 312 | { |
||
| 313 | if (isset($params[0])) { |
||
| 314 | switch ($params[0]) { |
||
| 315 | case '-h': |
||
| 316 | case '--help': |
||
| 317 | echo $this->getHelpMsg(); |
||
| 318 | return -1; |
||
| 319 | case '-t': |
||
| 320 | case '--tables': |
||
| 321 | $this->setDataBase($params[1] ?? null); |
||
| 322 | echo $this->getTablesMsg(); |
||
| 323 | return -1; |
||
| 324 | case '-g': |
||
| 325 | case '--gen': |
||
| 326 | $this->setTableName($params[1] ?? ''); |
||
| 327 | $this->setModelName($params[2] ?? ''); |
||
| 328 | $this->setFolderDstPath(\FS_FOLDER . $params[3] ?? ''); |
||
| 329 | $this->setDataBase($params[4] ?? null); |
||
| 330 | } |
||
| 331 | } |
||
| 332 | return 0; |
||
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Check if options are looking for help. |
||
| 337 | * |
||
| 338 | * @param array $params |
||
| 339 | * |
||
| 340 | * @return int |
||
| 341 | */ |
||
| 342 | private function checkParams(array $params = []): int |
||
| 343 | { |
||
| 344 | if (!isset($params[0])) { |
||
| 345 | echo 'No table name setted.' . \PHP_EOL; |
||
| 346 | return -1; |
||
| 347 | } |
||
| 348 | if (!isset($params[1])) { |
||
| 349 | echo 'No model name setted.' . \PHP_EOL; |
||
| 350 | return -1; |
||
| 351 | } |
||
| 352 | return 0; |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Launch basic checks. |
||
| 357 | * |
||
| 358 | * @return int |
||
| 359 | */ |
||
| 360 | private function check(): int |
||
| 383 | } |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Generate model file. |
||
| 387 | * |
||
| 388 | * @return int |
||
| 389 | */ |
||
| 390 | private function generateModel(): int |
||
| 391 | { |
||
| 392 | $loader = new Twig_Loader_Filesystem([__DIR__ . '/../Template']); |
||
| 393 | $twig = new Twig_Environment($loader, ['debug' => \FS_DEBUG,]); |
||
| 394 | $twig->addExtension(new Twig_Extension_Debug()); |
||
| 395 | $txt = $twig->render( |
||
| 396 | 'ListController.php.twig', |
||
| 397 | ['fsc' => $this] |
||
| 398 | ); |
||
| 399 | |||
| 400 | $status = $this->saveFile($this->folderDstPath . 'List' . $this->modelName . '.php', $txt); |
||
| 401 | if (\is_bool($status)) { |
||
| 402 | echo "Can't save " . $this->folderDstPath . 'List' . $this->modelName . '.php"' . \PHP_EOL; |
||
| 403 | return $status; |
||
| 404 | } |
||
| 405 | echo 'Finished! Look at "' . $this->folderDstPath . '"' . \PHP_EOL; |
||
| 406 | return self::RETURN_SUCCESS; |
||
| 407 | } |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Save file. |
||
| 411 | * |
||
| 412 | * @param string $pathName |
||
| 413 | * @param string $content |
||
| 414 | * |
||
| 415 | * @return int |
||
| 416 | */ |
||
| 417 | private function saveFile(string $pathName, string $content): int |
||
| 420 | } |
||
| 421 | } |
||
| 422 |
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