Skip to content

Commit d4f33db

Browse files
committed
All status, debug, and progress output is now sent to STDERR instead of STDOUT
Outputing buffering during fixing could be removed due to this. Timing info is now printed at the end of every run, although it goes to STDERR.
1 parent f5bcb21 commit d4f33db

File tree

21 files changed

+563
-627
lines changed

21 files changed

+563
-627
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ The file documents changes to the PHP_CodeSniffer project.
1313
- If you want to detect parse errors, use a linter instead
1414
- Changed the error code `Squiz.Classes.ValidClassName.NotCamelCaps` to `Squiz.Classes.ValidClassName.NotPascalCase`
1515
- This reflects that the sniff is actually checking for `ClassName` and not `className`
16+
- All status, debug, and progress output is now sent to STDERR instead of STDOUT
17+
- Only report output now goes through STDOUT
18+
- Piping output to a file will now only include report output
19+
- Pipe both STDERR and STDOUT to the same file to capture the entire output of the run
20+
- The `--report-file` functionality remains untouched
1621
- Composer installs no longer include any test files
1722

1823
### Removed

src/Files/File.php

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
use PHP_CodeSniffer\Ruleset;
1313
use PHP_CodeSniffer\Config;
1414
use PHP_CodeSniffer\Fixer;
15-
use PHP_CodeSniffer\Util;
15+
use PHP_CodeSniffer\Util\Common;
16+
use PHP_CodeSniffer\Util\Tokens;
1617
use PHP_CodeSniffer\Exceptions\RuntimeException;
1718
use PHP_CodeSniffer\Exceptions\TokenizerException;
1819
use PHP_CodeSniffer\Tokenizers\PHP;
@@ -259,7 +260,7 @@ public function setContent($content)
259260
$this->tokens = [];
260261

261262
try {
262-
$this->eolChar = Util\Common::detectLineEndings($content);
263+
$this->eolChar = Common::detectLineEndings($content);
263264
} catch (RuntimeException $e) {
264265
$this->addWarningOnLine($e->getMessage(), 1, 'Internal.DetectLineEndings');
265266
return;
@@ -321,7 +322,7 @@ public function process()
321322
$this->fixer->startFile($this);
322323

323324
if (PHP_CODESNIFFER_VERBOSITY > 2) {
324-
echo "\t*** START TOKEN PROCESSING ***".PHP_EOL;
325+
Common::printStatusMessage('*** START TOKEN PROCESSING ***', 1);
325326
}
326327

327328
$foundCode = false;
@@ -370,8 +371,8 @@ public function process()
370371

371372
if (PHP_CODESNIFFER_VERBOSITY > 2) {
372373
$type = $token['type'];
373-
$content = Util\Common::prepareForOutput($token['content']);
374-
echo "\t\tProcess token $stackPtr: $type => $content".PHP_EOL;
374+
$content = Common::prepareForOutput($token['content']);
375+
Common::printStatusMessage("Process token $stackPtr: $type => $content", 2);
375376
}
376377

377378
if ($token['code'] !== T_INLINE_HTML) {
@@ -440,7 +441,7 @@ public function process()
440441

441442
if (PHP_CODESNIFFER_VERBOSITY > 2) {
442443
$startTime = microtime(true);
443-
echo "\t\t\tProcessing ".$this->activeListener.'... ';
444+
Common::printStatusMessage('Processing '.$this->activeListener.'... ', 3, true);
444445
}
445446

446447
$ignoreTo = $this->ruleset->sniffs[$class]->process($this, $stackPtr);
@@ -457,7 +458,7 @@ public function process()
457458
$this->listenerTimes[$this->activeListener] += $timeTaken;
458459

459460
$timeTaken = round(($timeTaken), 4);
460-
echo "DONE in $timeTaken seconds".PHP_EOL;
461+
Common::printStatusMessage("DONE in $timeTaken seconds");
461462
}
462463

463464
$this->activeListener = '';
@@ -479,16 +480,16 @@ public function process()
479480
}
480481

481482
if (PHP_CODESNIFFER_VERBOSITY > 2) {
482-
echo "\t*** END TOKEN PROCESSING ***".PHP_EOL;
483-
echo "\t*** START SNIFF PROCESSING REPORT ***".PHP_EOL;
483+
Common::printStatusMessage("*** END TOKEN PROCESSING ***", 1);
484+
Common::printStatusMessage("*** START SNIFF PROCESSING REPORT ***", 1);
484485

485486
asort($this->listenerTimes, SORT_NUMERIC);
486487
$this->listenerTimes = array_reverse($this->listenerTimes, true);
487488
foreach ($this->listenerTimes as $listener => $timeTaken) {
488-
echo "\t$listener: ".round(($timeTaken), 4).' secs'.PHP_EOL;
489+
Common::printStatusMessage("$listener: ".round(($timeTaken), 4).' secs', 1);
489490
}
490491

491-
echo "\t*** END SNIFF PROCESSING REPORT ***".PHP_EOL;
492+
Common::printStatusMessage("*** END SNIFF PROCESSING REPORT ***", 1);
492493
}
493494

494495
$this->fixedCount += $this->fixer->getFixCount();
@@ -515,9 +516,9 @@ public function parse()
515516
$this->ignored = true;
516517
$this->addWarning($e->getMessage(), null, 'Internal.Tokenizer.Exception');
517518
if (PHP_CODESNIFFER_VERBOSITY > 0) {
518-
echo "[tokenizer error]... ";
519+
Common::printStatusMessage('[tokenizer error]... ', 0, true);
519520
if (PHP_CODESNIFFER_VERBOSITY > 1) {
520-
echo PHP_EOL;
521+
Common::printStatusMessage(PHP_EOL, 0, true);
521522
}
522523
}
523524

@@ -547,9 +548,9 @@ public function parse()
547548
$numLines = $this->tokens[($this->numTokens - 1)]['line'];
548549
}
549550

550-
echo "[$this->numTokens tokens in $numLines lines]... ";
551+
Common::printStatusMessage("[$this->numTokens tokens in $numLines lines]... ", 0, true);
551552
if (PHP_CODESNIFFER_VERBOSITY > 1) {
552-
echo PHP_EOL;
553+
Common::printStatusMessage(PHP_EOL, 0, true);
553554
}
554555
}
555556

@@ -791,7 +792,7 @@ protected function addMessage($error, $message, $line, $column, $code, $data, $s
791792
$parts = explode('.', $code);
792793
if ($parts[0] === 'Internal') {
793794
// An internal message.
794-
$listenerCode = Util\Common::getSniffCode($this->activeListener);
795+
$listenerCode = Common::getSniffCode($this->activeListener);
795796
$sniffCode = $code;
796797
$checkCodes = [$sniffCode];
797798
} else {
@@ -800,7 +801,7 @@ protected function addMessage($error, $message, $line, $column, $code, $data, $s
800801
$sniffCode = $code;
801802
$listenerCode = substr($sniffCode, 0, strrpos($sniffCode, '.'));
802803
} else {
803-
$listenerCode = Util\Common::getSniffCode($this->activeListener);
804+
$listenerCode = Common::getSniffCode($this->activeListener);
804805
$sniffCode = $listenerCode.'.'.$code;
805806
$parts = explode('.', $sniffCode);
806807
}
@@ -1019,9 +1020,7 @@ protected function addMessage($error, $message, $line, $column, $code, $data, $s
10191020
&& $this->fixer->enabled === true
10201021
&& $fixable === true
10211022
) {
1022-
@ob_end_clean();
1023-
echo "\tE: [Line $line] $message ($sniffCode)".PHP_EOL;
1024-
ob_start();
1023+
Common::forcePrintStatusMessage("E: [Line $line] $message ($sniffCode)", 1);
10251024
}
10261025

10271026
return true;
@@ -1459,7 +1458,7 @@ public function getMethodParameters($stackPtr)
14591458
$paramCount++;
14601459
break;
14611460
case T_EQUAL:
1462-
$defaultStart = $this->findNext(Util\Tokens::$emptyTokens, ($i + 1), null, true);
1461+
$defaultStart = $this->findNext(Tokens::$emptyTokens, ($i + 1), null, true);
14631462
$equalToken = $i;
14641463
break;
14651464
}//end switch
@@ -1709,7 +1708,7 @@ public function getMemberProperties($stackPtr)
17091708
T_VAR => T_VAR,
17101709
];
17111710

1712-
$valid += Util\Tokens::$emptyTokens;
1711+
$valid += Tokens::$emptyTokens;
17131712

17141713
$scope = 'public';
17151714
$scopeSpecified = false;
@@ -1877,7 +1876,7 @@ public function isReference($stackPtr)
18771876
}
18781877

18791878
$tokenBefore = $this->findPrevious(
1880-
Util\Tokens::$emptyTokens,
1879+
Tokens::$emptyTokens,
18811880
($stackPtr - 1),
18821881
null,
18831882
true
@@ -1900,14 +1899,14 @@ public function isReference($stackPtr)
19001899
return true;
19011900
}
19021901

1903-
if (isset(Util\Tokens::$assignmentTokens[$this->tokens[$tokenBefore]['code']]) === true) {
1902+
if (isset(Tokens::$assignmentTokens[$this->tokens[$tokenBefore]['code']]) === true) {
19041903
// This is directly after an assignment. It's a reference. Even if
19051904
// it is part of an operation, the other tests will handle it.
19061905
return true;
19071906
}
19081907

19091908
$tokenAfter = $this->findNext(
1910-
Util\Tokens::$emptyTokens,
1909+
Tokens::$emptyTokens,
19111910
($stackPtr + 1),
19121911
null,
19131912
true
@@ -1930,7 +1929,7 @@ public function isReference($stackPtr)
19301929
$varToken = $tokenAfter;
19311930
if ($param['variable_length'] === true) {
19321931
$varToken = $this->findNext(
1933-
(Util\Tokens::$emptyTokens + [T_ELLIPSIS]),
1932+
(Tokens::$emptyTokens + [T_ELLIPSIS]),
19341933
($stackPtr + 1),
19351934
null,
19361935
true
@@ -1969,7 +1968,7 @@ public function isReference($stackPtr)
19691968
if ($this->tokens[$tokenAfter]['code'] === T_VARIABLE) {
19701969
return true;
19711970
} else {
1972-
$skip = Util\Tokens::$emptyTokens;
1971+
$skip = Tokens::$emptyTokens;
19731972
$skip[] = T_NS_SEPARATOR;
19741973
$skip[] = T_SELF;
19751974
$skip[] = T_PARENT;
@@ -2196,7 +2195,7 @@ public function findNext(
21962195
*/
21972196
public function findStartOfStatement($start, $ignore=null)
21982197
{
2199-
$endTokens = Util\Tokens::$blockOpeners;
2198+
$endTokens = Tokens::$blockOpeners;
22002199

22012200
$endTokens[T_COLON] = true;
22022201
$endTokens[T_COMMA] = true;
@@ -2240,7 +2239,7 @@ public function findStartOfStatement($start, $ignore=null)
22402239
$i = $this->tokens[$i]['parenthesis_opener'];
22412240
}
22422241

2243-
if (isset(Util\Tokens::$emptyTokens[$this->tokens[$i]['code']]) === false) {
2242+
if (isset(Tokens::$emptyTokens[$this->tokens[$i]['code']]) === false) {
22442243
$lastNotEmpty = $i;
22452244
}
22462245
}//end for
@@ -2308,7 +2307,7 @@ public function findEndOfStatement($start, $ignore=null)
23082307
continue;
23092308
}
23102309

2311-
if ($i === $start && isset(Util\Tokens::$scopeOpeners[$this->tokens[$i]['code']]) === true) {
2310+
if ($i === $start && isset(Tokens::$scopeOpeners[$this->tokens[$i]['code']]) === true) {
23122311
return $this->tokens[$i]['scope_closer'];
23132312
}
23142313

@@ -2328,7 +2327,7 @@ public function findEndOfStatement($start, $ignore=null)
23282327
}
23292328
}//end if
23302329

2331-
if (isset(Util\Tokens::$emptyTokens[$this->tokens[$i]['code']]) === false) {
2330+
if (isset(Tokens::$emptyTokens[$this->tokens[$i]['code']]) === false) {
23322331
$lastNotEmpty = $i;
23332332
}
23342333
}//end for

src/Files/LocalFile.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PHP_CodeSniffer\Ruleset;
1313
use PHP_CodeSniffer\Config;
1414
use PHP_CodeSniffer\Util\Cache;
15+
use PHP_CodeSniffer\Util\Common;
1516

1617
class LocalFile extends File
1718
{
@@ -112,7 +113,7 @@ public function process()
112113
if (PHP_CODESNIFFER_VERBOSITY > 0
113114
|| (PHP_CODESNIFFER_CBF === true && empty($this->config->files) === false)
114115
) {
115-
echo "[loaded from cache]... ";
116+
Common::printStatusMessage('[loaded from cache]... ', 0, true);
116117
}
117118

118119
$this->numTokens = $cache['numTokens'];
@@ -121,7 +122,7 @@ public function process()
121122
}//end if
122123

123124
if (PHP_CODESNIFFER_VERBOSITY > 1) {
124-
echo PHP_EOL;
125+
Common::printStatusMessage(PHP_EOL, 0, true);
125126
}
126127

127128
parent::process();

0 commit comments

Comments
 (0)