| Total Complexity | 59 |
| Total Lines | 395 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like GeneratorTrait 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 GeneratorTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | trait GeneratorTrait |
||
| 29 | { |
||
| 30 | // all generated entities/resources |
||
| 31 | private $forms; |
||
| 32 | private $mappers; |
||
| 33 | private $routes; |
||
| 34 | private $migrations; |
||
| 35 | private $controllers; |
||
| 36 | private $tests; |
||
| 37 | |||
| 38 | // gen dir found in history |
||
| 39 | private $genDir; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Standard generation |
||
| 43 | */ |
||
| 44 | private function generateResources(): void |
||
| 45 | { |
||
| 46 | $this->outputEntity(); |
||
| 47 | |||
| 48 | // create controller |
||
| 49 | $this->solveControllers(); |
||
| 50 | |||
| 51 | // create FormRequest |
||
| 52 | $this->solveFormRequest(); |
||
| 53 | |||
| 54 | // create entities/models |
||
| 55 | $this->solveEntities(); |
||
| 56 | |||
| 57 | // create routes |
||
| 58 | $this->routes = new Routes($this); |
||
| 59 | $this->routes->create(); |
||
| 60 | |||
| 61 | // create tests |
||
| 62 | if (empty($this->options[ConsoleInterface::OPTION_TESTS]) === false) { |
||
| 63 | try { |
||
| 64 | FileManager::createPath($this->formatFuncTestsPath()); |
||
|
|
|||
| 65 | } catch (DirectoryException $e) { |
||
| 66 | $this->error($e->getTraceAsString()); |
||
| 67 | } |
||
| 68 | |||
| 69 | $this->tests = new Tests($this); |
||
| 70 | $this->tests->createEntity($this->formatFuncTestsPath(), DefaultInterface::FUNCTIONAL_POSTFIX); |
||
| 71 | } |
||
| 72 | |||
| 73 | $this->createMigrations(); |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Generation with merge option |
||
| 78 | */ |
||
| 79 | private function mergeResources(): void |
||
| 80 | { |
||
| 81 | $this->outputEntity(); |
||
| 82 | $this->solveControllers(); |
||
| 83 | |||
| 84 | $this->solveFormRequest(); |
||
| 85 | |||
| 86 | $this->solveEntities(); |
||
| 87 | |||
| 88 | // create routes |
||
| 89 | $this->routes = new Routes($this); |
||
| 90 | $this->routes->create(); |
||
| 91 | |||
| 92 | $this->createMigrations(); |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Creates Controllers and leaves those generated in case of merge |
||
| 97 | */ |
||
| 98 | private function solveControllers(): void |
||
| 99 | { |
||
| 100 | $this->controllers = new Controllers($this); |
||
| 101 | $controllerPath = $this->formatControllersPath(); |
||
| 102 | if (empty($this->options[ConsoleInterface::OPTION_REGENERATE]) === false |
||
| 103 | && file_exists($this->controllers->getEntityFile($controllerPath, |
||
| 104 | DefaultInterface::CONTROLLER_POSTFIX)) === true) { |
||
| 105 | $this->controllers->recreateEntity($controllerPath, DefaultInterface::CONTROLLER_POSTFIX); |
||
| 106 | } else { |
||
| 107 | $this->controllers->createDefault(); |
||
| 108 | $this->controllers->createEntity($controllerPath, DefaultInterface::CONTROLLER_POSTFIX); |
||
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 112 | private function solveFormRequest(): void |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Decide whether to generate new Entities or mutate existing |
||
| 127 | */ |
||
| 128 | private function solveEntities(): void |
||
| 139 | } |
||
| 140 | } |
||
| 141 | |||
| 142 | private function outputEntity(): void |
||
| 143 | { |
||
| 144 | Console::out( |
||
| 145 | '===============' . PhpInterface::SPACE . $this->objectName |
||
| 146 | . PhpInterface::SPACE . DirsInterface::ENTITIES_DIR |
||
| 147 | ); |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Creates migrations for every entity if there is merge option - adds additional |
||
| 152 | */ |
||
| 153 | private function createMigrations(): void |
||
| 154 | { |
||
| 155 | if (empty($this->options[ConsoleInterface::OPTION_MIGRATIONS]) === false) { |
||
| 156 | $this->migrations = new Migrations($this); |
||
| 157 | $this->migrations->create(); |
||
| 158 | $this->migrations->createPivot(); |
||
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Collects all attrs, Types and diffs for further code-generation |
||
| 164 | * |
||
| 165 | * @throws \Symfony\Component\Yaml\Exception\ParseException |
||
| 166 | */ |
||
| 167 | private function setMergedTypes(): void |
||
| 168 | { |
||
| 169 | $opMerge = $this->options[ConsoleInterface::OPTION_MERGE]; |
||
| 170 | $timeCheck = strtotime($opMerge); // only for validation - with respect to diff timezones |
||
| 171 | |||
| 172 | if (false !== $timeCheck) { |
||
| 173 | try { |
||
| 174 | $this->mergeTime($opMerge); |
||
| 175 | } catch (DirectoryException $e) { |
||
| 176 | $this->error($e->getTraceAsString()); |
||
| 177 | } |
||
| 178 | } else if (is_numeric($opMerge) !== false) { |
||
| 179 | $this->mergeStep($opMerge); |
||
| 180 | } else if ($opMerge === ConsoleInterface::MERGE_DEFAULT_VALUE) { |
||
| 181 | $this->mergeLast(); |
||
| 182 | } |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Merges history OAS files with current by time in the past |
||
| 187 | * |
||
| 188 | * @param string $opMerge |
||
| 189 | * @throws \Symfony\Component\Yaml\Exception\ParseException |
||
| 190 | * @throws DirectoryException |
||
| 191 | */ |
||
| 192 | private function mergeTime(string $opMerge): void |
||
| 193 | { |
||
| 194 | $dateTime = explode(PhpInterface::SPACE, $opMerge); |
||
| 195 | $this->composeTypes($this->composeTimeFiles($dateTime), $this->files); |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @param array $dateTime |
||
| 200 | * @return array |
||
| 201 | * @throws DirectoryException |
||
| 202 | */ |
||
| 203 | private function composeTimeFiles(array $dateTime): array |
||
| 204 | { |
||
| 205 | $time = str_replace(':', '', $dateTime[1]); |
||
| 206 | |||
| 207 | $this->genDir = $dateTime[0]; |
||
| 208 | $path = DirsInterface::GEN_DIR . DIRECTORY_SEPARATOR . $this->genDir . DIRECTORY_SEPARATOR; |
||
| 209 | |||
| 210 | if (is_dir($path) === false) { |
||
| 211 | throw new DirectoryException('The directory: ' . $path . ' was not found.', |
||
| 212 | ErrorsInterface::CODE_DIR_NOT_FOUND); |
||
| 213 | } |
||
| 214 | |||
| 215 | $files = glob($path . $time . '*'); |
||
| 216 | foreach ($files as &$fullPath) { |
||
| 217 | $fullPath = str_replace($path, '', $fullPath); |
||
| 218 | } |
||
| 219 | |||
| 220 | $files = array_diff($files, DirsInterface::EXCLUDED_DIRS); |
||
| 221 | |||
| 222 | return $this->adjustFiles($files); |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Merges history OAS files with current by backward steps |
||
| 227 | * |
||
| 228 | * @param int $step |
||
| 229 | * @throws \Symfony\Component\Yaml\Exception\ParseException |
||
| 230 | */ |
||
| 231 | private function mergeStep(int $step): void |
||
| 237 | } |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Composes files for step back in history via .gen dir |
||
| 242 | * |
||
| 243 | * @param array $dirs |
||
| 244 | * @param int $step |
||
| 245 | * @return array |
||
| 246 | */ |
||
| 247 | private function composeStepFiles(array $dirs, int $step): array |
||
| 248 | { |
||
| 249 | $filesToPass = []; |
||
| 250 | foreach ($dirs as $dir) { |
||
| 251 | $files = scandir(DirsInterface::GEN_DIR . DIRECTORY_SEPARATOR . $dir, SCANDIR_SORT_DESCENDING); |
||
| 252 | $files = array_diff($files, DirsInterface::EXCLUDED_DIRS); |
||
| 253 | |||
| 254 | $prefixFlag = ''; |
||
| 255 | foreach ($files as $kFile => $file) { |
||
| 256 | $prefix = substr($file, 0, 6); // Hms |
||
| 257 | $template = '/^' . $prefix . '.*$/i'; |
||
| 258 | |||
| 259 | if ($prefix !== $prefixFlag) { |
||
| 260 | --$step; |
||
| 261 | $prefixFlag = $prefix; |
||
| 262 | if ($step > 0) { |
||
| 263 | $skip = preg_grep($template, $files); |
||
| 264 | $files = array_diff($files, $skip); |
||
| 265 | } |
||
| 266 | } |
||
| 267 | |||
| 268 | if ($step <= 0) { |
||
| 269 | $files = preg_grep($template, $files); |
||
| 270 | $this->genDir = $dir; |
||
| 271 | $filesToPass = $files; |
||
| 272 | break 2; |
||
| 273 | } |
||
| 274 | } |
||
| 275 | } |
||
| 276 | |||
| 277 | return $this->adjustFiles($filesToPass); |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Merges current state with the last one |
||
| 282 | * |
||
| 283 | * @throws \Symfony\Component\Yaml\Exception\ParseException |
||
| 284 | */ |
||
| 285 | private function mergeLast(): void |
||
| 290 | } |
||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Gets last files according to main file named "openapi" by spec of OAS |
||
| 295 | * and it's included files defined in "uses" property |
||
| 296 | * |
||
| 297 | * @return array |
||
| 298 | * @throws \Symfony\Component\Yaml\Exception\ParseException |
||
| 299 | */ |
||
| 300 | private function getLastFiles(): array |
||
| 301 | { |
||
| 302 | $dirs = scandir(DirsInterface::GEN_DIR . DIRECTORY_SEPARATOR, SCANDIR_SORT_DESCENDING); |
||
| 303 | if ($dirs !== false) { |
||
| 304 | $dirs = array_diff($dirs, DirsInterface::EXCLUDED_DIRS); |
||
| 305 | $this->genDir = $dirs[0]; // desc last date YYYY-mm-dd |
||
| 306 | |||
| 307 | $files = scandir(DirsInterface::GEN_DIR . DIRECTORY_SEPARATOR . $this->genDir, SCANDIR_SORT_DESCENDING); |
||
| 308 | $files = array_diff($files, DirsInterface::EXCLUDED_DIRS); |
||
| 309 | |||
| 310 | $lastFiles = []; |
||
| 311 | foreach ($files as $file) { |
||
| 312 | if (($pos = strpos($file, ApiInterface::OPEN_API_KEY)) !== false) { |
||
| 313 | $lastFiles[] = $file; |
||
| 314 | $content = Yaml::parse(file_get_contents($this->formatGenPathByDir() . $file)); |
||
| 315 | if (empty($content[ApiInterface::RAML_KEY_USES]) === false) { |
||
| 316 | foreach ($content[ApiInterface::RAML_KEY_USES] as $subFile) { |
||
| 317 | $lastFiles[] = substr($file, 0, $pos) . basename($subFile); |
||
| 318 | } |
||
| 319 | } |
||
| 320 | break; |
||
| 321 | } |
||
| 322 | } |
||
| 323 | |||
| 324 | return $this->adjustFiles($lastFiles); |
||
| 325 | } |
||
| 326 | |||
| 327 | return []; |
||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Gets history files and merges them with current OAS files |
||
| 332 | * |
||
| 333 | * @param array $files files from .gen/ dir saved history |
||
| 334 | * @param array $inputFiles file that were passed as an option + files from uses RAML property |
||
| 335 | * @throws \Symfony\Component\Yaml\Exception\ParseException |
||
| 336 | */ |
||
| 337 | private function composeTypes(array $files, array $inputFiles): void |
||
| 365 | } |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Compares attributes for current and previous history and sets the diffTypes prop |
||
| 369 | * to process additional migrations creation |
||
| 370 | * |
||
| 371 | * @param array $attrsCurrent Current attributes |
||
| 372 | * @param array $attrsHistory History attributes |
||
| 373 | */ |
||
| 374 | private function composeDiffs(array $attrsCurrent, array $attrsHistory): void |
||
| 397 | } |
||
| 398 | } |
||
| 399 | } |
||
| 400 | } |
||
| 401 | } |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Gets an unordered array of files and returns an ordered one |
||
| 405 | * stating from *openapi.yaml |
||
| 406 | * |
||
| 407 | * @param array $files |
||
| 408 | * @return array |
||
| 409 | */ |
||
| 410 | private function adjustFiles(array $files): array |
||
| 423 | } |
||
| 424 | } |