Skip to content

Commit 477db09

Browse files
ondrejmirtesclaude
andcommitted
Trust PHPStan's own types when running from a source checkout too
The trusted-types optimizer pass only armed for a phar, so `bin/phpstan` of a checkout — `make phpstan` included — kept every argument and return check. The prefix is now the checkout root when there is no phar; --debug still keeps the checks and PHPUnit never gets here. `make phpstan` user CPU: 119.3s → 116.4s (−2.4%, 8/8 alternating pairs, t=7.3), output identical. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SRLigetKoiZzYBe4iRQb3J
1 parent a6144e2 commit 477db09

3 files changed

Lines changed: 19 additions & 18 deletions

File tree

‎src/Turbo/TurboDiagnoseExtension.php‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace PHPStan\Turbo;
44

5-
use Phar;
65
use PHPStan\Command\Output;
76
use PHPStan\DependencyInjection\AutowiredService;
87
use PHPStan\Diagnose\DiagnoseExtension;
@@ -80,9 +79,6 @@ private function describeTrustedTypes(): string
8079
if (!TurboExtensionEnabler::isActive()) {
8180
return 'off (extension inactive)';
8281
}
83-
if (Phar::running(false) === '') {
84-
return 'off (not running from a phar)';
85-
}
8682

8783
return 'off (--debug, or OPcache is not active)';
8884
}

‎src/Turbo/TurboExtensionEnabler.php‎

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use function is_file;
1313
use function json_decode;
1414
use function phpversion;
15+
use const DIRECTORY_SEPARATOR;
1516

1617
final class TurboExtensionEnabler
1718
{
@@ -177,12 +178,13 @@ public static function isTrustingOwnTypes(): bool
177178
* the engine's run-time checks of its parameter and return types re-check
178179
* what analysis already proved — at about 8% of the analysis CPU: a
179180
* class-typed parameter costs a class lookup and an instanceof on every
180-
* call, a typed return the same on the way out. With the extension active
181-
* and PHPStan running from a phar, its optimizer pass (TrustedTypes.cpp)
182-
* drops those checks from the code compiled out of the phar. Nothing else
183-
* is touched: extensions, bootstrap files and the analysed project keep
184-
* their checks, including on what they receive from PHPStan and return to
185-
* it — a check sits in the callee.
181+
* call, a typed return the same on the way out. With the extension
182+
* active, its optimizer pass (TrustedTypes.cpp) drops those checks from
183+
* the code compiled out of the running phar — or out of the source
184+
* checkout bin/phpstan runs from. Nothing else is touched: extensions,
185+
* bootstrap files and the analysed project keep their checks, including
186+
* on what they receive from PHPStan and return to it — a check sits in
187+
* the callee.
186188
*
187189
* What is lost is the TypeError at the boundary when such code passes a
188190
* wrong value into PHPStan: it surfaces later, deeper. That is why --debug
@@ -204,15 +206,17 @@ public static function trustOwnTypesIfSuitable(array $argv): void
204206
if (in_array('--debug', $argv, true)) {
205207
return;
206208
}
207-
if (!class_exists('Phar', false)) {
208-
return;
209-
}
210-
$pharPath = Phar::running(false);
211-
if ($pharPath === '') {
212-
return;
209+
$pharPath = class_exists('Phar', false) ? Phar::running(false) : '';
210+
if ($pharPath !== '') {
211+
$prefix = 'phar://' . $pharPath . '/';
212+
} else {
213+
// bin/phpstan of a source checkout: src/, vendor/ and build/ of the
214+
// checkout, the same code the phar would hold (compiled filenames
215+
// are resolved paths, as __DIR__ is)
216+
$prefix = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR;
213217
}
214218

215-
self::$trustingOwnTypes = Runtime::trustTypesUnder('phar://' . $pharPath . '/');
219+
self::$trustingOwnTypes = Runtime::trustTypesUnder($prefix);
216220
}
217221

218222
}

‎turbo-ext/README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ that does not exec() must `_exit()` instead.
9292
own code runs rather than replacing it: it arms an opcache optimizer pass
9393
(`TrustedTypes.cpp`) that drops the engine's argument and return type checks
9494
from scripts compiled under the given prefix — the running phar, passed by
95-
`TurboExtensionEnabler::trustOwnTypesIfSuitable()`. PHPStan's code is verified
95+
`TurboExtensionEnabler::trustOwnTypesIfSuitable()` — the running phar, or the
96+
source checkout `bin/phpstan` runs from. PHPStan's code is verified
9697
by PHPStan, so those checks re-check what analysis proved, at about 8% of the
9798
analysis CPU. Nothing outside the prefix is touched, and a check sits in the
9899
callee, so extensions and bootstrapped code keep checking what PHPStan hands

0 commit comments

Comments
 (0)