Skip to content

(Nearly) All status, debug, and progress output is now sent to STDERR instead of STDOUT #1010

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 17, 2025
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
7 changes: 7 additions & 0 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,13 @@ To run the tests specific to the use of `PHP_CODESNIFFER_CBF === true`:
In such cases, the `PHP_CodeSniffer\Tests\Core\Config\AbstractRealConfigTestCase` should be used as the base test class.
* Tests for the `Runner` class often can't create their own `Config` object in the tests, so run into the same issue.
Those tests should use the `PHP_CodeSniffer\Tests\Core\Runner\AbstractRunnerTestCase` base class, which will ensure the Config is clean.
* Testing output sent to `stdErr` via the `StatusWriter` class is not possible by default using PHPUnit.
A work-around is available, however, as the `StatusWriter` is a "static class", that work-around involves static properties which need to be (re-)set between tests to ensure tests are pure.
So, to test output sent to `stdErr` via the `StatusWriter`, use the `PHP_CodeSniffer\Tests\Core\AbstractWriterTestCase` base class if your tests do not need their own `setUp()` and `tearDown()` methods.
If your tests **_do_** need their own `setUp()` and `tearDown()` methods, or would benefit more from using one of the other base TestCase classes, use the `PHP_CodeSniffer\Tests\Core\StatusWriterTestHelper` trait and call the appropriate setup/teardown helper methods from within your own `setUp()` and `tearDown()` methods.
Tests using the `AbstractWriterTestCase` class or the trait, also get access to the following test helpers for use when testing output sent to `stdErr`: `expectNoStdoutOutput()`, `assertStderrOutputSameString($expected)` and `assertStderrOutputMatchesRegex($regex)`.
Generally speaking, it is a good idea to always add a call to `expectNoStdoutOutput()` in any test using the `assertStderrOutput*()` assertions to make sure there is no output leaking to `stdOut`.


### Submitting Your Pull Request

Expand Down
5 changes: 3 additions & 2 deletions requirements.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ function checkRequirements()

// Check the PHP version.
if (PHP_VERSION_ID < 70200) {
echo 'ERROR: PHP_CodeSniffer requires PHP version 7.2.0 or greater.'.PHP_EOL;
$error = 'ERROR: PHP_CodeSniffer requires PHP version 7.2.0 or greater.'.PHP_EOL;
fwrite(STDERR, $error);
exit($exitCode);
}

Expand Down Expand Up @@ -64,7 +65,7 @@ function checkRequirements()
}

$error = 'ERROR: PHP_CodeSniffer requires the %s extensions to be enabled. Please enable %s.'.PHP_EOL;
printf($error, $required, $missing);
fwrite(STDERR, sprintf($error, $required, $missing));
exit($exitCode);
}

Expand Down
31 changes: 18 additions & 13 deletions src/Files/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use PHP_CodeSniffer\Tokenizers\PHP;
use PHP_CodeSniffer\Util\Common;
use PHP_CodeSniffer\Util\Tokens;
use PHP_CodeSniffer\Util\Writers\StatusWriter;

class File
{
Expand Down Expand Up @@ -324,7 +325,7 @@ public function process()
$this->fixer->startFile($this);

if (PHP_CODESNIFFER_VERBOSITY > 2) {
echo "\t*** START TOKEN PROCESSING ***".PHP_EOL;
StatusWriter::write('*** START TOKEN PROCESSING ***', 1);
}

$foundCode = false;
Expand Down Expand Up @@ -377,7 +378,7 @@ public function process()
if (PHP_CODESNIFFER_VERBOSITY > 2) {
$type = $token['type'];
$content = Common::prepareForOutput($token['content']);
echo "\t\tProcess token $stackPtr: $type => $content".PHP_EOL;
StatusWriter::write("Process token $stackPtr: $type => $content", 2);
}

if ($token['code'] !== T_INLINE_HTML) {
Expand Down Expand Up @@ -452,7 +453,7 @@ public function process()
}

if (PHP_CODESNIFFER_VERBOSITY > 2) {
echo "\t\t\tProcessing ".$this->activeListener.'... ';
StatusWriter::write('Processing '.$this->activeListener.'... ', 3, 0);
}

$ignoreTo = $this->ruleset->sniffs[$class]->process($this, $stackPtr);
Expand All @@ -471,7 +472,7 @@ public function process()

if (PHP_CODESNIFFER_VERBOSITY > 2) {
$timeTaken = round(($timeTaken), 4);
echo "DONE in $timeTaken seconds".PHP_EOL;
StatusWriter::write("DONE in $timeTaken seconds");
}

$this->activeListener = '';
Expand All @@ -493,15 +494,15 @@ public function process()
}

if (PHP_CODESNIFFER_VERBOSITY > 2) {
echo "\t*** END TOKEN PROCESSING ***".PHP_EOL;
echo "\t*** START SNIFF PROCESSING REPORT ***".PHP_EOL;
StatusWriter::write('*** END TOKEN PROCESSING ***', 1);
StatusWriter::write('*** START SNIFF PROCESSING REPORT ***', 1);

arsort($this->listenerTimes, SORT_NUMERIC);
foreach ($this->listenerTimes as $listener => $timeTaken) {
echo "\t$listener: ".round(($timeTaken), 4).' secs'.PHP_EOL;
StatusWriter::write("$listener: ".round(($timeTaken), 4).' secs', 1);
}

echo "\t*** END SNIFF PROCESSING REPORT ***".PHP_EOL;
StatusWriter::write('*** END SNIFF PROCESSING REPORT ***', 1);
}

$this->fixedCount += $this->fixer->getFixCount();
Expand All @@ -528,10 +529,12 @@ public function parse()
$this->ignored = true;
$this->addWarning($e->getMessage(), null, 'Internal.Tokenizer.Exception');
if (PHP_CODESNIFFER_VERBOSITY > 0) {
echo '[tokenizer error]... ';
$newlines = 0;
if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo PHP_EOL;
$newlines = 1;
}

StatusWriter::write('[tokenizer error]... ', 0, $newlines);
}

return;
Expand Down Expand Up @@ -560,10 +563,12 @@ public function parse()
$numLines = $this->tokens[($this->numTokens - 1)]['line'];
}

echo "[$this->numTokens tokens in $numLines lines]... ";
$newlines = 0;
if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo PHP_EOL;
$newlines = 1;
}

StatusWriter::write("[$this->numTokens tokens in $numLines lines]... ", 0, $newlines);
}

}//end parse()
Expand Down Expand Up @@ -1043,7 +1048,7 @@ protected function addMessage($error, $message, $line, $column, $code, $data, $s
&& $fixable === true
) {
@ob_end_clean();
echo "\tE: [Line $line] $message ($sniffCode)".PHP_EOL;
StatusWriter::forceWrite("E: [Line $line] $message ($sniffCode)", 1);
ob_start();
}

Expand Down
5 changes: 3 additions & 2 deletions src/Files/LocalFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PHP_CodeSniffer\Ruleset;
use PHP_CodeSniffer\Util\Cache;
use PHP_CodeSniffer\Util\Common;
use PHP_CodeSniffer\Util\Writers\StatusWriter;

class LocalFile extends File
{
Expand Down Expand Up @@ -113,7 +114,7 @@ public function process()
if (PHP_CODESNIFFER_VERBOSITY > 0
|| (PHP_CODESNIFFER_CBF === true && empty($this->config->files) === false)
) {
echo "[loaded from cache]... ";
StatusWriter::write('[loaded from cache]... ', 0, 0);
}

$this->numTokens = $cache['numTokens'];
Expand All @@ -122,7 +123,7 @@ public function process()
}//end if

if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo PHP_EOL;
StatusWriter::writeNewline();
}

parent::process();
Expand Down
73 changes: 43 additions & 30 deletions src/Fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PHP_CodeSniffer\Exceptions\RuntimeException;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Util\Common;
use PHP_CodeSniffer\Util\Writers\StatusWriter;

class Fixer
{
Expand Down Expand Up @@ -149,6 +150,9 @@ public function fixFile()

$this->enabled = true;

// Pause the StatusWriter to silence Tokenizer debug info about the file being retokenized for each loop.
StatusWriter::pause();

$this->loops = 0;
while ($this->loops < 50) {
ob_start();
Expand All @@ -158,15 +162,15 @@ public function fixFile()

if (PHP_CODESNIFFER_VERBOSITY > 2) {
@ob_end_clean();
echo '---START FILE CONTENT---'.PHP_EOL;
StatusWriter::forceWrite('---START FILE CONTENT---');
$lines = explode($this->currentFile->eolChar, $contents);
$max = strlen(count($lines));
foreach ($lines as $lineNum => $line) {
$lineNum++;
echo str_pad($lineNum, $max, ' ', STR_PAD_LEFT).'|'.$line.PHP_EOL;
StatusWriter::forceWrite(str_pad($lineNum, $max, ' ', STR_PAD_LEFT).'|'.$line);
}

echo '--- END FILE CONTENT ---'.PHP_EOL;
StatusWriter::forceWrite('--- END FILE CONTENT ---');
ob_start();
}

Expand All @@ -179,35 +183,40 @@ public function fixFile()
$this->loops++;

if (PHP_CODESNIFFER_CBF === true && PHP_CODESNIFFER_VERBOSITY > 0) {
echo "\r".str_repeat(' ', 80)."\r";
echo "\t=> Fixing file: $this->numFixes/$fixable violations remaining [made $this->loops pass";
StatusWriter::forceWrite("\r".str_repeat(' ', 80)."\r", 0, 0);
$statusMessage = "=> Fixing file: $this->numFixes/$fixable violations remaining [made $this->loops pass";
if ($this->loops > 1) {
echo 'es';
$statusMessage .= 'es';
}

echo ']... ';
$statusMessage .= ']... ';
$newlines = 0;
if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo PHP_EOL;
$newlines = 1;
}

StatusWriter::forceWrite($statusMessage, 1, $newlines);
}

if ($this->numFixes === 0 && $this->inConflict === false) {
// Nothing left to do.
break;
} else if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo "\t* fixed $this->numFixes violations, starting loop ".($this->loops + 1).' *'.PHP_EOL;
StatusWriter::forceWrite("* fixed $this->numFixes violations, starting loop ".($this->loops + 1).' *', 1);
}
}//end while

$this->enabled = false;

StatusWriter::resume();

if ($this->numFixes > 0) {
if (PHP_CODESNIFFER_VERBOSITY > 1) {
if (ob_get_level() > 0) {
ob_end_clean();
}

echo "\t*** Reached maximum number of loops with $this->numFixes violations left unfixed ***".PHP_EOL;
StatusWriter::write("*** Reached maximum number of loops with $this->numFixes violations left unfixed ***", 1);
ob_start();
}

Expand Down Expand Up @@ -409,7 +418,7 @@ public function beginChangeset()
$line = $bt[0]['line'];

@ob_end_clean();
echo "\t=> Changeset started by $sniff:$line".PHP_EOL;
StatusWriter::forceWrite("=> Changeset started by $sniff:$line", 1);
ob_start();
}

Expand Down Expand Up @@ -451,13 +460,13 @@ public function endChangeset()

if (PHP_CODESNIFFER_VERBOSITY > 1) {
@ob_end_clean();
echo "\t=> Changeset failed to apply".PHP_EOL;
StatusWriter::forceWrite('=> Changeset failed to apply', 1);
ob_start();
}
} else if (PHP_CODESNIFFER_VERBOSITY > 1) {
$fixes = count($this->changeset);
@ob_end_clean();
echo "\t=> Changeset ended: $fixes changes applied".PHP_EOL;
StatusWriter::forceWrite("=> Changeset ended: $fixes changes applied", 1);
ob_start();
}

Expand Down Expand Up @@ -493,8 +502,8 @@ public function rollbackChangeset()
$numChanges = count($this->changeset);

@ob_end_clean();
echo "\t\tR: $sniff:$line rolled back the changeset ($numChanges changes)".PHP_EOL;
echo "\t=> Changeset rolled back".PHP_EOL;
StatusWriter::forceWrite("R: $sniff:$line rolled back the changeset ($numChanges changes)", 2);
StatusWriter::forceWrite('=> Changeset rolled back', 1);
ob_start();
}

Expand All @@ -521,14 +530,14 @@ public function replaceToken($stackPtr, $content)
if ($this->inChangeset === false
&& isset($this->fixedTokens[$stackPtr]) === true
) {
$indent = "\t";
$depth = 1;
if (empty($this->changeset) === false) {
$indent .= "\t";
$depth = 2;
}

if (PHP_CODESNIFFER_VERBOSITY > 1) {
@ob_end_clean();
echo "$indent* token $stackPtr has already been modified, skipping *".PHP_EOL;
StatusWriter::forceWrite("* token $stackPtr has already been modified, skipping *", $depth);
ob_start();
}

Expand Down Expand Up @@ -565,7 +574,7 @@ public function replaceToken($stackPtr, $content)

if (PHP_CODESNIFFER_VERBOSITY > 1) {
@ob_end_clean();
echo "\t\tQ: $sniff:$line replaced token $stackPtr ($type on line $tokenLine) \"$oldContent\" => \"$newContent\"".PHP_EOL;
StatusWriter::forceWrite("Q: $sniff:$line replaced token $stackPtr ($type on line $tokenLine) \"$oldContent\" => \"$newContent\"", 2);
ob_start();
}

Expand All @@ -583,22 +592,22 @@ public function replaceToken($stackPtr, $content)
&& $this->oldTokenValues[$stackPtr]['loop'] === ($this->loops - 1)
) {
if (PHP_CODESNIFFER_VERBOSITY > 1) {
$indent = "\t";
$depth = 1;
if (empty($this->changeset) === false) {
$indent .= "\t";
$depth = 2;
}

$loop = $this->oldTokenValues[$stackPtr]['loop'];

@ob_end_clean();
echo "$indent**** $sniff:$line has possible conflict with another sniff on loop $loop; caused by the following change ****".PHP_EOL;
echo "$indent**** replaced token $stackPtr ($type on line $tokenLine) \"$oldContent\" => \"$newContent\" ****".PHP_EOL;
StatusWriter::forceWrite("**** $sniff:$line has possible conflict with another sniff on loop $loop; caused by the following change ****", $depth);
StatusWriter::forceWrite("**** replaced token $stackPtr ($type on line $tokenLine) \"$oldContent\" => \"$newContent\" ****", $depth);
}

if ($this->oldTokenValues[$stackPtr]['loop'] >= ($this->loops - 1)) {
$this->inConflict = true;
if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo "$indent**** ignoring all changes until next loop ****".PHP_EOL;
StatusWriter::forceWrite('**** ignoring all changes until next loop ****', $depth);
}
}

Expand All @@ -619,16 +628,18 @@ public function replaceToken($stackPtr, $content)
$this->numFixes++;

if (PHP_CODESNIFFER_VERBOSITY > 1) {
$indent = "\t";
$statusMessage = "$sniff:$line replaced token $stackPtr ($type on line $tokenLine) \"$oldContent\" => \"$newContent\"";
$depth = 1;
if (empty($this->changeset) === false) {
$indent .= "\tA: ";
$statusMessage = 'A: '.$statusMessage;
$depth = 2;
}

if (ob_get_level() > 0) {
ob_end_clean();
}

echo "$indent$sniff:$line replaced token $stackPtr ($type on line $tokenLine) \"$oldContent\" => \"$newContent\"".PHP_EOL;
StatusWriter::forceWrite($statusMessage, $depth);
ob_start();
}

Expand Down Expand Up @@ -680,13 +691,15 @@ public function revertToken($stackPtr)
$this->numFixes--;

if (PHP_CODESNIFFER_VERBOSITY > 1) {
$indent = "\t";
$statusMessage = "$sniff:$line reverted token $stackPtr ($type on line $tokenLine) \"$oldContent\" => \"$newContent\"";
$depth = 1;
if (empty($this->changeset) === false) {
$indent .= "\tR: ";
$statusMessage = 'R: '.$statusMessage;
$depth = 2;
}

@ob_end_clean();
echo "$indent$sniff:$line reverted token $stackPtr ($type on line $tokenLine) \"$oldContent\" => \"$newContent\"".PHP_EOL;
StatusWriter::forceWrite($statusMessage, $depth);
ob_start();
}

Expand Down
Loading
Loading