Skip to content

Fixer: remove output buffering #1052

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 1 commit into from
Apr 29, 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
2 changes: 0 additions & 2 deletions src/Files/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -1047,9 +1047,7 @@ protected function addMessage($error, $message, $line, $column, $code, $data, $s
&& $this->fixer->enabled === true
&& $fixable === true
) {
@ob_end_clean();
StatusWriter::forceWrite("E: [Line $line] $message ($sniffCode)", 1);
ob_start();
}

return true;
Expand Down
34 changes: 0 additions & 34 deletions src/Fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,10 @@ public function fixFile()

$this->loops = 0;
while ($this->loops < 50) {
ob_start();

// Only needed once file content has changed.
$contents = $this->getContents();

if (PHP_CODESNIFFER_VERBOSITY > 2) {
@ob_end_clean();
StatusWriter::forceWrite('---START FILE CONTENT---');
$lines = explode($this->currentFile->eolChar, $contents);
$max = strlen(count($lines));
Expand All @@ -171,14 +168,12 @@ public function fixFile()
}

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

$this->inConflict = false;
$this->currentFile->ruleset->populateTokenListeners();
$this->currentFile->setContent($contents);
$this->currentFile->process();
ob_end_clean();

$this->loops++;

Expand Down Expand Up @@ -212,12 +207,7 @@ public function fixFile()

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

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

return false;
Expand Down Expand Up @@ -417,9 +407,7 @@ public function beginChangeset()

$line = $bt[0]['line'];

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

$this->changeset = [];
Expand Down Expand Up @@ -459,15 +447,11 @@ public function endChangeset()
}

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

$this->changeset = [];
Expand Down Expand Up @@ -501,10 +485,8 @@ public function rollbackChangeset()

$numChanges = count($this->changeset);

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

$this->changeset = [];
Expand Down Expand Up @@ -536,9 +518,7 @@ public function replaceToken($stackPtr, $content)
}

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

return false;
Expand Down Expand Up @@ -573,9 +553,7 @@ public function replaceToken($stackPtr, $content)
$this->changeset[$stackPtr] = $content;

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

return true;
Expand All @@ -599,7 +577,6 @@ public function replaceToken($stackPtr, $content)

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

@ob_end_clean();
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);
}
Expand All @@ -611,10 +588,6 @@ public function replaceToken($stackPtr, $content)
}
}

if (PHP_CODESNIFFER_VERBOSITY > 1) {
ob_start();
}

return false;
}//end if

Expand All @@ -635,12 +608,7 @@ public function replaceToken($stackPtr, $content)
$depth = 2;
}

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

StatusWriter::forceWrite($statusMessage, $depth);
ob_start();
}

return true;
Expand Down Expand Up @@ -698,9 +666,7 @@ public function revertToken($stackPtr)
$depth = 2;
}

@ob_end_clean();
StatusWriter::forceWrite($statusMessage, $depth);
ob_start();
}

return true;
Expand Down
5 changes: 0 additions & 5 deletions src/Reports/Cbf.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public function generateFileReport($report, File $phpcsFile, $showSources=false,
$errors = $phpcsFile->getFixableCount();
if ($errors !== 0) {
if (PHP_CODESNIFFER_VERBOSITY > 0) {
ob_end_clean();
$startTime = microtime(true);
$newlines = 0;
if (PHP_CODESNIFFER_VERBOSITY > 1) {
Expand Down Expand Up @@ -105,10 +104,6 @@ public function generateFileReport($report, File $phpcsFile, $showSources=false,
}
}

if (PHP_CODESNIFFER_VERBOSITY > 0) {
ob_start();
}

$errorCount = $phpcsFile->getErrorCount();
$warningCount = $phpcsFile->getWarningCount();
$fixableCount = $phpcsFile->getFixableCount();
Expand Down
2 changes: 0 additions & 2 deletions src/Reports/Diff.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,13 @@ public function generateFileReport($report, File $phpcsFile, $showSources=false,
}//end if

if (PHP_CODESNIFFER_VERBOSITY > 1) {
ob_end_clean();
StatusWriter::write('*** START FILE FIXING ***', 1);
}

$fixed = $phpcsFile->fixer->fixFile();

if (PHP_CODESNIFFER_VERBOSITY > 1) {
StatusWriter::write('*** END FILE FIXING ***', 1);
ob_start();
}

if ($fixed === false) {
Expand Down