@@ -115,56 +115,49 @@ public function generateFileReport($report, File $phpcsFile, $showSources=false,
115
115
// The maximum amount of space an error message can use.
116
116
$ maxErrorSpace = ($ width - $ paddingLength - 1 );
117
117
118
+ $ beforeMsg = '' ;
119
+ $ afterMsg = '' ;
118
120
if ($ showSources === true ) {
119
121
$ beforeMsg = "\033[1m " ;
120
122
$ afterMsg = "\033[0m " ;
121
- } else {
122
- $ beforeMsg = '' ;
123
- $ afterMsg = '' ;
124
123
}
125
124
126
125
foreach ($ report ['messages ' ] as $ line => $ lineErrors ) {
127
126
foreach ($ lineErrors as $ column => $ colErrors ) {
128
127
foreach ($ colErrors as $ error ) {
129
- $ message = $ error ['message ' ];
130
- $ msgLines = [$ message ];
131
- if (strpos ($ message , "\n" ) !== false ) {
132
- $ msgLines = explode ("\n" , $ message );
133
- }
128
+ $ errorMsg = wordwrap (
129
+ $ error ['message ' ],
130
+ $ maxErrorSpace
131
+ );
134
132
135
- $ errorMsg = '' ;
136
- $ lastLine = (count ($ msgLines ) - 1 );
137
- foreach ($ msgLines as $ k => $ msgLine ) {
138
- if ($ k === 0 ) {
139
- $ errorMsg .= $ beforeMsg ;
140
- } else {
141
- $ errorMsg .= $ afterMsg .PHP_EOL .$ paddingLine2 .$ beforeMsg ;
133
+ // Add the padding _after_ the wordwrap as the message itself may contain line breaks
134
+ // and those lines will also need to receive padding.
135
+ $ errorMsg = str_replace ("\n" , $ afterMsg .PHP_EOL .$ paddingLine2 .$ beforeMsg , $ errorMsg );
136
+ $ errorMsg = $ beforeMsg .$ errorMsg .$ afterMsg ;
137
+
138
+ if ($ showSources === true ) {
139
+ $ lastMsg = $ errorMsg ;
140
+ $ startPosLastLine = strrpos ($ errorMsg , $ paddingLine2 .$ beforeMsg );
141
+ if ($ startPosLastLine !== false ) {
142
+ // Message is multiline.
143
+ $ lastMsg = substr ($ errorMsg , ($ startPosLastLine + strlen ($ paddingLine2 .$ beforeMsg )));
142
144
}
143
145
144
- $ wrappedLines = wordwrap (
145
- $ msgLine ,
146
- $ maxErrorSpace ,
147
- $ afterMsg .PHP_EOL .$ paddingLine2 .$ beforeMsg
148
- );
149
- $ errorMsg .= $ wrappedLines ;
150
-
151
- if ($ k === $ lastLine ) {
152
- $ errorMsg .= $ afterMsg ;
153
- if ($ showSources === true ) {
154
- $ lastLineLength = strlen ($ wrappedLines );
155
- $ lastNewlinePos = strrpos ($ wrappedLines , PHP_EOL );
156
- if ($ lastNewlinePos !== false ) {
157
- $ lastLineLength -= ($ lastNewlinePos + strlen (PHP_EOL .$ paddingLine2 .$ beforeMsg ));
158
- }
159
-
160
- if (($ lastLineLength + strlen ($ error ['source ' ]) + 3 ) > $ maxErrorSpace ) {
161
- $ errorMsg .= PHP_EOL .$ paddingLine2 .'( ' .$ error ['source ' ].') ' ;
162
- } else {
163
- $ errorMsg .= ' ( ' .$ error ['source ' ].') ' ;
164
- }
165
- }
146
+ // When show sources is used, the message itself will be bolded, so we need to correct the length.
147
+ $ sourceSuffix = '( ' .$ error ['source ' ].') ' ;
148
+
149
+ $ lastMsgPlusSourceLength = strlen ($ lastMsg );
150
+ // Add space + source suffix length.
151
+ $ lastMsgPlusSourceLength += (1 + strlen ($ sourceSuffix ));
152
+ // Correct for the color codes.
153
+ $ lastMsgPlusSourceLength -= 8 ;
154
+
155
+ if ($ lastMsgPlusSourceLength > $ maxErrorSpace ) {
156
+ $ errorMsg .= PHP_EOL .$ paddingLine2 .$ sourceSuffix ;
157
+ } else {
158
+ $ errorMsg .= ' ' .$ sourceSuffix ;
166
159
}
167
- }//end foreach
160
+ }//end if
168
161
169
162
// The padding that goes on the front of the line.
170
163
$ padding = ($ maxLineNumLength - strlen ($ line ));
0 commit comments