Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce isDeprecated to CallableParametersAcceptor #3759

Open
wants to merge 1 commit into
base: 2.0.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -1342,6 +1342,7 @@ static function (Node $node, Scope $scope) use ($arrowScope, &$arrowFunctionImpu
$cachedClosureData['invalidateExpressions'],
$cachedClosureData['usedVariables'],
TrinaryLogic::createYes(),
TrinaryLogic::createNo(),
);
}
if (self::$resolveClosureTypeDepth >= 2) {
Expand Down Expand Up @@ -1557,6 +1558,7 @@ static function (Node $node, Scope $scope) use ($arrowScope, &$arrowFunctionImpu
$invalidateExpressions,
$usedVariables,
TrinaryLogic::createYes(),
TrinaryLogic::createNo(),
);
} elseif ($node instanceof New_) {
if ($node->class instanceof Name) {
Expand Down
2 changes: 2 additions & 0 deletions src/Reflection/Callables/CallableParametersAcceptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public function getThrowPoints(): array;

public function isPure(): TrinaryLogic;

public function isDeprecated(): TrinaryLogic;

public function acceptsNamedArguments(): TrinaryLogic;

/**
Expand Down
5 changes: 5 additions & 0 deletions src/Reflection/Callables/FunctionCallableVariant.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ public function isPure(): TrinaryLogic
return $certainCount > 0 ? TrinaryLogic::createNo() : TrinaryLogic::createMaybe();
}

public function isDeprecated(): TrinaryLogic
{
return $this->function->isDeprecated();
}

public function getImpurePoints(): array
{
if ($this->impurePoints !== null) {
Expand Down
6 changes: 6 additions & 0 deletions src/Reflection/ExtendedCallableFunctionVariant.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function __construct(
?TemplateTypeVarianceMap $callSiteVarianceMap,
private array $throwPoints,
private TrinaryLogic $isPure,
private TrinaryLogic $isDeprecated,
private array $impurePoints,
private array $invalidateExpressions,
private array $usedVariables,
Expand Down Expand Up @@ -60,6 +61,11 @@ public function isPure(): TrinaryLogic
return $this->isPure;
}

public function isDeprecated(): TrinaryLogic
{
return $this->isDeprecated;
}

public function getImpurePoints(): array
{
return $this->impurePoints;
Expand Down
1 change: 1 addition & 0 deletions src/Reflection/GenericParametersAcceptorResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ public static function resolve(array $argTypes, ParametersAcceptor $parametersAc
$result,
$originalParametersAcceptor->getThrowPoints(),
$originalParametersAcceptor->isPure(),
$originalParametersAcceptor->isDeprecated(),
$originalParametersAcceptor->getImpurePoints(),
$originalParametersAcceptor->getInvalidateExpressions(),
$originalParametersAcceptor->getUsedVariables(),
Expand Down
5 changes: 5 additions & 0 deletions src/Reflection/InaccessibleMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ public function isPure(): TrinaryLogic
return TrinaryLogic::createMaybe();
}

public function isDeprecated(): TrinaryLogic
{
return $this->methodReflection->isDeprecated();
}

public function getImpurePoints(): array
{
return [
Expand Down
4 changes: 4 additions & 0 deletions src/Reflection/ParametersAcceptorSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,7 @@ public static function combineAcceptors(array $acceptors): ExtendedParametersAcc
$callableOccurred = false;
$throwPoints = [];
$isPure = TrinaryLogic::createNo();
$isDeprecated = TrinaryLogic::createNo();
$impurePoints = [];
$invalidateExpressions = [];
$usedVariables = [];
Expand All @@ -627,6 +628,7 @@ public static function combineAcceptors(array $acceptors): ExtendedParametersAcc
$callableOccurred = true;
$throwPoints = array_merge($throwPoints, $acceptor->getThrowPoints());
$isPure = $isPure->or($acceptor->isPure());
$isDeprecated = $isDeprecated->or($acceptor->isDeprecated());
$impurePoints = array_merge($impurePoints, $acceptor->getImpurePoints());
$invalidateExpressions = array_merge($invalidateExpressions, $acceptor->getInvalidateExpressions());
$usedVariables = array_merge($usedVariables, $acceptor->getUsedVariables());
Expand Down Expand Up @@ -729,6 +731,7 @@ public static function combineAcceptors(array $acceptors): ExtendedParametersAcc
null,
$throwPoints,
$isPure,
$isDeprecated,
$impurePoints,
$invalidateExpressions,
$usedVariables,
Expand Down Expand Up @@ -765,6 +768,7 @@ private static function wrapAcceptor(ParametersAcceptor $acceptor): ExtendedPara
TemplateTypeVarianceMap::createEmpty(),
$acceptor->getThrowPoints(),
$acceptor->isPure(),
$acceptor->isDeprecated(),
$acceptor->getImpurePoints(),
$acceptor->getInvalidateExpressions(),
$acceptor->getUsedVariables(),
Expand Down
6 changes: 6 additions & 0 deletions src/Reflection/ResolvedFunctionVariantWithCallable.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function __construct(
private ResolvedFunctionVariant $parametersAcceptor,
private array $throwPoints,
private TrinaryLogic $isPure,
private TrinaryLogic $isDeprecated,
private array $impurePoints,
private array $invalidateExpressions,
private array $usedVariables,
Expand Down Expand Up @@ -97,6 +98,11 @@ public function isPure(): TrinaryLogic
return $this->isPure;
}

public function isDeprecated(): TrinaryLogic
{
return $this->isDeprecated;
}

public function getImpurePoints(): array
{
return $this->impurePoints;
Expand Down
5 changes: 5 additions & 0 deletions src/Reflection/TrivialParametersAcceptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ public function isPure(): TrinaryLogic
return TrinaryLogic::createMaybe();
}

public function isDeprecated(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function getImpurePoints(): array
{
return [
Expand Down
2 changes: 2 additions & 0 deletions src/Rules/RuleLevelHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ private function transformAcceptedType(Type $acceptingType, Type $acceptedType):
$acceptedType->getResolvedTemplateTypeMap(),
$acceptedType->getTemplateTags(),
$acceptedType->isPure(),
$acceptedType->isDeprecated(),
);
}

Expand All @@ -112,6 +113,7 @@ private function transformAcceptedType(Type $acceptingType, Type $acceptedType):
$acceptedType->getInvalidateExpressions(),
$acceptedType->getUsedVariables(),
$acceptedType->acceptsNamedArguments(),
$acceptedType->isDeprecated(),
);
}

Expand Down
15 changes: 15 additions & 0 deletions src/Type/CallableType.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ class CallableType implements CompoundType, CallableParametersAcceptor

private TrinaryLogic $isPure;

private TrinaryLogic $isDeprecated;

/**
* @api
* @param list<ParameterReflection>|null $parameters
Expand All @@ -78,6 +80,7 @@ public function __construct(
?TemplateTypeMap $resolvedTemplateTypeMap = null,
private array $templateTags = [],
?TrinaryLogic $isPure = null,
?TrinaryLogic $isDeprecated = null,
)
{
$this->parameters = $parameters ?? [];
Expand All @@ -86,6 +89,7 @@ public function __construct(
$this->templateTypeMap = $templateTypeMap ?? TemplateTypeMap::createEmpty();
$this->resolvedTemplateTypeMap = $resolvedTemplateTypeMap ?? TemplateTypeMap::createEmpty();
$this->isPure = $isPure ?? TrinaryLogic::createMaybe();
$this->isDeprecated = $isDeprecated ?? TrinaryLogic::createNo();
}

/**
Expand All @@ -101,6 +105,14 @@ public function isPure(): TrinaryLogic
return $this->isPure;
}

public function isDeprecated(): TrinaryLogic
{
return $this->isDeprecated;
}

/**
* @return list<string>
*/
public function getReferencedClasses(): array
{
$classes = [];
Expand Down Expand Up @@ -231,6 +243,7 @@ function (): string {
$this->resolvedTemplateTypeMap,
$this->templateTags,
$this->isPure,
$this->isDeprecated,
);

return $printer->print($selfWithoutParameterNames->toPhpDocNode());
Expand Down Expand Up @@ -445,6 +458,7 @@ public function traverse(callable $cb): Type
$this->resolvedTemplateTypeMap,
$this->templateTags,
$this->isPure,
$this->isDeprecated,
);
}

Expand Down Expand Up @@ -495,6 +509,7 @@ public function traverseSimultaneously(Type $right, callable $cb): Type
$this->resolvedTemplateTypeMap,
$this->templateTags,
$this->isPure,
$this->isDeprecated,
);
}

Expand Down
12 changes: 12 additions & 0 deletions src/Type/ClosureType.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ class ClosureType implements TypeWithClassName, CallableParametersAcceptor

private TrinaryLogic $acceptsNamedArguments;

private TrinaryLogic $isDeprecated;

/**
* @api
* @param list<ParameterReflection>|null $parameters
Expand All @@ -101,6 +103,7 @@ public function __construct(
private array $invalidateExpressions = [],
private array $usedVariables = [],
?TrinaryLogic $acceptsNamedArguments = null,
?TrinaryLogic $isDeprecated = null,
)
{
if ($acceptsNamedArguments === null) {
Expand All @@ -116,6 +119,7 @@ public function __construct(
$this->resolvedTemplateTypeMap = $resolvedTemplateTypeMap ?? TemplateTypeMap::createEmpty();
$this->callSiteVarianceMap = $callSiteVarianceMap ?? TemplateTypeVarianceMap::createEmpty();
$this->impurePoints = $impurePoints ?? [new SimpleImpurePoint('functionCall', 'call to an unknown Closure', false)];
$this->isDeprecated = $isDeprecated ?? TrinaryLogic::createNo();
}

/**
Expand Down Expand Up @@ -150,6 +154,11 @@ public function isPure(): TrinaryLogic
return $certainCount > 0 ? TrinaryLogic::createNo() : TrinaryLogic::createMaybe();
}

public function isDeprecated(): TrinaryLogic
{
return $this->isDeprecated;
}

public function getClassName(): string
{
return $this->objectType->getClassName();
Expand Down Expand Up @@ -262,6 +271,7 @@ function (): string {
$this->impurePoints,
$this->invalidateExpressions,
$this->usedVariables,
$this->isDeprecated,
);

return $printer->print($selfWithoutParameterNames->toPhpDocNode());
Expand Down Expand Up @@ -584,6 +594,7 @@ public function traverse(callable $cb): Type
$this->invalidateExpressions,
$this->usedVariables,
$this->acceptsNamedArguments,
$this->isDeprecated,
);
}

Expand Down Expand Up @@ -634,6 +645,7 @@ public function traverseSimultaneously(Type $right, callable $cb): Type
$this->invalidateExpressions,
$this->usedVariables,
$this->acceptsNamedArguments,
$this->isDeprecated,
);
}

Expand Down
17 changes: 16 additions & 1 deletion src/Type/ClosureTypeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use PHPStan\Reflection\ParameterReflection;
use PHPStan\Reflection\PassedByReference;
use PHPStan\ShouldNotHappenException;
use PHPStan\TrinaryLogic;
use ReflectionFunction;
use function array_map;
use function count;
Expand Down Expand Up @@ -113,7 +114,21 @@ public function getDefaultValue(): ?Type

}, $betterReflectionFunction->getParameters());

return new ClosureType($parameters, TypehintHelper::decideTypeFromReflection(ReflectionType::fromTypeOrNull($betterReflectionFunction->getReturnType())), $betterReflectionFunction->isVariadic());
return new ClosureType(
$parameters,
TypehintHelper::decideTypeFromReflection(ReflectionType::fromTypeOrNull($betterReflectionFunction->getReturnType())),
$betterReflectionFunction->isVariadic(),
null,
null,
null,
[],
[],
null,
[],
[],
null,
TrinaryLogic::createFromBoolean($betterReflectionFunction->isDeprecated()),
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function getTypeFromStaticMethodCall(MethodReflection $methodReflection,
$variant->getInvalidateExpressions(),
$variant->getUsedVariables(),
$variant->acceptsNamedArguments(),
$variant->isDeprecated(),
);
}

Expand Down
Loading