@@ -121,23 +121,57 @@ protected function processMemberVar(File $phpcsFile, $stackPtr)
121
121
}
122
122
123
123
/*
124
- * Note: per PSR-PER section 4.6, the order should be:
124
+ * Note: per PSR-PER section 4.6 v 2.1/3.0 , the order should be:
125
125
* - Inheritance modifier: `abstract` or `final`.
126
126
* - Visibility modifier: `public`, `protected`, or `private`.
127
+ * - Set-visibility modifier: `public(set)`, `protected(set)`, or `private(set)`
127
128
* - Scope modifier: `static`.
128
129
* - Mutation modifier: `readonly`.
129
130
* - Type declaration.
130
131
* - Name.
131
132
*
132
- * Ref: https://www.php-fig.org/per/coding-style/#46-modifier-keywords
133
+ * Ref:
134
+ * - https://www.php-fig.org/per/coding-style/#46-modifier-keywords
135
+ * - https://github.com/php-fig/per-coding-style/pull/99
133
136
*
134
137
* The `static` and `readonly` modifiers are mutually exclusive and cannot be used together.
135
138
*
136
139
* Based on that, the below modifier keyword order checks are sufficient (for now).
137
140
*/
138
141
139
- if ($ propertyInfo ['scope_specified ' ] === true && $ propertyInfo ['is_final ' ] === true ) {
140
- $ scopePtr = $ phpcsFile ->findPrevious (Tokens::$ scopeModifiers , ($ stackPtr - 1 ));
142
+ $ hasVisibilityModifier = ($ propertyInfo ['scope_specified ' ] === true || $ propertyInfo ['set_scope ' ] !== false );
143
+ $ lastVisibilityModifier = $ phpcsFile ->findPrevious (Tokens::$ scopeModifiers , ($ stackPtr - 1 ));
144
+ $ firstVisibilityModifier = $ lastVisibilityModifier ;
145
+
146
+ if ($ propertyInfo ['scope_specified ' ] === true && $ propertyInfo ['set_scope ' ] !== false ) {
147
+ $ scopePtr = $ phpcsFile ->findPrevious ([T_PUBLIC , T_PROTECTED , T_PRIVATE ], ($ stackPtr - 1 ));
148
+ $ setScopePtr = $ phpcsFile ->findPrevious ([T_PUBLIC_SET , T_PROTECTED_SET , T_PRIVATE_SET ], ($ stackPtr - 1 ));
149
+ if ($ scopePtr > $ setScopePtr ) {
150
+ $ error = 'The "read"-visibility must come before the "write"-visibility ' ;
151
+ $ fix = $ phpcsFile ->addFixableError ($ error , $ stackPtr , 'AvizKeywordOrder ' );
152
+ if ($ fix === true ) {
153
+ $ phpcsFile ->fixer ->beginChangeset ();
154
+
155
+ for ($ i = ($ scopePtr + 1 ); $ scopePtr < $ stackPtr ; $ i ++) {
156
+ if ($ tokens [$ i ]['code ' ] !== T_WHITESPACE ) {
157
+ break ;
158
+ }
159
+
160
+ $ phpcsFile ->fixer ->replaceToken ($ i , '' );
161
+ }
162
+
163
+ $ phpcsFile ->fixer ->replaceToken ($ scopePtr , '' );
164
+ $ phpcsFile ->fixer ->addContentBefore ($ setScopePtr , $ tokens [$ scopePtr ]['content ' ].' ' );
165
+
166
+ $ phpcsFile ->fixer ->endChangeset ();
167
+ }
168
+ }
169
+
170
+ $ firstVisibilityModifier = min ($ scopePtr , $ setScopePtr );
171
+ }//end if
172
+
173
+ if ($ hasVisibilityModifier === true && $ propertyInfo ['is_final ' ] === true ) {
174
+ $ scopePtr = $ firstVisibilityModifier ;
141
175
$ finalPtr = $ phpcsFile ->findPrevious (T_FINAL , ($ stackPtr - 1 ));
142
176
if ($ finalPtr > $ scopePtr ) {
143
177
$ error = 'The final declaration must come before the visibility declaration ' ;
@@ -161,50 +195,50 @@ protected function processMemberVar(File $phpcsFile, $stackPtr)
161
195
}
162
196
}//end if
163
197
164
- if ($ propertyInfo [ ' scope_specified ' ] === true && $ propertyInfo ['is_static ' ] === true ) {
165
- $ scopePtr = $ phpcsFile -> findPrevious (Tokens:: $ scopeModifiers , ( $ stackPtr - 1 )) ;
198
+ if ($ hasVisibilityModifier === true && $ propertyInfo ['is_static ' ] === true ) {
199
+ $ scopePtr = $ lastVisibilityModifier ;
166
200
$ staticPtr = $ phpcsFile ->findPrevious (T_STATIC , ($ stackPtr - 1 ));
167
201
if ($ scopePtr > $ staticPtr ) {
168
202
$ error = 'The static declaration must come after the visibility declaration ' ;
169
203
$ fix = $ phpcsFile ->addFixableError ($ error , $ stackPtr , 'StaticBeforeVisibility ' );
170
204
if ($ fix === true ) {
171
205
$ phpcsFile ->fixer ->beginChangeset ();
172
206
173
- for ($ i = ($ scopePtr + 1 ); $ scopePtr < $ stackPtr ; $ i ++) {
207
+ for ($ i = ($ staticPtr + 1 ); $ staticPtr < $ stackPtr ; $ i ++) {
174
208
if ($ tokens [$ i ]['code ' ] !== T_WHITESPACE ) {
175
209
break ;
176
210
}
177
211
178
212
$ phpcsFile ->fixer ->replaceToken ($ i , '' );
179
213
}
180
214
181
- $ phpcsFile ->fixer ->replaceToken ($ scopePtr , '' );
182
- $ phpcsFile ->fixer ->addContentBefore ( $ staticPtr , $ propertyInfo [ ' scope ' ]. ' ' );
215
+ $ phpcsFile ->fixer ->replaceToken ($ staticPtr , '' );
216
+ $ phpcsFile ->fixer ->addContent ( $ scopePtr , ' ' . $ tokens [ $ staticPtr ][ ' content ' ] );
183
217
184
218
$ phpcsFile ->fixer ->endChangeset ();
185
219
}
186
220
}
187
221
}//end if
188
222
189
- if ($ propertyInfo [ ' scope_specified ' ] === true && $ propertyInfo ['is_readonly ' ] === true ) {
190
- $ scopePtr = $ phpcsFile -> findPrevious (Tokens:: $ scopeModifiers , ( $ stackPtr - 1 )) ;
223
+ if ($ hasVisibilityModifier === true && $ propertyInfo ['is_readonly ' ] === true ) {
224
+ $ scopePtr = $ lastVisibilityModifier ;
191
225
$ readonlyPtr = $ phpcsFile ->findPrevious (T_READONLY , ($ stackPtr - 1 ));
192
226
if ($ scopePtr > $ readonlyPtr ) {
193
227
$ error = 'The readonly declaration must come after the visibility declaration ' ;
194
228
$ fix = $ phpcsFile ->addFixableError ($ error , $ stackPtr , 'ReadonlyBeforeVisibility ' );
195
229
if ($ fix === true ) {
196
230
$ phpcsFile ->fixer ->beginChangeset ();
197
231
198
- for ($ i = ($ scopePtr + 1 ); $ scopePtr < $ stackPtr ; $ i ++) {
232
+ for ($ i = ($ readonlyPtr + 1 ); $ readonlyPtr < $ stackPtr ; $ i ++) {
199
233
if ($ tokens [$ i ]['code ' ] !== T_WHITESPACE ) {
200
234
break ;
201
235
}
202
236
203
237
$ phpcsFile ->fixer ->replaceToken ($ i , '' );
204
238
}
205
239
206
- $ phpcsFile ->fixer ->replaceToken ($ scopePtr , '' );
207
- $ phpcsFile ->fixer ->addContentBefore ( $ readonlyPtr , $ propertyInfo [ ' scope ' ]. ' ' );
240
+ $ phpcsFile ->fixer ->replaceToken ($ readonlyPtr , '' );
241
+ $ phpcsFile ->fixer ->addContent ( $ scopePtr , ' ' . $ tokens [ $ readonlyPtr ][ ' content ' ] );
208
242
209
243
$ phpcsFile ->fixer ->endChangeset ();
210
244
}
0 commit comments