|
6 | 6 | use Stolt\LeanPackage\Analysers\ClassicExportIgnoreAnalyser; |
7 | 7 | use Stolt\LeanPackage\Analysers\NegatedExportIgnoreAnalyser; |
8 | 8 | use Stolt\LeanPackage\Gitattributes\FileRepository as GitattributesFileRepository; |
9 | | -use Stolt\LeanPackage\Helpers\Str; |
10 | 9 |
|
11 | 10 | class Analyser |
12 | 11 | { |
@@ -76,205 +75,7 @@ public function getExpectedGitattributesContent(array $postfixLessExportIgnores |
76 | 75 | */ |
77 | 76 | public function getReformattedGitattributesContent(): string |
78 | 77 | { |
79 | | - $gitattributesContent = $this->exportIgnoreAnalyser->getPresentGitAttributesContent(); |
80 | | - |
81 | | - if ($gitattributesContent === '') { |
82 | | - return ''; |
83 | | - } |
84 | | - |
85 | | - $eol = Str::detectEol($gitattributesContent); |
86 | | - |
87 | | - $gitattributesLines = \preg_split('/\\r\\n|\\r|\\n/', $gitattributesContent); |
88 | | - |
89 | | - if ($gitattributesLines === false) { |
90 | | - return $gitattributesContent; |
91 | | - } |
92 | | - |
93 | | - $exportIgnorePatterns = []; |
94 | | - |
95 | | - foreach ($gitattributesLines as $line) { |
96 | | - if ($this->exportIgnoreAnalyser->isAlignableExportIgnoreLine($line) === false || $line === '') { |
97 | | - continue; |
98 | | - } |
99 | | - |
100 | | - |
101 | | - [$pattern] = \explode('export-ignore', $line, 2); |
102 | | - |
103 | | - if ($this->exportIgnoreAnalyser->isNegatedExportIgnoreLine($line)) { |
104 | | - [$pattern] = \explode('-export-ignore', $line, 2); |
105 | | - } |
106 | | - |
107 | | - $exportIgnorePatterns[] = \rtrim($pattern); |
108 | | - } |
109 | | - |
110 | | - if ($exportIgnorePatterns === []) { |
111 | | - return $gitattributesContent; |
112 | | - } |
113 | | - |
114 | | - $longestPattern = \max(\array_map('strlen', $exportIgnorePatterns)); |
115 | | - |
116 | | - $alignedLines = \array_map(function (string $line) use ($longestPattern): string { |
117 | | - if ($this->exportIgnoreAnalyser->isAlignableExportIgnoreLine($line) === false) { |
118 | | - return $line; |
119 | | - } |
120 | | - $exportIgnorePattern = 'export-ignore'; |
121 | | - |
122 | | - if ($this->exportIgnoreAnalyser->isNegatedExportIgnoreLine($line)) { |
123 | | - $exportIgnorePattern = '-export-ignore'; |
124 | | - } |
125 | | - |
126 | | - [$pattern, $suffix] = \explode($exportIgnorePattern, $line, 2); |
127 | | - |
128 | | - $pattern = \trim($pattern); |
129 | | - |
130 | | - if (\str_starts_with($pattern, '/')) { |
131 | | - $pattern = \ltrim($pattern, '/'); |
132 | | - } |
133 | | - |
134 | | - if (\is_dir($this->exportIgnoreAnalyser->directory . DIRECTORY_SEPARATOR . $pattern) && \str_ends_with($pattern, '/') === false) { |
135 | | - $pattern .= DIRECTORY_SEPARATOR; |
136 | | - } |
137 | | - |
138 | | - if (\strlen($pattern) > $longestPattern) { |
139 | | - $longestPattern = \strlen($pattern); |
140 | | - } |
141 | | - |
142 | | - return $pattern . \str_repeat(' ', $longestPattern - \strlen($pattern) + 1) . $exportIgnorePattern . $suffix; |
143 | | - }, $gitattributesLines); |
144 | | - |
145 | | - if ($this->exportIgnoreAnalyser->sortAlphabetically === true) { |
146 | | - \sort($alignedLines, SORT_STRING); |
147 | | - } |
148 | | - |
149 | | - if ($this->exportIgnoreAnalyser->sortFromDirectoriesToFiles === true) { |
150 | | - $directories = \array_filter($alignedLines, static function (string $line): bool { |
151 | | - return \dirname($line) !== '.'; |
152 | | - }); |
153 | | - |
154 | | - $files = \array_filter($alignedLines, static function (string $line): bool { |
155 | | - return \dirname($line) === '.'; |
156 | | - }); |
157 | | - |
158 | | - \sort($directories, SORT_NATURAL); |
159 | | - \usort($files, static function (string $a, string $b): int { |
160 | | - return \strnatcasecmp(\basename($a), \basename($b)); |
161 | | - }); |
162 | | - |
163 | | - $alignedLines = \array_merge($directories, $files); |
164 | | - } |
165 | | - |
166 | | - if ($this->exportIgnoreAnalyser->groupNonExportIgnores) { |
167 | | - return $this->applyGrouping($alignedLines, $eol); |
168 | | - } |
169 | | - |
170 | | - return \implode($eol, $alignedLines); |
171 | | - } |
172 | | - |
173 | | - /** |
174 | | - * Reorganise lines into a non-export-ignore section followed by an |
175 | | - * export-ignore section. A comment immediately preceding an export-ignore |
176 | | - * line (no blank line between them) is treated as "sticky" and kept in the |
177 | | - * export-ignore section alongside the entries it describes. |
178 | | - * |
179 | | - * @param array<int, string> $lines |
180 | | - */ |
181 | | - private function applyGrouping(array $lines, string $eol): string |
182 | | - { |
183 | | - $count = \count($lines); |
184 | | - $isExportIgnoresGroup = \array_fill(0, $count, false); |
185 | | - $nextIsAnExportIgnore = false; |
186 | | - |
187 | | - for ($i = $count - 1; $i >= 0; $i--) { |
188 | | - $line = $lines[$i]; |
189 | | - if ($this->exportIgnoreAnalyser->isAlignableExportIgnoreLine($line)) { |
190 | | - $isExportIgnoresGroup[$i] = true; |
191 | | - $nextIsAnExportIgnore = true; |
192 | | - } elseif (\trim($line) === '') { |
193 | | - $isExportIgnoresGroup[$i] = false; |
194 | | - $nextIsAnExportIgnore = false; |
195 | | - } elseif (\str_starts_with(\ltrim($line), '#')) { |
196 | | - $isExportIgnoresGroup[$i] = $nextIsAnExportIgnore; |
197 | | - } else { |
198 | | - $isExportIgnoresGroup[$i] = false; |
199 | | - $nextIsAnExportIgnore = false; |
200 | | - } |
201 | | - } |
202 | | - |
203 | | - $nonExportIgnoreLines = []; |
204 | | - $exportIgnoreLines = []; |
205 | | - |
206 | | - foreach ($lines as $i => $line) { |
207 | | - if ($isExportIgnoresGroup[$i]) { |
208 | | - $exportIgnoreLines[] = $line; |
209 | | - } else { |
210 | | - $nonExportIgnoreLines[] = $line; |
211 | | - } |
212 | | - } |
213 | | - |
214 | | - $nonExportIgnoreLines = $this->exportIgnoreAnalyser->collapseAndTrimBlankLines($nonExportIgnoreLines); |
215 | | - $exportIgnoreLines = $this->exportIgnoreAnalyser->collapseAndTrimBlankLines($exportIgnoreLines); |
216 | | - |
217 | | - $nonExportIgnoreLines = $this->subGroupNonExportIgnoreLines($nonExportIgnoreLines); |
218 | | - |
219 | | - if ($nonExportIgnoreLines === [] && $exportIgnoreLines === []) { |
220 | | - return ''; |
221 | | - } |
222 | | - |
223 | | - if ($nonExportIgnoreLines === []) { |
224 | | - return \implode($eol, $exportIgnoreLines); |
225 | | - } |
226 | | - |
227 | | - if ($exportIgnoreLines === []) { |
228 | | - return \implode($eol, $nonExportIgnoreLines); |
229 | | - } |
230 | | - |
231 | | - return \implode($eol, $nonExportIgnoreLines) . $eol . $eol . \implode($eol, $exportIgnoreLines); |
232 | | - } |
233 | | - |
234 | | - /** |
235 | | - * Subgroup non-export-ignore lines: glob/wildcard patterns first, |
236 | | - * then specific-file attribute directives, comments before both. |
237 | | - * |
238 | | - * @param array<int, string> $lines |
239 | | - * @return array<int, string> |
240 | | - */ |
241 | | - private function subGroupNonExportIgnoreLines(array $lines): array |
242 | | - { |
243 | | - $commentLines = []; |
244 | | - $globPatternLines = []; |
245 | | - $specificFileLines = []; |
246 | | - |
247 | | - foreach ($lines as $line) { |
248 | | - if (\str_starts_with(\ltrim($line), '#')) { |
249 | | - $commentLines[] = $line; |
250 | | - } elseif ($line !== '' && \str_starts_with(\ltrim($line), '*')) { |
251 | | - $globPatternLines[] = $line; |
252 | | - } elseif ($line !== '') { |
253 | | - $specificFileLines[] = $line; |
254 | | - } |
255 | | - } |
256 | | - |
257 | | - $result = []; |
258 | | - |
259 | | - if ($commentLines !== []) { |
260 | | - $result = \array_merge($result, $commentLines); |
261 | | - if ($globPatternLines !== [] || $specificFileLines !== []) { |
262 | | - $result[] = ''; |
263 | | - } |
264 | | - } |
265 | | - |
266 | | - if ($globPatternLines !== []) { |
267 | | - $result = \array_merge($result, $globPatternLines); |
268 | | - if ($specificFileLines !== []) { |
269 | | - $result[] = ''; |
270 | | - } |
271 | | - } |
272 | | - |
273 | | - if ($specificFileLines !== []) { |
274 | | - $result = \array_merge($result, $specificFileLines); |
275 | | - } |
276 | | - |
277 | | - return $result; |
| 78 | + return (new Reformatter())->reformat($this->exportIgnoreAnalyser); |
278 | 79 | } |
279 | 80 |
|
280 | 81 | public function usesNegatedExportIgnoreStrategy(string $gitattributesContent = ''): bool |
|
0 commit comments