Skip to content

Commit 9199c4a

Browse files
ondrejmirtesclaude
andcommitted
Merge remote-tracking branch 'origin/2.2.x' into 2.3.x
The ternary exact-context narrowing (#6580) is ported onto the composed (cond && if) || (!cond && else) narrowing: in an exact context the arms become `arm === true` / `arm !== false` (...) operands of a truthy decomposition, narrowed like the comparison node would be, with a native mirror in turbo-ext/src/TernaryHandler.cpp. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LuUxEfBoMtVfZmJsUxA85s
2 parents 17972fe + fadf8d6 commit 9199c4a

14 files changed

Lines changed: 496 additions & 44 deletions

File tree

‎.github/workflows/e2e-tests.yml‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,19 @@ jobs:
186186
OUTPUT=$(../../bin/phpstan -vvv 2>&1)
187187
echo "$OUTPUT"
188188
../bashunit -a contains 'Result cache restored.' "$OUTPUT"
189+
- script: |
190+
cd e2e/result-cache-worker-count
191+
OUTPUT=$(../../bin/phpstan -vvv 2>&1)
192+
echo "$OUTPUT"
193+
../bashunit -a contains 'largest of 2' "$OUTPUT"
194+
# Only some files are analysed again, so the result is rebuilt from the cache
195+
# and must still report the peak memory of the workers.
196+
echo '// changed' >> src/Foo.php
197+
echo '// changed' >> src/Bar.php
198+
OUTPUT=$(../../bin/phpstan -vvv 2>&1)
199+
echo "$OUTPUT"
200+
../bashunit -a contains 'Result cache restored. 2 files will be reanalysed.' "$OUTPUT"
201+
../bashunit -a contains 'largest of 2' "$OUTPUT"
189202
- script: |
190203
cd e2e/result-cache-stale-objects
191204
OUTPUT=$(../bashunit -a exit_code "1" "../../bin/phpstan -vvv")

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ e2e/bashunit
2222
/e2e/result-cache-moved-tmpdir/tmp-b
2323
/e2e/result-cache-stale-objects/tmp
2424
/e2e/result-cache-truncated/tmp
25+
/e2e/result-cache-worker-count/tmp
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
parameters:
2+
level: 0
3+
tmpDir: tmp
4+
paths:
5+
- src
6+
parallel:
7+
jobSize: 1
8+
maximumNumberOfProcesses: 2
9+
minimumNumberOfJobsPerProcess: 1
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace TestResultCacheWorkerCount;
4+
5+
class Bar
6+
{
7+
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace TestResultCacheWorkerCount;
4+
5+
class Baz
6+
{
7+
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace TestResultCacheWorkerCount;
4+
5+
class Foo
6+
{
7+
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace TestResultCacheWorkerCount;
4+
5+
class Qux
6+
{
7+
8+
}

‎src/Analyser/ExprHandler/TernaryHandler.php‎

Lines changed: 105 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,25 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
209209
return $this->defaultNarrowingHelper->specifyDefaultTypes($expr, $context);
210210
}
211211

212+
// An exact context (`=== true`, `!== false`, ...) asks about the taken
213+
// arm's value, not its truthiness - `0 !== false` holds although `0` is
214+
// falsey. The decomposition then holds for the arms compared to the
215+
// constant (`(cond && if !== false) || ...`) being true, the condition
216+
// still read by truthiness.
217+
$armContext = null;
218+
if ($context !== TypeSpecifierContext::createTruthy() && $context !== TypeSpecifierContext::createFalsey()) {
219+
// `=== false` / `!== true` of an arm are false when the arm is
220+
// true: a disjunction arm would be split as if it were negated
221+
if (
222+
!$context->true()
223+
&& (self::isDisjunction($expr->if ?? $expr->cond) || self::isDisjunction($expr->else))
224+
) {
225+
return $this->defaultNarrowingHelper->specifyDefaultTypes($expr, $context);
226+
}
227+
$armContext = $context;
228+
$context = TypeSpecifierContext::createTruthy();
229+
}
230+
212231
// cond ? if : else narrows like (cond && if) || (!cond && else),
213232
// composed from the walk's results through the boolean helpers -
214233
// the fabricated nodes are only printed into holder keys
@@ -240,8 +259,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
240259

241260
return new BooleanType();
242261
};
243-
$elseTypes = static fn (MutatingScope $scope, TypeSpecifierContext $ctx): SpecifiedTypes => $elseResult->getSpecifiedTypesForScope($scope, $ctx);
244-
$elseType = static fn (bool $nativeTypesPromoted): Type => $elseResult->getTypeOnScope($elseProcessingScope, $nativeTypesPromoted);
262+
[$elseTypes, $elseType, $elseTruthyScope, $elseFalseyScope] = $this->createArmOperand($expr->else, $elseResult, $elseProcessingScope, $elseResult->getScope(), $armContext);
245263

246264
// the decomposition's branch scopes are the operand walks' own
247265
// memoized branch scopes (the evaluation points), not ask-derived;
@@ -251,7 +269,6 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
251269

252270
// right disjunct: !cond && else
253271
$bNode = new BooleanAnd($notCondNode, $expr->else);
254-
$elseFalseyOnCondFalseyScope = static fn (): MutatingScope => $elseResult->getFalseyScope();
255272
$bTypes = fn (MutatingScope $scope, TypeSpecifierContext $ctx): SpecifiedTypes => $this->booleanNarrowingHelper->specifyConjunction(
256273
$nodeScopeResolver,
257274
$scope,
@@ -263,17 +280,23 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
263280
$condTruthyScope,
264281
$expr->else,
265282
$elseTypes,
266-
$elseFalseyOnCondFalseyScope,
283+
$elseFalseyScope,
267284
);
268285
$bType = $andVerdict($notCondType, $elseType);
269-
$bTruthyScope = static fn (): MutatingScope => $elseResult->getTruthyScope();
270286

271-
if ($ifResult !== null && $expr->if !== null) {
287+
// the short ternary's truthy value is the condition itself - in an
288+
// exact context it is compared like an arm: cond && (cond === true)
289+
$shortTernaryArm = $armContext !== null && ($ifResult === null || $expr->if === null);
290+
if (($ifResult !== null && $expr->if !== null) || $shortTernaryArm) {
272291
// left disjunct: cond && if
273-
$aNode = new BooleanAnd($expr->cond, $expr->if);
274-
$ifTypes = static fn (MutatingScope $scope, TypeSpecifierContext $ctx): SpecifiedTypes => $ifResult->getSpecifiedTypesForScope($scope, $ctx);
275-
$ifType = static fn (bool $nativeTypesPromoted): Type => $ifResult->getTypeOnScope($ifProcessingScope, $nativeTypesPromoted);
276-
$ifFalseyOnCondTruthyScope = static fn (): MutatingScope => $ifResult->getFalseyScope();
292+
if ($ifResult !== null && $expr->if !== null) {
293+
$ifExpr = $expr->if;
294+
[$ifTypes, $ifType, $ifTruthyScope, $ifFalseyScope] = $this->createArmOperand($expr->if, $ifResult, $ifProcessingScope, $ifResult->getScope(), $armContext);
295+
} else {
296+
$ifExpr = $expr->cond;
297+
[$ifTypes, $ifType, $ifTruthyScope, $ifFalseyScope] = $this->createArmOperand($expr->cond, $ternaryCondResult, $ifProcessingScope, $ifProcessingScope, $armContext);
298+
}
299+
$aNode = new BooleanAnd($expr->cond, $ifExpr);
277300
$aTypes = fn (MutatingScope $scope, TypeSpecifierContext $ctx): SpecifiedTypes => $this->booleanNarrowingHelper->specifyConjunction(
278301
$nodeScopeResolver,
279302
$scope,
@@ -283,12 +306,11 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
283306
$condTypes,
284307
$condTruthyScope,
285308
$condFalseyScope,
286-
$expr->if,
309+
$ifExpr,
287310
$ifTypes,
288-
$ifFalseyOnCondTruthyScope,
311+
$ifFalseyScope,
289312
);
290313
$aType = $andVerdict($condType, $ifType);
291-
$aTruthyScope = static fn (): MutatingScope => $ifResult->getTruthyScope();
292314
// the merged falsey of (cond && if) has no single walk scope -
293315
// derived from the evaluation point on first demand, reused across asks
294316
$aFalseyScopeThunk = static function () use ($scope, $aTypes, &$aFalseyScope): MutatingScope {
@@ -303,12 +325,12 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
303325
$aNode,
304326
$aTypes,
305327
$aType,
306-
$aTruthyScope,
328+
$ifTruthyScope,
307329
$aFalseyScopeThunk,
308330
$bNode,
309331
$bTypes,
310332
$bType,
311-
$bTruthyScope,
333+
$elseTruthyScope,
312334
)->setRootExpr($expr);
313335
}
314336

@@ -326,10 +348,77 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
326348
$bNode,
327349
$bTypes,
328350
$bType,
329-
$bTruthyScope,
351+
$elseTruthyScope,
330352
)->setRootExpr($expr);
331353
},
332354
);
333355
}
334356

357+
private static function isDisjunction(Expr $expr): bool
358+
{
359+
return $expr instanceof Expr\BinaryOp\BooleanOr || $expr instanceof Expr\BinaryOp\LogicalOr;
360+
}
361+
362+
/**
363+
* An arm as an operand of the (cond && if) || (!cond && else) decomposition:
364+
* its narrowing, verdict and branch scopes. With an exact context the operand
365+
* is the arm compared to the bool constant, narrowed like the comparison
366+
* node would be - the constant pinned onto the arm plus the arm's own
367+
* narrowing in the bool context - with branch scopes derived from the arm's
368+
* evaluation point.
369+
*
370+
* @return array{callable(MutatingScope, TypeSpecifierContext): SpecifiedTypes, callable(bool): Type, callable(): MutatingScope, callable(): MutatingScope}
371+
*/
372+
private function createArmOperand(Expr $armExpr, ExpressionResult $armResult, MutatingScope $processingScope, MutatingScope $evaluatedScope, ?TypeSpecifierContext $armContext): array
373+
{
374+
if ($armContext === null) {
375+
return [
376+
static fn (MutatingScope $scope, TypeSpecifierContext $ctx): SpecifiedTypes => $armResult->getSpecifiedTypesForScope($scope, $ctx),
377+
static fn (bool $nativeTypesPromoted): Type => $armResult->getTypeOnScope($processingScope, $nativeTypesPromoted),
378+
static fn (): MutatingScope => $armResult->getTruthyScope(),
379+
static fn (): MutatingScope => $armResult->getFalseyScope(),
380+
];
381+
}
382+
383+
// `!== true` / `!== false` are the mixed contexts, `=== true` / `=== false` the pure ones
384+
$isIdentical = !($armContext->truthy() && $armContext->falsey());
385+
$value = $isIdentical ? $armContext->true() : !$armContext->true();
386+
387+
$types = function (MutatingScope $scope, TypeSpecifierContext $ctx) use ($armExpr, $armResult, $isIdentical, $value): SpecifiedTypes {
388+
$identicalContext = $isIdentical ? $ctx : $ctx->negate();
389+
$types = $this->defaultNarrowingHelper->createSubjectTypes($scope, $armExpr, $armResult, new ConstantBooleanType($value), $identicalContext);
390+
391+
// a nullsafe chain that did not produce the constant may have
392+
// short-circuited instead
393+
if (!$identicalContext->true() && ($armExpr instanceof Expr\NullsafeMethodCall || $armExpr instanceof Expr\NullsafePropertyFetch)) {
394+
return $types;
395+
}
396+
397+
$boolContext = $value ? TypeSpecifierContext::createTrue() : TypeSpecifierContext::createFalse();
398+
399+
return $types->unionWith($armResult->getSpecifiedTypesForScope($scope, $identicalContext->true() ? $boolContext : $boolContext->negate()));
400+
};
401+
402+
return [
403+
$types,
404+
static function (bool $nativeTypesPromoted) use ($armResult, $processingScope, $isIdentical, $value): Type {
405+
$armType = $armResult->getTypeOnScope($processingScope, $nativeTypesPromoted);
406+
$matches = $value ? $armType->isTrue() : $armType->isFalse();
407+
if (!$isIdentical) {
408+
$matches = $matches->negate();
409+
}
410+
if ($matches->yes()) {
411+
return new ConstantBooleanType(true);
412+
}
413+
if ($matches->no()) {
414+
return new ConstantBooleanType(false);
415+
}
416+
417+
return new BooleanType();
418+
},
419+
static fn (): MutatingScope => $evaluatedScope->applySpecifiedTypes($types($evaluatedScope, TypeSpecifierContext::createTruthy())),
420+
static fn (): MutatingScope => $evaluatedScope->applySpecifiedTypes($types($evaluatedScope, TypeSpecifierContext::createFalsey())),
421+
];
422+
}
423+
335424
}

‎src/Analyser/ResultCache/ResultCacheManager.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,6 +1211,7 @@ public function process(AnalyserResult $analyserResult, ResultCache $resultCache
12111211
reachedInternalErrorsCountLimit: $analyserResult->hasReachedInternalErrorsCountLimit(),
12121212
peakMemoryUsageBytes: $analyserResult->getPeakMemoryUsageBytes(),
12131213
processedFiles: $analyserResult->getProcessedFiles(),
1214+
workerCount: $analyserResult->getWorkerCount(),
12141215
), $saved);
12151216
}
12161217

‎src/Command/AnalyseApplication.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ private function switchTmpFileInAnalyserResult(
344344
reachedInternalErrorsCountLimit: $analyserResult->hasReachedInternalErrorsCountLimit(),
345345
peakMemoryUsageBytes: $analyserResult->getPeakMemoryUsageBytes(),
346346
processedFiles: $analyserResult->getProcessedFiles(),
347+
workerCount: $analyserResult->getWorkerCount(),
347348
);
348349
}
349350

0 commit comments

Comments
 (0)