Skip to content

Commit a046505

Browse files
authored
Merge pull request #242 from laravel/add-package-priority-system-to-guideline-inclusion
feat: add package priority guideline inclusion
2 parents 9a48d13 + b670dbf commit a046505

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

src/Install/GuidelineComposer.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Illuminate\Support\Collection;
88
use Illuminate\Support\Facades\Blade;
9+
use Laravel\Roster\Enums\Packages;
910
use Laravel\Roster\Roster;
1011
use Symfony\Component\Finder\Exception\DirectoryNotFoundException;
1112
use Symfony\Component\Finder\Finder;
@@ -21,6 +22,16 @@ class GuidelineComposer
2122

2223
protected GuidelineAssist $guidelineAssist;
2324

25+
/**
26+
* Package priority system to handle conflicts between packages.
27+
* When a higher-priority package is present, lower-priority packages are excluded from guidelines.
28+
*
29+
* @var array<string, string[]>
30+
*/
31+
protected array $packagePriorities = [
32+
Packages::PEST->value => [Packages::PHPUNIT->value],
33+
];
34+
2435
public function __construct(protected Roster $roster, protected Herd $herd)
2536
{
2637
$this->config = new GuidelineConfig;
@@ -118,6 +129,11 @@ protected function find(): Collection
118129
// Add all core and version specific docs for Roster supported packages
119130
// We don't add guidelines for packages unsupported by Roster right now
120131
foreach ($this->roster->packages() as $package) {
132+
// Skip packages that should be excluded due to priority rules
133+
if ($this->shouldExcludePackage($package->package()->value)) {
134+
continue;
135+
}
136+
121137
$guidelineDir = str_replace('_', '-', strtolower($package->name()));
122138

123139
$guidelines->put(
@@ -152,6 +168,23 @@ protected function find(): Collection
152168
->where(fn (array $guideline) => ! empty(trim($guideline['content'])));
153169
}
154170

171+
/**
172+
* Determines if a package should be excluded from guidelines based on priority rules.
173+
*/
174+
protected function shouldExcludePackage(string $packageName): bool
175+
{
176+
foreach ($this->packagePriorities as $priorityPackage => $excludedPackages) {
177+
if (in_array($packageName, $excludedPackages)) {
178+
$priorityEnum = Packages::from($priorityPackage);
179+
if ($this->roster->uses($priorityEnum)) {
180+
return true;
181+
}
182+
}
183+
}
184+
185+
return false;
186+
}
187+
155188
/**
156189
* @param string $dirPath
157190
* @return array<array{content: string, name: string, path: ?string, custom: bool}>

tests/Feature/Install/GuidelineComposerTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,3 +298,36 @@
298298
->toContain('.ai/custom-rule')
299299
->toContain('.ai/project-specific');
300300
});
301+
302+
test('excludes PHPUnit guidelines when Pest is present due to package priority', function () {
303+
$packages = new PackageCollection([
304+
new Package(Packages::LARAVEL, 'laravel/framework', '11.0.0'),
305+
new Package(Packages::PEST, 'pestphp/pest', '3.0.0'),
306+
new Package(Packages::PHPUNIT, 'phpunit/phpunit', '10.0.0'),
307+
]);
308+
309+
$this->roster->shouldReceive('packages')->andReturn($packages);
310+
$this->roster->shouldReceive('uses')->with(Packages::PEST)->andReturn(true);
311+
312+
$guidelines = $this->composer->compose();
313+
314+
expect($guidelines)
315+
->toContain('=== pest/core rules ===')
316+
->not->toContain('=== phpunit/core rules ===');
317+
});
318+
319+
test('includes PHPUnit guidelines when Pest is not present', function () {
320+
$packages = new PackageCollection([
321+
new Package(Packages::LARAVEL, 'laravel/framework', '11.0.0'),
322+
new Package(Packages::PHPUNIT, 'phpunit/phpunit', '10.0.0'),
323+
]);
324+
325+
$this->roster->shouldReceive('packages')->andReturn($packages);
326+
$this->roster->shouldReceive('uses')->with(Packages::PEST)->andReturn(false);
327+
328+
$guidelines = $this->composer->compose();
329+
330+
expect($guidelines)
331+
->toContain('=== phpunit/core rules ===')
332+
->not->toContain('=== pest/core rules ===');
333+
});

0 commit comments

Comments
 (0)