Skip to content

Commit 0b7dbab

Browse files
committed
Utilises the gitattributes repository
1 parent 2a52fa7 commit 0b7dbab

18 files changed

Lines changed: 338 additions & 234 deletions

bin/lean-package-validator

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ use Symfony\Component\Console\Application;
4545

4646
$finder = new Finder(new PhpPreset());
4747
$archive = new Archive(WORKING_DIRECTORY);
48-
$analyser = new Analyser(new ClassicExportIgnoreAnalyser($finder));
49-
$gitattributesFileRepository = new GitattributesFileRepository($analyser);
48+
$gitattributesFileRepository = new GitattributesFileRepository(WORKING_DIRECTORY);
49+
$analyser = new Analyser(new ClassicExportIgnoreAnalyser($finder, $gitattributesFileRepository));
5050

5151
$initCommand = new InitCommand(
5252
$analyser

mago.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Mago configuration file
2+
23
# For more information, see https://mago.carthage.software/#/getting-started/configuration
34
php-version = "8.2.0"
45

@@ -21,6 +22,9 @@ level = "warning"
2122
[linter.rules.too-many-methods]
2223
level = "warning"
2324

25+
[linter.rules.excessive-parameter-list]
26+
level = "warning"
27+
2428
[linter.rules.too-many-properties]
2529
level = "warning"
2630

src/Analyser.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
use Stolt\LeanPackage\Analysers\AbstractExportIgnoreAnalyser;
66
use Stolt\LeanPackage\Analysers\ClassicExportIgnoreAnalyser;
77
use Stolt\LeanPackage\Analysers\NegatedExportIgnoreAnalyser;
8+
use Stolt\LeanPackage\Gitattributes\FileRepository as GitattributesFileRepository;
89
use Stolt\LeanPackage\Helpers\Str;
910

1011
class Analyser
1112
{
1213
private AbstractExportIgnoreAnalyser $exportIgnoreAnalyser;
1314

14-
public function __construct(AbstractExportIgnoreAnalyser $actualExportIgnoreAnalyser) {
15+
public function __construct(
16+
AbstractExportIgnoreAnalyser $actualExportIgnoreAnalyser) {
1517
$this->exportIgnoreAnalyser = $actualExportIgnoreAnalyser;
1618
}
1719

@@ -51,6 +53,7 @@ public function getExpectedGitattributesContent(array $postfixLessExportIgnores
5153
$formerExportIgnoreAnalyserConfiguration = $this->exportIgnoreAnalyser->getConfiguration();
5254
$this->exportIgnoreAnalyser = new NegatedExportIgnoreAnalyser(
5355
$this->exportIgnoreAnalyser->getFinder(),
56+
$this->exportIgnoreAnalyser->getGitattributesFileRepository(),
5457
$this->exportIgnoreAnalyser->getDirectory(),
5558
$formerExportIgnoreAnalyserConfiguration
5659
);
@@ -309,12 +312,12 @@ public function hasCompleteExportIgnoresFromString(string $gitattributesContent)
309312
\sort($expectedExportIgnores, SORT_STRING | SORT_FLAG_CASE);
310313

311314
if ($this->getActualExportIgnoreAnalyser()->getConfiguration()->enforceStrictOrderComparison === true) {
312-
return \array_values($expectedExportIgnores) === \array_values($presentExportIgnores);
315+
return $expectedExportIgnores === $presentExportIgnores;
313316
}
314317

315318
\sort($presentExportIgnores, SORT_STRING | SORT_FLAG_CASE);
316319

317-
return \array_values($expectedExportIgnores) === \array_values($presentExportIgnores);
320+
return $expectedExportIgnores === $presentExportIgnores;
318321
}
319322

320323
/**
@@ -339,6 +342,7 @@ private function buildNegatedAnalyser(): NegatedExportIgnoreAnalyser
339342

340343
$analyser = new NegatedExportIgnoreAnalyser(
341344
$this->exportIgnoreAnalyser->getFinder(),
345+
new GitattributesFileRepository($this->exportIgnoreAnalyser->getDirectory()),
342346
$directory,
343347
$this->exportIgnoreAnalyser->getConfiguration(),
344348
);

src/Analysers/AbstractExportIgnoreAnalyser.php

Lines changed: 80 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Stolt\LeanPackage\Exceptions\NonExistentGlobPatternFile;
1010
use Stolt\LeanPackage\Exceptions\PresetNotAvailable;
1111
use Stolt\LeanPackage\Gitattributes\ValueObject as GitattributesValueObject;
12+
use Stolt\LeanPackage\Gitattributes\FileRepository as GitattributesFileRepository;
1213
use Stolt\LeanPackage\Glob;
1314
use Stolt\LeanPackage\Helpers\Str;
1415
use Stolt\LeanPackage\Presets\Finder;
@@ -150,14 +151,15 @@ abstract class AbstractExportIgnoreAnalyser
150151

151152
public bool $groupNonExportIgnores = false;
152153

153-
private Finder $finder;
154-
155154
/**
156155
* Initialize.
157156
*/
158-
public function __construct(Finder $finder, string $directory = '', ?ExportIgnoreConfiguration $configuration = null)
157+
public function __construct(
158+
protected readonly Finder $finder,
159+
protected readonly GitattributesFileRepository $gitattributesFileRepository,
160+
string $directory = '',
161+
?ExportIgnoreConfiguration $configuration = null)
159162
{
160-
$this->finder = $finder;
161163
$this->defaultGlobPattern = $finder->getDefaultPreset();
162164

163165
$configuration ??= new ExportIgnoreConfiguration(
@@ -168,6 +170,11 @@ public function __construct(Finder $finder, string $directory = '', ?ExportIgnor
168170
$this->configuration = $configuration;
169171

170172
$this->directory = $configuration->directory;
173+
174+
if (!is_dir($this->directory) && defined('WORKING_DIRECTORY')) {
175+
$this->directory = WORKING_DIRECTORY;
176+
}
177+
171178
$this->gitattributesFile = $this->directory . DIRECTORY_SEPARATOR . '.gitattributes';
172179

173180
$this->globPattern = $configuration->globPattern;
@@ -200,7 +207,6 @@ public function getDirectory(): string
200207
*
201208
* @param string $directory The directory to analyse.
202209
* @return AbstractExportIgnoreAnalyser
203-
* @return Analyser
204210
*
205211
* @throws RuntimeException
206212
*/
@@ -210,11 +216,14 @@ public function setDirectory(string $directory = __DIR__): AbstractExportIgnoreA
210216
$message = "Directory {$directory} doesn't exist.";
211217
throw new \RuntimeException($message);
212218
}
219+
213220
$this->directory = $directory;
214221
$this->gitattributesFile = $directory
215222
. DIRECTORY_SEPARATOR
216223
. '.gitattributes';
217224

225+
$this->gitattributesFileRepository->setWorkingDirectory($directory);
226+
218227
return $this;
219228
}
220229

@@ -507,7 +516,7 @@ public function getPresentGitAttributesContent(): string
507516
return '';
508517
}
509518

510-
return (string) \file_get_contents($this->gitattributesFile);
519+
return $this->gitattributesFileRepository->getGitattributesContent();
511520
}
512521

513522
/**
@@ -520,6 +529,11 @@ public function getFinder(): Finder
520529
return $this->finder;
521530
}
522531

532+
public function getGitattributesFileRepository(): GitattributesFileRepository
533+
{
534+
return $this->gitattributesFileRepository;
535+
}
536+
523537
/**
524538
* Set the glob pattern file.
525539
*
@@ -567,11 +581,10 @@ public function setGlobPatternFromFile(string $file): AbstractExportIgnoreAnalys
567581
* @param string $pattern The glob pattern to use to detect expected export-ignores files.
568582
*
569583
* @return AbstractExportIgnoreAnalyser
570-
* @return Analyser
571584
*
572585
* @throws InvalidGlobPattern
573586
*/
574-
public function setGlobPattern($pattern): AbstractExportIgnoreAnalyser
587+
public function setGlobPattern(string $pattern): AbstractExportIgnoreAnalyser
575588
{
576589
$this->globPattern = \trim($pattern);
577590
$this->guardGlobPattern($this->globPattern);
@@ -684,6 +697,61 @@ protected function patternHasMatch(string $globPattern): bool
684697
return \is_array($matches) && \count($matches) > 0;
685698
}
686699

700+
/**
701+
* Return export ignores in a .gitattributes file to preserve.
702+
*
703+
* @param array $globPatternMatchingExportIgnores Export ignores matching glob pattern.
704+
*
705+
* @return array
706+
*/
707+
public function getPresentExportIgnoresToPreserve(array $globPatternMatchingExportIgnores): array
708+
{
709+
$gitattributesContent = $this->gitattributesFileRepository->getGitattributesContent();
710+
711+
if (\preg_match("/(\*\h*)(text\h*)(=\h*auto)/", $gitattributesContent)) {
712+
$this->textAutoconfiguration();
713+
}
714+
715+
$gitattributesLines = \preg_split(
716+
'/\\r\\n|\\r|\\n/',
717+
$gitattributesContent
718+
);
719+
720+
$basenamedGlobPatternMatchingExportIgnores = \array_map(
721+
'basename',
722+
$globPatternMatchingExportIgnores
723+
);
724+
725+
$exportIgnoresToPreserve = [];
726+
727+
\array_filter($gitattributesLines, function ($line) use (
728+
&$exportIgnoresToPreserve,
729+
&$globPatternMatchingExportIgnores,
730+
&$basenamedGlobPatternMatchingExportIgnores
731+
) {
732+
if (\strstr($line, 'export-ignore') && !\str_contains($line, '-export-ignore') && \strpos($line, '#') === false) {
733+
list($pattern, $void) = \explode('export-ignore', $line);
734+
if (\substr($pattern, 0, 1) === '/') {
735+
$pattern = \substr($pattern, 1);
736+
$this->hasPrecedingSlashesInExportIgnorePattern = true;
737+
}
738+
$patternMatches = $this->patternHasMatch($pattern);
739+
$pattern = \trim($pattern);
740+
741+
if ($patternMatches
742+
&& !\in_array($pattern, $globPatternMatchingExportIgnores, strict: true)
743+
&& !\in_array($pattern, $basenamedGlobPatternMatchingExportIgnores, strict: true)
744+
) {
745+
if (\file_exists($this->directory . DIRECTORY_SEPARATOR . $pattern)) {
746+
return $exportIgnoresToPreserve[] = \trim($pattern);
747+
}
748+
}
749+
}
750+
});
751+
752+
return $exportIgnoresToPreserve;
753+
}
754+
687755
protected function mergeWithExistingGitattributes(string $content): string
688756
{
689757
$exportIgnoreContent = \rtrim($content);
@@ -717,7 +785,7 @@ public function getPresentNonExportIgnoresContent(): string
717785
return '';
718786
}
719787

720-
$gitattributesContent = (string) \file_get_contents($this->gitattributesFile);
788+
$gitattributesContent = $this->gitattributesFileRepository->getGitattributesContent();
721789
$eol = Str::detectEol($gitattributesContent);
722790
$this->preferredEol = $eol;
723791

@@ -786,9 +854,9 @@ protected function getAlignedExportIgnoreArtifacts(array $artifacts): array
786854
return \array_map(static function (string $artifact) use (&$longestArtifact) {
787855
if (\strlen($artifact) < $longestArtifact) {
788856
return $artifact . \str_repeat(
789-
' ',
790-
$longestArtifact - \strlen($artifact)
791-
);
857+
' ',
858+
$longestArtifact - \strlen($artifact)
859+
);
792860
}
793861
return $artifact;
794862
}, $artifacts);

src/Analysers/ClassicExportIgnoreAnalyser.php

Lines changed: 1 addition & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -94,61 +94,6 @@ public function collectExpectedExportIgnores(): array
9494
return \array_unique($expectedExportIgnores);
9595
}
9696

97-
/**
98-
* Return export ignores in a .gitattributes file to preserve.
99-
*
100-
* @param array $globPatternMatchingExportIgnores Export ignores matching glob pattern.
101-
*
102-
* @return array
103-
*/
104-
public function getPresentExportIgnoresToPreserve(array $globPatternMatchingExportIgnores): array
105-
{
106-
$gitattributesContent = (string) \file_get_contents($this->gitattributesFile);
107-
108-
if (\preg_match("/(\*\h*)(text\h*)(=\h*auto)/", $gitattributesContent)) {
109-
$this->textAutoconfiguration();
110-
}
111-
112-
$gitattributesLines = \preg_split(
113-
'/\\r\\n|\\r|\\n/',
114-
$gitattributesContent
115-
);
116-
117-
$basenamedGlobPatternMatchingExportIgnores = \array_map(
118-
'basename',
119-
$globPatternMatchingExportIgnores
120-
);
121-
122-
$exportIgnoresToPreserve = [];
123-
124-
\array_filter($gitattributesLines, function ($line) use (
125-
&$exportIgnoresToPreserve,
126-
&$globPatternMatchingExportIgnores,
127-
&$basenamedGlobPatternMatchingExportIgnores
128-
) {
129-
if (\strstr($line, 'export-ignore') && !\str_contains($line, '-export-ignore') && \strpos($line, '#') === false) {
130-
list($pattern, $void) = \explode('export-ignore', $line);
131-
if (\substr($pattern, 0, 1) === '/') {
132-
$pattern = \substr($pattern, 1);
133-
$this->hasPrecedingSlashesInExportIgnorePattern = true;
134-
}
135-
$patternMatches = $this->patternHasMatch($pattern);
136-
$pattern = \trim($pattern);
137-
138-
if ($patternMatches
139-
&& !\in_array($pattern, $globPatternMatchingExportIgnores, strict: true)
140-
&& !\in_array($pattern, $basenamedGlobPatternMatchingExportIgnores, strict: true)
141-
) {
142-
if (\file_exists($this->directory . DIRECTORY_SEPARATOR . $pattern)) {
143-
return $exportIgnoresToPreserve[] = \trim($pattern);
144-
}
145-
}
146-
}
147-
});
148-
149-
return $exportIgnoresToPreserve;
150-
}
151-
15297
public function hasCompleteExportIgnores(): bool
15398
{
15499
$expectedExportIgnores = $this->collectExpectedExportIgnores();
@@ -199,7 +144,7 @@ public function getPresentExportIgnores(bool $applyGlob = true, string $gitattri
199144
}
200145

201146
if ($gitattributesContent === '') {
202-
$gitattributesContent = (string) \file_get_contents($this->gitattributesFile);
147+
$gitattributesContent = $this->gitattributesFileRepository->getGitattributesContent();
203148
}
204149

205150
$gitattributesLines = \preg_split(

0 commit comments

Comments
 (0)