Compare commits

...

11 commits

Author SHA1 Message Date
Denis Žoljom
3dcb08a849
Merge pull request #2550 from rodrigoprimo/readonly-anonymous-classes
Some checks failed
Basic QA checks / Run code sniffs (push) Has been cancelled
Basic QA checks / XML Code style (push) Has been cancelled
Basic QA checks / Ruleset test: PHP latest on PHPCS dev (push) Has been cancelled
Basic QA checks / Ruleset test: PHP latest on PHPCS lowest (push) Has been cancelled
Basic QA checks / Ruleset test: PHP latest on PHPCS stable (push) Has been cancelled
Basic QA checks / PHPStan (push) Has been cancelled
Basic QA checks / Find typos (push) Has been cancelled
Quick Tests / QTest - PHP 5.4 on PHPCS lowest (push) Has been cancelled
Quick Tests / QTest - PHP latest on PHPCS lowest (push) Has been cancelled
Quick Tests / QTest - PHP 5.4 on PHPCS stable (push) Has been cancelled
Quick Tests / QTest - PHP latest on PHPCS stable (push) Has been cancelled
PHP 8.3 readonly anonymous classes: add tests to two sniffs
2025-08-25 09:16:34 +02:00
Denis Žoljom
be6312aa4b
Merge pull request #2551 from rodrigoprimo/asymmetric-visibility-properties
PHP 8.4 asymmetric visibility properties: add tests to four sniffs
2025-08-25 09:14:51 +02:00
Rodrigo Primo
a1f0108978 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.
2025-07-18 16:41:56 -03:00
Rodrigo Primo
f5ebe184f3 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.
2025-07-18 16:32:24 -03:00
Rodrigo Primo
88d311f981 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.
2025-07-18 16:32:19 -03:00
Rodrigo Primo
fb50666da9 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.
2025-07-18 16:31:49 -03:00
Rodrigo Primo
9e94d3b810 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.
2025-07-18 16:30:03 -03:00
Rodrigo Primo
d541631e93 NamingConventions/PrefixAllGlobals: modify a test to use readonly anonymous classes
This commit modifies an existing test to use readonly anonymous classes to the `WordPress.NamingConventions.PrefixAllGlobals` tests. This is just to ensure that the part of the sniff code that checks for `T_ANON_CLASS` tokens works correctly with readonly anonymous class added in PHP 8.3.

The sniff was already handling readonly anonymous classes correctly, and no change to the sniff code is needed.
2025-07-18 15:55:18 -03:00
Rodrigo Primo
0620684fe4 NamingConventions/ValidFunctionName: add tests using readonly anonymous classes
This commit adds a few tests using readonly anonymous classes to the `WordPress.NamingConventions.ValidFunctionName` tests. This is just to ensure that the part of the sniff code that checks for `T_ANON_CLASS` tokens works correctly with readonly anonymous class added in PHP 8.3.

The sniff was already handling readonly anonymous classes correctly, and no change to the sniff code is needed.
2025-07-18 15:55:18 -03:00
Rodrigo Primo
328d7026da NamingConventions/ValidFunctionName: move test with intentional parse error to a separate file 2025-07-18 15:55:18 -03:00
Rodrigo Primo
0076d49824 NamingConventions/ValidFunctionName: rename test case file
This is necessary to be able to create more test case files with syntax errors in a future commit.
2025-07-18 15:55:18 -03:00
10 changed files with 95 additions and 38 deletions

View file

@ -174,7 +174,7 @@ final class GlobalVariablesOverrideSniff extends Sniff {
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;
}

View file

@ -137,7 +137,7 @@ class Acronym_Example {
function do_something( $param = 'default' ) {}
}
$acronym_class = new class {
$acronym_class = new readonly class {
const SOME_CONSTANT = 'value';
public $var = 'abc';
@ -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[]

View file

@ -223,6 +223,11 @@ function lähtöaika() {} // OK.
function lÄhtÖaika() {} // Bad, but only handled by the sniff if Mbstring is available.
function lÄhtOaika() {} // Bad, handled via transliteration of non-ASCII chars if Mbstring is not available.
// Live coding/parse error.
// This has to be the last test in the file.
function
/*
* Safeguard that PHP 8.3+ readonly anonymous classes are handled correctly.
*/
$anon_class = new readonly class() {
public function camelCase() {} // Bad.
protected function __something() {} // Bad.
private function snake_case() {} // Ok.
};

View file

@ -0,0 +1,8 @@
<?php
/*
* Intentional parse error (nothing after T_FUNCTION).
* This should be the only test in this file.
*/
function

View file

@ -25,38 +25,48 @@ final class ValidFunctionNameUnitTest 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(
3 => 1,
9 => 1,
13 => 1,
15 => 1,
79 => 2,
80 => 2,
81 => 2,
82 => 2,
83 => 2,
84 => 2,
85 => 2,
86 => 2,
87 => 2,
88 => 2,
89 => 2,
106 => 2,
116 => 1,
117 => 1,
157 => 2,
183 => 1,
184 => 1,
185 => 1,
199 => 1,
208 => 2,
210 => 1,
223 => function_exists( 'mb_strtolower' ) ? 1 : 0,
224 => 1,
);
public function getErrorList( $testFile = '' ) {
switch ( $testFile ) {
case 'ValidFunctionNameUnitTest.1.inc':
return array(
3 => 1,
9 => 1,
13 => 1,
15 => 1,
79 => 2,
80 => 2,
81 => 2,
82 => 2,
83 => 2,
84 => 2,
85 => 2,
86 => 2,
87 => 2,
88 => 2,
89 => 2,
106 => 2,
116 => 1,
117 => 1,
157 => 2,
183 => 1,
184 => 1,
185 => 1,
199 => 1,
208 => 2,
210 => 1,
223 => function_exists( 'mb_strtolower' ) ? 1 : 0,
224 => 1,
230 => 1,
231 => 1,
);
default:
return array();
}
}
/**

View file

@ -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.
}

View file

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

View file

@ -3,4 +3,5 @@
class IgnoreProperties {
public $_GET = array( 'key' => 'something' ); // 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]
] = $array;
// Live coding/parse error.
// This has to be the last test in the file!
list( $tab, $tabs
/*
* 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.
}

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