Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 28 additions & 17 deletions src/Files/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,22 +153,13 @@ class File
protected $warningCount = 0;

/**
* The original total number of errors that can be fixed (first run on a file).
* The original total number of errors and warnings (first run on a file).
*
* {@internal This should be regarded as an immutable property.}
*
* @var integer
*/
private $fixableErrorCountFirstRun;

/**
* The original total number of warnings that can be fixed (first run on a file).
*
* {@internal This should be regarded as an immutable property.}
*
* @var integer
* @var array<string, int>
*/
private $fixableWarningCountFirstRun;
private $firstRunCounts;

/**
* The current total number of errors that can be fixed.
Expand Down Expand Up @@ -555,14 +546,18 @@ public function process()
StatusWriter::write('*** END SNIFF PROCESSING REPORT ***', 1);
}

if (isset($this->fixableErrorCountFirstRun, $this->fixableWarningCountFirstRun) === false) {
$this->fixableErrorCountFirstRun = $this->fixableErrorCount;
$this->fixableWarningCountFirstRun = $this->fixableWarningCount;
if (isset($this->firstRunCounts) === false) {
$this->firstRunCounts = [
'error' => $this->errorCount,
'warning' => $this->warningCount,
'fixableError' => $this->fixableErrorCount,
'fixableWarning' => $this->fixableWarningCount,
];
}

$this->fixedCount += $this->fixer->getFixCount();
$this->fixedErrorCount = ($this->fixableErrorCountFirstRun - $this->fixableErrorCount);
$this->fixedWarningCount = ($this->fixableWarningCountFirstRun - $this->fixableWarningCount);
$this->fixedErrorCount = ($this->firstRunCounts['fixableError'] - $this->fixableErrorCount);
$this->fixedWarningCount = ($this->firstRunCounts['fixableWarning'] - $this->fixableWarningCount);

}//end process()

Expand Down Expand Up @@ -1217,6 +1212,22 @@ public function getFixedWarningCount()
}//end getFixedWarningCount()


/**
* Retrieve information about the first run.
*
* @param $type string
*
* @internal This method does not form part of any public API nor backwards compatibility guarantee.
*
* @return int
*/
public function getFirstRunCount(string $type):int
{
return $this->firstRunCounts[$type];

}//end getFirstRunCount()


/**
* Returns the list of ignored lines.
*
Expand Down
46 changes: 44 additions & 2 deletions src/Reports/Cbf.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@
* report from the command line.
*
* @author Greg Sherwood <[email protected]>
* @author Juliette Reinders Folmer <[email protected]>
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
* @copyright 2025 PHPCSStandards and contributors
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/

namespace PHP_CodeSniffer\Reports;

use PHP_CodeSniffer\Exceptions\DeepExitException;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Reporter;
use PHP_CodeSniffer\Util\ExitCode;
use PHP_CodeSniffer\Util\Timing;
use PHP_CodeSniffer\Util\Writers\StatusWriter;
Expand Down Expand Up @@ -60,8 +63,12 @@ public function generateFileReport($report, File $phpcsFile, $showSources=false,
// Replacing STDIN, so output current file to STDOUT
// even if nothing was fixed. Exit here because we
// can't process any more than 1 file in this setup.
$fixedContent = $phpcsFile->fixer->getContents();
throw new DeepExitException($fixedContent, ExitCode::OKAY);
echo $phpcsFile->fixer->getContents();

// Fake a Reporter instance to allow for getting a proper exit code.
$reporter = $this->createReporterInstance($phpcsFile);

throw new DeepExitException('', ExitCode::calculate($reporter));
}

if ($errors === 0) {
Expand Down Expand Up @@ -246,4 +253,39 @@ public function generate(
}//end generate()


/**
* Create a "fake" Reporter instance to allow for getting a proper exit code when scanning code provided via STDIN.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being reported on.
*
* @return \PHP_CodeSniffer\Reporter
*/
private function createReporterInstance(File $phpcsFile)
{
$reporter = new class extends Reporter {


/**
* Overload the constructor as we don't need it.
*/
public function __construct()
{
}//end __construct()


};

$reporter->totalFiles = 1;
$reporter->totalErrors = $phpcsFile->getFirstRunCount('error');
$reporter->totalWarnings = $phpcsFile->getFirstRunCount('warning');
$reporter->totalFixableErrors = $phpcsFile->getFixableErrorCount();
$reporter->totalFixableWarnings = $phpcsFile->getFixableWarningCount();
$reporter->totalFixedErrors = $phpcsFile->getFixedErrorCount();
$reporter->totalFixedWarnings = $phpcsFile->getFixedWarningCount();

return $reporter;

}//end createReporterInstance()


}//end class
14 changes: 14 additions & 0 deletions tests/EndToEnd/Fixtures/ClassWithTwoStyleErrors.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

/**
* Class containing a simple style error that phpcbf cannot fix, and another that it can fix.
*
* @copyright 2025 PHPCSStandards and contributors
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/

namespace PHP_CodeSniffer\Tests\EndToEnd\Fixtures;

// The brace on the following line should be on a line by itself. This can be fixed with phpcbf.
class ClassWithTwoStyleErrors {
} // This comment does not belong here, according to PSR12. This cannot be fixed with phpcbf.
14 changes: 14 additions & 0 deletions tests/EndToEnd/Fixtures/ClassWithUnfixableStyleError.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

/**
* Class containing a simple style error that phpcbf cannot fix.
*
* @copyright 2025 PHPCSStandards and contributors
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/

namespace PHP_CodeSniffer\Tests\EndToEnd\Fixtures;

class ClassWithUnfixableStyleError
{
} // This comment does not belong here, according to PSR12.
73 changes: 73 additions & 0 deletions tests/EndToEnd/exit_code_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/usr/bin/env bash

function tear_down() {
rm -f tests/EndToEnd/Fixtures/*.fixed
}

function test_phpcs_exit_code_clean_file() {
bin/phpcs --standard=tests/EndToEnd/Fixtures/endtoend.xml.dist tests/EndToEnd/Fixtures/ClassOneWithoutStyleError.inc
assert_exit_code 0
}
function test_phpcs_exit_code_clean_stdin() {
bin/phpcs --standard=tests/EndToEnd/Fixtures/endtoend.xml.dist < tests/EndToEnd/Fixtures/ClassOneWithoutStyleError.inc
assert_exit_code 0
}
function test_phpcbf_exit_code_clean_file() {
bin/phpcbf --suffix=.fixed --standard=tests/EndToEnd/Fixtures/endtoend.xml.dist tests/EndToEnd/Fixtures/ClassOneWithoutStyleError.inc
assert_exit_code 0
}
function test_phpcbf_exit_code_clean_stdin() {
bin/phpcbf --suffix=.fixed --standard=tests/EndToEnd/Fixtures/endtoend.xml.dist < tests/EndToEnd/Fixtures/ClassOneWithoutStyleError.inc
assert_exit_code 0
}

function test_phpcs_exit_code_fixable_file() {
bin/phpcs --standard=tests/EndToEnd/Fixtures/endtoend.xml.dist tests/EndToEnd/Fixtures/ClassWithStyleError.inc
assert_exit_code 1
}
function test_phpcs_exit_code_fixable_stdin() {
bin/phpcs --standard=tests/EndToEnd/Fixtures/endtoend.xml.dist < tests/EndToEnd/Fixtures/ClassWithStyleError.inc
assert_exit_code 1
}
function test_phpcbf_exit_code_fixable_file() {
bin/phpcbf --suffix=.fixed --standard=tests/EndToEnd/Fixtures/endtoend.xml.dist tests/EndToEnd/Fixtures/ClassWithStyleError.inc
assert_exit_code 0
}
function test_phpcbf_exit_code_fixable_stdin() {
bin/phpcbf --suffix=.fixed --standard=tests/EndToEnd/Fixtures/endtoend.xml.dist < tests/EndToEnd/Fixtures/ClassWithStyleError.inc
assert_exit_code 0
}

function test_phpcs_exit_code_non_fixable_file() {
bin/phpcs --standard=tests/EndToEnd/Fixtures/endtoend.xml.dist tests/EndToEnd/Fixtures/ClassWithUnfixableStyleError.inc
assert_exit_code 2
}
function test_phpcs_exit_code_non_fixable_stdin() {
bin/phpcs --standard=tests/EndToEnd/Fixtures/endtoend.xml.dist < tests/EndToEnd/Fixtures/ClassWithUnfixableStyleError.inc
assert_exit_code 2
}
function test_phpcbf_exit_code_non_fixable_file() {
bin/phpcbf --suffix=.fixed --standard=tests/EndToEnd/Fixtures/endtoend.xml.dist tests/EndToEnd/Fixtures/ClassWithUnfixableStyleError.inc
assert_exit_code 2
}
function test_phpcbf_exit_code_non_fixable_stdin() {
bin/phpcbf --suffix=.fixed --standard=tests/EndToEnd/Fixtures/endtoend.xml.dist < tests/EndToEnd/Fixtures/ClassWithUnfixableStyleError.inc
assert_exit_code 2
}

function test_phpcs_exit_code_fixable_and_non_fixable_file() {
bin/phpcs --standard=tests/EndToEnd/Fixtures/endtoend.xml.dist tests/EndToEnd/Fixtures/ClassWithTwoStyleErrors.inc
assert_exit_code 3
}
function test_phpcs_exit_code_fixable_and_non_fixable_stdin() {
bin/phpcs --standard=tests/EndToEnd/Fixtures/endtoend.xml.dist < tests/EndToEnd/Fixtures/ClassWithTwoStyleErrors.inc
assert_exit_code 3
}
function test_phpcbf_exit_code_fixable_and_non_fixable_file() {
bin/phpcbf --suffix=.fixed --standard=tests/EndToEnd/Fixtures/endtoend.xml.dist tests/EndToEnd/Fixtures/ClassWithTwoStyleErrors.inc
assert_exit_code 2
}
function test_phpcbf_exit_code_fixable_and_non_fixable_stdin() {
bin/phpcbf --suffix=.fixed --standard=tests/EndToEnd/Fixtures/endtoend.xml.dist < tests/EndToEnd/Fixtures/ClassWithTwoStyleErrors.inc
assert_exit_code 2
}
Loading