diff --git a/app/Support/PhpCodeSniffer.php b/app/Support/PhpCodeSniffer.php index 3f901d7..60f6e7f 100644 --- a/app/Support/PhpCodeSniffer.php +++ b/app/Support/PhpCodeSniffer.php @@ -14,25 +14,23 @@ public function lint(): int { $this->heading('Linting using PHP_CodeSniffer'); - if (! $this->hasCustomConfig()) { - if (empty($paths = $this->getPaths())) { - return 0; - } + $paths = $this->resolvePaths(); + + if ($paths === null) { + return 0; } - return $this->process('runPHPCS', $paths ?? []); + return $this->process('runPHPCS', $paths); } public function fix(): int { $this->heading('Fixing using PHP_CodeSniffer'); - if ($this->hasCustomConfig()) { - $paths = []; - } else { - if (empty($paths = $this->getPaths())) { - return 0; - } + $paths = $this->resolvePaths(); + + if ($paths === null) { + return 0; } $fix = $this->process('runPHPCBF', $paths); @@ -88,13 +86,45 @@ private function process(string $tool, array $params = []): int } /** - * @return array + * Resolve the paths to hand to PHP_CodeSniffer. + * + * Returns: + * - `null` when PHPCS should be skipped entirely (e.g. only Blade + * files were passed explicitly, or there is nothing to lint). + * - An empty array when PHPCS should fall back to the project's custom + * ruleset (custom config present and no explicit paths). + * - A list of paths otherwise. + * + * @return array|null */ - private function getPaths(): array + private function resolvePaths(): ?array { - $paths = $this->dusterConfig->get('paths') === [Project::path()] - ? $this->getDefaultDirectories() : $this->dusterConfig->get('paths'); + if ($this->hasExplicitPaths()) { + $paths = $this->filterBladeFiles($this->dusterConfig->get('paths')); + + return empty($paths) ? null : $paths; + } + + if ($this->hasCustomConfig()) { + return []; + } + $paths = $this->filterBladeFiles($this->getDefaultDirectories()); + + return empty($paths) ? null : $paths; + } + + private function hasExplicitPaths(): bool + { + return $this->dusterConfig->get('paths') !== [Project::path()]; + } + + /** + * @param array $paths + * @return array + */ + private function filterBladeFiles(array $paths): array + { return array_values(array_filter($paths, function ($path) { if (is_dir($path)) { return true; diff --git a/tests/Feature/PhpCodeSnifferConfigOverrideTest.php b/tests/Feature/PhpCodeSnifferConfigOverrideTest.php index fe6d4d4..cc80eae 100644 --- a/tests/Feature/PhpCodeSnifferConfigOverrideTest.php +++ b/tests/Feature/PhpCodeSnifferConfigOverrideTest.php @@ -12,3 +12,31 @@ ->toContain('Linting using PHP_CodeSniffer') ->toContain('Comment refers to a TODO task'); }); + +it('lints only the explicit paths when project has a custom phpcs config', function () { + chdir(__DIR__ . '/../Fixtures/PhpCodeSnifferProjectConfigWithExplicitFile'); + + [$statusCode, $output] = run('lint', [ + 'path' => base_path('tests/Fixtures/PhpCodeSnifferProjectConfigWithExplicitFile/CleanClass.php'), + '--using' => 'phpcs', + ]); + + expect($statusCode)->toBe(0) + ->and($output) + ->toContain('Linting using PHP_CodeSniffer') + ->not->toContain('Comment refers to a TODO task'); +}); + +it('falls back to ruleset file directives when no explicit paths are provided', function () { + chdir(__DIR__ . '/../Fixtures/PhpCodeSnifferProjectConfigWithExplicitFile'); + + [$statusCode, $output] = run('lint', [ + 'path' => base_path('tests/Fixtures/PhpCodeSnifferProjectConfigWithExplicitFile'), + '--using' => 'phpcs', + ]); + + expect($statusCode)->toBe(1) + ->and($output) + ->toContain('Linting using PHP_CodeSniffer') + ->toContain('Comment refers to a TODO task'); +}); diff --git a/tests/Fixtures/PhpCodeSnifferProjectConfigWithExplicitFile/.phpcs.xml.dist b/tests/Fixtures/PhpCodeSnifferProjectConfigWithExplicitFile/.phpcs.xml.dist new file mode 100644 index 0000000..ff1b140 --- /dev/null +++ b/tests/Fixtures/PhpCodeSnifferProjectConfigWithExplicitFile/.phpcs.xml.dist @@ -0,0 +1,8 @@ + + + BadClass.php + + + + + diff --git a/tests/Fixtures/PhpCodeSnifferProjectConfigWithExplicitFile/BadClass.php b/tests/Fixtures/PhpCodeSnifferProjectConfigWithExplicitFile/BadClass.php new file mode 100644 index 0000000..b4993ae --- /dev/null +++ b/tests/Fixtures/PhpCodeSnifferProjectConfigWithExplicitFile/BadClass.php @@ -0,0 +1,9 @@ +