Skip to content

Commit 6b716a5

Browse files
authored
Merge pull request #2563 from WordPress/feature/use-phpcsutils-1.1.0
Use PHPCSUtils 1.1.0 features
2 parents d2421de + 59b7919 commit 6b716a5

File tree

8 files changed

+29
-21
lines changed

8 files changed

+29
-21
lines changed

WordPress/Helpers/ListHelper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
namespace WordPressCS\WordPress\Helpers;
1111

12-
use PHP_CodeSniffer\Exceptions\RuntimeException;
1312
use PHP_CodeSniffer\Files\File;
13+
use PHPCSUtils\Exceptions\UnexpectedTokenType;
1414
use PHPCSUtils\Tokens\Collections;
1515
use PHPCSUtils\Utils\Lists;
1616

@@ -67,7 +67,7 @@ public static function get_list_variables( File $phpcsFile, $stackPtr ) {
6767

6868
try {
6969
$assignments = Lists::getAssignments( $phpcsFile, $stackPtr );
70-
} catch ( RuntimeException $e ) {
70+
} catch ( UnexpectedTokenType $e ) {
7171
// Parse error/live coding.
7272
return array();
7373
}

WordPress/Sniffs/Files/FileNameSniff.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
namespace WordPressCS\WordPress\Sniffs\Files;
1111

1212
use PHPCSUtils\Tokens\Collections;
13+
use PHPCSUtils\Utils\FilePath;
1314
use PHPCSUtils\Utils\ObjectDeclarations;
14-
use PHPCSUtils\Utils\TextStrings;
1515
use WordPressCS\WordPress\Helpers\IsUnitTestTrait;
1616
use WordPressCS\WordPress\Sniff;
1717

@@ -151,8 +151,7 @@ public function register() {
151151
* normal file processing.
152152
*/
153153
public function process_token( $stackPtr ) {
154-
// Usage of `stripQuotes` is to ensure `stdin_path` passed by IDEs does not include quotes.
155-
$file = TextStrings::stripQuotes( $this->phpcsFile->getFileName() );
154+
$file = FilePath::getName( $this->phpcsFile );
156155
if ( 'STDIN' === $file ) {
157156
return $this->phpcsFile->numTokens;
158157
}
@@ -197,7 +196,7 @@ public function process_token( $stackPtr ) {
197196
$this->check_filename_has_class_prefix( $class_ptr, $file_name );
198197
}
199198

200-
if ( false !== strpos( $file, \DIRECTORY_SEPARATOR . 'wp-includes' . \DIRECTORY_SEPARATOR )
199+
if ( false !== strpos( $file, '/wp-includes/' )
201200
&& false === $class_ptr
202201
) {
203202
$this->check_filename_for_template_suffix( $stackPtr, $file_name );

WordPress/Sniffs/PHP/YodaConditionsSniff.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,9 @@ public function register() {
4343

4444
$starters = Tokens::$booleanOperators;
4545
$starters += Tokens::$assignmentTokens;
46+
$starters += Collections::ternaryOperators();
4647
$starters[ \T_CASE ] = \T_CASE;
4748
$starters[ \T_RETURN ] = \T_RETURN;
48-
$starters[ \T_INLINE_THEN ] = \T_INLINE_THEN;
49-
$starters[ \T_INLINE_ELSE ] = \T_INLINE_ELSE;
5049
$starters[ \T_SEMICOLON ] = \T_SEMICOLON;
5150
$starters[ \T_OPEN_PARENTHESIS ] = \T_OPEN_PARENTHESIS;
5251

WordPress/Sniffs/Security/EscapeOutputSniff.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -200,19 +200,19 @@ public function process_token( $stackPtr ) {
200200
return parent::process_token( $stackPtr );
201201

202202
case \T_EXIT:
203-
$next_non_empty = $this->phpcsFile->findNext( Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true );
204-
if ( false === $next_non_empty
205-
|| \T_OPEN_PARENTHESIS !== $this->tokens[ $next_non_empty ]['code']
206-
|| isset( $this->tokens[ $next_non_empty ]['parenthesis_closer'] ) === false
207-
) {
208-
// Live coding/parse error or an exit/die which doesn't pass a status code. Ignore.
203+
$params = PassedParameters::getParameters( $this->phpcsFile, $stackPtr );
204+
if ( empty( $params ) ) {
205+
// Live coding/parse error or an exit/die which doesn't pass a status. Ignore.
209206
return;
210207
}
211208

212-
// $end is not examined, so make sure the parentheses are balanced.
213-
$start = $next_non_empty;
214-
$end = ( $this->tokens[ $next_non_empty ]['parenthesis_closer'] + 1 );
215-
break;
209+
// There should only be one parameter ($status), but just to be on the safe side.
210+
foreach ( $params as $param ) {
211+
$this->check_code_is_escaped( $param['start'], ( $param['end'] + 1 ) );
212+
}
213+
214+
// Skip to the end of the last found parameter.
215+
return ( $param['end'] + 1 );
216216

217217
case \T_THROW:
218218
// Find the open parentheses, while stepping over the exception creation tokens.

WordPress/Sniffs/Utils/I18nTextDomainFixerSniff.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use PHP_CodeSniffer\Util\Tokens;
1313
use PHPCSUtils\BackCompat\Helper;
14+
use PHPCSUtils\Utils\FilePath;
1415
use PHPCSUtils\Utils\GetTokensAsString;
1516
use PHPCSUtils\Utils\PassedParameters;
1617
use PHPCSUtils\Utils\TextStrings;
@@ -675,7 +676,7 @@ public function process_comments( $stackPtr ) {
675676
$headers = $this->plugin_headers;
676677
$type = 'plugin';
677678

678-
$file = TextStrings::stripQuotes( $this->phpcsFile->getFileName() );
679+
$file = FilePath::getName( $this->phpcsFile );
679680
if ( 'STDIN' === $file ) {
680681
return;
681682
}

WordPress/Sniffs/WP/EnqueuedResourcesSniff.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
namespace WordPressCS\WordPress\Sniffs\WP;
1111

12-
use PHP_CodeSniffer\Exceptions\RuntimeException;
1312
use PHP_CodeSniffer\Util\Tokens;
13+
use PHPCSUtils\Exceptions\ValueError;
1414
use PHPCSUtils\Tokens\Collections;
1515
use PHPCSUtils\Utils\TextStrings;
1616
use WordPressCS\WordPress\Sniff;
@@ -54,7 +54,7 @@ public function process_token( $stackPtr ) {
5454
try {
5555
$end_ptr = TextStrings::getEndOfCompleteTextString( $this->phpcsFile, $stackPtr );
5656
$content = TextStrings::getCompleteTextString( $this->phpcsFile, $stackPtr );
57-
} catch ( RuntimeException $e ) {
57+
} catch ( ValueError $e ) {
5858
// Parse error/live coding.
5959
return;
6060
}

WordPress/Tests/Security/EscapeOutputUnitTest.1.inc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,3 +655,10 @@ echo '<input type="search" value="' . get_search_query( false ) . '">'; // Bad.
655655
echo '<input type="search" value="' . get_search_query( 0 ) . '">'; // Bad.
656656
echo '<input type="search" value="' . get_search_query( escape: false ) . '">'; // OK, well not really, typo in param name, but that's not our concern.
657657
echo '<input type="search" value="' . get_search_query( escaped: false ) . '">'; // Bad.
658+
659+
// PHP 8.4: exit/die using named parameters.
660+
exit( status: esc_html( $foo ) ); // Ok.
661+
die( status: esc_html( $foo ) ); // Ok.
662+
663+
exit( status: $foo ); // Bad.
664+
die( status: $foo ); // Bad.

WordPress/Tests/Security/EscapeOutputUnitTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ public function getErrorList( $testFile = '' ) {
159159
654 => 1,
160160
655 => 1,
161161
657 => 1,
162+
663 => 1,
163+
664 => 1,
162164
);
163165

164166
case 'EscapeOutputUnitTest.6.inc':

0 commit comments

Comments
 (0)