@@ -73,6 +73,13 @@ class XmlDocValidator
7373 */
7474 private $ writer ;
7575
76+ /**
77+ * Configuration as passed on the command line.
78+ *
79+ * @var \PHPCSDevTools\Scripts\DocCodeExamples\Config
80+ */
81+ private $ config ;
82+
7683 /**
7784 * Whether the first error message for this XML file has been displayed or not. Used to control
7885 * when to display a header for the error messages with the XML file name.
@@ -88,6 +95,7 @@ class XmlDocValidator
8895 * @param CodeBlocksExtractor $extractor CodeBlocksExtractor instance.
8996 * @param PHPCSConfig $phpcsConfig PHPCS configuration object.
9097 * @param Writer $writer A helper object to write output.
98+ * @param Config $config Configuration as passed on the command line.
9199 *
92100 * @throws \RuntimeException If the XML file doesn't exist, the file name or directory structure
93101 * is invalid, or the standard is not installed in PHPCS.
@@ -96,7 +104,8 @@ public function __construct(
96104 string $ xmlDocFile ,
97105 CodeBlocksExtractor $ extractor ,
98106 PHPCSConfig $ phpcsConfig ,
99- Writer $ writer
107+ Writer $ writer ,
108+ Config $ config
100109 ) {
101110 if (!\file_exists ($ xmlDocFile )) {
102111 throw new \RuntimeException ("The XML file \"{$ xmlDocFile }\" does not exist. " );
@@ -105,12 +114,13 @@ public function __construct(
105114 $ this ->extractor = $ extractor ;
106115 $ this ->phpcsConfig = $ phpcsConfig ;
107116 $ this ->writer = $ writer ;
117+ $ this ->config = $ config ;
108118 $ this ->path = $ xmlDocFile ;
109119 $ pathInfo = \pathinfo ($ this ->path );
110120
111121 if (\substr ($ pathInfo ['basename ' ], -12 ) !== 'Standard.xml ' ) {
112122 throw new \RuntimeException (
113- "The XML file \"{$ pathInfo ['basename ' ]}\" is invalid. File names should end in \"Standard.xml \". "
123+ "ERROR: The XML file \"{$ pathInfo ['basename ' ]}\" is invalid. File names should end in \"Standard.xml \". "
114124 );
115125 }
116126
@@ -119,7 +129,7 @@ public function __construct(
119129
120130 if (\count ($ dirs ) < 3 || $ dirs [\count ($ dirs ) - 2 ] !== 'Docs ' ) {
121131 throw new \RuntimeException (
122- 'Invalid directory structure in the XML file path. Expected: '
132+ 'ERROR: Invalid directory structure in the XML file path. Expected: '
123133 . '{STANDARD_NAME}/Docs/{CATEGORY_NAME}/ ' . $ pathInfo ['basename ' ] . '. '
124134 );
125135 }
@@ -130,7 +140,7 @@ public function __construct(
130140
131141 if (Standards::isInstalledStandard ($ this ->standard ) === false ) {
132142 throw new \RuntimeException (
133- "The standard \"{$ this ->standard }\" is not installed in PHPCS. "
143+ "ERROR: The standard \"{$ this ->standard }\" is not installed in PHPCS. "
134144 );
135145 }
136146 }
@@ -230,7 +240,7 @@ private function verifyCodeBlock(CodeBlock $codeBlock, bool $isExpectedToBeValid
230240 // @phpstan-ignore function.resultUnused (maybe a PHPStan false positive?)
231241 \token_get_all ($ codeBlockContent , \TOKEN_PARSE );
232242 } catch (\Throwable $ e ) {
233- $ message = 'ERROR: There is a syntax error in the code block. ' . \PHP_EOL ;
243+ $ message = 'There is a syntax error in the code block. ' . \PHP_EOL ;
234244 $ message .= $ e ->getMessage () . \PHP_EOL ;
235245 $ this ->displayErrorMessage (
236246 $ this ->prepareCodeBlockErrorMessage ($ message , $ codeBlock )
@@ -248,7 +258,7 @@ private function verifyCodeBlock(CodeBlock $codeBlock, bool $isExpectedToBeValid
248258 $ file ->process ();
249259
250260 if ($ isExpectedToBeValid === true && ($ file ->getErrorCount () !== 0 || $ file ->getWarningCount () !== 0 )) {
251- $ message = 'ERROR: Code block is valid and PHPCS should have returned nothing, ' .
261+ $ message = 'Code block is valid and PHPCS should have returned nothing, ' .
252262 'but instead it returned an error. ' ;
253263 $ this ->displayErrorMessage (
254264 $ this ->prepareCodeBlockErrorMessage ($ message , $ codeBlock )
@@ -257,7 +267,7 @@ private function verifyCodeBlock(CodeBlock $codeBlock, bool $isExpectedToBeValid
257267 }
258268
259269 if ($ isExpectedToBeValid === false && ($ file ->getErrorCount () === 0 && $ file ->getWarningCount () === 0 )) {
260- $ message = 'ERROR: Code block is invalid and PHPCS should have returned an error message, ' .
270+ $ message = 'Code block is invalid and PHPCS should have returned an error message, ' .
261271 'but instead it returned nothing. ' ;
262272 $ this ->displayErrorMessage (
263273 $ this ->prepareCodeBlockErrorMessage ($ message , $ codeBlock )
@@ -278,7 +288,13 @@ private function verifyCodeBlock(CodeBlock $codeBlock, bool $isExpectedToBeValid
278288 */
279289 private function prepareCodeBlockErrorMessage (string $ message , CodeBlock $ codeBlock ): string
280290 {
281- $ errorMessage = $ message . \PHP_EOL ;
291+ $ errorPrefix = 'ERROR: ' ;
292+
293+ if ($ this ->config ->getProperty ('showColored ' ) === true ) {
294+ $ errorPrefix = "\033[31m {$ errorPrefix }\033[0m " ;
295+ }
296+
297+ $ errorMessage = $ errorPrefix . $ message . \PHP_EOL ;
282298 $ errorMessage .= "Code block title: \"{$ codeBlock ->title }\"" . \PHP_EOL ;
283299 $ errorMessage .= "Code block content: \"{$ codeBlock ->content }\"" . \PHP_EOL ;
284300
@@ -296,7 +312,13 @@ private function prepareCodeBlockErrorMessage(string $message, CodeBlock $codeBl
296312 private function displayErrorMessage (string $ message )
297313 {
298314 if ($ this ->firstErrorMessageDisplayed === false ) {
299- $ this ->writer ->toStderr ("Errors found while processing {$ this ->path }" . \PHP_EOL . \PHP_EOL );
315+ $ errorHeader = "Errors found while processing {$ this ->path }" ;
316+
317+ if ($ this ->config ->getProperty ('showColored ' ) === true ) {
318+ $ errorHeader = "\033[31m {$ errorHeader }\033[0m " ;
319+ }
320+
321+ $ this ->writer ->toStderr ($ errorHeader . \PHP_EOL . \PHP_EOL );
300322 }
301323
302324 $ this ->firstErrorMessageDisplayed = true ;
0 commit comments