@@ -43,6 +43,7 @@ protected function processMemberVar(File $phpcsFile, $stackPtr)
43
43
$ find [] = T_VARIABLE ;
44
44
$ find [] = T_VAR ;
45
45
$ find [] = T_READONLY ;
46
+ $ find [] = T_FINAL ;
46
47
$ find [] = T_SEMICOLON ;
47
48
$ find [] = T_OPEN_CURLY_BRACKET ;
48
49
@@ -130,12 +131,36 @@ protected function processMemberVar(File $phpcsFile, $stackPtr)
130
131
*
131
132
* Ref: https://www.php-fig.org/per/coding-style/#46-modifier-keywords
132
133
*
133
- * At this time (PHP 8.2), inheritance modifiers cannot be applied to properties and
134
- * the `static` and `readonly` modifiers are mutually exclusive and cannot be used together.
134
+ * The `static` and `readonly` modifiers are mutually exclusive and cannot be used together.
135
135
*
136
136
* Based on that, the below modifier keyword order checks are sufficient (for now).
137
137
*/
138
138
139
+ if ($ propertyInfo ['scope_specified ' ] === true && $ propertyInfo ['is_final ' ] === true ) {
140
+ $ scopePtr = $ phpcsFile ->findPrevious (Tokens::$ scopeModifiers , ($ stackPtr - 1 ));
141
+ $ finalPtr = $ phpcsFile ->findPrevious (T_FINAL , ($ stackPtr - 1 ));
142
+ if ($ finalPtr > $ scopePtr ) {
143
+ $ error = 'The final declaration must come before the visibility declaration ' ;
144
+ $ fix = $ phpcsFile ->addFixableError ($ error , $ stackPtr , 'FinalAfterVisibility ' );
145
+ if ($ fix === true ) {
146
+ $ phpcsFile ->fixer ->beginChangeset ();
147
+
148
+ for ($ i = ($ finalPtr + 1 ); $ finalPtr < $ stackPtr ; $ i ++) {
149
+ if ($ tokens [$ i ]['code ' ] !== T_WHITESPACE ) {
150
+ break ;
151
+ }
152
+
153
+ $ phpcsFile ->fixer ->replaceToken ($ i , '' );
154
+ }
155
+
156
+ $ phpcsFile ->fixer ->replaceToken ($ finalPtr , '' );
157
+ $ phpcsFile ->fixer ->addContentBefore ($ scopePtr , $ tokens [$ finalPtr ]['content ' ].' ' );
158
+
159
+ $ phpcsFile ->fixer ->endChangeset ();
160
+ }
161
+ }
162
+ }//end if
163
+
139
164
if ($ propertyInfo ['scope_specified ' ] === true && $ propertyInfo ['is_static ' ] === true ) {
140
165
$ scopePtr = $ phpcsFile ->findPrevious (Tokens::$ scopeModifiers , ($ stackPtr - 1 ));
141
166
$ staticPtr = $ phpcsFile ->findPrevious (T_STATIC , ($ stackPtr - 1 ));
0 commit comments