Skip to content

Commit ed365ba

Browse files
phpstan-botclaude
andcommitted
Add getAsserts() to CallableParametersAcceptor interface
Move assertion retrieval from ClosureType-specific code to the CallableParametersAcceptor interface, so TypeSpecifier can use the interface method instead of instanceof ClosureType checks. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 61b5d3e commit ed365ba

10 files changed

+42
-6
lines changed

src/Analyser/TypeSpecifier.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
use PHPStan\Type\Accessory\NonEmptyArrayType;
4646
use PHPStan\Type\ArrayType;
4747
use PHPStan\Type\BooleanType;
48-
use PHPStan\Type\ClosureType;
4948
use PHPStan\Type\ConditionalTypeForParameter;
5049
use PHPStan\Type\Constant\ConstantArrayType;
5150
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
@@ -1784,13 +1783,9 @@ private function specifyTypesFromCallableCall(TypeSpecifierContext $context, Fun
17841783
$assertions = null;
17851784
$parametersAcceptor = null;
17861785

1787-
// Check for ClosureType with assertions (from first-class callables)
1786+
// Check for CallableParametersAcceptor with assertions (from first-class callables)
17881787
if ($calleeType->isCallable()->yes()) {
17891788
foreach ($calleeType->getCallableParametersAcceptors($scope) as $variant) {
1790-
if (!$variant instanceof ClosureType) {
1791-
continue;
1792-
}
1793-
17941789
$variantAssertions = $variant->getAsserts();
17951790
if ($variantAssertions->getAll() === []) {
17961791
continue;

src/Reflection/Callables/CallableParametersAcceptor.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PHPStan\Reflection\Callables;
44

55
use PHPStan\Node\InvalidateExprNode;
6+
use PHPStan\Reflection\Assertions;
67
use PHPStan\Reflection\ParametersAcceptor;
78
use PHPStan\TrinaryLogic;
89

@@ -57,4 +58,6 @@ public function getUsedVariables(): array;
5758
*/
5859
public function mustUseReturnValue(): TrinaryLogic;
5960

61+
public function getAsserts(): Assertions;
62+
6063
}

src/Reflection/Callables/FunctionCallableVariant.php

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

33
namespace PHPStan\Reflection\Callables;
44

5+
use PHPStan\Reflection\Assertions;
56
use PHPStan\Reflection\ExtendedMethodReflection;
67
use PHPStan\Reflection\ExtendedParameterReflection;
78
use PHPStan\Reflection\ExtendedParametersAcceptor;
@@ -173,4 +174,9 @@ public function mustUseReturnValue(): TrinaryLogic
173174
return $this->function->mustUseReturnValue();
174175
}
175176

177+
public function getAsserts(): Assertions
178+
{
179+
return $this->function->getAsserts();
180+
}
181+
176182
}

src/Reflection/ExtendedCallableFunctionVariant.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PHPStan\Reflection\Callables\CallableParametersAcceptor;
77
use PHPStan\Reflection\Callables\SimpleImpurePoint;
88
use PHPStan\Reflection\Callables\SimpleThrowPoint;
9+
use PHPStan\Reflection\Assertions;
910
use PHPStan\TrinaryLogic;
1011
use PHPStan\Type\Generic\TemplateTypeMap;
1112
use PHPStan\Type\Generic\TemplateTypeVarianceMap;
@@ -37,6 +38,7 @@ public function __construct(
3738
private array $usedVariables,
3839
private TrinaryLogic $acceptsNamedArguments,
3940
private TrinaryLogic $mustUseReturnValue,
41+
private ?Assertions $assertions = null,
4042
)
4143
{
4244
parent::__construct(
@@ -86,4 +88,9 @@ public function mustUseReturnValue(): TrinaryLogic
8688
return $this->mustUseReturnValue;
8789
}
8890

91+
public function getAsserts(): Assertions
92+
{
93+
return $this->assertions ?? Assertions::createEmpty();
94+
}
95+
8996
}

src/Reflection/GenericParametersAcceptorResolver.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ public static function resolve(array $argTypes, ParametersAcceptor $parametersAc
130130
$originalParametersAcceptor->getUsedVariables(),
131131
$originalParametersAcceptor->acceptsNamedArguments(),
132132
$originalParametersAcceptor->mustUseReturnValue(),
133+
$originalParametersAcceptor->getAsserts(),
133134
);
134135
}
135136

src/Reflection/InaccessibleMethod.php

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

55
use PHPStan\Reflection\Callables\CallableParametersAcceptor;
66
use PHPStan\Reflection\Callables\SimpleImpurePoint;
7+
use PHPStan\Reflection\Assertions;
78
use PHPStan\TrinaryLogic;
89
use PHPStan\Type\Generic\TemplateTypeMap;
910
use PHPStan\Type\Generic\TemplateTypeVarianceMap;
@@ -93,4 +94,9 @@ public function mustUseReturnValue(): TrinaryLogic
9394
return TrinaryLogic::createMaybe();
9495
}
9596

97+
public function getAsserts(): Assertions
98+
{
99+
return Assertions::createEmpty();
100+
}
101+
96102
}

src/Reflection/ParametersAcceptorSelector.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,7 @@ private static function wrapAcceptor(ParametersAcceptor $acceptor): ExtendedPara
897897
$acceptor->getUsedVariables(),
898898
$acceptor->acceptsNamedArguments(),
899899
$acceptor->mustUseReturnValue(),
900+
$acceptor->getAsserts(),
900901
);
901902
}
902903

src/Reflection/ResolvedFunctionVariantWithCallable.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public function __construct(
2929
private array $usedVariables,
3030
private TrinaryLogic $acceptsNamedArguments,
3131
private TrinaryLogic $mustUseReturnValue,
32+
private ?Assertions $assertions = null,
3233
)
3334
{
3435
}
@@ -118,4 +119,9 @@ public function mustUseReturnValue(): TrinaryLogic
118119
return $this->mustUseReturnValue;
119120
}
120121

122+
public function getAsserts(): Assertions
123+
{
124+
return $this->assertions ?? Assertions::createEmpty();
125+
}
126+
121127
}

src/Reflection/TrivialParametersAcceptor.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,9 @@ public function mustUseReturnValue(): TrinaryLogic
103103
return TrinaryLogic::createMaybe();
104104
}
105105

106+
public function getAsserts(): Assertions
107+
{
108+
return Assertions::createEmpty();
109+
}
110+
106111
}

src/Type/CallableType.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
1313
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
1414
use PHPStan\PhpDocParser\Printer\Printer;
15+
use PHPStan\Reflection\Assertions;
1516
use PHPStan\Reflection\Callables\CallableParametersAcceptor;
1617
use PHPStan\Reflection\Callables\SimpleImpurePoint;
1718
use PHPStan\Reflection\Callables\SimpleThrowPoint;
@@ -398,6 +399,11 @@ public function mustUseReturnValue(): TrinaryLogic
398399
return TrinaryLogic::createMaybe();
399400
}
400401

402+
public function getAsserts(): Assertions
403+
{
404+
return Assertions::createEmpty();
405+
}
406+
401407
public function toNumber(): Type
402408
{
403409
return new ErrorType();

0 commit comments

Comments
 (0)