From 9e94d3b810fb20736b265c4390dcd1ad6e4ac4e9 Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Thu, 17 Jul 2025 11:00:18 -0300 Subject: [PATCH 1/5] NamingConventions/PrefixAllGlobals: add tests using asymmetric visibility properties This commit adds a few tests using asymmetric visibility properties (including in constructor property promotion) to the `WordPress.NamingConventions.PrefixAllGlobals` tests. This is just to ensure that the part of the sniff code that ignores properties or method parameters (in the case of constructor property promotion) continues to handle PHP 8.4+ asymmetric visibility properties correctly. No change to the sniff code is needed. --- .../NamingConventions/PrefixAllGlobalsUnitTest.1.inc | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/WordPress/Tests/NamingConventions/PrefixAllGlobalsUnitTest.1.inc b/WordPress/Tests/NamingConventions/PrefixAllGlobalsUnitTest.1.inc index cdbe22dbcb..7fff84a00f 100644 --- a/WordPress/Tests/NamingConventions/PrefixAllGlobalsUnitTest.1.inc +++ b/WordPress/Tests/NamingConventions/PrefixAllGlobalsUnitTest.1.inc @@ -673,4 +673,14 @@ class WP_Atom_Server { } } +/* + * Safeguard that PHP 8.4+ asymmetric visibility properties don't lead to false positives. + * Including those defined using constructor property promotion. + */ +class Acronym_AsymmetricVisibilityProperties { + public private(set) string $bar = 'bar'; // Ok. + + public function __construct(public protected(set) int $foo = 0) {} // Ok. +} + // phpcs:set WordPress.NamingConventions.PrefixAllGlobals prefixes[] From fb50666da90d89fc156a5de06d410f22cdc408e4 Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Thu, 17 Jul 2025 11:27:40 -0300 Subject: [PATCH 2/5] NamingConventions/ValidVariableName: add tests using asymmetric visibility properties This commit adds a few tests using asymmetric visibility properties (including in constructor property promotion) to the `WordPress.NamingConventions.ValidVariableName` tests. This is just to ensure that the sniff continues to apply its variable name rules when dealing with asymmetric visibility properties added in PHP 8.4. The sniff was already handling asymmetric visibility properties correctly, and no change to the sniff code is needed. --- .../Tests/NamingConventions/ValidVariableNameUnitTest.inc | 8 ++++++++ .../Tests/NamingConventions/ValidVariableNameUnitTest.php | 1 + 2 files changed, 9 insertions(+) diff --git a/WordPress/Tests/NamingConventions/ValidVariableNameUnitTest.inc b/WordPress/Tests/NamingConventions/ValidVariableNameUnitTest.inc index a8cac33a54..ec7c009cef 100644 --- a/WordPress/Tests/NamingConventions/ValidVariableNameUnitTest.inc +++ b/WordPress/Tests/NamingConventions/ValidVariableNameUnitTest.inc @@ -237,3 +237,11 @@ class Has_Mixed_Case_Property { $lähtöaika = true; // OK. $lÄhtÖaika = true; // Bad, but only handled by the sniff if Mbstring is available. $lÄhtOaika = true; // Bad, handled via transliteration of non-ASCII chars if Mbstring is not available. + +/* + * Safeguard that the sniff handles PHP 8.4+ asymmetric visibility properties correctly. + */ +class Acronym_AsymmetricVisibilityProperties { + public private(set) string $valid_name = 'bar'; // Ok. + public(set) string $invalidName = 'bar'; // Bad. +} diff --git a/WordPress/Tests/NamingConventions/ValidVariableNameUnitTest.php b/WordPress/Tests/NamingConventions/ValidVariableNameUnitTest.php index 68b2b64a5c..0ac884c991 100644 --- a/WordPress/Tests/NamingConventions/ValidVariableNameUnitTest.php +++ b/WordPress/Tests/NamingConventions/ValidVariableNameUnitTest.php @@ -98,6 +98,7 @@ public function getErrorList() { 227 => 1, 238 => function_exists( 'mb_strtolower' ) ? 1 : 0, 239 => 1, + 246 => 1, ); } From 88d311f98159af6fde422f16e250e7c2b8e8b7fc Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Thu, 17 Jul 2025 15:05:24 -0300 Subject: [PATCH 3/5] Security/NonceVerification: add test using asymmetric visibility properties This commit adds a test using asymmetric visibility properties to the `WordPress.Security.NonceVerification` tests. This is just to ensure that the sniff continues to ignore PHP 8.4+ asymmetric visibility properties. The sniff was already handling asymmetric visibility properties correctly, and no change to the sniff code is needed. --- WordPress/Tests/Security/NonceVerificationUnitTest.8.inc | 1 + 1 file changed, 1 insertion(+) diff --git a/WordPress/Tests/Security/NonceVerificationUnitTest.8.inc b/WordPress/Tests/Security/NonceVerificationUnitTest.8.inc index c2c134ddb7..7d3a70d8b6 100644 --- a/WordPress/Tests/Security/NonceVerificationUnitTest.8.inc +++ b/WordPress/Tests/Security/NonceVerificationUnitTest.8.inc @@ -3,4 +3,5 @@ class IgnoreProperties { public $_GET = array( 'key' => 'something' ); // OK. public $_POST; // OK. + public private(set) string $_REQUEST; // Ok. } From f5ebe184f3add29e3d212024d2634a3ef2df37ba Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Thu, 17 Jul 2025 15:30:22 -0300 Subject: [PATCH 4/5] WP/GlobalVariablesOverride: move parse error test to its own file Also update code comment related to the moved parse error test to include one more case where the code might bow out. --- WordPress/Sniffs/WP/GlobalVariablesOverrideSniff.php | 2 +- WordPress/Tests/WP/GlobalVariablesOverrideUnitTest.1.inc | 4 ---- WordPress/Tests/WP/GlobalVariablesOverrideUnitTest.8.inc | 8 ++++++++ 3 files changed, 9 insertions(+), 5 deletions(-) create mode 100644 WordPress/Tests/WP/GlobalVariablesOverrideUnitTest.8.inc diff --git a/WordPress/Sniffs/WP/GlobalVariablesOverrideSniff.php b/WordPress/Sniffs/WP/GlobalVariablesOverrideSniff.php index ab2aa8bd91..934c3e686b 100644 --- a/WordPress/Sniffs/WP/GlobalVariablesOverrideSniff.php +++ b/WordPress/Sniffs/WP/GlobalVariablesOverrideSniff.php @@ -174,7 +174,7 @@ public function process_token( $stackPtr ) { protected function process_list_assignment( $stackPtr ) { $list_open_close = Lists::getOpenClose( $this->phpcsFile, $stackPtr ); if ( false === $list_open_close ) { - // Short array, not short list. + // Live coding or short array, not short list. return; } diff --git a/WordPress/Tests/WP/GlobalVariablesOverrideUnitTest.1.inc b/WordPress/Tests/WP/GlobalVariablesOverrideUnitTest.1.inc index 80603e7e8b..4daeb20fde 100644 --- a/WordPress/Tests/WP/GlobalVariablesOverrideUnitTest.1.inc +++ b/WordPress/Tests/WP/GlobalVariablesOverrideUnitTest.1.inc @@ -310,7 +310,3 @@ list( array( $tab, $tabs ) => $not_a_wp_global, get($year, $day) => &$not_a_wp_global[$year] ] = $array; - -// Live coding/parse error. -// This has to be the last test in the file! -list( $tab, $tabs diff --git a/WordPress/Tests/WP/GlobalVariablesOverrideUnitTest.8.inc b/WordPress/Tests/WP/GlobalVariablesOverrideUnitTest.8.inc new file mode 100644 index 0000000000..7dba2caad2 --- /dev/null +++ b/WordPress/Tests/WP/GlobalVariablesOverrideUnitTest.8.inc @@ -0,0 +1,8 @@ + Date: Thu, 17 Jul 2025 15:43:24 -0300 Subject: [PATCH 5/5] WP/GlobalVariablesOverride: add tests using asymmetric visibility properties This commit adds a few tests using asymmetric visibility properties (including in constructor property promotion) to the `WordPress.WP.GlobalVariablesOverride` tests. This is just to ensure that the part of the sniff code that ignores properties or method parameters (in the case of constructor property promotion) continues to handle PHP 8.4+ asymmetric visibility properties correctly. No change to the sniff code is needed. --- .../Tests/WP/GlobalVariablesOverrideUnitTest.1.inc | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/WordPress/Tests/WP/GlobalVariablesOverrideUnitTest.1.inc b/WordPress/Tests/WP/GlobalVariablesOverrideUnitTest.1.inc index 4daeb20fde..092fd2e589 100644 --- a/WordPress/Tests/WP/GlobalVariablesOverrideUnitTest.1.inc +++ b/WordPress/Tests/WP/GlobalVariablesOverrideUnitTest.1.inc @@ -310,3 +310,13 @@ list( array( $tab, $tabs ) => $not_a_wp_global, get($year, $day) => &$not_a_wp_global[$year] ] = $array; + +/* + * Safeguard that PHP 8.4+ asymmetric visibility properties don't lead to false positives. + * Including those defined using constructor property promotion. + */ +class AsymmetricVisibilityProperties { + public private(set) string $pagenow = 'bar'; // Ok. + + public function __construct(public protected(set) int $page = 0) {} // Ok. +}