| Total Complexity | 43 | 
| Total Lines | 263 | 
| Duplicated Lines | 0 % | 
| Changes | 0 | ||
Complex classes like HistoryTrait 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 HistoryTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 19 | trait HistoryTrait  | 
            ||
| 20 | { | 
            ||
| 21 | /**  | 
            ||
| 22 | * Collects all attrs, Types and diffs for further code-generation  | 
            ||
| 23 | *  | 
            ||
| 24 | * @throws \Symfony\Component\Yaml\Exception\ParseException  | 
            ||
| 25 | */  | 
            ||
| 26 | private function setMergedTypes(): void  | 
            ||
| 27 |     { | 
            ||
| 28 | $opMerge = $this->options[ConsoleInterface::OPTION_MERGE];  | 
            ||
| 29 | $timeCheck = strtotime($opMerge); // only for validation - with respect to diff timezones  | 
            ||
| 30 | |||
| 31 |         if (false !== $timeCheck) { | 
            ||
| 32 |             try { | 
            ||
| 33 | $this->mergeTime($opMerge);  | 
            ||
| 34 |             } catch (DirectoryException $e) { | 
            ||
| 35 | $this->error($e->getTraceAsString());  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 36 | }  | 
            ||
| 37 |         } else if (is_numeric($opMerge) !== false) { | 
            ||
| 38 | $this->mergeStep($opMerge);  | 
            ||
| 39 |         } else if ($opMerge === ConsoleInterface::MERGE_DEFAULT_VALUE) { | 
            ||
| 40 | $this->mergeLast();  | 
            ||
| 41 | }  | 
            ||
| 42 | }  | 
            ||
| 43 | |||
| 44 | /**  | 
            ||
| 45 | * Merges history OAS files with current by time in the past  | 
            ||
| 46 | *  | 
            ||
| 47 | * @param string $opMerge  | 
            ||
| 48 | * @throws \Symfony\Component\Yaml\Exception\ParseException  | 
            ||
| 49 | * @throws DirectoryException  | 
            ||
| 50 | */  | 
            ||
| 51 | private function mergeTime(string $opMerge): void  | 
            ||
| 52 |     { | 
            ||
| 53 | $dateTime = explode(PhpInterface::SPACE, $opMerge);  | 
            ||
| 54 | $this->composeTypes($this->composeTimeFiles($dateTime), $this->files);  | 
            ||
| 55 | }  | 
            ||
| 56 | |||
| 57 | /**  | 
            ||
| 58 | * @param array $dateTime  | 
            ||
| 59 | * @return array  | 
            ||
| 60 | * @throws DirectoryException  | 
            ||
| 61 | */  | 
            ||
| 62 | private function composeTimeFiles(array $dateTime): array  | 
            ||
| 63 |     { | 
            ||
| 64 |         $time = str_replace(':', '', $dateTime[1]); | 
            ||
| 65 | |||
| 66 | $this->genDir = $dateTime[0];  | 
            ||
| 67 | $path = DirsInterface::GEN_DIR . DIRECTORY_SEPARATOR . $this->genDir . DIRECTORY_SEPARATOR;  | 
            ||
| 68 | |||
| 69 |         if (is_dir($path) === false) { | 
            ||
| 70 |             throw new DirectoryException('The directory: ' . $path . ' was not found.', | 
            ||
| 71 | ErrorsInterface::CODE_DIR_NOT_FOUND);  | 
            ||
| 72 | }  | 
            ||
| 73 | |||
| 74 | $files = glob($path . $time . '*');  | 
            ||
| 75 |         foreach ($files as &$fullPath) { | 
            ||
| 76 | $fullPath = str_replace($path, '', $fullPath);  | 
            ||
| 77 | }  | 
            ||
| 78 | |||
| 79 | $files = array_diff($files, DirsInterface::EXCLUDED_DIRS);  | 
            ||
| 80 | |||
| 81 | return $this->adjustFiles($files);  | 
            ||
| 82 | }  | 
            ||
| 83 | |||
| 84 | /**  | 
            ||
| 85 | * Merges history OAS files with current by backward steps  | 
            ||
| 86 | *  | 
            ||
| 87 | * @param int $step  | 
            ||
| 88 | * @throws \Symfony\Component\Yaml\Exception\ParseException  | 
            ||
| 89 | */  | 
            ||
| 90 | private function mergeStep(int $step): void  | 
            ||
| 91 |     { | 
            ||
| 92 | $dirs = scandir(DirsInterface::GEN_DIR . DIRECTORY_SEPARATOR, SCANDIR_SORT_DESCENDING);  | 
            ||
| 93 |         if ($dirs !== false) { | 
            ||
| 94 | $dirs = array_diff($dirs, DirsInterface::EXCLUDED_DIRS);  | 
            ||
| 95 | $this->composeTypes($this->composeStepFiles($dirs, $step), $this->files);  | 
            ||
| 96 | }  | 
            ||
| 97 | }  | 
            ||
| 98 | |||
| 99 | /**  | 
            ||
| 100 | * Composes files for step back in history via .gen dir  | 
            ||
| 101 | *  | 
            ||
| 102 | * @param array $dirs  | 
            ||
| 103 | * @param int $step  | 
            ||
| 104 | * @return array  | 
            ||
| 105 | */  | 
            ||
| 106 | private function composeStepFiles(array $dirs, int $step): array  | 
            ||
| 137 | }  | 
            ||
| 138 | |||
| 139 | /**  | 
            ||
| 140 | * Merges current state with the last one  | 
            ||
| 141 | *  | 
            ||
| 142 | * @throws \Symfony\Component\Yaml\Exception\ParseException  | 
            ||
| 143 | */  | 
            ||
| 144 | private function mergeLast(): void  | 
            ||
| 149 | }  | 
            ||
| 150 | }  | 
            ||
| 151 | |||
| 152 | /**  | 
            ||
| 153 | * Gets last files according to main file named "openapi" by spec of OAS  | 
            ||
| 154 | * and it's included files defined in "uses" property  | 
            ||
| 155 | *  | 
            ||
| 156 | * @return array  | 
            ||
| 157 | * @throws \Symfony\Component\Yaml\Exception\ParseException  | 
            ||
| 158 | */  | 
            ||
| 159 | private function getLastFiles(): array  | 
            ||
| 160 |     { | 
            ||
| 161 | $dirs = scandir(DirsInterface::GEN_DIR . DIRECTORY_SEPARATOR, SCANDIR_SORT_DESCENDING);  | 
            ||
| 162 |         if ($dirs !== false) { | 
            ||
| 163 | $dirs = array_diff($dirs, DirsInterface::EXCLUDED_DIRS);  | 
            ||
| 164 | $this->genDir = $dirs[0]; // desc last date YYYY-mm-dd  | 
            ||
| 165 | |||
| 166 | $files = scandir(DirsInterface::GEN_DIR . DIRECTORY_SEPARATOR . $this->genDir, SCANDIR_SORT_DESCENDING);  | 
            ||
| 167 | $files = array_diff($files, DirsInterface::EXCLUDED_DIRS);  | 
            ||
| 168 | |||
| 169 | $lastFiles = [];  | 
            ||
| 170 |             foreach ($files as $file) { | 
            ||
| 171 |                 if (($pos = strpos($file, ApiInterface::OPEN_API_KEY)) !== false) { | 
            ||
| 172 | $lastFiles[] = $file;  | 
            ||
| 173 | $content = Yaml::parse(file_get_contents($this->formatGenPathByDir() . $file));  | 
            ||
| 174 |                     if (empty($content[ApiInterface::RAML_KEY_USES]) === false) { | 
            ||
| 175 |                         foreach ($content[ApiInterface::RAML_KEY_USES] as $subFile) { | 
            ||
| 176 | $lastFiles[] = substr($file, 0, $pos) . basename($subFile);  | 
            ||
| 177 | }  | 
            ||
| 178 | }  | 
            ||
| 179 | break;  | 
            ||
| 180 | }  | 
            ||
| 181 | }  | 
            ||
| 182 | |||
| 183 | return $this->adjustFiles($lastFiles);  | 
            ||
| 184 | }  | 
            ||
| 185 | |||
| 186 | return [];  | 
            ||
| 187 | }  | 
            ||
| 188 | |||
| 189 | /**  | 
            ||
| 190 | * Gets history files and merges them with current OAS files  | 
            ||
| 191 | *  | 
            ||
| 192 | * @param array $files files from .gen/ dir saved history  | 
            ||
| 193 | * @param array $inputFiles file that were passed as an option + files from uses RAML property  | 
            ||
| 194 | * @throws \Symfony\Component\Yaml\Exception\ParseException  | 
            ||
| 195 | */  | 
            ||
| 196 | private function composeTypes(array $files, array $inputFiles): void  | 
            ||
| 197 |     { | 
            ||
| 198 | $attrsCurrent = [];  | 
            ||
| 199 | $attrsHistory = [];  | 
            ||
| 200 | |||
| 201 | $path = DirsInterface::GEN_DIR . DIRECTORY_SEPARATOR . $this->genDir . DIRECTORY_SEPARATOR;  | 
            ||
| 202 |         foreach ($files as $file) { | 
            ||
| 203 |             foreach ($inputFiles as $inFile) { | 
            ||
| 204 | |||
| 205 |                 if (mb_strpos($file, basename($inFile), null, PhpInterface::ENCODING_UTF8) !== false) { | 
            ||
| 206 | $dataCurrent = Yaml::parse(file_get_contents($inFile));  | 
            ||
| 207 | $dataHistory = Yaml::parse(file_get_contents($path . $file));  | 
            ||
| 208 | |||
| 209 | $this->currentTypes = $dataCurrent[ApiInterface::API_COMPONENTS][ApiInterface::API_SCHEMAS];  | 
            ||
| 210 | $this->historyTypes = $dataHistory[ApiInterface::API_COMPONENTS][ApiInterface::API_SCHEMAS];  | 
            ||
| 211 | $this->types += array_merge_recursive($this->historyTypes, $this->currentTypes);  | 
            ||
| 212 | |||
| 213 |                     $attrsCurrent += array_filter($this->currentTypes, function ($k) { | 
            ||
| 214 | return strpos($k, CustomsInterface::CUSTOM_TYPES_ATTRIBUTES) !== false;  | 
            ||
| 215 | }, ARRAY_FILTER_USE_KEY);  | 
            ||
| 216 |                     $attrsHistory += array_filter($this->historyTypes, function ($k) { | 
            ||
| 217 | return strpos($k, CustomsInterface::CUSTOM_TYPES_ATTRIBUTES) !== false;  | 
            ||
| 218 | }, ARRAY_FILTER_USE_KEY);  | 
            ||
| 219 | }  | 
            ||
| 220 | }  | 
            ||
| 221 | }  | 
            ||
| 222 | |||
| 223 | $this->composeDiffs($attrsCurrent, $attrsHistory);  | 
            ||
| 224 | }  | 
            ||
| 225 | |||
| 226 | /**  | 
            ||
| 227 | * Compares attributes for current and previous history and sets the diffTypes prop  | 
            ||
| 228 | * to process additional migrations creation  | 
            ||
| 229 | *  | 
            ||
| 230 | * @param array $attrsCurrent Current attributes  | 
            ||
| 231 | * @param array $attrsHistory History attributes  | 
            ||
| 232 | */  | 
            ||
| 233 | private function composeDiffs(array $attrsCurrent, array $attrsHistory): void  | 
            ||
| 256 | }  | 
            ||
| 257 | }  | 
            ||
| 258 | }  | 
            ||
| 259 | }  | 
            ||
| 260 | }  | 
            ||
| 261 | |||
| 262 | /**  | 
            ||
| 263 | * Gets an unordered array of files and returns an ordered one  | 
            ||
| 264 | * stating from *openapi.yaml  | 
            ||
| 265 | *  | 
            ||
| 266 | * @param array $files  | 
            ||
| 267 | * @return array  | 
            ||
| 268 | */  | 
            ||
| 269 | private function adjustFiles(array $files): array  | 
            ||
| 282 | }  | 
            ||
| 283 | }  |