Skip to content

Commit e95a98f

Browse files
committed
WP/AlternativeFunctions: fix handling of FQN references to global stream constants
This commit fixes the handling of fully qualified name (FQN) references to the global PHP stream constants `\STDIN`, `\STDOUT`, and `\STDERR` by normalizing the passed parameter before checking it against the allowed list. Tests have been added to cover all namespace forms of references to the global PHP stream constants.
1 parent 15c8cb7 commit e95a98f

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

WordPress/Sniffs/WP/AlternativeFunctionsSniff.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,9 @@ public function process_matched_token( $stackPtr, $group_name, $matched_content
353353
*/
354354
protected function is_local_data_stream( $clean_param_value ) {
355355

356-
$stripped = TextStrings::stripQuotes( $clean_param_value );
356+
$stripped = TextStrings::stripQuotes( $clean_param_value );
357+
$clean_param_value = ltrim( $clean_param_value, '\\' );
358+
357359
if ( isset( $this->allowed_local_streams[ $stripped ] )
358360
|| isset( $this->allowed_local_stream_constants[ $clean_param_value ] )
359361
) {

WordPress/Tests/WP/AlternativeFunctionsUnitTest.inc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,16 @@ file_get_contents( $this?->plugin_dir_path() . 'subdir/file.inc' );
167167
file_get_contents( MyClass::ABSPATH . 'subdir/file.inc' );
168168
file_get_contents( $this->WPMU_PLUGIN_DIR . 'subdir/file.inc' );
169169
file_get_contents( $this?->TEMPLATEPATH . 'subdir/file.inc' );
170+
171+
/*
172+
* Safeguard correct handling of namespaced variants of STDIN/STDOUT/STDERR constants.
173+
*
174+
* Note: passing stream resources to these functions is not valid PHP and will be addressed in
175+
* https://github.com/WordPress/WordPress-Coding-Standards/issues/2602. These tests document the current behavior of the
176+
* sniff.
177+
*/
178+
fopen( \STDIN, 'r' );
179+
file_put_contents( MyNamespace\STDOUT, $data );
180+
file_get_contents( \MyNamespace\STDIN );
181+
file_put_contents( namespace\STDERR, $data ); // The sniff should not flag this once it can resolve relative namespaces.
182+
readfile( namespace\Sub\STDIN );

WordPress/Tests/WP/AlternativeFunctionsUnitTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ public function getWarningList() {
9696
167 => 1,
9797
168 => 1,
9898
169 => 1,
99+
179 => 1,
100+
180 => 1,
101+
181 => 1,
102+
182 => 1,
99103
);
100104
}
101105
}

0 commit comments

Comments
 (0)