diff --git a/WordPress/Helpers/ListHelper.php b/WordPress/Helpers/ListHelper.php index 99373b2aec..656757346a 100644 --- a/WordPress/Helpers/ListHelper.php +++ b/WordPress/Helpers/ListHelper.php @@ -9,8 +9,8 @@ namespace WordPressCS\WordPress\Helpers; -use PHP_CodeSniffer\Exceptions\RuntimeException; use PHP_CodeSniffer\Files\File; +use PHPCSUtils\Exceptions\UnexpectedTokenType; use PHPCSUtils\Tokens\Collections; use PHPCSUtils\Utils\Lists; @@ -67,7 +67,7 @@ public static function get_list_variables( File $phpcsFile, $stackPtr ) { try { $assignments = Lists::getAssignments( $phpcsFile, $stackPtr ); - } catch ( RuntimeException $e ) { + } catch ( UnexpectedTokenType $e ) { // Parse error/live coding. return array(); } diff --git a/WordPress/Sniffs/Files/FileNameSniff.php b/WordPress/Sniffs/Files/FileNameSniff.php index 72c3e3d44d..2cdbd96d74 100644 --- a/WordPress/Sniffs/Files/FileNameSniff.php +++ b/WordPress/Sniffs/Files/FileNameSniff.php @@ -10,8 +10,8 @@ namespace WordPressCS\WordPress\Sniffs\Files; use PHPCSUtils\Tokens\Collections; +use PHPCSUtils\Utils\FilePath; use PHPCSUtils\Utils\ObjectDeclarations; -use PHPCSUtils\Utils\TextStrings; use WordPressCS\WordPress\Helpers\IsUnitTestTrait; use WordPressCS\WordPress\Sniff; @@ -151,8 +151,7 @@ public function register() { * normal file processing. */ public function process_token( $stackPtr ) { - // Usage of `stripQuotes` is to ensure `stdin_path` passed by IDEs does not include quotes. - $file = TextStrings::stripQuotes( $this->phpcsFile->getFileName() ); + $file = FilePath::getName( $this->phpcsFile ); if ( 'STDIN' === $file ) { return $this->phpcsFile->numTokens; } @@ -197,7 +196,7 @@ public function process_token( $stackPtr ) { $this->check_filename_has_class_prefix( $class_ptr, $file_name ); } - if ( false !== strpos( $file, \DIRECTORY_SEPARATOR . 'wp-includes' . \DIRECTORY_SEPARATOR ) + if ( false !== strpos( $file, '/wp-includes/' ) && false === $class_ptr ) { $this->check_filename_for_template_suffix( $stackPtr, $file_name ); diff --git a/WordPress/Sniffs/PHP/YodaConditionsSniff.php b/WordPress/Sniffs/PHP/YodaConditionsSniff.php index 150993c6a4..cff816119f 100644 --- a/WordPress/Sniffs/PHP/YodaConditionsSniff.php +++ b/WordPress/Sniffs/PHP/YodaConditionsSniff.php @@ -43,10 +43,9 @@ public function register() { $starters = Tokens::$booleanOperators; $starters += Tokens::$assignmentTokens; + $starters += Collections::ternaryOperators(); $starters[ \T_CASE ] = \T_CASE; $starters[ \T_RETURN ] = \T_RETURN; - $starters[ \T_INLINE_THEN ] = \T_INLINE_THEN; - $starters[ \T_INLINE_ELSE ] = \T_INLINE_ELSE; $starters[ \T_SEMICOLON ] = \T_SEMICOLON; $starters[ \T_OPEN_PARENTHESIS ] = \T_OPEN_PARENTHESIS; diff --git a/WordPress/Sniffs/Security/EscapeOutputSniff.php b/WordPress/Sniffs/Security/EscapeOutputSniff.php index 7861940caa..5d5a8272a5 100644 --- a/WordPress/Sniffs/Security/EscapeOutputSniff.php +++ b/WordPress/Sniffs/Security/EscapeOutputSniff.php @@ -200,19 +200,19 @@ public function process_token( $stackPtr ) { return parent::process_token( $stackPtr ); case \T_EXIT: - $next_non_empty = $this->phpcsFile->findNext( Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true ); - if ( false === $next_non_empty - || \T_OPEN_PARENTHESIS !== $this->tokens[ $next_non_empty ]['code'] - || isset( $this->tokens[ $next_non_empty ]['parenthesis_closer'] ) === false - ) { - // Live coding/parse error or an exit/die which doesn't pass a status code. Ignore. + $params = PassedParameters::getParameters( $this->phpcsFile, $stackPtr ); + if ( empty( $params ) ) { + // Live coding/parse error or an exit/die which doesn't pass a status. Ignore. return; } - // $end is not examined, so make sure the parentheses are balanced. - $start = $next_non_empty; - $end = ( $this->tokens[ $next_non_empty ]['parenthesis_closer'] + 1 ); - break; + // There should only be one parameter ($status), but just to be on the safe side. + foreach ( $params as $param ) { + $this->check_code_is_escaped( $param['start'], ( $param['end'] + 1 ) ); + } + + // Skip to the end of the last found parameter. + return ( $param['end'] + 1 ); case \T_THROW: // Find the open parentheses, while stepping over the exception creation tokens. diff --git a/WordPress/Sniffs/Utils/I18nTextDomainFixerSniff.php b/WordPress/Sniffs/Utils/I18nTextDomainFixerSniff.php index af176326a6..1d85ea0e1d 100644 --- a/WordPress/Sniffs/Utils/I18nTextDomainFixerSniff.php +++ b/WordPress/Sniffs/Utils/I18nTextDomainFixerSniff.php @@ -11,6 +11,7 @@ use PHP_CodeSniffer\Util\Tokens; use PHPCSUtils\BackCompat\Helper; +use PHPCSUtils\Utils\FilePath; use PHPCSUtils\Utils\GetTokensAsString; use PHPCSUtils\Utils\PassedParameters; use PHPCSUtils\Utils\TextStrings; @@ -675,7 +676,7 @@ public function process_comments( $stackPtr ) { $headers = $this->plugin_headers; $type = 'plugin'; - $file = TextStrings::stripQuotes( $this->phpcsFile->getFileName() ); + $file = FilePath::getName( $this->phpcsFile ); if ( 'STDIN' === $file ) { return; } diff --git a/WordPress/Sniffs/WP/EnqueuedResourcesSniff.php b/WordPress/Sniffs/WP/EnqueuedResourcesSniff.php index c7ed63c303..bc7e9c94fa 100644 --- a/WordPress/Sniffs/WP/EnqueuedResourcesSniff.php +++ b/WordPress/Sniffs/WP/EnqueuedResourcesSniff.php @@ -9,8 +9,8 @@ namespace WordPressCS\WordPress\Sniffs\WP; -use PHP_CodeSniffer\Exceptions\RuntimeException; use PHP_CodeSniffer\Util\Tokens; +use PHPCSUtils\Exceptions\ValueError; use PHPCSUtils\Tokens\Collections; use PHPCSUtils\Utils\TextStrings; use WordPressCS\WordPress\Sniff; @@ -54,7 +54,7 @@ public function process_token( $stackPtr ) { try { $end_ptr = TextStrings::getEndOfCompleteTextString( $this->phpcsFile, $stackPtr ); $content = TextStrings::getCompleteTextString( $this->phpcsFile, $stackPtr ); - } catch ( RuntimeException $e ) { + } catch ( ValueError $e ) { // Parse error/live coding. return; } diff --git a/WordPress/Tests/Security/EscapeOutputUnitTest.1.inc b/WordPress/Tests/Security/EscapeOutputUnitTest.1.inc index 5309a4693b..eaf96f5ba3 100644 --- a/WordPress/Tests/Security/EscapeOutputUnitTest.1.inc +++ b/WordPress/Tests/Security/EscapeOutputUnitTest.1.inc @@ -655,3 +655,10 @@ echo ''; // Bad. echo ''; // Bad. echo ''; // OK, well not really, typo in param name, but that's not our concern. echo ''; // Bad. + +// PHP 8.4: exit/die using named parameters. +exit( status: esc_html( $foo ) ); // Ok. +die( status: esc_html( $foo ) ); // Ok. + +exit( status: $foo ); // Bad. +die( status: $foo ); // Bad. diff --git a/WordPress/Tests/Security/EscapeOutputUnitTest.php b/WordPress/Tests/Security/EscapeOutputUnitTest.php index 6150a4eeb4..2f70eebaeb 100644 --- a/WordPress/Tests/Security/EscapeOutputUnitTest.php +++ b/WordPress/Tests/Security/EscapeOutputUnitTest.php @@ -159,6 +159,8 @@ public function getErrorList( $testFile = '' ) { 654 => 1, 655 => 1, 657 => 1, + 663 => 1, + 664 => 1, ); case 'EscapeOutputUnitTest.6.inc':