Skip to content

Commit

Permalink
fix Scope->issetCheck() on NeverType
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Nov 26, 2023
1 parent 3ef341f commit 6567226
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
14 changes: 9 additions & 5 deletions src/Rules/IssetCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PHPStan\Rules\Properties\PropertyDescriptor;
use PHPStan\Rules\Properties\PropertyReflectionFinder;
use PHPStan\Type\MixedType;
use PHPStan\Type\NeverType;
use PHPStan\Type\Type;
use PHPStan\Type\VerbosityLevel;
use function is_string;
Expand Down Expand Up @@ -44,11 +45,14 @@ public function check(Expr $expr, Scope $scope, string $operatorDescription, cal
return null;
}

return $this->generateError(
$this->treatPhpDocTypesAsCertain ? $scope->getType($expr) : $scope->getNativeType($expr),
sprintf('Variable $%s %s always exists and', $expr->name, $operatorDescription),
$typeMessageCallback,
);
$type = $this->treatPhpDocTypesAsCertain ? $scope->getType($expr) : $scope->getNativeType($expr);
if (!$type instanceof NeverType) {
return $this->generateError(
$type,
sprintf('Variable $%s %s always exists and', $expr->name, $operatorDescription),
$typeMessageCallback,
);
}
}

return RuleErrorBuilder::message(sprintf('Variable $%s %s is never defined.', $expr->name, $operatorDescription))->build();
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Variables/EmptyRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ public function processNode(Node $node, Scope $scope): array
{
$error = $this->issetCheck->check($node->expr, $scope, 'in empty()', static function (Type $type): ?string {
$isNull = $type->isNull();
$isFalsey = $type->toBoolean()->isFalse();
if ($isNull->maybe()) {
return null;
}
$isFalsey = $type->toBoolean()->isFalse();
if ($isFalsey->maybe()) {
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Rules/Variables/IssetRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ public function testVariableCertaintyInIsset(): void
116,
],
[
'Variable $variableInSecondCase in isset() always exists and is not nullable.',
'Variable $variableInSecondCase in isset() is never defined.',
117,
],
[
Expand Down

0 comments on commit 6567226

Please sign in to comment.