Skip to content

Commit 3521965

Browse files
committed
Extracts reformatter
1 parent 82a19d2 commit 3521965

3 files changed

Lines changed: 210 additions & 202 deletions

File tree

src/Analyser.php

Lines changed: 1 addition & 200 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use Stolt\LeanPackage\Analysers\ClassicExportIgnoreAnalyser;
77
use Stolt\LeanPackage\Analysers\NegatedExportIgnoreAnalyser;
88
use Stolt\LeanPackage\Gitattributes\FileRepository as GitattributesFileRepository;
9-
use Stolt\LeanPackage\Helpers\Str;
109

1110
class Analyser
1211
{
@@ -76,205 +75,7 @@ public function getExpectedGitattributesContent(array $postfixLessExportIgnores
7675
*/
7776
public function getReformattedGitattributesContent(): string
7877
{
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);
27879
}
27980

28081
public function usesNegatedExportIgnoreStrategy(string $gitattributesContent = ''): bool

src/Analysers/AbstractExportIgnoreAnalyser.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -739,9 +739,9 @@ public function getPresentExportIgnoresToPreserve(array $globPatternMatchingExpo
739739
&$globPatternMatchingExportIgnores,
740740
&$basenamedGlobPatternMatchingExportIgnores
741741
) {
742-
if (\strstr($line, 'export-ignore') && !\str_contains($line, '-export-ignore') && \strpos($line, '#') === false) {
742+
if (\strstr($line, 'export-ignore') && !\str_contains($line, '-export-ignore') && !\str_contains($line, '#')) {
743743
list($pattern, $void) = \explode('export-ignore', $line);
744-
if (\substr($pattern, 0, 1) === '/') {
744+
if (\str_starts_with($pattern, '/')) {
745745
$pattern = \substr($pattern, 1);
746746
$this->hasPrecedingSlashesInExportIgnorePattern = true;
747747
}

0 commit comments

Comments
 (0)