Skip to content

Commit

Permalink
Files/FileName: only clean up the OO prefixes when needed
Browse files Browse the repository at this point in the history
Efficiency tweak. Cleaning the ruleset passed OO prefixes doesn't need to be done time and again every single time this sniff is called.

For a normal PHPCS run, where the property is set in a ruleset, doing it once and reusing the cleaned up version in subsequent calls to the sniff will be sufficient.

For test runs, this may need to be done more often, but that can be handled by storing the previous value of `$oo_prefixes` property and checking if it has changed before doing the validation.

This commit implements this.
  • Loading branch information
jrfnl committed Nov 19, 2023
1 parent 7ef1578 commit ac6bec0
Showing 1 changed file with 43 additions and 5 deletions.
48 changes: 43 additions & 5 deletions Yoast/Sniffs/Files/FileNameSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,22 @@ final class FileNameSniff implements Sniff {
*/
public $excluded_files_strict_check = [];

/**
* Cache of previously set OO prefixes.
*
* Prevents having to do the same prefix validation over and over again.
*
* @var array<string>
*/
private $previous_oo_prefixes = [];

/**
* Validated & cleaned up OO set prefixes.
*
* @var array<string>
*/
private $clean_oo_prefixes = [];

/**
* Returns an array of tokens this test wants to listen for.
*
Expand Down Expand Up @@ -153,11 +169,9 @@ public function process( File $phpcsFile, $stackPtr ) {
$oo_name = ObjectDeclarations::getName( $phpcsFile, $oo_structure );

if ( ! empty( $oo_name ) ) {
$prefixes = $this->clean_custom_array_property( $this->oo_prefixes );
if ( ! empty( $prefixes ) ) {
// Use reverse natural sorting to get the longest of overlapping prefixes first.
\rsort( $prefixes, ( \SORT_NATURAL | \SORT_FLAG_CASE ) );
foreach ( $prefixes as $prefix ) {
$this->validate_oo_prefixes();
if ( ! empty( $this->clean_oo_prefixes ) ) {
foreach ( $this->clean_oo_prefixes as $prefix ) {
if ( $oo_name !== $prefix && \stripos( $oo_name, $prefix ) === 0 ) {
$oo_name = \substr( $oo_name, \strlen( $prefix ) );
$oo_name = \ltrim( $oo_name, '_-' );
Expand Down Expand Up @@ -285,4 +299,28 @@ private function clean_custom_array_property( $property ) {
private function normalize_directory_separators( $path ) {
return \ltrim( \strtr( $path, '\\', '/' ), '/' );
}

/**
* Validate and sort the OO prefixes passed from a custom ruleset.
*
* This will only need to be done once in a normal PHPCS run, though for
* tests the function may be called multiple times.
*
* @return void
*/
private function validate_oo_prefixes() {
if ( $this->previous_oo_prefixes === $this->oo_prefixes ) {
return;
}

// Set the cache *before* validation so as to not break the above compare.
$this->previous_oo_prefixes = $this->oo_prefixes;

$this->clean_oo_prefixes = $this->clean_custom_array_property( $this->oo_prefixes );

if ( ! empty( $this->clean_oo_prefixes ) ) {
// Use reverse natural sorting to get the longest of overlapping prefixes first.
\rsort( $this->clean_oo_prefixes, ( \SORT_NATURAL | \SORT_FLAG_CASE ) );
}
}
}

0 comments on commit ac6bec0

Please sign in to comment.