Skip to content

Commit

Permalink
Files/FileName: only throw the "missing basepath" warning once
Browse files Browse the repository at this point in the history
No need to throw this for every single file. Throwing it once should be sufficient.

Includes moving the check for the missing basepath to earlier in the process flow.
  • Loading branch information
jrfnl committed Nov 19, 2023
1 parent a0e1125 commit 2677da6
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions Yoast/Sniffs/Files/FileNameSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ final class FileNameSniff implements Sniff {
*/
private $validated_excluded_files = [];

/**
* Track if the "missing basepath" warning has been thrown.
*
* This prevents this warning potentially being thrown for every single file in a PHPCS run.
*
* @var bool
*/
private $basepath_warning_thrown = false;

/**
* Returns an array of tokens this test wants to listen for.
*
Expand Down Expand Up @@ -177,6 +186,10 @@ public function process( File $phpcsFile, $stackPtr ) {
$error_code = 'NotHyphenatedLowercase';
$expected = \strtolower( \preg_replace( '`[[:punct:]]`', '-', $file_name ) );

if ( ! isset( $phpcsFile->config->basepath ) ) {
$this->add_missing_basepath_warning( $phpcsFile );
}

if ( $this->is_file_excluded( $phpcsFile, $file ) === false ) {
$oo_structure = $phpcsFile->findNext( self::NAMED_OO_TOKENS, $stackPtr );
if ( $oo_structure !== false ) {
Expand Down Expand Up @@ -337,12 +350,6 @@ private function validate_oo_prefixes() {
private function validate_excluded_files( $phpcsFile ) {
// The basepath check needs to be done first as otherwise the previous/current comparison would be broken.
if ( ! isset( $phpcsFile->config->basepath ) ) {
$phpcsFile->addWarning(
'For the exclude property to work with relative file path files, the --basepath needs to be set.',
0,
'MissingBasePath'
);

// Only relevant for the tests: make sure previously set validated paths are cleared out.
$this->validated_excluded_files = [];

Expand Down Expand Up @@ -389,4 +396,25 @@ private function validate_excluded_files( $phpcsFile ) {
$this->validated_excluded_files = \array_flip( $this->validated_excluded_files );
}
}

/**
* Throw a warning if the basepath is missing (and this warning hasn't been thrown before).
*
* @param File $phpcsFile The file being scanned.
*
* @return void
*/
private function add_missing_basepath_warning( File $phpcsFile ) {
if ( $this->basepath_warning_thrown === true ) {
return;
}

$phpcsFile->addWarning(
'For the exclude property to work with relative file path files, the --basepath needs to be set.',
0,
'MissingBasePath'
);

$this->basepath_warning_thrown = true;
}
}

0 comments on commit 2677da6

Please sign in to comment.