Skip to content

Improve support switch on ::class #4011

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

Open
wants to merge 1 commit into
base: 2.1.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
12 changes: 10 additions & 2 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -1656,13 +1656,11 @@ private function findTypeExpressionsFromBinaryOperation(Scope $scope, Node\Expr\
if (
$leftType instanceof ConstantScalarType
&& !$rightExpr instanceof ConstFetch
&& !$rightExpr instanceof ClassConstFetch
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The findTypeExpressionFromBinaryOperation is used two times:

  • Inside resolveIdentical
  • Inside resolveEqual

Inside resolveIdentical the ClassConstFetch scenario is not needed. But removing the check does not have impact since then the following code

$expressions = $this->findTypeExpressionsFromBinaryOperation($scope, $expr);
		if ($expressions !== null) {
			$exprNode = $expressions[0];
			$constantType = $expressions[1];

			$unwrappedExprNode = $exprNode;
			if ($exprNode instanceof AlwaysRememberedExpr) {
				$unwrappedExprNode = $exprNode->getExpr();
			}

			$specifiedType = $this->specifyTypesForConstantBinaryExpression($unwrappedExprNode, $constantType, $context, $scope, $expr);

gives a NULL $specifiedType.

Inside resolveEqual the ClassConstFetch is needed for an easy check

$context->true()
				&& $exprNode instanceof ClassConstFetch
				&& $exprNode->name instanceof Node\Identifier
				&& strtolower($exprNode->name->toString()) === 'class'
				&& $constantType->isString()->yes()

without checking both the right side and the left side.

Not sure if you're ok with such change.
I could had a boolean parameter to remove or not the ClassConstFetch check but I'm not sure it's worth it.

) {
return [$binaryOperation->right, $leftType, $rightType];
} elseif (
$rightType instanceof ConstantScalarType
&& !$leftExpr instanceof ConstFetch
&& !$leftExpr instanceof ClassConstFetch
) {
return [$binaryOperation->left, $rightType, $leftType];
}
Expand Down Expand Up @@ -2061,6 +2059,16 @@ public function resolveEqual(Expr\BinaryOp\Equal $expr, Scope $scope, TypeSpecif
) {
return $this->specifyTypesInCondition($scope, new Expr\BinaryOp\Identical($expr->left, $expr->right), $context)->setRootExpr($expr);
}

if (
$context->true()
&& $exprNode instanceof ClassConstFetch
&& $exprNode->name instanceof Node\Identifier
&& strtolower($exprNode->name->toString()) === 'class'
&& $constantType->isString()->yes()
) {
return $this->specifyTypesInCondition($scope, new Expr\BinaryOp\Identical($expr->left, $expr->right), $context)->setRootExpr($expr);
}
}

$leftType = $scope->getType($expr->left);
Expand Down
31 changes: 31 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-13069.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Bug13069;

use function PHPStan\Testing\assertType;

interface ResourceInterface {}

class Person implements ResourceInterface
{
public function getName(): string { return 'Name'; }
}

class Account implements ResourceInterface
{
public function getMail(): string { return 'Mail'; }
}

function foo(?ResourceInterface $object = null): void
{
switch ($object::class) {
case Person::class:
assertType(Person::class, $object);
echo $object->getName();
break;
case Account::class:
assertType(Account::class, $object);
echo $object->getMail();
break;
}
}
Loading