1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-14 18:36:58 +01:00
psalm/tests/TypeReconciliation/ArrayKeyExistsTest.php

596 lines
23 KiB
PHP
Raw Normal View History

2020-12-02 20:42:16 +01:00
<?php
2020-12-02 20:42:16 +01:00
namespace Psalm\Tests\TypeReconciliation;
2022-09-17 15:11:28 +02:00
use Psalm\Config;
use Psalm\Context;
2021-12-03 20:11:20 +01:00
use Psalm\Tests\TestCase;
use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait;
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
class ArrayKeyExistsTest extends TestCase
2020-12-02 20:42:16 +01:00
{
2021-12-03 20:11:20 +01:00
use ValidCodeAnalysisTestTrait;
use InvalidCodeAnalysisTestTrait;
2020-12-02 20:42:16 +01:00
/**
Add support for strict arrays, fix type alias intersection, fix array_is_list assertion on non-lists (#8395) * Immutable CodeLocation * Remove excess clones * Remove external clones * Remove leftover clones * Fix final clone issue * Immutable storages * Refactoring * Fixes * Fixes * Fix * Fix * Fixes * Simplify * Fixes * Fix * Fixes * Update * Fix * Cache global types * Fix * Update * Update * Fixes * Fixes * Refactor * Fixes * Fix * Fix * More caching * Fix * Fix * Update * Update * Fix * Fixes * Update * Refactor * Update * Fixes * Break one more test * Fix * FIx * Fix * Fix * Fix * Fix * Improve performance and readability * Equivalent logic * Fixes * Revert * Revert "Revert" This reverts commit f9175100c8452c80559234200663fd4c4f4dd889. * Fix * Fix reference bug * Make default TypeVisitor immutable * Bugfix * Remove clones * Partial refactoring * Refactoring * Fixes * Fix * Fixes * Fixes * cs-fix * Fix final bugs * Add test * Misc fixes * Update * Fixes * Experiment with removing different property * revert "Experiment with removing different property" This reverts commit ac1156e077fc4ea633530d51096d27b6e88bfdf9. * Uniform naming * Uniform naming * Hack hotfix * Clean up $_FILES ref #8621 * Undo hack, try fixing properly * Helper method * Remove redundant call * Partially fix bugs * Cleanup * Change defaults * Fix bug * Fix (?, hope this doesn't break anything else) * cs-fix * Review fixes * Bugfix * Bugfix * Improve logic * Add support for list{} and callable-list{} types, properly implement array_is_list assertions (fixes #8389) * Default to sealed arrays * Fix array_merge bug * Fixes * Fix * Sealed type checks * Properly infer properties-of and get_object_vars on final classes * Fix array_map zipping * Fix tests * Fixes * Fixes * Fix more stuff * Recursively resolve type aliases * Fix typo * Fixes * Fix array_is_list assertion on keyed array * Add BC docs * Fixes * fix * Update * Update * Update * Update * Seal arrays with count assertions * Fix #8528 * Fix * Update * Improve sealed array foreach logic * get_object_vars on template properties * Fix sealed array assertion reconciler logic * Improved reconciler * Add tests * Single source of truth for test types * Fix tests * Fixup tests * Fixup tests * Fixup tests * Update * Fix tests * Fix tests * Final fixes * Fixes * Use list syntax only when needed * Fix tests * Cs-fix * Update docs * Update docs * Update docs * Update docs * Update docs * Document missing types * Update docs * Improve class-string-map docs * Update * Update * I love working on psalm :) * Keep arrays unsealed by default * Fixup tests * Fix syntax mistake * cs-fix * Fix typo * Re-import missing types * Keep strict types only in return types * argc/argv fixes * argc/argv fixes * Fix test * Comment-out valinor code, pinging @romm pls merge https://github.com/CuyZ/Valinor/pull/246 so we can add valinor to the psalm docs :)
2022-11-05 22:34:42 +01:00
*
2020-12-02 20:42:16 +01:00
*/
public function providerValidCodeParse(): iterable
{
return [
'arrayKeyExistsOnStringArrayShouldInformArrayness' => [
'code' => '<?php
2020-12-02 20:42:16 +01:00
/**
* @param string[] $a
* @return array{b: string, ...}
2020-12-02 20:42:16 +01:00
*/
function foo(array $a) {
if (array_key_exists("b", $a)) {
return $a;
}
throw new \Exception("bad");
}'
],
'arrayKeyExistsThrice' => [
'code' => '<?php
2020-12-02 20:42:16 +01:00
function three(array $a): void {
if (!array_key_exists("a", $a)
|| !array_key_exists("b", $a)
|| !array_key_exists("c", $a)
|| (!is_string($a["a"]) && !is_int($a["a"]))
|| (!is_string($a["b"]) && !is_int($a["b"]))
|| (!is_string($a["c"]) && !is_int($a["c"]))
) {
throw new \Exception();
}
echo $a["a"];
echo $a["b"];
}'
],
'arrayKeyExistsTwice' => [
'code' => '<?php
2020-12-02 20:42:16 +01:00
function two(array $a): void {
if (!array_key_exists("a", $a) || !(is_string($a["a"]) || is_int($a["a"])) ||
!array_key_exists("b", $a) || !(is_string($a["b"]) || is_int($a["b"]))
) {
throw new \Exception();
}
echo $a["a"];
echo $a["b"];
}'
],
'assertConstantOffsetsInMethod' => [
'code' => '<?php
2020-12-02 20:42:16 +01:00
class C {
public const ARR = [
"a" => ["foo" => true],
"b" => []
];
public function bar(string $key): bool {
if (!array_key_exists($key, self::ARR) || !array_key_exists("foo", self::ARR[$key])) {
return false;
}
return self::ARR[$key]["foo"];
}
}',
'assertions' => [],
'ignored_issues' => ['MixedReturnStatement', 'MixedInferredReturnType'],
2020-12-02 20:42:16 +01:00
],
'assertSelfClassConstantOffsetsInFunction' => [
'code' => '<?php
2020-12-02 20:42:16 +01:00
namespace Ns;
class C {
public const ARR = [
"a" => ["foo" => true],
"b" => []
];
public function bar(?string $key): bool {
if ($key === null || !array_key_exists($key, self::ARR) || !array_key_exists("foo", self::ARR[$key])) {
return false;
}
return self::ARR[$key]["foo"];
}
}',
'assertions' => [],
'ignored_issues' => ['MixedReturnStatement', 'MixedInferredReturnType'],
2020-12-02 20:42:16 +01:00
],
'assertNamedClassConstantOffsetsInFunction' => [
'code' => '<?php
2020-12-02 20:42:16 +01:00
namespace Ns;
class C {
public const ARR = [
"a" => ["foo" => true],
"b" => [],
];
}
function bar(?string $key): bool {
if ($key === null || !array_key_exists($key, C::ARR) || !array_key_exists("foo", C::ARR[$key])) {
return false;
}
return C::ARR[$key]["foo"];
}',
'assertions' => [],
'ignored_issues' => ['MixedReturnStatement', 'MixedInferredReturnType'],
2020-12-02 20:42:16 +01:00
],
'possiblyUndefinedArrayAccessWithArrayKeyExists' => [
'code' => '<?php
2020-12-02 20:42:16 +01:00
if (rand(0,1)) {
$a = ["a" => 1];
} else {
$a = [2, 3];
}
if (array_key_exists(0, $a)) {
echo $a[0];
}',
],
'arrayKeyExistsShoudldNotModifyIntType' => [
'code' => '<?php
2020-12-02 20:42:16 +01:00
class HttpError {
const ERRS = [
403 => "a",
404 => "b",
500 => "c"
];
}
function init(string $code) : string {
if (array_key_exists($code, HttpError::ERRS)) {
return $code;
}
return "";
}'
],
'arrayKeyExistsWithClassConst' => [
'code' => '<?php
2020-12-02 20:42:16 +01:00
class C {}
class D {}
class A {
const FLAGS = [
0 => [C::class => "foo"],
1 => [D::class => "bar"],
];
private function foo(int $i) : void {
if (array_key_exists(C::class, self::FLAGS[$i])) {
echo self::FLAGS[$i][C::class];
}
}
}'
],
'constantArrayKeyExistsWithClassConstant' => [
'code' => '<?php
2020-12-02 20:42:16 +01:00
class Foo {
public const F = "key";
}
/** @param array{key?: string} $a */
2020-12-02 20:42:16 +01:00
function one(array $a): void {
if (array_key_exists(Foo::F, $a)) {
echo $a[Foo::F];
}
}'
],
'assertTypeNarrowedByNestedIsset' => [
'code' => '<?php
2020-12-02 20:42:16 +01:00
/**
* @psalm-suppress MixedMethodCall
* @psalm-suppress MixedArgument
*/
function foo(array $array = []): void {
if (array_key_exists("a", $array)) {
echo $array["a"];
}
if (array_key_exists("b", $array)) {
echo $array["b"]->format("Y-m-d");
}
}',
],
'assertArrayKeyExistsRefinesType' => [
'code' => '<?php
2020-12-02 20:42:16 +01:00
class Foo {
public const DAYS = [
1 => "mon",
2 => "tue",
3 => "wed",
4 => "thu",
5 => "fri",
6 => "sat",
7 => "sun",
];
/** @param key-of<self::DAYS> $dayNum*/
private static function doGetDayName(int $dayNum): string {
return self::DAYS[$dayNum];
}
/** @throws LogicException */
public static function getDayName(int $dayNum): string {
if (! array_key_exists($dayNum, self::DAYS)) {
throw new \LogicException();
}
return self::doGetDayName($dayNum);
}
}'
],
'arrayKeyExistsInferString' => [
'code' => '<?php
2020-12-02 20:42:16 +01:00
function foo(mixed $file) : string {
/** @psalm-suppress MixedArgument */
if (array_key_exists($file, ["a" => 1, "b" => 2])) {
return $file;
}
return "";
}',
'assertions' => [],
'ignored_issues' => [],
'php_version' => '8.0'
2020-12-02 20:42:16 +01:00
],
'arrayKeyExistsComplex' => [
'code' => '<?php
2020-12-02 20:42:16 +01:00
class A {
private const MAP = [
"a" => 1,
"b" => 2,
"c" => 3,
"d" => 4,
"e" => 5,
"f" => 6,
"g" => 7,
"h" => 8,
"i" => 9,
"j" => 10,
"k" => 11,
];
public function doWork(string $a): void {
if (!array_key_exists($a, self::MAP)) {}
}
}'
],
'arrayKeyExistsAccess' => [
'code' => '<?php
2020-12-02 20:42:16 +01:00
/** @param array<int, string> $arr */
function foo(array $arr) : void {
if (array_key_exists(1, $arr)) {
$a = ($arr[1] === "b") ? true : false;
}
}',
],
'arrayKeyExistsVariable' => [
'code' => '<?php
class pony
{
}
/**
* @param array{0?: string, test?: string, pony?: string} $params
* @return string|null
*/
function a(array $params = [])
{
foreach ([0, "test", pony::class] as $key) {
if (\array_key_exists($key, $params)) {
return $params[$key];
}
}
}'
],
2020-12-02 20:42:16 +01:00
'noCrashOnArrayKeyExistsBracket' => [
'code' => '<?php
2020-12-02 20:42:16 +01:00
class MyCollection {
/**
* @param int $commenter
* @param int $numToGet
* @return int[]
*/
public function getPosters($commenter, $numToGet=10) {
$posters = array();
$count = 0;
$a = new ArrayObject([[1234]]);
$iter = $a->getIterator();
while ($iter->valid() && $count < $numToGet) {
$value = $iter->current();
if ($value[0] != $commenter) {
if (!array_key_exists($value[0], $posters)) {
$posters[$value[0]] = 1;
$count++;
}
}
$iter->next();
}
return array_keys($posters);
}
}',
'assertions' => [],
'ignored_issues' => [
2020-12-02 20:42:16 +01:00
'MixedArrayAccess', 'MixedAssignment', 'MixedArrayOffset',
'MixedArgument',
],
],
'arrayKeyExistsTwoVars' => [
'code' => '<?php
/**
* @param array{a: string, b: string, c?: string} $info
*/
function getReason(array $info, string $key, string $value): bool {
if (array_key_exists($key, $info) && $info[$key] === $value) {
return true;
}
return false;
}'
],
'allowIntKeysToo' => [
'code' => '<?php
/**
* @param array<1|2|3, string> $arr
* @return 1|2|3
*/
function checkArrayKeyExistsInt(array $arr, int $int): int
{
if (array_key_exists($int, $arr)) {
return $int;
}
return 1;
}'
],
'comparesStringAndAllIntKeysCorrectly' => [
'code' => '<?php
/**
* @param array<1|2|3, string> $arr
* @return bool
*/
function checkArrayKeyExistsComparison(array $arr, string $key): bool
{
if (array_key_exists($key, $arr)) {
return true;
}
return false;
}'
],
'comparesStringAndAllIntKeysCorrectlyNegated' => [
'code' => '<?php
/**
* @param array<1|2|3, string> $arr
* @return bool
*/
function checkArrayKeyExistsComparisonNegated(array $arr, string $key): bool
{
if (!array_key_exists($key, $arr)) {
return false;
}
return true;
}'
],
2022-01-21 00:26:37 +01:00
'arrayKeyExistsAssertBothWays' => [
'code' => '<?php
class a {
const STATE_A = 0;
const STATE_B = 1;
const STATE_C = 2;
/**
* @return array<self::STATE_*, non-empty-string>
* @psalm-pure
*/
public static function getStateLabels(): array {
return [
self::STATE_A => "A",
self::STATE_B => "B",
self::STATE_C => "C",
];
}
/**
* @param self::STATE_* $state
*/
public static function getStateLabelIf(int $state): string {
$states = self::getStateLabels();
if (array_key_exists($state, $states)) {
return $states[$state];
}
return "";
}
}'
],
'arrayKeyExistsComplex2' => [
'code' => '<?php
/** @var array{
* address_components: list<array{
* long_name: string,
* short_name: string,
* types: list<("accounting"|"administrative_area_level_1"|"administrative_area_level_2"|"administrative_area_level_3"|
* "administrative_area_level_4"|"administrative_area_level_5"|"airport"|"amusement_park"|"art_gallery"|"bar"|"bus_station"|"cafe"|
* "campground"|"car_rental"|"cemetery"|"colloquial_area"|"continent"|"country"|"courthouse"|"embassy"|"establishment"|"finance"|
* "floor"|"food"|"funeral_home"|"general_contractor"|"gym"|"health"|"hospital"|"intersection"|"lawyer"|"light_rail_station"|
* "local_government_office"|"locality"|"lodging"|"moving_company"|"museum"|"natural_feature"|"neighborhood"|"night_club"|"park"|
* "parking"|"plus_code"|"point_of_interest"|"police"|"political"|"post_box"|"post_office"|"postal_code"|"postal_code_prefix"|
* "postal_code_suffix"|"postal_town"|"premise"|"real_estate_agency"|"restaurant"|"route"|"rv_park"|"school"|"spa"|"storage"|"store"|
* "street_address"|"street_number"|"sublocality"|"sublocality_level_1"|"sublocality_level_2"|"sublocality_level_3"|
* "sublocality_level_4"|"sublocality_level_5"|"subpremise"|"subway_station"|"tourist_attraction"|"town_square"|"train_station"|
* "transit_station"|"travel_agency"|"university"|"ward"|"zoo")>
* }>,
* formatted_address: string,
* geometry: array{
* location: array{ lat: float, lng: float },
* location_type: string,
* viewport: array{
* northeast: array{ lat: float, lng: float },
* southwest: array{ lat: float, lng: float }
* }
* },
* partial_match: bool,
* types: list<string>
* }
*/
$data = [];
$cmp = [];
foreach ($data["address_components"] as $component) {
foreach ($component["types"] as $type) {
$cmp[$type] = $component["long_name"];
}
}
if (!\array_key_exists("locality", $cmp)) {
$cmp["locality"] = "";
}
if (!\array_key_exists("administrative_area_level_1", $cmp)) {
$cmp["administrative_area_level_1"] = "";
}
if ($cmp["administrative_area_level_1"] === "test") {
$cmp["administrative_area_level_1"] = "";
}'
],
'arrayKeyExistsPoorPerformance' => [
'code' => '<?php
class A {
private const CRITICAL_ERRORS = [
"category" => [],
"name" => [],
"geo" => [],
"city" => [],
"url" => [],
"comment_critical" => [],
"place" => [],
"price" => [],
"robot_error" => [],
"manual" => [],
"contacts" => [],
"not_confirmed_by_other_source" => [],
];
public function isCriticalError(int|string $key): bool {
if (!\array_key_exists($key, A::CRITICAL_ERRORS)) {
return false;
}
return true;
}
}',
'assertions' => [],
'ignored_issues' => [],
'php_version' => '8.0',
],
2020-12-02 20:42:16 +01:00
];
}
/**
Add support for strict arrays, fix type alias intersection, fix array_is_list assertion on non-lists (#8395) * Immutable CodeLocation * Remove excess clones * Remove external clones * Remove leftover clones * Fix final clone issue * Immutable storages * Refactoring * Fixes * Fixes * Fix * Fix * Fixes * Simplify * Fixes * Fix * Fixes * Update * Fix * Cache global types * Fix * Update * Update * Fixes * Fixes * Refactor * Fixes * Fix * Fix * More caching * Fix * Fix * Update * Update * Fix * Fixes * Update * Refactor * Update * Fixes * Break one more test * Fix * FIx * Fix * Fix * Fix * Fix * Improve performance and readability * Equivalent logic * Fixes * Revert * Revert "Revert" This reverts commit f9175100c8452c80559234200663fd4c4f4dd889. * Fix * Fix reference bug * Make default TypeVisitor immutable * Bugfix * Remove clones * Partial refactoring * Refactoring * Fixes * Fix * Fixes * Fixes * cs-fix * Fix final bugs * Add test * Misc fixes * Update * Fixes * Experiment with removing different property * revert "Experiment with removing different property" This reverts commit ac1156e077fc4ea633530d51096d27b6e88bfdf9. * Uniform naming * Uniform naming * Hack hotfix * Clean up $_FILES ref #8621 * Undo hack, try fixing properly * Helper method * Remove redundant call * Partially fix bugs * Cleanup * Change defaults * Fix bug * Fix (?, hope this doesn't break anything else) * cs-fix * Review fixes * Bugfix * Bugfix * Improve logic * Add support for list{} and callable-list{} types, properly implement array_is_list assertions (fixes #8389) * Default to sealed arrays * Fix array_merge bug * Fixes * Fix * Sealed type checks * Properly infer properties-of and get_object_vars on final classes * Fix array_map zipping * Fix tests * Fixes * Fixes * Fix more stuff * Recursively resolve type aliases * Fix typo * Fixes * Fix array_is_list assertion on keyed array * Add BC docs * Fixes * fix * Update * Update * Update * Update * Seal arrays with count assertions * Fix #8528 * Fix * Update * Improve sealed array foreach logic * get_object_vars on template properties * Fix sealed array assertion reconciler logic * Improved reconciler * Add tests * Single source of truth for test types * Fix tests * Fixup tests * Fixup tests * Fixup tests * Update * Fix tests * Fix tests * Final fixes * Fixes * Use list syntax only when needed * Fix tests * Cs-fix * Update docs * Update docs * Update docs * Update docs * Update docs * Document missing types * Update docs * Improve class-string-map docs * Update * Update * I love working on psalm :) * Keep arrays unsealed by default * Fixup tests * Fix syntax mistake * cs-fix * Fix typo * Re-import missing types * Keep strict types only in return types * argc/argv fixes * argc/argv fixes * Fix test * Comment-out valinor code, pinging @romm pls merge https://github.com/CuyZ/Valinor/pull/246 so we can add valinor to the psalm docs :)
2022-11-05 22:34:42 +01:00
*
2020-12-02 20:42:16 +01:00
*/
public function providerInvalidCodeParse(): iterable
{
return [
'possiblyUndefinedArrayAccessWithArrayKeyExistsOnWrongKey' => [
'code' => '<?php
2020-12-02 20:42:16 +01:00
if (rand(0,1)) {
$a = ["a" => 1];
} else {
$a = [2, 3];
}
if (array_key_exists("a", $a)) {
echo $a[0];
}',
'error_message' => 'PossiblyUndefinedArrayOffset',
],
'possiblyUndefinedArrayAccessWithArrayKeyExistsOnMissingKey' => [
'code' => '<?php
2020-12-02 20:42:16 +01:00
if (rand(0,1)) {
$a = ["a" => 1];
} else {
$a = [2, 3];
}
if (array_key_exists("b", $a)) {
echo $a[0];
}',
'error_message' => 'PossiblyUndefinedArrayOffset',
],
'dontCreateWeirdString' => [
'code' => '<?php
/**
* @psalm-param array{inner:string} $options
*/
function go(array $options): void {
if (!array_key_exists(\'size\', $options)) {
throw new Exception(\'bad\');
}
/** @psalm-suppress MixedArgument */
echo $options[\'\\\'size\\\'\'];
}',
'error_message' => 'InvalidArrayOffset',
],
2020-12-02 20:42:16 +01:00
];
}
2022-09-17 15:11:28 +02:00
public function testAllowPropertyFetchAsNeedle(): void
{
Config::getInstance()->ensure_array_int_offsets_exist = true;
$this->addFile(
'somefile.php',
'<?php
class Foo {
/** @var self::STATE_* $status */
public int $status = self::STATE_A;
public const STATE_A = 0;
public const STATE_B = 1;
}
$foo = new Foo;
/** @var array<string> $bar */
$bar = [];
if (array_key_exists($foo->status, $bar)) {
echo $bar[$foo->status];
}'
);
$this->analyzeFile('somefile.php', new Context());
}
2022-09-17 16:02:25 +02:00
public function testAllowStaticPropertyFetchAsNeedle(): void
{
Config::getInstance()->ensure_array_int_offsets_exist = true;
$this->addFile(
'somefile.php',
'<?php
class Foo {
/** @var self::STATE_* $status */
public static int $status = self::STATE_A;
public const STATE_A = 0;
public const STATE_B = 1;
}
/** @var array<string> $bar */
$bar = [];
if (array_key_exists(Foo::$status, $bar)) {
echo $bar[Foo::$status];
}'
);
$this->analyzeFile('somefile.php', new Context());
}
2020-12-02 20:42:16 +01:00
}