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' ) {} function do_something( $param = 'default' ) {}
} }
$acronym_class = new class { $acronym_class = new readonly class {
const SOME_CONSTANT = 'value'; const SOME_CONSTANT = 'value';
public $var = 'abc'; 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Ä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. 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. * Safeguard that PHP 8.3+ readonly anonymous classes are handled correctly.
function */
$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. * 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. * @return array<int, int> Key is the line number, value is the number of expected errors.
*/ */
public function getErrorList() { public function getErrorList( $testFile = '' ) {
return array( switch ( $testFile ) {
3 => 1, case 'ValidFunctionNameUnitTest.1.inc':
9 => 1, return array(
13 => 1, 3 => 1,
15 => 1, 9 => 1,
79 => 2, 13 => 1,
80 => 2, 15 => 1,
81 => 2, 79 => 2,
82 => 2, 80 => 2,
83 => 2, 81 => 2,
84 => 2, 82 => 2,
85 => 2, 83 => 2,
86 => 2, 84 => 2,
87 => 2, 85 => 2,
88 => 2, 86 => 2,
89 => 2, 87 => 2,
106 => 2, 88 => 2,
116 => 1, 89 => 2,
117 => 1, 106 => 2,
157 => 2, 116 => 1,
183 => 1, 117 => 1,
184 => 1, 157 => 2,
185 => 1, 183 => 1,
199 => 1, 184 => 1,
208 => 2, 185 => 1,
210 => 1, 199 => 1,
223 => function_exists( 'mb_strtolower' ) ? 1 : 0, 208 => 2,
224 => 1, 210 => 1,
); 223 => function_exists( 'mb_strtolower' ) ? 1 : 0,
224 => 1,
230 => 1,
231 => 1,
);
default:
return array();
}
} }
/** /**