Skip to content

Allow PHPCS Sniffs to Disable Themselves #229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
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
40 changes: 37 additions & 3 deletions src/SniffRunner/ValueObject/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ final class File extends BaseFile

private ?string $filePath = null;

/**
* @var array<class-string<Sniff>, int> A map of which sniffers have
* requested themselves to be disabled, pointing to the index in
* the files token stack that they are to be re-enabled.
*/
private array $disabledSniffers = [];

/**
* @var array<class-string<Sniff>>
*/
Expand Down Expand Up @@ -90,17 +97,20 @@ public function process(): void
}

foreach ($this->tokenListeners[$token['code']] as $sniff) {
if ($this->skipper->shouldSkipElementAndFilePath($sniff, $currentFilePath)) {
$shouldSkipSniff = $this->skipper->shouldSkipElementAndFilePath($sniff, $currentFilePath);
$sniffIsDisabled = $this->isSniffStillDisabled($sniff::class, $stackPtr);

if ($shouldSkipSniff || $sniffIsDisabled) {
continue;
}

$this->reportActiveSniffClass($sniff);

$sniff->process($this, $stackPtr);
$this->disableSnifferUntil($sniff::class, $sniff->process($this, $stackPtr));
}
}

$this->fixedCount += $this->fixer->getFixCount();
$this->disabledSniffers = [];
}

/**
Expand Down Expand Up @@ -256,4 +266,28 @@ private function shouldSkipClassWarnings(string $sniffClass): bool

return true;
}

private function isSniffStillDisabled(string $sniffClass, int $targetStackPtr): bool
{
$disabledUntil = $this->disabledSniffers[$sniffClass] ?? 0;

if ($disabledUntil > $targetStackPtr) {
return true;
}

unset($this->disabledSniffers[$sniffClass]);
return false;
}

/**
* @param class-string<Sniff> $sniffClass
*/
private function disableSnifferUntil(string $sniffClass, ?int $targetStackPtr = null): void
{
if ($targetStackPtr === null) {
return;
}

$this->disabledSniffers[$sniffClass] = $targetStackPtr;
}
}
9 changes: 9 additions & 0 deletions tests/Error/ErrorCollector/SniffFileProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Symplify\EasyCodingStandard\Tests\Error\ErrorCollector;

use PHP_CodeSniffer\Standards\PSR12\Sniffs\Operators\OperatorSpacingSniff;
use Symplify\EasyCodingStandard\Caching\ChangedFilesDetector;
use Symplify\EasyCodingStandard\SniffRunner\Application\SniffFileProcessor;
use Symplify\EasyCodingStandard\SniffRunner\ValueObject\Error\CodingStandardError;
Expand Down Expand Up @@ -31,6 +32,14 @@ public function test(): void
$fileDiffs = $errorsAndFileDiffs['file_diffs'] ?? [];
$this->assertCount(1, $fileDiffs);

// Make sure the strict typing declaration isn't affected when it shouldn't be.
foreach ($fileDiffs[0]->getAppliedCheckers() as $appliedCheck) {
$this->assertDoesNotMatchRegularExpression(
sprintf('{%s(?:\..*)?}', preg_quote(OperatorSpacingSniff::class)),
$appliedCheck
);
}

/** @var CodingStandardError[] $codingStandardErrors */
$codingStandardErrors = $errorsAndFileDiffs['coding_standard_errors'] ?? [];
$this->assertCount(0, $codingStandardErrors);
Expand Down
Loading