Skip to content

Commit

Permalink
InArrayFunctionTypeSpecifyingExtension - fix calls with less than 2 p…
Browse files Browse the repository at this point in the history
…arameters
  • Loading branch information
ondrejmirtes committed Aug 5, 2023
1 parent 1c9e90f commit 4d77e98
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Type/Php/InArrayFunctionTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,16 @@ public function isFunctionSupported(FunctionReflection $functionReflection, Func
public function specifyTypes(FunctionReflection $functionReflection, FuncCall $node, Scope $scope, TypeSpecifierContext $context): SpecifiedTypes
{
$isStrictComparison = false;
if (count($node->getArgs()) >= 3) {
$argsCount = count($node->getArgs());
if ($argsCount >= 3) {
$strictNodeType = $scope->getType($node->getArgs()[2]->value);
$isStrictComparison = (new ConstantBooleanType(true))->isSuperTypeOf($strictNodeType)->yes();
}

if ($argsCount < 2) {
return new SpecifiedTypes();
}

$needleType = $scope->getType($node->getArgs()[0]->value);
$arrayType = $scope->getType($node->getArgs()[1]->value);
$arrayValueType = $arrayType->getIterableValueType();
Expand Down
7 changes: 7 additions & 0 deletions tests/PHPStan/Analyser/AnalyserIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,13 @@ public function testBug1843(): void
$this->assertNoErrors($errors);
}

public function testBug9711(): void
{
$errors = $this->runAnalyse(__DIR__ . '/data/bug-9711.php');
$this->assertCount(1, $errors);
$this->assertSame('Function in_array invoked with 1 parameter, 2-3 required.', $errors[0]->getMessage());
}

public function testBug4713(): void
{
$errors = $this->runAnalyse(__DIR__ . '/data/bug-4713.php');
Expand Down
5 changes: 5 additions & 0 deletions tests/PHPStan/Analyser/data/bug-9711.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace Bug9711;

echo in_array(new \stdClass());

0 comments on commit 4d77e98

Please sign in to comment.