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
This commit is contained in:
Denis Žoljom 2025-08-25 09:16:34 +02:00 committed by GitHub
commit 3dcb08a849
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 57 additions and 34 deletions

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';

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();
}
}
/**