Merge pull request #2551 from rodrigoprimo/asymmetric-visibility-properties

PHP 8.4 asymmetric visibility properties: add tests to four sniffs
This commit is contained in:
Denis Žoljom 2025-08-25 09:14:51 +02:00 committed by GitHub
commit be6312aa4b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 38 additions and 4 deletions

View file

@ -174,7 +174,7 @@ final class GlobalVariablesOverrideSniff extends Sniff {
protected function process_list_assignment( $stackPtr ) { protected function process_list_assignment( $stackPtr ) {
$list_open_close = Lists::getOpenClose( $this->phpcsFile, $stackPtr ); $list_open_close = Lists::getOpenClose( $this->phpcsFile, $stackPtr );
if ( false === $list_open_close ) { if ( false === $list_open_close ) {
// Short array, not short list. // Live coding or short array, not short list.
return; return;
} }

View file

@ -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[] // phpcs:set WordPress.NamingConventions.PrefixAllGlobals prefixes[]

View file

@ -237,3 +237,11 @@ class Has_Mixed_Case_Property {
$lähtöaika = true; // OK. $lähtöaika = true; // OK.
$lÄhtÖaika = true; // Bad, but only handled by the sniff if Mbstring is available. $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. $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.
}

View file

@ -98,6 +98,7 @@ final class ValidVariableNameUnitTest extends AbstractSniffUnitTest {
227 => 1, 227 => 1,
238 => function_exists( 'mb_strtolower' ) ? 1 : 0, 238 => function_exists( 'mb_strtolower' ) ? 1 : 0,
239 => 1, 239 => 1,
246 => 1,
); );
} }

View file

@ -3,4 +3,5 @@
class IgnoreProperties { class IgnoreProperties {
public $_GET = array( 'key' => 'something' ); // OK. public $_GET = array( 'key' => 'something' ); // OK.
public $_POST; // OK. public $_POST; // OK.
public private(set) string $_REQUEST; // Ok.
} }

View file

@ -311,6 +311,12 @@ list(
get($year, $day) => &$not_a_wp_global[$year] get($year, $day) => &$not_a_wp_global[$year]
] = $array; ] = $array;
// Live coding/parse error. /*
// This has to be the last test in the file! * Safeguard that PHP 8.4+ asymmetric visibility properties don't lead to false positives.
list( $tab, $tabs * 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.
}

View file

@ -0,0 +1,8 @@
<?php
/*
* Intentional parse error (missing closing parenthesis).
* This should be the only test in this file.
*/
list( $tab, $tabs