Skip to content

Commit

Permalink
[DX] Avoid reporting deprecated errors
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Dec 27, 2021
1 parent 889a682 commit 71ec35f
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/Application/ApplicationFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@

final class ApplicationFileProcessor
{
/**
* @var SystemError[]
*/
private array $systemErrors = [];

/**
* @param FileProcessorInterface[] $fileProcessors
*/
Expand All @@ -39,12 +44,21 @@ public function __construct(
*/
public function run(array $files, Configuration $configuration): array
{
$this->setCustomErrorHandler();

$systemErrorsAndFileDiffs = $this->processFiles($files, $configuration);
$this->fileFormatter->format($files);

$this->fileDiffFileDecorator->decorate($files);
$this->printFiles($files, $configuration);

$this->restoreErrorHandler();

$systemErrorsAndFileDiffs['system_errors'] = array_merge(
$systemErrorsAndFileDiffs['system_errors'],
$this->systemErrors
);

return $systemErrorsAndFileDiffs;
}

Expand Down Expand Up @@ -113,4 +127,31 @@ private function printFile(File $file): void
$this->smartFileSystem->dumpFile($smartFileInfo->getPathname(), $file->getFileContent());
$this->smartFileSystem->chmod($smartFileInfo->getRealPath(), $smartFileInfo->getPerms());
}

/**
* Inspired by @see https://github.com/phpstan/phpstan-src/blob/89af4e7db257750cdee5d4259ad312941b6b25e8/src/Analyser/Analyser.php#L134
*/
private function setCustomErrorHandler(): void
{
set_error_handler(function (int $errno, string $errstr, string $errfile, int $errline): bool {
if (error_reporting() === 0) {
// silence @ operator
return true;
}

// not relevant for us
if ($errno === E_DEPRECATED) {
return true;
}

$this->systemErrors[] = new SystemError($errstr, $errfile, $errline);

return true;
});
}

private function restoreErrorHandler(): void
{
restore_error_handler();
}
}

0 comments on commit 71ec35f

Please sign in to comment.