addFile( 'somefile.php', 'vars_in_scope['$b'] = \Psalm\Type::getBool(); $context->vars_in_scope['$foo'] = \Psalm\Type::getArray(); $this->analyzeFile('somefile.php', $context); $this->assertFalse(isset($context->vars_in_scope['$foo[\'a\']'])); } /** * @return iterable,error_levels?:string[]}> */ public function providerValidCodeParse() { return [ 'genericArrayCreationWithSingleIntValue' => [ ' [ '$out' => 'non-empty-list', ], ], 'genericArrayCreationWithInt' => [ ' [ '$out' => 'non-empty-list', ], ], 'generic2dArrayCreation' => [ ' [ '$out' => 'non-empty-list', ], ], 'generic2dArrayCreationAddedInIf' => [ ' 50) { $out[] = $bits; $bits = []; } $bits[] = 4; } $out[] = $bits;', 'assertions' => [ '$out' => 'non-empty-list>', ], ], 'genericArrayCreationWithObjectAddedInIf' => [ ' [ '$out' => 'list', ], ], 'genericArrayCreationWithElementAddedInSwitch' => [ ' [ '$out' => 'list', ], ], 'genericArrayCreationWithElementsAddedInSwitch' => [ ' [ '$out' => 'list', ], ], 'genericArrayCreationWithElementsAddedInSwitchWithNothing' => [ ' [ '$out' => 'list', ], ], 'implicit2dIntArrayCreation' => [ ' [ '$foo' => 'non-empty-list>', ], ], 'implicit3dIntArrayCreation' => [ ' [ '$foo' => 'non-empty-list>>', ], ], 'implicit4dIntArrayCreation' => [ ' [ '$foo' => 'non-empty-list>>>', ], ], 'implicitIndexedIntArrayCreation' => [ ' $text) { $bat[$text] = $bar[$i]; }', 'assertions' => [ '$foo' => 'array{0: string, 1: string, 2: string}', '$bar' => 'array{int, int, int}', '$bat' => 'non-empty-array', ], ], 'implicitStringArrayCreation' => [ ' [ '$foo' => 'array{bar: string}', '$foo[\'bar\']' => 'string', ], ], 'implicit2dStringArrayCreation' => [ ' [ '$foo' => 'array{bar: array{baz: string}}', '$foo[\'bar\'][\'baz\']' => 'string', ], ], 'implicit3dStringArrayCreation' => [ ' [ '$foo' => 'array{bar: array{baz: array{bat: string}}}', '$foo[\'bar\'][\'baz\'][\'bat\']' => 'string', ], ], 'implicit4dStringArrayCreation' => [ ' [ '$foo' => 'array{bar: array{baz: array{bat: array{bap: string}}}}', '$foo[\'bar\'][\'baz\'][\'bat\'][\'bap\']' => 'string', ], ], '2Step2dStringArrayCreation' => [ ' []]; $foo["bar"]["baz"] = "hello";', 'assertions' => [ '$foo' => 'array{bar: array{baz: string}}', '$foo[\'bar\'][\'baz\']' => 'string', ], ], '2StepImplicit3dStringArrayCreation' => [ ' []]; $foo["bar"]["baz"]["bat"] = "hello";', 'assertions' => [ '$foo' => 'array{bar: array{baz: array{bat: string}}}', ], ], 'conflictingTypesWithNoAssignment' => [ ' ["a" => "b"], "baz" => [1] ];', 'assertions' => [ '$foo' => 'array{bar: array{a: string}, baz: array{int}}', ], ], 'implicitTKeyedArrayCreation' => [ ' 1, ]; $foo["baz"] = "a";', 'assertions' => [ '$foo' => 'array{bar: int, baz: string}', ], ], 'conflictingTypesWithAssignment' => [ ' ["a" => "b"], "baz" => [1] ]; $foo["bar"]["bam"]["baz"] = "hello";', 'assertions' => [ '$foo' => 'array{bar: array{a: string, bam: array{baz: string}}, baz: array{int}}', ], ], 'conflictingTypesWithAssignment2' => [ ' [ '$foo' => 'array{a: string, b: non-empty-list}', '$foo[\'a\']' => 'string', '$foo[\'b\']' => 'non-empty-list', '$bar' => 'string', ], ], 'conflictingTypesWithAssignment3' => [ ' [ '$foo' => 'array{a: string, b: array{c: array{d: string}}}', ], ], 'nestedTKeyedArrayAssignment' => [ ' [ '$foo' => 'array{a: array{b: string, c: int}}', ], ], 'conditionalTKeyedArrayAssignment' => [ ' "hello"]; if (rand(0, 10) === 5) { $foo["b"] = 1; } else { $foo["b"] = 2; }', 'assertions' => [ '$foo' => 'array{a: string, b: int}', ], ], 'arrayKey' => [ ' "foo", "b"=> "bar"]; $d = "a"; $e = $c[$d];', 'assertions' => [ '$b' => 'string', '$e' => 'string', ], ], 'conditionalCheck' => [ ' [], ], 'variableKeyArrayCreate' => [ ' [ '$a' => 'array{boop: non-empty-list}', '$c' => 'array{boop: array{boop: non-empty-list}}', ], ], 'assignExplicitValueToGeneric' => [ '> */ $a = []; $a["foo"] = ["bar" => "baz"];', 'assertions' => [ '$a' => 'non-empty-array>', ], ], 'additionWithEmpty' => [ ' [ '$a' => 'array{0: string}', '$b' => 'array{0: string}', ], ], 'additionDifferentType' => [ ' [ '$a' => 'array{0: string}', '$b' => 'array{0: string}', ], ], 'present1dArrayTypeWithVarKeys' => [ '> */ $a = []; $foo = "foo"; $a[$foo][] = "bat";', 'assertions' => [], ], 'present2dArrayTypeWithVarKeys' => [ '>> */ $b = []; $foo = "foo"; $bar = "bar"; $b[$foo][$bar][] = "bat";', 'assertions' => [], ], 'objectLikeWithIntegerKeys' => [ ' [ '$b' => 'string', '$c' => 'int', '$d' => 'string', '$e' => 'int', ], ], 'objectLikeArrayAdditionNotNested' => [ ' [2, 3]];', 'assertions' => [ '$foo' => 'array{a: int, b: array{int, int}}', ], ], 'nestedTKeyedArrayAddition' => [ ' [2, 3]];', 'assertions' => [ '$foo' => 'array{root: array{a: int, b: array{int, int}}}', ], ], 'updateStringIntKey1' => [ ' [ '$a' => 'array{0: int, a: int}', ], ], 'updateStringIntKey2' => [ ' [ '$b' => 'array{0: int, c: int}', ], ], 'updateStringIntKey3' => [ ' [ '$c' => 'array{0: int, c: int}', ], ], 'updateStringIntKey4' => [ ' [ '$d' => 'array{5: int, a: int}', ], ], 'updateStringIntKey5' => [ ' [ '$e' => 'array{5: int, c: int}', ], ], 'updateStringIntKeyWithIntRootAndNumberOffset' => [ ' [ '$a' => 'array{0: array{0: int, a: int}}', ], ], 'updateStringIntKeyWithIntRoot' => [ ' [ '$b' => 'array{0: array{0: int, c: int}}', '$c' => 'array{0: array{0: int, c: int}}', '$d' => 'array{0: array{5: int, a: int}}', '$e' => 'array{0: array{5: int, c: int}}', ], ], 'updateStringIntKeyWithTKeyedArrayRootAndNumberOffset' => [ ' [ '$a' => 'array{root: array{0: int, a: int}}', ], ], 'updateStringIntKeyWithTKeyedArrayRoot' => [ ' [ '$b' => 'array{root: array{0: int, c: int}}', '$c' => 'array{root: array{0: int, c: int}}', '$d' => 'array{root: array{5: int, a: int}}', '$e' => 'array{root: array{5: int, c: int}}', ], ], 'mixedArrayAssignmentWithStringKeys' => [ ' [ ' [ 'id] = $a; } echo $arr[0];', 'assertions' => [], 'error_levels' => ['MixedAssignment', 'MixedPropertyFetch', 'MixedArrayOffset', 'MixedArgument'], ], 'changeTKeyedArrayType' => [ ' "c"]; $a["d"] = ["e" => "f"]; $a["b"] = 4; $a["d"]["e"] = 5;', 'assertions' => [ '$a[\'b\']' => 'int', '$a[\'d\']' => 'array{e: int}', '$a[\'d\'][\'e\']' => 'int', '$a' => 'array{b: int, d: array{e: int}}', ], ], 'changeTKeyedArrayTypeInIf' => [ ' 3) { $a["b"] = new stdClass; } else { $a["b"] = ["e" => "f"]; } if ($a["b"] instanceof stdClass) { $a["b"] = []; } $a["b"]["e"] = "d";', 'assertions' => [ '$a' => 'array{b: array{e: string}}', '$a[\'b\']' => 'array{e: string}', '$a[\'b\'][\'e\']' => 'string', ], ], 'implementsArrayAccess' => [ 'foo();', 'assertions' => [ '$a' => 'A', ], 'error_levels' => ['MixedMethodCall'], ], 'mixedSwallowsArrayAssignment' => [ 'offsetExists("baz");', ], 'implementsArrayAccessInheritingDocblock' => [ ' */ protected $data = []; /** * @param array $data */ public function __construct(array $data = []) { $this->data = $data; } /** * @param string $offset */ public function offsetExists($offset): bool { return isset($this->data[$offset]); } /** * @param string $offset */ public function offsetGet($offset) { return $this->data[$offset]; } /** * @param string $offset * @param mixed $value */ public function offsetSet($offset, $value): void { $this->data[$offset] = $value; } /** * @param string $offset */ public function offsetUnset($offset): void { unset($this->data[$offset]); } } class B extends A { /** * {@inheritdoc} */ public function offsetSet($offset, $value): void { echo "some log"; $this->data[$offset] = $value; } }', 'assertions' => [], 'error_levels' => ['MixedAssignment', 'MixedReturnStatement'], ], 'assignToNullDontDie' => [ ' [ '$a' => 'array{0: non-empty-list}', ], 'error_levels' => ['PossiblyNullArrayAssignment'], ], 'stringAssignment' => [ ' [ '$str' => 'string', ], ], 'ignoreInvalidArrayOffset' => [ ' [], ]; $a["b"]["c"] = 0; foreach ([1, 2, 3] as $i) { /** * @psalm-suppress InvalidArrayOffset * @psalm-suppress MixedOperand * @psalm-suppress PossiblyUndefinedArrayOffset */ $a["b"]["d"] += $a["b"][$i]; }', 'assertions' => [], ], 'keyedIntOffsetArrayValues' => [ ' [ '$a' => 'array{string, int}', '$a_values' => 'non-empty-list', '$a_keys' => 'non-empty-list', ], ], 'changeIntOffsetKeyValuesWithDirectAssignment' => [ ' [ '$b' => 'array{int, int}', ], ], 'changeIntOffsetKeyValuesAfterCopy' => [ ' [ '$b' => 'array{string, int}', '$c' => 'array{int, int}', ], ], 'mergeIntOffsetValues' => [ ' [ '$d' => 'array{0: string, 1: int}', '$e' => 'array{0: string, 1: int, 2: string}', ], ], 'addIntOffsetToEmptyArray' => [ ' [ '$f' => 'array{0: string}', ], ], 'assignArrayOrSetNull' => [ ' [ '$a' => 'non-empty-list|null', ], ], 'assignArrayOrSetNullInElseIf' => [ ' [ '$a' => 'list|null', ], ], 'assignArrayOrSetNullInElse' => [ ' [ '$a' => 'non-empty-list|null', ], ], 'mixedMethodCallArrayAccess' => [ 'foo()] = 1; return $ret["a"]; }', 'assertions' => [], 'error_levels' => ['MixedMethodCall', 'MixedArrayOffset'], ], 'mixedAccessNestedKeys' => [ ' $item) { $arr[$i]["a"]["b"] = 5; $arr[$i]["a"]["c"] = takesString($arr[$i]["a"]["c"]); } return $arr; }', 'assertions' => [], 'error_levels' => [ 'MixedArrayAccess', 'MixedAssignment', 'MixedArrayOffset', 'MixedArrayAssignment', 'MixedArgument', ], ], 'possiblyUndefinedArrayAccessWithIsset' => [ ' 1]; } else { $a = [2, 3]; } if (isset($a[0])) { echo $a[0]; }', ], 'possiblyUndefinedArrayAccessWithArrayKeyExists' => [ ' 1]; } else { $a = [2, 3]; } if (array_key_exists(0, $a)) { echo $a[0]; }', ], 'noCrashOnArrayKeyExistsBracket' => [ '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' => [], 'error_levels' => [ 'MixedArrayAccess', 'MixedAssignment', 'MixedArrayOffset', 'MixedArgument', ], ], 'accessArrayAfterSuppressingBugs' => [ ' [ ' 0, 1, 2, 3]; $arr = [1 => "one", 2 => "two", "three"];', ], 'noDuplicateImplicitIntArrayKeyLargeOffset' => [ ' "A", 95 => "a", "b", ];', ], 'constArrayAssignment' => [ ' 2]; $arr[BAR] = [6]; $bar = $arr[BAR][0];', ], 'castToArray' => [ ' "one"] : 0); $b = (array) null;', 'assertions' => [ '$a' => 'array{0?: int, 1?: string}', '$b' => 'array', ], ], 'coerceListToArray' => [ ' $_bar */ function foo(array $_bar) : void {} /** * @param list $bar */ function baz(array $bar) : void { foo((array) $bar); }', ], 'getOnCoercedArray' => [ ' []] : []; } $out = getArray(); $out["attr"] = (array) ($out["attr"] ?? []); $out["attr"]["bar"] = 1;', 'assertions' => [ '$out[\'attr\'][\'bar\']' => 'int', ], ], 'arrayAssignmentOnMixedArray' => [ ' [], 'error_levels' => ['MixedAssignment'], ], 'implementsArrayAccessAllowNullOffset' => [ ' */ class C implements ArrayAccess { public function offsetExists(int $offset) : bool { return true; } public function offsetGet($offset) : string { return "";} public function offsetSet(?int $offset, string $value) : void {} public function offsetUnset(int $offset) : void { } } $c = new C(); $c[] = "hello";', ], 'addToMixedArray' => [ ' [ 'arr["a"] = "hello"; } if (!$this->arr) {} } }' ], 'arrayAssignmentAddsTypePossibilities' => [ ' 0]; if (is_int($value["a"])) {} }' ], 'falseArrayAssignment' => [ ' [ ' */ function foo(): array { $array = []; /** @psalm-suppress PossiblyNullArrayOffset */ $array[int_or_null()] = null; return $array; }' ], 'coerceNullKeyToZero' => [ ' */ function foo(): array { $array = []; /** @psalm-suppress NullArrayOffset */ $array[null] = null; return $array; }' ], 'listUsedAsArray' => [ ' [ '$a' => 'non-empty-list' ], ], 'listTakesEmptyArray' => [ ' $arr */ function takesList(array $arr) : void {} $a = []; takesList($a);', 'assertions' => [ '$a' => 'array' ], ], 'listCreatedInSingleStatementUsedAsArray' => [ ' $arr */ function takesList(array $arr) : void {} $a = [1, 2]; takesArray($a); takesList($a); $a[] = 3; takesArray($a); takesList($a); $b = $a; $b[] = rand(0, 10);', 'assertions' => [ '$a' => 'array{int, int, int}', '$b' => 'array{int, int, int, int}', ], ], 'listMergedWithTKeyedArrayList' => [ ' $arr */ function takesAnotherList(array $arr) : void {} /** @param list $arr */ function takesList(array $arr) : void { if (rand(0, 1)) { $arr = [1, 2, 3]; } takesAnotherList($arr); }', ], 'listMergedWithTKeyedArrayListAfterAssertion' => [ ' $arr */ function takesAnotherList(array $arr) : void {} /** @param list $arr */ function takesList(array $arr) : void { if ($arr) { $arr = [4, 5, 6]; } takesAnotherList($arr); }', ], 'nonEmptyAssertionOnListElement' => [ '> $arr */ function takesList(array $arr) : void { if (!empty($arr[0])) { foreach ($arr[0] as $k => $v) {} } }', ], 'nonEmptyAssignmentToListElement' => [ ' $arr * @return non-empty-list */ function takesList(array $arr) : array { $arr[0] = "food"; return $arr; }', ], 'unpackedArgIsList' => [ ' */ private $ints = []; public function set(int ...$ints): void { $this->ints = $ints; } }' ], 'assignStringFirstChar' => [ ' $arr */ function foo(array $arr) : string { $arr[0][0] = "a"; return $arr[0]; }' ], 'arraySpread' => [ ' 1, 1 => 2, 3]; $arr2 = [...$arr1]; $arr3 = [1 => 0, ...$arr1];', [ '$result' => 'array{int, int, int, int, int, int, int, int}', '$arr2' => 'array{int, int, int}', '$arr3' => 'array{1: int, 2: int, 3: int, 4: int}', ] ], 'listPropertyAssignmentAfterIsset' => [ ' */ private $list = []; public function override(int $offset): void { if (isset($this->list[$offset])) { $this->list[$offset] = "a"; } } }', ], 'propertyAssignmentToTKeyedArrayIntKeys' => [ 'baz[rand(0, 1) ? 0 : 1] = $str; } }' ], 'propertyAssignmentToTKeyedArrayStringKeys' => [ ' "c", "b" => "d"]; public function append(string $str) : void { $this->baz[rand(0, 1) ? "a" : "b"] = $str; } }', ], 'arrayMixedMixedNotAllowedFromObject' => [ ' $v) { $arr[$k] = $v; } return $arr; }', ], 'arrayMixedMixedNotAllowedFromMixed' => [ ' "foo"]; /** * @psalm-suppress MixedAssignment * @psalm-suppress MixedArrayOffset */ foreach ($a as $k => $v) { $arr[$k] = $v; } return $arr; }', ], 'assignNestedKey' => [ ' */ function getAutoComplete(array $data): array { $response = ["s" => []]; foreach ($data as $suggestion) { $response["s"][$suggestion] = true; } return $response["s"]; }' ], 'assignArrayUnion' => [ ' [ ' 'array', ] ], 'dontUpdateMixedArrayWithStringKey' => [ ' [ ' 'mixed' ] ], 'assignWithLiteralStringKey' => [ ' $i * @return array */ function addOneEntry(array $i, int $id): array { $i[$id][rand(0, 1) ? "internal" : "ported"] = true; return $i; }' ], 'binaryOperation' => [ ' ""] ); $a += ["e" => new RuntimeException()];', [ '$a' => 'array{c: RuntimeException, e: RuntimeException}', ] ], 'mergeArrayKeysProperly' => [ ', bool> $arr * @return array, bool> */ function createForEntity(array $arr) { $arr[SomeEntity::class] = true; return $arr; }' ], 'lowercaseStringMergeWithLiteral' => [ ' $foo * @return array */ function foo(array $foo) : array { $foo["hello"] = true; return $foo; }' ], 'updateListValueAndMaintainListnessAfterGreaterThanOrEqual' => [ ' $l * @return list */ function takesList(array $l) { if (count($l) < 2) { throw new \Exception("bad"); } $l[1] = $l[1] + 1; return $l; }' ], 'updateListValueAndMaintainListnessAfterNotIdentical' => [ ' $l * @return list */ function takesList(array $l) { if (count($l) !== 2) { throw new \Exception("bad"); } $l[1] = $l[1] + 1; return $l; }' ], ]; } /** * @return iterable */ public function providerInvalidCodeParse() { return [ 'objectAssignment' => [ ' 'UndefinedMethod', ], 'invalidArrayAccess' => [ ' 'InvalidArrayAssignment', ], 'possiblyUndefinedArrayAccess' => [ ' 1]; } else { $a = [2, 3]; } echo $a[0];', 'error_message' => 'PossiblyUndefinedArrayOffset', ], 'mixedStringOffsetAssignment' => [ ' 'MixedStringOffsetAssignment', 'error_level' => ['MixedAssignment'], ], 'mixedArrayArgument' => [ ' $foo */ function fooFoo(array $foo): void { } function barBar(array $bar): void { fooFoo($bar); } barBar([1, "2"]);', 'error_message' => 'MixedArgumentTypeCoercion', 'error_level' => ['MixedAssignment'], ], 'arrayPropertyAssignment' => [ 'strs = [new stdClass()]; // no issue emitted } }', 'error_message' => 'InvalidPropertyAssignmentValue', ], 'incrementalArrayPropertyAssignment' => [ 'strs[] = new stdClass(); // no issue emitted } }', 'error_message' => 'InvalidPropertyAssignmentValue', ], 'possiblyUndefinedArrayAccessWithArrayKeyExistsOnWrongKey' => [ ' 1]; } else { $a = [2, 3]; } if (array_key_exists("a", $a)) { echo $a[0]; }', 'error_message' => 'PossiblyUndefinedArrayOffset', ], 'possiblyUndefinedArrayAccessWithArrayKeyExistsOnMissingKey' => [ ' 1]; } else { $a = [2, 3]; } if (array_key_exists("b", $a)) { echo $a[0]; }', 'error_message' => 'PossiblyUndefinedArrayOffset', ], 'duplicateStringArrayKey' => [ ' 1, "b" => 2, "c" => 3, "c" => 4, ];', 'error_message' => 'DuplicateArrayKey', ], 'duplicateIntArrayKey' => [ ' 1, 1 => 2, 2 => 3, 2 => 4, ];', 'error_message' => 'DuplicateArrayKey', ], 'duplicateImplicitIntArrayKey' => [ ' 4, ];', 'error_message' => 'DuplicateArrayKey', ], 'mixedArrayAssignmentOnVariable' => [ ' 'MixedArrayAssignment', ], 'implementsArrayAccessPreventNullOffset' => [ ' */ class C implements ArrayAccess { public function offsetExists(int $offset) : bool { return true; } public function offsetGet($offset) : string { return "";} public function offsetSet(int $offset, string $value) : void {} public function offsetUnset(int $offset) : void { } } $c = new C(); $c[] = "hello";', 'error_message' => 'NullArgument', ], 'storageKeyMustBeObject' => [ ' 'InvalidArgument', ], 'listUsedAsArrayWrongType' => [ ' 'InvalidScalarArgument', ], 'listUsedAsArrayWrongListType' => [ ' $arr */ function takesArray(array $arr) : void {} $a = []; $a[] = 1; $a[] = 2; takesArray($a);', 'error_message' => 'InvalidScalarArgument', ], 'nonEmptyAssignmentToListElementChangeType' => [ ' $arr * @return non-empty-list */ function takesList(array $arr) : array { $arr[0] = 5; return $arr; }', 'error_message' => 'InvalidReturnStatement', ], 'preventArrayAssignmentOnReturnValue' => [ 'foo()[3] = 5;', 'error_message' => 'InvalidArrayAssignment', ], 'mergeIntWithMixed' => [ ' 'InvalidReturnStatement', ], 'mergeIntWithNestedMixed' => [ ' 'InvalidReturnStatement', ], 'mergeWithDeeplyNestedArray' => [ ' 'NullableReturnStatement', ], ]; } }