Skip to content

Commit 65b7945

Browse files
authored
Merge branch refs/heads/2.1.x into 2.2.x
2 parents ed4f0c2 + 430c598 commit 65b7945

File tree

2 files changed

+63
-33
lines changed

2 files changed

+63
-33
lines changed

src/Internal/AgentDetector.php

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,51 +4,40 @@
44

55
use function file_exists;
66
use function getenv;
7-
use function is_string;
87
use function trim;
98

109
final class AgentDetector
1110
{
1211

12+
public const ENV_VARS = [
13+
'AI_AGENT',
14+
'CURSOR_TRACE_ID',
15+
'CURSOR_AGENT',
16+
'GEMINI_CLI',
17+
'CODEX_SANDBOX',
18+
'AUGMENT_AGENT',
19+
'OPENCODE_CLIENT',
20+
'OPENCODE',
21+
'CLAUDECODE',
22+
'CLAUDE_CODE',
23+
'REPL_ID',
24+
];
25+
1326
public static function isRunningInAgent(): bool
1427
{
1528
// Copyright (c) Pushpak Chhajed pushpak1300@gmail.com
1629
// from https://github.com/shipfastlabs/agent-detector/blob/98766473b2dfe183b0c2605ceb04e587a78d1872/src/AgentDetector.php
1730

18-
$aiAgent = getenv('AI_AGENT');
19-
if (is_string($aiAgent) && trim($aiAgent) !== '') {
20-
return true;
21-
}
22-
23-
if (getenv('CURSOR_TRACE_ID') !== false) {
24-
return true;
25-
}
26-
27-
if (getenv('CURSOR_AGENT') !== false) {
28-
return true;
29-
}
30-
31-
if (getenv('GEMINI_CLI') !== false) {
32-
return true;
33-
}
31+
foreach (self::ENV_VARS as $envVar) {
32+
$value = getenv($envVar);
33+
if ($value === false) {
34+
continue;
35+
}
3436

35-
if (getenv('CODEX_SANDBOX') !== false) {
36-
return true;
37-
}
38-
39-
if (getenv('AUGMENT_AGENT') !== false) {
40-
return true;
41-
}
42-
43-
if (getenv('OPENCODE_CLIENT') !== false || getenv('OPENCODE') !== false) {
44-
return true;
45-
}
46-
47-
if (getenv('CLAUDECODE') !== false || getenv('CLAUDE_CODE') !== false) {
48-
return true;
49-
}
37+
if ($envVar === 'AI_AGENT' && trim($value) === '') {
38+
continue;
39+
}
5040

51-
if (getenv('REPL_ID') !== false) {
5241
return true;
5342
}
5443

tests/PHPStan/Command/ErrorFormatter/RawErrorFormatterTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,37 @@
22

33
namespace PHPStan\Command\ErrorFormatter;
44

5+
use Override;
6+
use PHPStan\Internal\AgentDetector;
57
use PHPStan\Testing\ErrorFormatterTestCase;
68
use PHPUnit\Framework\Attributes\DataProvider;
9+
use function getenv;
10+
use function putenv;
711
use function sprintf;
812

913
class RawErrorFormatterTest extends ErrorFormatterTestCase
1014
{
1115

16+
/** @var array<string, string|false> */
17+
private array $originalEnvVars = [];
18+
19+
#[Override]
20+
protected function setUp(): void
21+
{
22+
foreach (AgentDetector::ENV_VARS as $var) {
23+
$this->originalEnvVars[$var] = getenv($var);
24+
putenv($var);
25+
}
26+
}
27+
28+
#[Override]
29+
protected function tearDown(): void
30+
{
31+
foreach (AgentDetector::ENV_VARS as $var) {
32+
putenv($var . ($this->originalEnvVars[$var] !== false ? '=' . $this->originalEnvVars[$var] : ''));
33+
}
34+
}
35+
1236
public static function dataFormatterOutputProvider(): iterable
1337
{
1438
yield [
@@ -125,4 +149,21 @@ public function testFormatErrors(
125149
$this->assertSame($expected, $this->getOutputContent(false, $verbose), sprintf('%s: output do not match', $message));
126150
}
127151

152+
public function testFormatErrorsInAgent(): void
153+
{
154+
putenv('AI_AGENT=1');
155+
156+
$formatter = new RawErrorFormatter();
157+
158+
$this->assertSame(1, $formatter->formatErrors(
159+
$this->getAnalysisResult([5, 1], 0),
160+
$this->getOutput(false, false),
161+
));
162+
163+
$this->assertSame(
164+
'/data/folder/with space/and unicode 😃/project/foo.php:5:Foobar\Buz [identifier=foobar.buz]' . "\n",
165+
$this->getOutputContent(false, false),
166+
);
167+
}
168+
128169
}

0 commit comments

Comments
 (0)