From febac6fa6ea7f03eb311710add7b5566f6f04598 Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Mon, 8 Apr 2024 22:28:33 +0200 Subject: [PATCH] Remove duplicate code. --- .../FunctionCommentSinceTagSniff.php | 34 ++++++++++++------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/test/php/gutenberg-coding-standards/Gutenberg/Sniffs/Commenting/FunctionCommentSinceTagSniff.php b/test/php/gutenberg-coding-standards/Gutenberg/Sniffs/Commenting/FunctionCommentSinceTagSniff.php index 24634c3598ca0..622093ad6bc40 100644 --- a/test/php/gutenberg-coding-standards/Gutenberg/Sniffs/Commenting/FunctionCommentSinceTagSniff.php +++ b/test/php/gutenberg-coding-standards/Gutenberg/Sniffs/Commenting/FunctionCommentSinceTagSniff.php @@ -271,12 +271,8 @@ protected function process_property_token( File $phpcs_file, $stack_pointer ) { $violation_code = 'MissingPropertySinceTag'; - $scope_modifier = Variables::getMemberProperties( $phpcs_file, $stack_pointer )['scope']; - if ( ( $scope_modifier === 'protected' - && $this->minimumVisibility === 'public' ) - || ( $scope_modifier === 'private' - && ( $this->minimumVisibility === 'public' || $this->minimumVisibility === 'protected' ) ) - ) { + $visibility = Variables::getMemberProperties( $phpcs_file, $stack_pointer )['scope']; + if ( $this->check_below_minimum_visibility( $visibility ) ) { return; } @@ -335,12 +331,8 @@ protected function process_function_token( File $phpcs_file, $stack_pointer ) { $violation_code = 'MissingFunctionSinceTag'; if ( $is_oo_method ) { - $scope_modifier = FunctionDeclarations::getProperties( $phpcs_file, $stack_pointer )['scope']; - if ( ( $scope_modifier === 'protected' - && $this->minimumVisibility === 'public' ) - || ( $scope_modifier === 'private' - && ( $this->minimumVisibility === 'public' || $this->minimumVisibility === 'protected' ) ) - ) { + $visibility = FunctionDeclarations::getProperties( $phpcs_file, $stack_pointer )['scope']; + if ( $this->check_below_minimum_visibility( $visibility ) ) { return; } @@ -408,6 +400,24 @@ protected static function validate_version( $version ) { return version_compare( $version, '0.0.1', '>=' ); } + /** + * Checks if the provided visibility level is below the set minimum visibility level. + * + * @param string $visibility The visibility level to check. + * @return bool Returns true if the provided visibility level is below the minimum visibility level, false otherwise. + */ + protected function check_below_minimum_visibility( $visibility ) { + if ( 'public' === $this->minimumVisibility && in_array( $visibility, array( 'protected', 'private' ), true ) ) { + return true; + } + + if ( 'protected' === $this->minimumVisibility && 'private' === $visibility ) { + return true; + } + + return false; + } + /** * Finds the docblock associated with a hook, starting from a specified position in the token stack. * Since a line containing a hook can include any type of tokens, this method backtracks through the tokens