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
24 changes: 24 additions & 0 deletions WordPress/Sniffs/WP/EnqueuedResourceParametersSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,30 @@ protected function is_falsy( $start, $end ) {
continue;
}

// Make sure that when deprecated casts are used in the code under scan and the sniff is run on PHP 8.5,
// the eval() won't cause a deprecation notice, borking the scan of the file.
if ( \PHP_VERSION_ID >= 80500 ) {
if ( \T_INT_CAST === $this->tokens[ $i ]['code'] ) {
$code_string .= '(int)';
continue;
}

if ( \T_DOUBLE_CAST === $this->tokens[ $i ]['code'] ) {
$code_string .= '(float)';
continue;
}

if ( \T_BOOL_CAST === $this->tokens[ $i ]['code'] ) {
$code_string .= '(bool)';
continue;
}

if ( \T_BINARY_CAST === $this->tokens[ $i ]['code'] ) {
$code_string .= '(string)';
continue;
}
}

$code_string .= $this->tokens[ $i ]['content'];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,14 @@ wp_register_script( 'someScript-js', $url, [], 0_0.0_0, true ); // Error - 0, fa
// Safeguard handling of PHP 8.1 explicit octals.
wp_register_script( 'someScript-js', $url, [], 0o0, true ); // Error - 0, false or NULL are not allowed.

// Live coding/parse error.
wp_register_style( src: 'https://example.com/someScript.js', ver: /*to do*/, handle: 'someScript-js', );
// Safeguard against PHP 8.5 deprecation of non-standard cast names.
wp_register_script( 'someScript-js', 'https://example.com/someScript.js' , array( 'jquery' ), (boolean) 1, true ); // OK.
wp_register_script( 'someScript-js', 'https://example.com/someScript.js' , array( 'jquery' ), (boolean) 0, true ); // Error - 0, false or NULL are not allowed.

wp_register_script( 'someScript-js', 'https://example.com/someScript.js' , array( 'jquery' ), (integer) 1, true ); // OK.
wp_register_script( 'someScript-js', 'https://example.com/someScript.js' , array( 'jquery' ), (integer) 0, true ); // Error - 0, false or NULL are not allowed.

wp_register_script( 'someScript-js', 'https://example.com/someScript.js' , array( 'jquery' ), (double) 1, true ); // OK.
wp_register_script( 'someScript-js', 'https://example.com/someScript.js' , array( 'jquery' ), (double) 0, true ); // Error - 0, false or NULL are not allowed.

wp_register_script( 'someScript-js', 'https://example.com/someScript.js' , array( 'jquery' ), (binary) 0, true ); // Error - 0, false or NULL are not allowed.
5 changes: 5 additions & 0 deletions WordPress/Tests/WP/EnqueuedResourceParametersUnitTest.2.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

// Live coding/parse error.
// This should be the only test in the file.
wp_register_style( src: 'https://example.com/someScript.js', ver: /*to do*/, handle: 'someScript-js', );
78 changes: 51 additions & 27 deletions WordPress/Tests/WP/EnqueuedResourceParametersUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,41 +23,65 @@ final class EnqueuedResourceParametersUnitTest extends AbstractSniffUnitTest {
/**
* Returns the lines where errors should occur.
*
* @param string $testFile The name of the file being tested.
*
* @return array<int, int> Key is the line number, value is the number of expected errors.
*/
public function getErrorList() {
return array(
6 => 1,
9 => 1,
10 => 1,
12 => 1,
13 => 1,
14 => 1,
22 => 1,
54 => 1,
57 => 1,
61 => 1,
82 => 1,
85 => 1,
88 => 1,
);
public function getErrorList( $testFile = '' ) {
switch ( $testFile ) {
case 'EnqueuedResourceParametersUnitTest.1.inc':
return array(
6 => 1,
9 => 1,
10 => 1,
12 => 1,
13 => 1,
14 => 1,
22 => 1,
54 => 1,
57 => 1,
61 => 1,
82 => 1,
85 => 1,
89 => 1,
92 => 1,
95 => 1,
97 => 1,
);

case 'EnqueuedResourceParametersUnitTest.2.inc':
return array(
5 => 1,
);

default:
return array();
}
}

/**
* Returns the lines where warnings should occur.
*
* @param string $testFile The name of the file being tested.
*
* @return array<int, int> Key is the line number, value is the number of expected warnings.
*/
public function getWarningList() {
return array(
3 => 2,
11 => 1,
32 => 1,
39 => 2,
42 => 1,
45 => 1,
66 => 2,
77 => 1,
);
public function getWarningList( $testFile = '' ) {
switch ( $testFile ) {
case 'EnqueuedResourceParametersUnitTest.1.inc':
return array(
3 => 2,
11 => 1,
32 => 1,
39 => 2,
42 => 1,
45 => 1,
66 => 2,
77 => 1,
);

default:
return array();
}
}
}
Loading