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

improve handling of scalar literals with in_array #685

Closed
wants to merge 5 commits into from
Closed
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
23 changes: 22 additions & 1 deletion src/Type/Php/InArrayFunctionTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@
use PHPStan\Analyser\TypeSpecifierAwareExtension;
use PHPStan\Analyser\TypeSpecifierContext;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\ConstantScalarType;
use PHPStan\Type\FunctionTypeSpecifyingExtension;
use PHPStan\Type\TypeUtils;
use PHPStan\Type\UnionType;

class InArrayFunctionTypeSpecifyingExtension implements FunctionTypeSpecifyingExtension, TypeSpecifierAwareExtension
{
Expand Down Expand Up @@ -39,7 +43,24 @@ public function specifyTypes(FunctionReflection $functionReflection, FuncCall $n
return new SpecifiedTypes([], []);
}

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


if ($arrayValueType instanceof UnionType|| $arrayValueType instanceof ConstantScalarType) {
if ($arrayType->isIterableAtLeastOnce()->yes()) {
if ($arrayValueType instanceof ConstantScalarType && $needleType instanceof ConstantScalarType) {
if ($arrayValueType->getValue() !== $needleType->getValue()) {
return new SpecifiedTypes();
}
} else {
return new SpecifiedTypes();
}
} elseif ($needleType instanceof ConstantScalarType) {
return new SpecifiedTypes();
}
}

if (
$context->truthy()
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,7 @@ public function dataFileAsserts(): iterable

yield from $this->gatherAssertTypes(__DIR__ . '/data/eval-implicit-throw.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5628.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5668.php');
}

/**
Expand Down
42 changes: 42 additions & 0 deletions tests/PHPStan/Analyser/data/bug-5668.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php declare(strict_types=1);

namespace Bug5668;

use function PHPStan\Testing\assertType;

class Foo
{

/**
* @param array<int, 'test'|'bar'> $in
*/
function has(array $in): void
{
assertType('bool', in_array('test', $in, true));
}

/**
* @param array<int, 'test'> $in
*/
function has2(array $in): void
{
assertType('bool', in_array('test', $in, true));
}

/**
* @param non-empty-array<int, 'test'|'bar'> $in
*/
function has3(array $in): void
{
assertType('bool', in_array('test', $in, true));
}


/**
* @param non-empty-array<int, 'test'> $in
*/
function has4(array $in): void
{
assertType('true', in_array('test', $in, true));
}
}