From ac6bec07829a74d55ded29d63d686494cf9f7dfb Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 22 Oct 2023 08:09:12 +0200 Subject: [PATCH] Files/FileName: only clean up the OO prefixes when needed 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. --- Yoast/Sniffs/Files/FileNameSniff.php | 48 +++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 5 deletions(-) diff --git a/Yoast/Sniffs/Files/FileNameSniff.php b/Yoast/Sniffs/Files/FileNameSniff.php index a8337a0..63a84f4 100644 --- a/Yoast/Sniffs/Files/FileNameSniff.php +++ b/Yoast/Sniffs/Files/FileNameSniff.php @@ -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 + */ + private $previous_oo_prefixes = []; + + /** + * Validated & cleaned up OO set prefixes. + * + * @var array + */ + private $clean_oo_prefixes = []; + /** * Returns an array of tokens this test wants to listen for. * @@ -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, '_-' ); @@ -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 ) ); + } + } }