addFile( 'somefile.php', 'project_checker); $context = new Context(); $context->vars_in_scope['$b'] = \Psalm\Type::getBool(); $context->vars_in_scope['$foo'] = \Psalm\Type::getArray(); $file_checker->visitAndAnalyzeMethods($context); $this->assertFalse(isset($context->vars_in_scope['$foo[\'a\']'])); } /** * @return array */ public function providerFileCheckerValidCodeParse() { return [ 'genericArrayCreationWithInt' => [ ' [ '$out' => 'array', ], ], 'generic2dArrayCreation' => [ ' [ '$out' => 'array', ], ], 'generic2dArrayCreationAddedInIf' => [ ' 50) { $out[] = $bits; $bits = []; } $bits[] = 4; } if ($bits) { $out[] = $bits; }', 'assertions' => [ '$out' => 'array>', ], ], 'genericArrayCreationWithObjectAddedInIf' => [ ' [ '$out' => 'array', ], ], 'genericArrayCreationWithElementAddedInSwitch' => [ ' [ '$out' => 'array', ], ], 'genericArrayCreationWithElementsAddedInSwitch' => [ ' [ '$out' => 'array', ], ], 'genericArrayCreationWithElementsAddedInSwitchWithNothing' => [ ' [ '$out' => 'array', ], ], 'implicitIntArrayCreation' => [ ' [ '$foo' => 'array', ], ], 'implicit2dIntArrayCreation' => [ ' [ '$foo' => 'array>', ], ], 'implicit3dIntArrayCreation' => [ ' [ '$foo' => 'array>>', ], ], 'implicit4dIntArrayCreation' => [ ' [ '$foo' => 'array>>>', ], ], 'implicitIndexedIntArrayCreation' => [ ' $text) { $bat[$text] = $bar[$i]; }', 'assertions' => [ '$foo' => 'array{0:string, 1:string, 2:string}', '$bar' => 'array{0:int, 1:int, 2:int}', '$bat' => '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}}}', ], ], 'conflictingTypes' => [ ' ["a" => "b"], "baz" => [1] ];', 'assertions' => [ '$foo' => 'array{bar:array{a:string}, baz:array{0:int}}', ], ], 'implicitObjectLikeCreation' => [ ' 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{0:int}}', ], ], 'conflictingTypesWithAssignment2' => [ ' [ '$foo' => 'array{a:string, b:array}', '$foo[\'a\']' => 'string', '$foo[\'b\']' => 'array', '$bar' => 'string', ], ], 'conflictingTypesWithAssignment3' => [ ' [ '$foo' => 'array{a:string, b:array{c:array{d:string}}}', ], ], 'nestedObjectLikeAssignment' => [ ' [ '$foo' => 'array{a:array{b:string, c:int}}', ], ], 'conditionalObjectLikeAssignment' => [ ' "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>', '$c' => 'array>>', ], ], 'assignExplicitValueToGeneric' => [ '> */ $a = []; $a["foo"] = ["bar" => "baz"];', 'assertions' => [ '$a' => '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', ], ], 'objectLikeArrayAddition' => [ ' [2, 3]];', 'assertions' => [ '$foo' => 'array{a:int, b:array{0:int, 1:int}}', ], ], 'nestedObjectLikeArrayAddition' => [ ' [2, 3]];', 'assertions' => [ '$foo' => 'array{root:array{a:int, b:array{0:int, 1:int}}}', ], ], 'updateStringIntKey' => [ ' [ '$a' => 'array{a:int, 0:int}', '$b' => 'array', '$c' => 'array', '$d' => 'array', '$e' => 'array', ], ], 'updateStringIntKeyWithIntRootAndNumberOffset' => [ ' [ '$a' => 'array{0:array{a:int, 0:int}}', ], ], 'updateStringIntKeyWithIntRoot' => [ ' [ '$b' => 'array{0:array}', '$c' => 'array{0:array}', '$d' => 'array{0:array}', '$e' => 'array{0:array}', ], ], 'updateStringIntKeyWithObjectLikeRootAndNumberOffset' => [ ' [ '$a' => 'array{root:array{a:int, 0:int}}', ], ], 'updateStringIntKeyWithObjectLikeRoot' => [ ' [ '$b' => 'array{root:array}', '$c' => 'array{root:array}', '$d' => 'array{root:array}', '$e' => 'array{root:array}', ], ], 'mixedArrayAssignmentWithStringKeys' => [ ' */ $a = []; $a["b"]["c"] = 5; echo $a["b"]["d"];', 'assertions' => [ '$a' => 'array', ], 'error_levels' => ['MixedArrayAssignment', 'MixedArrayAccess', 'MixedArgument'], ], 'mixedArrayCoercion' => [ ' [], 'error_levels' => ['MixedTypeCoercion', 'MixedArgument'], ], 'suppressMixedObjectOffset' => [ 'id] = $a; } echo $arr[0];', 'assertions' => [], 'error_levels' => ['MixedAssignment', 'MixedPropertyFetch', 'MixedArrayOffset', 'MixedArgument'], ], 'changeObjectLikeType' => [ ' "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}}', ], ], 'changeObjectLikeTypeInIf' => [ ' 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'], ], 'assignToNullDontDie' => [ ' [ '$a' => 'array{0:array}', ], 'error_levels' => ['PossiblyNullArrayAssignment'], ], 'stringAssignment' => [ ' [ '$str' => 'string', ], ], 'ignoreInvalidArrayOffset' => [ ' [], ]; $a["b"]["c"] = 0; foreach ([1, 2, 3] as $i) { /** * @psalm-suppress InvalidArrayOffset * @psalm-suppress MixedOperand */ $a["b"]["d"] += $a["b"][$i]; }', 'assertions' => [], ], 'keyedIntOffsetArrayValues' => [ ' [ '$a' => 'array{0:string, 1:int}', '$a_values' => 'array', '$a_keys' => 'array', ], ], 'changeIntOffsetKeyValuesWithDirectAssignment' => [ ' [ '$b' => 'array{0:int, 1:int}', ], ], 'changeIntOffsetKeyValuesAfterCopy' => [ ' [ '$b' => 'array{0:string, 1:int}', '$c' => 'array{0:int, 1:int}', ], ], 'mergeIntOffsetValues' => [ ' [ '$d' => 'array{0:string, 1:int}', '$e' => 'array{0:string, 1:int, 2:string}', ], ], 'addIntOffsetToEmptyArray' => [ ' [ '$f' => 'array{0:string}', ], ], ]; } /** * @return array */ public function providerFileCheckerInvalidCodeParse() { return [ 'objectAssignment' => [ ' 'InvalidArrayAssignment', ], 'invalidArrayAccess' => [ ' 'InvalidArrayAssignment', ], 'mixedStringOffsetAssignment' => [ ' 'MixedStringOffsetAssignment', 'error_level' => ['MixedAssignment'], ], 'mixedArrayArgument' => [ ' $foo */ function fooFoo(array $foo) : void { } function barBar(array $bar) : void { fooFoo($bar); } barBar([1, "2"]);', 'error_message' => 'MixedTypeCoercion', 'error_level' => ['MixedAssignment'], ], 'arrayPropertyAssignment' => [ 'strs = [new stdClass()]; // no issue emitted } }', 'error_message' => 'InvalidPropertyAssignment', ], 'incrementalArrayPropertyAssignment' => [ 'strs[] = new stdClass(); // no issue emitted } }', 'error_message' => 'InvalidPropertyAssignment', ], ]; } }