Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 45 additions & 15 deletions app/Support/PhpCodeSniffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -88,13 +86,45 @@ private function process(string $tool, array $params = []): int
}

/**
* @return array<int, string>
* 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<int, string>|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<int, string> $paths
* @return array<int, string>
*/
private function filterBladeFiles(array $paths): array
{
return array_values(array_filter($paths, function ($path) {
if (is_dir($path)) {
return true;
Expand Down
28 changes: 28 additions & 0 deletions tests/Feature/PhpCodeSnifferConfigOverrideTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0"?>
<ruleset>
<file>BadClass.php</file>

<rule ref="Tighten"/>

<rule ref="Generic.Commenting.Todo"/>
</ruleset>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

class BadClass
{
public function __construct()
{
// Fix this sniffer Generic.Commenting.Todo
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

class CleanClass
{
public function __construct()
{
}
}