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

Add Type::isVoid() #1982

Merged
merged 1 commit into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@
use PHPStan\Type\TypeUtils;
use PHPStan\Type\TypeWithClassName;
use PHPStan\Type\UnionType;
use PHPStan\Type\VoidType;
use Throwable;
use Traversable;
use TypeError;
Expand Down Expand Up @@ -1498,7 +1497,7 @@ private function getOverridingThrowPoints(Node\Stmt $statement, MutatingScope $s
$throwsTag = $resolvedPhpDoc->getThrowsTag();
if ($throwsTag !== null) {
$throwsType = $throwsTag->getType();
if ($throwsType instanceof VoidType) {
if ($throwsType->isVoid()->yes()) {
return [];
}

Expand Down Expand Up @@ -2820,7 +2819,7 @@ private function getFunctionThrowPoint(
}

if ($throwType !== null) {
if (!$throwType instanceof VoidType) {
if (!$throwType->isVoid()->yes()) {
return ThrowPoint::createExplicit($scope, $throwType, $funcCall, true);
}
} elseif ($this->implicitThrows) {
Expand Down Expand Up @@ -2878,7 +2877,7 @@ private function getMethodThrowPoint(MethodReflection $methodReflection, Paramet
}

if ($throwType !== null) {
if (!$throwType instanceof VoidType) {
if (!$throwType->isVoid()->yes()) {
return ThrowPoint::createExplicit($scope, $throwType, $methodCall, true);
}
} elseif ($this->implicitThrows) {
Expand Down Expand Up @@ -2915,7 +2914,7 @@ private function getConstructorThrowPoint(MethodReflection $constructorReflectio

if ($constructorReflection->getThrowType() !== null) {
$throwType = $constructorReflection->getThrowType();
if (!$throwType instanceof VoidType) {
if (!$throwType->isVoid()->yes()) {
return ThrowPoint::createExplicit($scope, $throwType, $new, true);
}
} elseif ($this->implicitThrows) {
Expand Down Expand Up @@ -2947,7 +2946,7 @@ private function getStaticMethodThrowPoint(MethodReflection $methodReflection, P

if ($methodReflection->getThrowType() !== null) {
$throwType = $methodReflection->getThrowType();
if (!$throwType instanceof VoidType) {
if (!$throwType->isVoid()->yes()) {
return ThrowPoint::createExplicit($scope, $throwType, $methodCall, true);
}
} elseif ($this->implicitThrows) {
Expand Down
3 changes: 1 addition & 2 deletions src/Reflection/Native/NativeFunctionReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use PHPStan\Reflection\ParametersAcceptorWithPhpDocs;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Type;
use PHPStan\Type\VoidType;

class NativeFunctionReflection implements FunctionReflection
{
Expand Down Expand Up @@ -89,7 +88,7 @@ public function hasSideEffects(): TrinaryLogic
private function isVoid(): bool
{
foreach ($this->variants as $variant) {
if (!$variant->getReturnType() instanceof VoidType) {
if (!$variant->getReturnType()->isVoid()->yes()) {
return false;
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/Reflection/Native/NativeMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use PHPStan\TrinaryLogic;
use PHPStan\Type\Type;
use PHPStan\Type\TypehintHelper;
use PHPStan\Type\VoidType;
use ReflectionException;
use function strtolower;

Expand Down Expand Up @@ -144,7 +143,7 @@ public function hasSideEffects(): TrinaryLogic
private function isVoid(): bool
{
foreach ($this->variants as $variant) {
if (!$variant->getReturnType() instanceof VoidType) {
if (!$variant->getReturnType()->isVoid()->yes()) {
return false;
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/Reflection/Php/PhpFunctionFromParserNodeReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use PHPStan\Type\TypehintHelper;
use PHPStan\Type\VoidType;
use function array_reverse;
use function is_array;
use function is_string;
Expand Down Expand Up @@ -194,7 +193,7 @@ public function getThrowType(): ?Type

public function hasSideEffects(): TrinaryLogic
{
if ($this->getReturnType() instanceof VoidType) {
if ($this->getReturnType()->isVoid()->yes()) {
return TrinaryLogic::createYes();
}
if ($this->isPure !== null) {
Expand Down
3 changes: 1 addition & 2 deletions src/Reflection/Php/PhpFunctionReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use PHPStan\Type\TypehintHelper;
use PHPStan\Type\VoidType;
use function array_map;
use function filemtime;
use function is_file;
Expand Down Expand Up @@ -240,7 +239,7 @@ public function getThrowType(): ?Type

public function hasSideEffects(): TrinaryLogic
{
if ($this->getReturnType() instanceof VoidType) {
if ($this->getReturnType()->isVoid()->yes()) {
return TrinaryLogic::createYes();
}
if ($this->isPure !== null) {
Expand Down
7 changes: 2 additions & 5 deletions src/Reflection/Php/PhpMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -405,12 +405,9 @@ public function getThrowType(): ?Type

public function hasSideEffects(): TrinaryLogic
{
$name = strtolower($this->getName());
$isVoid = $this->getReturnType() instanceof VoidType;

if (
$name !== '__construct'
&& $isVoid
strtolower($this->getName()) !== '__construct'
&& $this->getReturnType()->isVoid()->yes()
) {
return TrinaryLogic::createYes();
}
Expand Down
7 changes: 3 additions & 4 deletions src/Rules/Comparison/ImpossibleCheckTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
use PHPStan\Type\TypeUtils;
use PHPStan\Type\TypeWithClassName;
use PHPStan\Type\VerbosityLevel;
use PHPStan\Type\VoidType;
use function array_map;
use function array_pop;
use function count;
Expand Down Expand Up @@ -331,7 +330,7 @@ private function determineContext(Scope $scope, Expr $node): TypeSpecifierContex
$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs($scope, $node->getArgs(), $functionReflection->getVariants());
$returnType = TypeUtils::resolveLateResolvableTypes($parametersAcceptor->getReturnType());

return $returnType instanceof VoidType ? TypeSpecifierContext::createNull() : TypeSpecifierContext::createTruthy();
return $returnType->isVoid()->yes() ? TypeSpecifierContext::createNull() : TypeSpecifierContext::createTruthy();
}
} elseif ($node instanceof MethodCall && $node->name instanceof Node\Identifier) {
$methodCalledOnType = $scope->getType($node->var);
Expand All @@ -340,7 +339,7 @@ private function determineContext(Scope $scope, Expr $node): TypeSpecifierContex
$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs($scope, $node->getArgs(), $methodReflection->getVariants());
$returnType = TypeUtils::resolveLateResolvableTypes($parametersAcceptor->getReturnType());

return $returnType instanceof VoidType ? TypeSpecifierContext::createNull() : TypeSpecifierContext::createTruthy();
return $returnType->isVoid()->yes() ? TypeSpecifierContext::createNull() : TypeSpecifierContext::createTruthy();
}
} elseif ($node instanceof StaticCall && $node->name instanceof Node\Identifier) {
if ($node->class instanceof Node\Name) {
Expand All @@ -354,7 +353,7 @@ private function determineContext(Scope $scope, Expr $node): TypeSpecifierContex
$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs($scope, $node->getArgs(), $staticMethodReflection->getVariants());
$returnType = TypeUtils::resolveLateResolvableTypes($parametersAcceptor->getReturnType());

return $returnType instanceof VoidType ? TypeSpecifierContext::createNull() : TypeSpecifierContext::createTruthy();
return $returnType->isVoid()->yes() ? TypeSpecifierContext::createNull() : TypeSpecifierContext::createTruthy();
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/Rules/Comparison/UsageOfVoidMatchExpressionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\VoidType;

/**
* @implements Rule<Node\Expr\Match_>
Expand All @@ -23,7 +22,7 @@ public function processNode(Node $node, Scope $scope): array
{
$matchResultType = $scope->getType($node);
if (
$matchResultType instanceof VoidType
$matchResultType->isVoid()->yes()
&& !$scope->isInFirstLevelStatement()
) {
return [RuleErrorBuilder::message('Result of match expression (void) is used.')->build()];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use PHPStan\Type\TypeUtils;
use PHPStan\Type\TypeWithClassName;
use PHPStan\Type\VerbosityLevel;
use PHPStan\Type\VoidType;
use function sprintf;

/**
Expand Down Expand Up @@ -41,7 +40,7 @@ public function processNode(Node $node, Scope $scope): array
throw new ShouldNotHappenException();
}

if (!$functionReflection->getThrowType() instanceof VoidType) {
if ($functionReflection->getThrowType() === null || !$functionReflection->getThrowType()->isVoid()->yes()) {
return [];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use PHPStan\Type\TypeUtils;
use PHPStan\Type\TypeWithClassName;
use PHPStan\Type\VerbosityLevel;
use PHPStan\Type\VoidType;
use function sprintf;

/**
Expand Down Expand Up @@ -41,7 +40,7 @@ public function processNode(Node $node, Scope $scope): array
throw new ShouldNotHappenException();
}

if (!$methodReflection->getThrowType() instanceof VoidType) {
if ($methodReflection->getThrowType() === null || !$methodReflection->getThrowType()->isVoid()->yes()) {
return [];
}

Expand Down
3 changes: 1 addition & 2 deletions src/Rules/Exceptions/TooWideThrowTypeCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeUtils;
use PHPStan\Type\VerbosityLevel;
use PHPStan\Type\VoidType;
use function array_map;

class TooWideThrowTypeCheck
Expand All @@ -20,7 +19,7 @@ class TooWideThrowTypeCheck
*/
public function check(Type $throwType, array $throwPoints): array
{
if ($throwType instanceof VoidType) {
if ($throwType->isVoid()->yes()) {
return [];
}

Expand Down
3 changes: 1 addition & 2 deletions src/Rules/FunctionCallParametersCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
use PHPStan\Type\TypeTraverser;
use PHPStan\Type\TypeUtils;
use PHPStan\Type\VerbosityLevel;
use PHPStan\Type\VoidType;
use function array_fill;
use function array_key_exists;
use function count;
Expand Down Expand Up @@ -209,7 +208,7 @@ public function check(
}

if (
$scope->getType($funcCall) instanceof VoidType
$scope->getType($funcCall)->isVoid()->yes()
&& !$scope->isInFirstLevelStatement()
&& !$funcCall instanceof Node\Expr\New_
) {
Expand Down
5 changes: 2 additions & 3 deletions src/Rules/FunctionDefinitionCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
use PHPStan\Type\Type;
use PHPStan\Type\TypeTraverser;
use PHPStan\Type\VerbosityLevel;
use PHPStan\Type\VoidType;
use function array_keys;
use function array_map;
use function array_merge;
Expand Down Expand Up @@ -114,7 +113,7 @@ public function checkAnonymousFunction(
throw new ShouldNotHappenException();
}
$type = $scope->getFunctionType($param->type, false, false);
if ($type instanceof VoidType) {
if ($type->isVoid()->yes()) {
$errors[] = RuleErrorBuilder::message(sprintf($parameterMessage, $param->var->name, 'void'))->line($param->type->getLine())->nonIgnorable()->build();
}
if (
Expand Down Expand Up @@ -260,7 +259,7 @@ private function checkParametersAcceptor(
if (!$parameterVar instanceof Variable || !is_string($parameterVar->name)) {
throw new ShouldNotHappenException();
}
if ($parameter->getNativeType() instanceof VoidType) {
if ($parameter->getNativeType()->isVoid()->yes()) {
$errors[] = RuleErrorBuilder::message(sprintf($parameterMessage, $parameterVar->name, 'void'))->line($parameterNodeCallback()->getLine())->nonIgnorable()->build();
}
if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\NeverType;
use PHPStan\Type\VoidType;
use function in_array;
use function sprintf;

Expand Down Expand Up @@ -46,7 +45,7 @@ public function processNode(Node $node, Scope $scope): array
if ($function->hasSideEffects()->no() || $node->expr->isFirstClassCallable()) {
if (!$node->expr->isFirstClassCallable()) {
$throwsType = $function->getThrowType();
if ($throwsType !== null && !$throwsType instanceof VoidType) {
if ($throwsType !== null && !$throwsType->isVoid()->yes()) {
return [];
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/Rules/Generators/YieldFromTypeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use PHPStan\Type\MixedType;
use PHPStan\Type\TypeWithClassName;
use PHPStan\Type\VerbosityLevel;
use PHPStan\Type\VoidType;
use function sprintf;

/**
Expand Down Expand Up @@ -127,7 +126,7 @@ public function processNode(Node $node, Scope $scope): array
))->build();
}

if ($scope->getType($node) instanceof VoidType && !$scope->isInFirstLevelStatement()) {
if ($scope->getType($node)->isVoid()->yes() && !$scope->isInFirstLevelStatement()) {
$messages[] = RuleErrorBuilder::message('Result of yield from (void) is used.')->build();
}

Expand Down
3 changes: 1 addition & 2 deletions src/Rules/Generators/YieldTypeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use PHPStan\Type\MixedType;
use PHPStan\Type\NullType;
use PHPStan\Type\VerbosityLevel;
use PHPStan\Type\VoidType;
use function sprintf;

/**
Expand Down Expand Up @@ -77,7 +76,7 @@ public function processNode(Node $node, Scope $scope): array
$valueType->describe($verbosityLevel),
))->build();
}
if ($scope->getType($node) instanceof VoidType && !$scope->isInFirstLevelStatement()) {
if ($scope->getType($node)->isVoid()->yes() && !$scope->isInFirstLevelStatement()) {
$messages[] = RuleErrorBuilder::message('Result of yield (void) is used.')->build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\NeverType;
use PHPStan\Type\VoidType;
use function sprintf;

/**
Expand Down Expand Up @@ -50,7 +49,7 @@ public function processNode(Node $node, Scope $scope): array
$constructor = $classReflection->getConstructor();
if ($constructor->hasSideEffects()->no()) {
$throwsType = $constructor->getThrowType();
if ($throwsType !== null && !$throwsType instanceof VoidType) {
if ($throwsType !== null && !$throwsType->isVoid()->yes()) {
return [];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use PHPStan\Type\ErrorType;
use PHPStan\Type\NeverType;
use PHPStan\Type\Type;
use PHPStan\Type\VoidType;
use function sprintf;

/**
Expand Down Expand Up @@ -65,7 +64,7 @@ public function processNode(Node $node, Scope $scope): array
if ($method->hasSideEffects()->no() || $node->expr->isFirstClassCallable()) {
if (!$node->expr->isFirstClassCallable()) {
$throwsType = $method->getThrowType();
if ($throwsType !== null && !$throwsType instanceof VoidType) {
if ($throwsType !== null && !$throwsType->isVoid()->yes()) {
return [];
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use PHPStan\Type\NeverType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\VoidType;
use function sprintf;
use function strtolower;

Expand Down Expand Up @@ -88,7 +87,7 @@ public function processNode(Node $node, Scope $scope): array
if ($method->hasSideEffects()->no() || $node->expr->isFirstClassCallable()) {
if (!$node->expr->isFirstClassCallable()) {
$throwsType = $method->getThrowType();
if ($throwsType !== null && !$throwsType instanceof VoidType) {
if ($throwsType !== null && !$throwsType->isVoid()->yes()) {
return [];
}
}
Expand Down