Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
<exclude name="PHPCompatibility.Constants.NewConstants.t_coalesceFound"/>
<exclude name="PHPCompatibility.Constants.NewConstants.t_coalesce_equalFound"/>
<exclude name="PHPCompatibility.Constants.NewConstants.t_yield_fromFound"/>
<exclude name="PHPCompatibility.Constants.NewConstants.t_readonlyFound"/>
</rule>

<!-- Enforce PSR1 compatible namespaces. -->
Expand Down
37 changes: 25 additions & 12 deletions WordPress/Sniffs/Security/EscapeOutputSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,22 +216,35 @@ public function process_token( $stackPtr ) {

case \T_THROW:
// Find the open parentheses, while stepping over the exception creation tokens.
$ignore = Tokens::$emptyTokens;
$ignore += Collections::namespacedNameTokens();
$ignore += Collections::functionCallTokens();
$ignore += Collections::objectOperators();

$next_relevant = $this->phpcsFile->findNext( $ignore, ( $stackPtr + 1 ), null, true );
if ( false === $next_relevant ) {
return;
}

if ( \T_NEW === $this->tokens[ $next_relevant ]['code'] ) {
$ignore = Tokens::$emptyTokens;
$ignore += Collections::namespacedNameTokens();
$ignore += Collections::functionCallTokens();
$ignore += Collections::objectOperators();
$ignore[ \T_READONLY ] = \T_READONLY;

$next_relevant = $stackPtr;
do {
$next_relevant = $this->phpcsFile->findNext( $ignore, ( $next_relevant + 1 ), null, true );
if ( false === $next_relevant ) {
return;
}
}

if ( \T_NEW === $this->tokens[ $next_relevant ]['code'] ) {
continue;
}

// Skip over attribute declarations when searching for the open parenthesis.
if ( \T_ATTRIBUTE === $this->tokens[ $next_relevant ]['code'] ) {
if ( isset( $this->tokens[ $next_relevant ]['attribute_closer'] ) === false ) {
return;
}

$next_relevant = $this->tokens[ $next_relevant ]['attribute_closer'];
continue;
}

break;
} while ( $next_relevant < ( $this->phpcsFile->numTokens - 1 ) );

if ( \T_OPEN_PARENTHESIS !== $this->tokens[ $next_relevant ]['code']
|| isset( $this->tokens[ $next_relevant ]['parenthesis_closer'] ) === false
Expand Down
14 changes: 14 additions & 0 deletions WordPress/Tests/Security/EscapeOutputUnitTest.1.inc
Original file line number Diff line number Diff line change
Expand Up @@ -662,3 +662,17 @@ die( status: esc_html( $foo ) ); // Ok.

exit( status: $foo ); // Bad.
die( status: $foo ); // Bad.

/*
* Issue https://github.com/WordPress/WordPress-Coding-Standards/issues/2552
* Ensure that readonly anonymous classes and anonymous classes with attributes are handled
* correctly when part of a throw statement.
*/
throw new #[MyAttribute] readonly class( esc_html( $message ) ) extends Exception {}; // Good.
throw new readonly class( $unescaped ) {}; // Bad.
throw new #[MyAttribute] class( $unescaped ) extends Exception {}; // Bad.
throw new
#[Attribute1]
/* some comment */
#[Attribute2('text', 10)]
readonly class( $unescaped ) {}; // Bad.
8 changes: 8 additions & 0 deletions WordPress/Tests/Security/EscapeOutputUnitTest.21.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

/*
* Intentional parse error (nothing after T_ATTRIBUTE).
* This should be the only test in this file.
*/

throw new #[
8 changes: 8 additions & 0 deletions WordPress/Tests/Security/EscapeOutputUnitTest.22.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

/*
* Intentional parse error (only whitespaces after T_ATTRIBUTE_END).
* This should be the only test in this file.
*/

throw new #[MyAttribute]
9 changes: 9 additions & 0 deletions WordPress/Tests/Security/EscapeOutputUnitTest.23.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

/*
* Intentional parse error (nothing after T_ATTRIBUTE_END).
* There should be no whitespaces at the end of this file.
* This should be the only test in this file.
*/

throw new #[MyAttribute]
3 changes: 3 additions & 0 deletions WordPress/Tests/Security/EscapeOutputUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ public function getErrorList( $testFile = '' ) {
657 => 1,
663 => 1,
664 => 1,
672 => 1,
673 => 1,
678 => 1,
);

case 'EscapeOutputUnitTest.6.inc':
Expand Down
Loading