Skip to content

Commit 3dcb08a

Browse files
authored
Merge pull request #2550 from rodrigoprimo/readonly-anonymous-classes
PHP 8.3 readonly anonymous classes: add tests to two sniffs
2 parents be6312a + d541631 commit 3dcb08a

File tree

4 files changed

+57
-34
lines changed

4 files changed

+57
-34
lines changed

WordPress/Tests/NamingConventions/PrefixAllGlobalsUnitTest.1.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ class Acronym_Example {
137137
function do_something( $param = 'default' ) {}
138138
}
139139

140-
$acronym_class = new class {
140+
$acronym_class = new readonly class {
141141
const SOME_CONSTANT = 'value';
142142

143143
public $var = 'abc';

WordPress/Tests/NamingConventions/ValidFunctionNameUnitTest.inc renamed to WordPress/Tests/NamingConventions/ValidFunctionNameUnitTest.1.inc

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,11 @@ function lähtöaika() {} // OK.
223223
function lÄhtÖaika() {} // Bad, but only handled by the sniff if Mbstring is available.
224224
function lÄhtOaika() {} // Bad, handled via transliteration of non-ASCII chars if Mbstring is not available.
225225

226-
// Live coding/parse error.
227-
// This has to be the last test in the file.
228-
function
226+
/*
227+
* Safeguard that PHP 8.3+ readonly anonymous classes are handled correctly.
228+
*/
229+
$anon_class = new readonly class() {
230+
public function camelCase() {} // Bad.
231+
protected function __something() {} // Bad.
232+
private function snake_case() {} // Ok.
233+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
/*
4+
* Intentional parse error (nothing after T_FUNCTION).
5+
* This should be the only test in this file.
6+
*/
7+
8+
function

WordPress/Tests/NamingConventions/ValidFunctionNameUnitTest.php

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,38 +25,48 @@ final class ValidFunctionNameUnitTest extends AbstractSniffUnitTest {
2525
/**
2626
* Returns the lines where errors should occur.
2727
*
28+
* @param string $testFile The name of the file being tested.
29+
*
2830
* @return array<int, int> Key is the line number, value is the number of expected errors.
2931
*/
30-
public function getErrorList() {
31-
return array(
32-
3 => 1,
33-
9 => 1,
34-
13 => 1,
35-
15 => 1,
36-
79 => 2,
37-
80 => 2,
38-
81 => 2,
39-
82 => 2,
40-
83 => 2,
41-
84 => 2,
42-
85 => 2,
43-
86 => 2,
44-
87 => 2,
45-
88 => 2,
46-
89 => 2,
47-
106 => 2,
48-
116 => 1,
49-
117 => 1,
50-
157 => 2,
51-
183 => 1,
52-
184 => 1,
53-
185 => 1,
54-
199 => 1,
55-
208 => 2,
56-
210 => 1,
57-
223 => function_exists( 'mb_strtolower' ) ? 1 : 0,
58-
224 => 1,
59-
);
32+
public function getErrorList( $testFile = '' ) {
33+
switch ( $testFile ) {
34+
case 'ValidFunctionNameUnitTest.1.inc':
35+
return array(
36+
3 => 1,
37+
9 => 1,
38+
13 => 1,
39+
15 => 1,
40+
79 => 2,
41+
80 => 2,
42+
81 => 2,
43+
82 => 2,
44+
83 => 2,
45+
84 => 2,
46+
85 => 2,
47+
86 => 2,
48+
87 => 2,
49+
88 => 2,
50+
89 => 2,
51+
106 => 2,
52+
116 => 1,
53+
117 => 1,
54+
157 => 2,
55+
183 => 1,
56+
184 => 1,
57+
185 => 1,
58+
199 => 1,
59+
208 => 2,
60+
210 => 1,
61+
223 => function_exists( 'mb_strtolower' ) ? 1 : 0,
62+
224 => 1,
63+
230 => 1,
64+
231 => 1,
65+
);
66+
67+
default:
68+
return array();
69+
}
6070
}
6171

6272
/**

0 commit comments

Comments
 (0)