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