Skip to content

Commit

Permalink
Files/FileName: implement use of new PathHelper and PathValidationHel…
Browse files Browse the repository at this point in the history
…per classes
  • Loading branch information
jrfnl committed Nov 19, 2023
1 parent 2677da6 commit b94cf92
Showing 1 changed file with 8 additions and 44 deletions.
52 changes: 8 additions & 44 deletions Yoast/Sniffs/Files/FileNameSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use PHPCSUtils\Tokens\Collections;
use PHPCSUtils\Utils\ObjectDeclarations;
use PHPCSUtils\Utils\TextStrings;
use YoastCS\Yoast\Utils\PathHelper;
use YoastCS\Yoast\Utils\PathValidationHelper;

/**
* Ensures files comply with the Yoast file name rules.
Expand Down Expand Up @@ -100,7 +102,7 @@ final class FileNameSniff implements Sniff {
/**
* Validated & cleaned up list of absolute paths to the excluded files.
*
* @var array<string, int> Key is the path, value irrelevant.
* @var array<string, string> Both the key and the value will be the same absolute path.
*/
private $validated_excluded_files = [];

Expand Down Expand Up @@ -281,8 +283,7 @@ private function is_file_excluded( File $phpcsFile, $path_to_file ) {
return false;
}

$path_to_file = $this->normalize_directory_separators( $path_to_file );
$path_to_file = \ltrim( $path_to_file, '/' );
$path_to_file = PathHelper::normalize_path( $path_to_file );

return isset( $this->validated_excluded_files[ $path_to_file ] );
}
Expand All @@ -302,17 +303,6 @@ private function clean_custom_array_property( $property ) {
return \array_filter( \array_map( 'trim', $property ) );
}

/**
* Normalize all directory separators to be a forward slash and remove prefixed slash.
*
* @param string $path Path to normalize.
*
* @return string
*/
private function normalize_directory_separators( $path ) {
return \ltrim( \strtr( $path, '\\', '/' ), '/' );
}

/**
* Validate and sort the OO prefixes passed from a custom ruleset.
*
Expand Down Expand Up @@ -364,37 +354,11 @@ private function validate_excluded_files( $phpcsFile ) {
// Set the cache *before* validation so as to not break the above compare.
$this->previous_excluded_files = $this->excluded_files_strict_check;

// Reset a potentially previous set validated value.
$this->validated_excluded_files = [];

$exclude = $this->clean_custom_array_property( $this->excluded_files_strict_check );
if ( empty( $exclude ) ) {
return;
}
$absolute_paths = PathValidationHelper::relative_to_absolute( $phpcsFile, $this->excluded_files_strict_check );
$absolute_paths = \array_unique( $absolute_paths );
$absolute_paths = \array_values( $absolute_paths );

$base_path = $this->normalize_directory_separators( $phpcsFile->config->basepath );
$exclude = \array_map( [ $this, 'normalize_directory_separators' ], $exclude );

foreach ( $exclude as $relative ) {
if ( \strpos( $relative, '..' ) !== false ) {
// Ignore paths containing path walking.
continue;
}

if ( \strpos( $relative, './' ) === 0 ) {
$relative = \substr( $relative, 2 );
}

/*
* Note: no need to check if the file really exists. We'll be doing a literal absolute path comparison,
* so if the file doesn't exist, it will never match.
*/
$this->validated_excluded_files[] = $base_path . '/' . $relative;
}

if ( ! empty( $this->validated_excluded_files ) ) {
$this->validated_excluded_files = \array_flip( $this->validated_excluded_files );
}
$this->validated_excluded_files = \array_combine( $absolute_paths, $absolute_paths );
}

/**
Expand Down

0 comments on commit b94cf92

Please sign in to comment.