Skip to content

Commit 1d697c1

Browse files
committed
Improves the section grouping
1 parent 2edb115 commit 1d697c1

3 files changed

Lines changed: 54 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
77

88
## [Unreleased]
99

10+
### Changed
11+
- Improved the section grouping.
12+
1013
## [v5.9.0] - 2026-05-16
1114

1215
### Added

src/Analyser.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,6 +1076,8 @@ private function applyGrouping(array $lines, string $eol): string
10761076
$nonExportIgnoreLines = $this->collapseAndTrimBlankLines($nonExportIgnoreLines);
10771077
$exportIgnoreLines = $this->collapseAndTrimBlankLines($exportIgnoreLines);
10781078

1079+
$nonExportIgnoreLines = $this->subGroupNonExportIgnoreLines($nonExportIgnoreLines);
1080+
10791081
if ($nonExportIgnoreLines === [] && $exportIgnoreLines === []) {
10801082
return '';
10811083
}
@@ -1091,6 +1093,52 @@ private function applyGrouping(array $lines, string $eol): string
10911093
return \implode($eol, $nonExportIgnoreLines) . $eol . $eol . \implode($eol, $exportIgnoreLines);
10921094
}
10931095

1096+
/**
1097+
* Sub-group non-export-ignore lines: glob/wildcard patterns first,
1098+
* then specific-file attribute directives, comments before both.
1099+
*
1100+
* @param array<int, string> $lines
1101+
* @return array<int, string>
1102+
*/
1103+
private function subGroupNonExportIgnoreLines(array $lines): array
1104+
{
1105+
$commentLines = [];
1106+
$globPatternLines = [];
1107+
$specificFileLines = [];
1108+
1109+
foreach ($lines as $line) {
1110+
if (\str_starts_with(\ltrim($line), '#')) {
1111+
$commentLines[] = $line;
1112+
} elseif ($line !== '' && \str_starts_with(\ltrim($line), '*')) {
1113+
$globPatternLines[] = $line;
1114+
} elseif ($line !== '') {
1115+
$specificFileLines[] = $line;
1116+
}
1117+
}
1118+
1119+
$result = [];
1120+
1121+
if ($commentLines !== []) {
1122+
$result = \array_merge($result, $commentLines);
1123+
if ($globPatternLines !== [] || $specificFileLines !== []) {
1124+
$result[] = '';
1125+
}
1126+
}
1127+
1128+
if ($globPatternLines !== []) {
1129+
$result = \array_merge($result, $globPatternLines);
1130+
if ($specificFileLines !== []) {
1131+
$result[] = '';
1132+
}
1133+
}
1134+
1135+
if ($specificFileLines !== []) {
1136+
$result = \array_merge($result, $specificFileLines);
1137+
}
1138+
1139+
return $result;
1140+
}
1141+
10941142
/**
10951143
* Get the present export-ignore entries of
10961144
* the .gitattributes file.

tests/Commands/ReformatCommandTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,8 @@ public function groupsNonExportIgnoreDirectivesAndWritesFile(): void
396396
.gitattributes export-ignore
397397
CHANGELOG.md merge=union
398398
.gitignore export-ignore
399+
400+
*.phar binary
399401
CONTENT;
400402

401403
$this->createTemporaryGitattributesFile($gitattributesContent);
@@ -411,6 +413,7 @@ public function groupsNonExportIgnoreDirectivesAndWritesFile(): void
411413
# This file was reformatted by the lean package validator (http://git.io/lean-package-validator).
412414
413415
* text=auto eol=lf
416+
*.phar binary
414417
415418
composer.lock diff=json
416419
CHANGELOG.md merge=union

0 commit comments

Comments
 (0)