@@ -115,56 +115,49 @@ public function generateFileReport($report, File $phpcsFile, $showSources=false,
115115 // The maximum amount of space an error message can use.
116116 $ maxErrorSpace = ($ width - $ paddingLength - 1 );
117117
118+ $ beforeMsg = '' ;
119+ $ afterMsg = '' ;
118120 if ($ showSources === true ) {
119121 $ beforeMsg = "\033[1m " ;
120122 $ afterMsg = "\033[0m " ;
121- } else {
122- $ beforeMsg = '' ;
123- $ afterMsg = '' ;
124123 }
125124
126125 foreach ($ report ['messages ' ] as $ line => $ lineErrors ) {
127126 foreach ($ lineErrors as $ column => $ colErrors ) {
128127 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+ );
134132
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 )));
142144 }
143145
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 ;
166159 }
167- }//end foreach
160+ }//end if
168161
169162 // The padding that goes on the front of the line.
170163 $ padding = ($ maxLineNumLength - strlen ($ line ));
0 commit comments