Skip to content

Commit bb42c9f

Browse files
committed
Show instructions to coding agent about how to handle errors
1 parent d64ce37 commit bb42c9f

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/Command/AnalyseCommand.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use PHPStan\Fixable\FileChangedException;
2424
use PHPStan\Fixable\MergeConflictException;
2525
use PHPStan\Fixable\Patcher;
26+
use PHPStan\Internal\AgentDetector;
2627
use PHPStan\Internal\BytesHelper;
2728
use PHPStan\Internal\DirectoryCreator;
2829
use PHPStan\Internal\DirectoryCreatorException;
@@ -578,6 +579,24 @@ protected function execute(InputInterface $input, OutputInterface $output): int
578579
$exitCode = 0;
579580
}
580581
} else {
582+
if (AgentDetector::isRunningInAgent() && count($analysisResult->getFileSpecificErrors()) > 0) {
583+
$errorOutput->writeLineFormatted('Instructions for interpreting errors');
584+
$errorOutput->writeLineFormatted('---------');
585+
$errorOutput->writeLineFormatted('');
586+
$errorOutput->writeLineFormatted('Each error has an associated identifier, like `argument.type`');
587+
$errorOutput->writeLineFormatted('or `return.missing`.');
588+
$errorOutput->writeLineFormatted('');
589+
$errorOutput->writeLineFormatted('Each error identifier has documentation at URL https://phpstan.org/error-identifiers/<identifier>.');
590+
$errorOutput->writeLineFormatted('This page contains code example, explanation why is this an error');
591+
$errorOutput->writeLineFormatted('and instruction how to fix it.');
592+
$errorOutput->writeLineFormatted('Before fixing the error, fetch the documentation page for its identifier.');
593+
$errorOutput->writeLineFormatted('');
594+
$errorOutput->writeLineFormatted('The error usually indicates a real bug or incorrect type in the code. Fix the underlying cause, do not just make the error go away.');
595+
$errorOutput->writeLineFormatted('Do not add `@phpstan-ignore` comments, `@phpstan-ignore-next-line` comments, or baseline entries to suppress the error.');
596+
$errorOutput->writeLineFormatted('Do not use assert() or inline @var PHPDoc tag to override PHPStan\'s inferred type.');
597+
$errorOutput->writeLineFormatted('Do not add type casts just to silence errors.');
598+
$errorOutput->writeLineFormatted('Do not widen parameter or return types just to make the error go away.');
599+
}
581600
$exitCode = $errorFormatter->formatErrors($analysisResult, $inceptionResult->getStdOutput());
582601
}
583602

src/Command/ErrorFormatter/RawErrorFormatter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PHPStan\Command\AnalysisResult;
66
use PHPStan\Command\Output;
77
use PHPStan\DependencyInjection\AutowiredService;
8+
use PHPStan\Internal\AgentDetector;
89
use function sprintf;
910

1011
#[AutowiredService(name: 'errorFormatter.raw')]
@@ -21,7 +22,7 @@ public function formatErrors(
2122
$output->writeLineFormatted('');
2223
}
2324

24-
$outputIdentifiers = $output->isVerbose();
25+
$outputIdentifiers = $output->isVerbose() || AgentDetector::isRunningInAgent();
2526
foreach ($analysisResult->getFileSpecificErrors() as $fileSpecificError) {
2627
$identifier = '';
2728
if ($outputIdentifiers && $fileSpecificError->getIdentifier() !== null) {

0 commit comments

Comments
 (0)