Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
in_array returns false in strict mode if types are incompatibles
see #5552
- Loading branch information
Showing
5 changed files
with
158 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/Psalm/Internal/Provider/ReturnTypeProvider/InArrayReturnTypeProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
namespace Psalm\Internal\Provider\ReturnTypeProvider; | ||
|
||
use Psalm\Internal\Type\Comparator\UnionTypeComparator; | ||
use Psalm\Plugin\EventHandler\Event\FunctionReturnTypeProviderEvent; | ||
use Psalm\Plugin\EventHandler\FunctionReturnTypeProviderInterface; | ||
use Psalm\Type; | ||
use Psalm\Type\Atomic\TArray; | ||
use Psalm\Type\Atomic\TKeyedArray; | ||
use Psalm\Type\Atomic\TList; | ||
|
||
class InArrayReturnTypeProvider implements FunctionReturnTypeProviderInterface | ||
{ | ||
/** | ||
* @return array<lowercase-string> | ||
*/ | ||
public static function getFunctionIds(): array | ||
{ | ||
return ['in_array']; | ||
} | ||
|
||
public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $event): Type\Union | ||
{ | ||
$call_args = $event->getCallArgs(); | ||
$bool = Type::getBool(); | ||
|
||
if (!isset($call_args[0]) || !isset($call_args[1])) { | ||
return $bool; | ||
} | ||
|
||
$needle_type = $event->getStatementsSource()->getNodeTypeProvider()->getType($call_args[0]->value); | ||
$haystack_type = $event->getStatementsSource()->getNodeTypeProvider()->getType($call_args[1]->value); | ||
|
||
if ($needle_type === null || $haystack_type === null) { | ||
return $bool; | ||
} | ||
|
||
$false = Type::getFalse(); | ||
$false->from_docblock = $bool->from_docblock = $needle_type->from_docblock || $haystack_type->from_docblock; | ||
|
||
if (!isset($call_args[2])) { | ||
return $bool; | ||
} | ||
|
||
$strict_type = $event->getStatementsSource()->getNodeTypeProvider()->getType($call_args[2]->value); | ||
|
||
if ($strict_type === null || !$strict_type->isTrue()) { | ||
return $bool; | ||
} | ||
|
||
/** | ||
* @var TKeyedArray|TArray|TList|null | ||
*/ | ||
$array_arg_type = ($types = $haystack_type->getAtomicTypes()) && isset($types['array']) | ||
? $types['array'] | ||
: null; | ||
|
||
if ($array_arg_type instanceof TKeyedArray) { | ||
$array_arg_type = $array_arg_type->getGenericArrayType(); | ||
} | ||
|
||
if ($array_arg_type instanceof TList) { | ||
$array_arg_type = new TArray([Type::getInt(), $array_arg_type->type_param]); | ||
} | ||
|
||
if (!$array_arg_type instanceof TArray) { | ||
return $bool; | ||
} | ||
|
||
$haystack_item_type = $array_arg_type->type_params[1]; | ||
|
||
if (UnionTypeComparator::canExpressionTypesBeIdentical( | ||
$event->getStatementsSource()->getCodebase(), | ||
$needle_type, | ||
$haystack_item_type | ||
)) { | ||
return $bool; | ||
} | ||
|
||
return $false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
namespace Psalm\Tests\ReturnTypeProvider; | ||
|
||
use Psalm\Tests\TestCase; | ||
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait; | ||
|
||
class InArrayTest extends TestCase | ||
{ | ||
use ValidCodeAnalysisTestTrait; | ||
|
||
public function providerValidCodeParse(): iterable | ||
{ | ||
yield 'inArrayNonStrictCallReturnsBoolWhenTypesAreCompatible' => [ | ||
'<?php | ||
/** | ||
* @return string[] | ||
*/ | ||
function f(): array { | ||
return ["1"]; | ||
} | ||
$ret = in_array("1", f()); | ||
', | ||
['$ret' => 'bool'], | ||
]; | ||
|
||
yield 'inArrayNonStrictCallReturnsBoolWhenTypesAreIncompatible' => [ | ||
'<?php | ||
/** | ||
* @return string[] | ||
*/ | ||
function f(): array { | ||
return ["1"]; | ||
} | ||
$ret = in_array(1, f()); | ||
', | ||
['$ret' => 'bool'], | ||
]; | ||
|
||
yield 'inArrayStrictCallReturnsFalseWhenTypesAreIncompatible' => [ | ||
'<?php | ||
/** | ||
* @return string[] | ||
*/ | ||
function f(): array { | ||
return ["1"]; | ||
} | ||
$ret = in_array(1, f(), true); | ||
', | ||
['$ret' => 'false'], | ||
]; | ||
|
||
yield 'inArrayStrictCallReturnsBoolWhenTypesAreCompatible' => [ | ||
'<?php | ||
/** | ||
* @return string[] | ||
*/ | ||
function f(): array { | ||
return ["1"]; | ||
} | ||
$ret = in_array("1", f(), true); | ||
', | ||
['$ret' => 'bool'], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters