Skip to content

Commit 6d432e2

Browse files
committed
TODO: figure out how to set exit code for STDIN
1 parent 796c647 commit 6d432e2

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

src/Reports/Cbf.php

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@
77
* report from the command line.
88
*
99
* @author Greg Sherwood <[email protected]>
10+
* @author Juliette Reinders Folmer <[email protected]>
1011
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
12+
* @copyright 2025 PHPCSStandards and contributors
1113
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
1214
*/
1315

1416
namespace PHP_CodeSniffer\Reports;
1517

1618
use PHP_CodeSniffer\Exceptions\DeepExitException;
1719
use PHP_CodeSniffer\Files\File;
20+
use PHP_CodeSniffer\Reporter;
21+
use PHP_CodeSniffer\Util\ExitCode;
1822
use PHP_CodeSniffer\Util\Writers\StatusWriter;
1923

2024
class Cbf implements Report
@@ -59,7 +63,11 @@ public function generateFileReport($report, File $phpcsFile, $showSources=false,
5963
// even if nothing was fixed. Exit here because we
6064
// can't process any more than 1 file in this setup.
6165
$fixedContent = $phpcsFile->fixer->getContents();
62-
throw new DeepExitException($fixedContent, 1);
66+
67+
// Fake a Reporter instance to allow for getting a proper exit code.
68+
$reporter = $this->createReporterInstance($phpcsFile);
69+
70+
throw new DeepExitException($fixedContent, ExitCode::calculate($reporter));
6371
}
6472

6573
if ($errors === 0) {
@@ -251,4 +259,39 @@ public function generate(
251259
}//end generate()
252260

253261

262+
/**
263+
* Create a "fake" Reporter instance to allow for getting a proper exit code when scanning code provided via STDIN.
264+
*
265+
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being reported on.
266+
*
267+
* @return \PHP_CodeSniffer\Reporter
268+
*/
269+
private function createReporterInstance(File $phpcsFile)
270+
{
271+
$reporter = new class extends Reporter {
272+
273+
274+
/**
275+
* Overload the constructor as we don't need it.
276+
*/
277+
public function __construct()
278+
{
279+
}//end __construct()
280+
281+
282+
};
283+
284+
$reporter->totalFiles = 1;
285+
$reporter->totalErrors = $phpcsFile->getErrorCount();
286+
$reporter->totalWarnings = $phpcsFile->getWarningCount();
287+
$reporter->totalFixableErrors = $phpcsFile->getFixableErrorCount();
288+
$reporter->totalFixableWarnings = $phpcsFile->getFixableWarningCount();
289+
$reporter->totalFixedErrors = $phpcsFile->getFixedErrorCount();
290+
$reporter->totalFixedWarnings = $phpcsFile->getFixedWarningCount();
291+
292+
return $reporter;
293+
294+
}//end createReporterInstance()
295+
296+
254297
}//end class

0 commit comments

Comments
 (0)