Skip to content

Commit 0dfcae6

Browse files
ondrejmirtesclaude
andcommitted
Answer a walked argument-less call from its stored result in the state read
resolveScopeStateType() prices an argument-less instance call from the declared return type of its method - the shape @phpstan-assert subjects take, synthetic nodes never stored. The arm also caught calls the walk had processed and stored, and the declared type lacks what the walk resolved against the arguments: a conditional return type stays a conditional (`($asResource is true ? resource : string)` narrowed truthy became `resource|non-falsy-string` instead of `non-falsy-string`, and getKeepVoidType() no longer saw the `void` a `($callback is null ? void : TReturn)` call resolves to, losing the method.void report), a template stays a template, and on the PHP 7.4 downgrade a native `self|false` union resolved its `false` as a class in the narrowing base. A call with a stored result now answers through getType(), i.e. from that result; the reflection route stays for the synthetic subjects. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01R7pqAkCx4xP6WYxBo2nMJE
1 parent 3ac002b commit 0dfcae6

5 files changed

Lines changed: 251 additions & 3 deletions

File tree

‎src/Analyser/MutatingScope.php‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
use PHPStan\Reflection\ClassMemberReflection;
5555
use PHPStan\Reflection\ClassReflection;
5656
use PHPStan\Reflection\ExtendedMethodReflection;
57+
use PHPStan\Reflection\ExtendedParametersAcceptor;
5758
use PHPStan\Reflection\ExtendedPropertyReflection;
5859
use PHPStan\Reflection\FunctionReflection;
5960
use PHPStan\Reflection\InitializerExprContext;
@@ -3314,13 +3315,21 @@ private function resolveScopeStateType(Expr $expr, bool $native): Type
33143315
// an argument-less instance call - the shape @phpstan-assert subjects
33153316
// take (synthetic nodes built fresh from the assert tag, never stored):
33163317
// its declared return type on the receiver's state is the narrowing
3317-
// base, derived from reflection instead of walking the synthetic node
3318+
// base, derived from reflection instead of walking the synthetic node.
3319+
// A call the walk did store answers from that result: the declared
3320+
// return type lacks what the walk resolved against the arguments (a
3321+
// conditional return type, a template, a dynamic return type extension)
33183322
if (
33193323
$expr instanceof Expr\MethodCall
33203324
&& $expr->name instanceof Identifier
33213325
&& !$expr->isFirstClassCallable()
33223326
&& $expr->getArgs() === []
33233327
) {
3328+
$storage = $this->expressionResultStorageStack->getCurrent();
3329+
if ($storage !== null && $storage->findExpressionResult($expr) !== null) {
3330+
return $native ? $this->getNativeType($expr) : $this->getType($expr);
3331+
}
3332+
33243333
$methodReflection = $this->getMethodReflection(
33253334
$this->resolveScopeStateType($expr->var, $native),
33263335
$expr->name->toString(),
@@ -3329,9 +3338,12 @@ private function resolveScopeStateType(Expr $expr, bool $native): Type
33293338
return new ErrorType();
33303339
}
33313340

3332-
$variant = ParametersAcceptorSelector::combineAcceptors($methodReflection->getVariants());
3341+
// resolved against the (empty) argument list so a template inferred
3342+
// from an omitted parameter's default resolves the way a walk
3343+
// resolves it (Collection::first()'s TFirstDefault -> null)
3344+
$variant = ParametersAcceptorSelector::selectFromArgs($this, [], $methodReflection->getVariants(), $methodReflection->getNamedArgumentsVariants());
33333345

3334-
return $native ? $variant->getNativeReturnType() : $variant->getReturnType();
3346+
return $native && $variant instanceof ExtendedParametersAcceptor ? $variant->getNativeReturnType() : $variant->getReturnType();
33353347
}
33363348

33373349
// position-independent constant expressions (isset()/?? dimensions and
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace ArglessCallNarrowingWalkedType;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class Request
8+
{
9+
10+
/**
11+
* @return ($asResource is true ? resource : string)
12+
*/
13+
public function getContent(bool $asResource = false)
14+
{
15+
return '';
16+
}
17+
18+
}
19+
20+
class RequestEvent
21+
{
22+
23+
public function getRequest(): Request
24+
{
25+
return new Request();
26+
}
27+
28+
}
29+
30+
/**
31+
* @template T of object
32+
*/
33+
class Collection
34+
{
35+
36+
/** @var list<T> */
37+
private array $items = [];
38+
39+
/** @return T|null */
40+
public function first()
41+
{
42+
return $this->items[0] ?? null;
43+
}
44+
45+
}
46+
47+
final class Item
48+
{
49+
50+
public string $name = '';
51+
52+
}
53+
54+
class Node
55+
{
56+
57+
/** @return static|false */
58+
public function getParent()
59+
{
60+
return false;
61+
}
62+
63+
}
64+
65+
/**
66+
* @param Collection<Item> $items
67+
*/
68+
function test(RequestEvent $event, string $contentType, Collection $items, Node $node): void
69+
{
70+
if (str_starts_with($contentType, 'application/json') && $event->getRequest()->getContent()) {
71+
assertType('non-falsy-string', $event->getRequest()->getContent());
72+
}
73+
74+
if ($event->getRequest()->getContent()) {
75+
assertType('non-falsy-string', $event->getRequest()->getContent());
76+
} else {
77+
assertType("''|'0'", $event->getRequest()->getContent());
78+
}
79+
80+
if ($items->first() !== null) {
81+
assertType('ArglessCallNarrowingWalkedType\Item', $items->first());
82+
}
83+
84+
while ($node->getParent() !== false) {
85+
$node = $node->getParent();
86+
assertType('ArglessCallNarrowingWalkedType\Node', $node);
87+
}
88+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace AssertArglessCallTemplateDefault;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class User
8+
{
9+
10+
}
11+
12+
/**
13+
* @template TKey of array-key
14+
* @template-covariant TValue
15+
*/
16+
class Collection
17+
{
18+
19+
/**
20+
* @template TFirstDefault
21+
*
22+
* @param (callable(TValue, TKey): bool)|null $callback
23+
* @param TFirstDefault|(\Closure(): TFirstDefault) $default
24+
* @return TValue|TFirstDefault
25+
*/
26+
public function first(?callable $callback = null, $default = null)
27+
{
28+
throw new \LogicException();
29+
}
30+
31+
/**
32+
* @template TLastDefault
33+
*
34+
* @param (callable(TValue, TKey): bool)|null $callback
35+
* @param TLastDefault|(\Closure(): TLastDefault) $default
36+
* @return TValue|TLastDefault
37+
*/
38+
public function last(?callable $callback = null, $default = null)
39+
{
40+
throw new \LogicException();
41+
}
42+
43+
/**
44+
* @phpstan-assert-if-true null $this->first()
45+
* @phpstan-assert-if-true null $this->last()
46+
*
47+
* @phpstan-assert-if-false TValue $this->first()
48+
* @phpstan-assert-if-false TValue $this->last()
49+
*
50+
* @return bool
51+
*/
52+
public function isEmpty()
53+
{
54+
return true;
55+
}
56+
57+
/**
58+
* @phpstan-assert-if-true TValue $this->first()
59+
* @phpstan-assert-if-true TValue $this->last()
60+
*
61+
* @phpstan-assert-if-false null $this->first()
62+
* @phpstan-assert-if-false null $this->last()
63+
*
64+
* @return bool
65+
*/
66+
public function isNotEmpty()
67+
{
68+
return true;
69+
}
70+
71+
}
72+
73+
/**
74+
* @param Collection<int, User> $collection
75+
*/
76+
function test(Collection $collection): void
77+
{
78+
assertType('AssertArglessCallTemplateDefault\User|null', $collection->first());
79+
if ($collection->isNotEmpty()) {
80+
assertType('AssertArglessCallTemplateDefault\User', $collection->first());
81+
assertType("'foo'|AssertArglessCallTemplateDefault\User", $collection->first(null, 'foo'));
82+
} else {
83+
assertType('null', $collection->first());
84+
assertType("'foo'|AssertArglessCallTemplateDefault\User", $collection->first(null, 'foo'));
85+
}
86+
if ($collection->isEmpty()) {
87+
assertType('null', $collection->first());
88+
} else {
89+
assertType('AssertArglessCallTemplateDefault\User', $collection->first());
90+
}
91+
92+
assertType('AssertArglessCallTemplateDefault\User|null', $collection->last());
93+
}

‎tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,23 @@ public function testBug14720(): void
987987
$this->analyse([__DIR__ . '/data/bug-14720.php'], []);
988988
}
989989

990+
public function testVoidConditionalReturnType(): void
991+
{
992+
$this->checkThisOnly = false;
993+
$this->checkNullables = true;
994+
$this->checkUnionTypes = true;
995+
$this->analyse([__DIR__ . '/data/void-conditional-return-type.php'], [
996+
[
997+
'Result of method VoidConditionalReturnType\Wormhole::seconds() (void) is used.',
998+
34,
999+
],
1000+
[
1001+
'Result of method VoidConditionalReturnType\Wormhole::seconds() (void) is used.',
1002+
37,
1003+
],
1004+
]);
1005+
}
1006+
9901007
public function testClosureBind(): void
9911008
{
9921009
$this->checkThisOnly = false;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace VoidConditionalReturnType;
4+
5+
class Wormhole
6+
{
7+
8+
/**
9+
* @template TReturn of mixed
10+
*
11+
* @param (callable(): TReturn)|null $callback
12+
* @return ($callback is null ? void : TReturn)
13+
*/
14+
public function seconds($callback = null)
15+
{
16+
return $this->handleCallback($callback);
17+
}
18+
19+
/**
20+
* @param callable|null $callback
21+
* @return mixed
22+
*/
23+
protected function handleCallback($callback)
24+
{
25+
if ($callback) {
26+
return $callback();
27+
}
28+
}
29+
30+
}
31+
32+
function test(Wormhole $wormhole): void
33+
{
34+
$x = $wormhole->seconds();
35+
$wormhole->seconds();
36+
$y = $wormhole->seconds(static fn () => 1);
37+
echo $wormhole->seconds();
38+
}

0 commit comments

Comments
 (0)