@@ -121,29 +121,63 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
121
121
$ phpcsFile ->fixer ->replaceToken (($ opener - 1 ), '' );
122
122
}
123
123
}
124
+
125
+ $ next = $ phpcsFile ->findNext (T_WHITESPACE , ($ opener + 1 ), null , true );
126
+ if ($ tokens [$ next ]['line ' ] !== ($ tokens [$ opener ]['line ' ] + 1 )) {
127
+ $ error = 'The ' .strtoupper ($ type ).' body must start on the line following the statement ' ;
128
+ $ fix = $ phpcsFile ->addFixableError ($ error , $ nextCase , 'SpaceBeforeColon ' .strtoupper ($ type ));
129
+ if ($ fix === true ) {
130
+ if ($ tokens [$ next ]['line ' ] === $ tokens [$ opener ]['line ' ]) {
131
+ $ padding = str_repeat (' ' , ($ caseAlignment + $ this ->indent - 1 ));
132
+ $ phpcsFile ->fixer ->addContentBefore ($ next , $ phpcsFile ->eolChar .$ padding );
133
+ } else {
134
+ $ phpcsFile ->fixer ->beginChangeset ();
135
+ for ($ i = ($ opener + 1 ); $ i < $ next ; $ i ++) {
136
+ if ($ tokens [$ i ]['line ' ] === $ tokens [$ next ]['line ' ]) {
137
+ break ;
138
+ }
139
+
140
+ $ phpcsFile ->fixer ->replaceToken ($ i , '' );
141
+ }
142
+
143
+ $ phpcsFile ->fixer ->addNewLineBefore ($ i );
144
+ $ phpcsFile ->fixer ->endChangeset ();
145
+ }
146
+ }
147
+ }//end if
124
148
} else {
125
149
$ error = strtoupper ($ type ).' statements must be defined using a colon ' ;
126
150
$ phpcsFile ->addError ($ error , $ nextCase , 'WrongOpener ' .$ type );
127
- }
151
+ }//end if
128
152
129
153
$ nextCloser = $ tokens [$ nextCase ]['scope_closer ' ];
130
154
if ($ tokens [$ nextCloser ]['scope_condition ' ] === $ nextCase ) {
131
155
// Only need to check some things once, even if the
132
156
// closer is shared between multiple case statements, or even
133
157
// the default case.
134
- $ diff = ( $ caseAlignment + $ this -> indent - $ tokens [ $ nextCloser ][ ' column ' ] );
135
- if ($ diff !== 0 ) {
136
- $ error = 'Terminating statement must be indented to the same level as the CASE body ' ;
137
- $ fix = $ phpcsFile ->addFixableError ($ error , $ nextCloser , 'BreakIndent ' );
158
+ $ prev = $ phpcsFile -> findPrevious ( T_WHITESPACE , ( $ nextCloser - 1 ), $ nextCase , true );
159
+ if ($ tokens [ $ prev ][ ' line ' ] === $ tokens [ $ nextCloser ][ ' line ' ] ) {
160
+ $ error = 'Terminating statement must be on a line by itself ' ;
161
+ $ fix = $ phpcsFile ->addFixableError ($ error , $ nextCloser , 'BreakNotNewLine ' );
138
162
if ($ fix === true ) {
139
- if ($ diff > 0 ) {
140
- $ phpcsFile ->fixer ->addContentBefore ($ nextCloser , str_repeat (' ' , $ diff ));
141
- } else {
142
- $ phpcsFile ->fixer ->substrToken (($ nextCloser - 1 ), 0 , $ diff );
163
+ $ phpcsFile ->fixer ->addNewLine ($ prev );
164
+ $ phpcsFile ->fixer ->replaceToken ($ nextCloser , trim ($ tokens [$ nextCloser ]['content ' ]));
165
+ }
166
+ } else {
167
+ $ diff = ($ caseAlignment + $ this ->indent - $ tokens [$ nextCloser ]['column ' ]);
168
+ if ($ diff !== 0 ) {
169
+ $ error = 'Terminating statement must be indented to the same level as the CASE body ' ;
170
+ $ fix = $ phpcsFile ->addFixableError ($ error , $ nextCloser , 'BreakIndent ' );
171
+ if ($ fix === true ) {
172
+ if ($ diff > 0 ) {
173
+ $ phpcsFile ->fixer ->addContentBefore ($ nextCloser , str_repeat (' ' , $ diff ));
174
+ } else {
175
+ $ phpcsFile ->fixer ->substrToken (($ nextCloser - 1 ), 0 , $ diff );
176
+ }
143
177
}
144
178
}
145
- }
146
- }
179
+ }//end if
180
+ }//end if
147
181
148
182
// We only want cases from here on in.
149
183
if ($ type !== 'case ' ) {
0 commit comments