Skip to content

Commit

Permalink
in_array - simulate Identical/Equal handling from TypeSpecifier for l…
Browse files Browse the repository at this point in the history
…iteral arrays
  • Loading branch information
ondrejmirtes committed Nov 27, 2023
1 parent a92585a commit 0177e33
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -1556,7 +1556,7 @@ private function getTypeSpecifyingExtensionsForType(array $extensions, string $c
return array_merge(...$extensionsForClass);
}

public function resolveEqual(Expr\BinaryOp\Equal $expr, Scope $scope, TypeSpecifierContext $context, Expr $rootExpr): SpecifiedTypes
public function resolveEqual(Expr\BinaryOp\Equal $expr, Scope $scope, TypeSpecifierContext $context, ?Expr $rootExpr): SpecifiedTypes
{
$expressions = $this->findTypeExpressionsFromBinaryOperation($scope, $expr);
if ($expressions !== null) {
Expand Down Expand Up @@ -1673,7 +1673,7 @@ public function resolveEqual(Expr\BinaryOp\Equal $expr, Scope $scope, TypeSpecif
: $leftTypes->normalize($scope)->intersectWith($rightTypes->normalize($scope));
}

public function resolveIdentical(Expr\BinaryOp\Identical $expr, Scope $scope, TypeSpecifierContext $context, Expr $rootExpr): SpecifiedTypes
public function resolveIdentical(Expr\BinaryOp\Identical $expr, Scope $scope, TypeSpecifierContext $context, ?Expr $rootExpr): SpecifiedTypes
{
$leftExpr = $expr->left;
$rightExpr = $expr->right;
Expand Down
27 changes: 26 additions & 1 deletion src/Type/Php/InArrayFunctionTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace PHPStan\Type\Php;

use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Analyser\SpecifiedTypes;
Expand Down Expand Up @@ -49,9 +51,32 @@ public function specifyTypes(FunctionReflection $functionReflection, FuncCall $n
}

$needleExpr = $node->getArgs()[0]->value;
$arrayExpr = $node->getArgs()[1]->value;
if ($arrayExpr instanceof Array_ && $isStrictComparison) {
$types = null;
foreach ($arrayExpr->items as $item) {
if ($item === null) {
continue;
}
$itemTypes = $this->typeSpecifier->resolveIdentical(new Identical($needleExpr, $item->value), $scope, $context, null);

if ($types === null) {
$types = $itemTypes;
continue;
}

$types = $context->true() ? $types->normalize($scope)->intersectWith($itemTypes->normalize($scope)) : $types->unionWith($itemTypes);
}

if ($types !== null) {
return $types;
}
}

$needleType = $scope->getType($needleExpr);
$arrayType = $scope->getType($node->getArgs()[1]->value);
$arrayType = $scope->getType($arrayExpr);
$arrayValueType = $arrayType->getIterableValueType();

$isStrictComparison = $isStrictComparison
|| $needleType->isEnum()->yes()
|| $arrayValueType->isEnum()->yes();
Expand Down

0 comments on commit 0177e33

Please sign in to comment.