project_checker, self::$parser->parse('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 void */ public function testImplementsArrayAccess() { $stmts = self::$parser->parse('project_checker, $stmts); $context = new Context(); $file_checker->visitAndAnalyzeMethods($context); $this->assertSame('A', (string) $context->vars_in_scope['$a']); $this->assertFalse(isset($context->vars_in_scope['$a[\'bar\']'])); } /** * @return array */ public function providerFileCheckerValidCodeParse() { return [ 'genericArrayCreation' => [ ' [ ['array' => '$out'], ], ], 'generic2dArrayCreation' => [ ' [ ['array>' => '$out'], ], ], 'generic2dArrayCreationAddedInIf' => [ ' 50) { $out[] = $bits; $bits = []; } $bits[] = 4; } if ($bits) { $out[] = $bits; }', 'assertions' => [ ['array>' => '$out'], ], ], 'genericArrayCreationWithObjectAddedInIf' => [ ' [ ['array' => '$out'], ], ], 'genericArrayCreationWithElementAddedInSwitch' => [ ' [ ['array' => '$out'], ], ], 'genericArrayCreationWithElementsAddedInSwitch' => [ ' [ ['array' => '$out'], ], ], 'genericArrayCreationWithElementsAddedInSwitchWithNothing' => [ ' [ ['array' => '$out'], ], ], 'implicitIntArrayCreation' => [ ' [ ['array' => '$foo'], ], ], 'implicit2dIntArrayCreation' => [ ' [ ['array>' => '$foo'], ], ], 'implicit3dIntArrayCreation' => [ ' [ ['array>>' => '$foo'], ], ], 'implicit4dIntArrayCreation' => [ ' [ ['array>>>' => '$foo'], ], ], 'implicitIndexedIntArrayCreation' => [ ' $text) { $bat[$text] = $bar[$i]; }', 'assertions' => [ ['array' => '$foo'], ['array' => '$bar'], ['array' => '$bat'], ], ], 'implicitStringArrayCreation' => [ ' [ ['array{bar:string}' => '$foo'], ['string' => '$foo[\'bar\']'], ], ], 'implicit2dStringArrayCreation' => [ ' [ ['array{bar:array{baz:string}}' => '$foo'], ['string' => '$foo[\'bar\'][\'baz\']'], ], ], 'implicit3dStringArrayCreation' => [ ' [ ['array{bar:array{baz:array{bat:string}}}' => '$foo'], ['string' => '$foo[\'bar\'][\'baz\'][\'bat\']'], ], ], 'implicit4dStringArrayCreation' => [ ' [ ['array{bar:array{baz:array{bat:array{bap:string}}}}' => '$foo'], ['string' => '$foo[\'bar\'][\'baz\'][\'bat\'][\'bap\']'], ], ], '2Step2dStringArrayCreation' => [ ' []]; $foo["bar"]["baz"] = "hello";', 'assertions' => [ ['array{bar:array{baz:string}}' => '$foo'], ['string' => '$foo[\'bar\'][\'baz\']'], ], ], '2StepImplicit3dStringArrayCreation' => [ ' []]; $foo["bar"]["baz"]["bat"] = "hello";', 'assertions' => [ ['array{bar:array{baz:array{bat:string}}}' => '$foo'], ], ], 'conflictingTypes' => [ ' ["a" => "b"], "baz" => [1] ];', 'assertions' => [ ['array{bar:array{a:string}, baz:array}' => '$foo'], ], ], 'implicitObjectLikeCreation' => [ ' 1, ]; $foo["baz"] = "a";', 'assertions' => [ ['array{bar:int, baz:string}' => '$foo'], ], ], 'conflictingTypesWithAssignment' => [ ' ["a" => "b"], "baz" => [1] ]; $foo["bar"]["bam"]["baz"] = "hello";', 'assertions' => [ ['array{bar:array{a:string, bam:array{baz:string}}, baz:array}' => '$foo'], ], ], 'conflictingTypesWithAssignment2' => [ ' [ ['array{a:string, b:array}' => '$foo'], ['string' => '$foo[\'a\']'], ['array' => '$foo[\'b\']'], ['string' => '$bar'], ], ], 'conflictingTypesWithAssignment3' => [ ' [ ['array{a:string, b:array{c:array{d:string}}}' => '$foo'], ], ], 'nestedObjectLikeAssignment' => [ ' [ ['array{a:array{b:string, c:int}}' => '$foo'], ], ], 'conditionalObjectLikeAssignment' => [ ' "hello"]; if (rand(0, 10) === 5) { $foo["b"] = 1; } else { $foo["b"] = 2; }', 'assertions' => [ ['array{a:string, b:int}' => '$foo'], ], ], 'arrayKey' => [ ' "foo", "b"=> "bar"]; $d = "a"; $e = $a[$d];', 'assertions' => [ ['string' => '$b'], ['string' => '$e'], ], ], 'conditionalCheck' => [ ' [], ], 'variableKeyArrayCreate' => [ ' [ ['array>' => '$a'], ['array>>' => '$c'], ], ], 'assignExplicitValueToGeneric' => [ '> */ $a = []; $a["foo"] = ["bar" => "baz"];', 'assertions' => [ ['array>' => '$a'], ], ], 'additionWithEmpty' => [ ' [ ['array' => '$a'], ['array' => '$b'], ], ], 'additionDifferentType' => [ ' [ ['array' => '$a'], ['array' => '$b'], ], ], 'present1dArrayTypeWithVarKeys' => [ '> */ $a = []; $foo = "foo"; $a[$foo][] = "bat";', 'assertions' => [], ], 'present2dArrayTypeWithVarKeys' => [ '>> */ $b = []; $foo = "foo"; $bar = "bar"; $b[$foo][$bar][] = "bat";', 'assertions' => [], ], 'objectLikeWithIntegerKeys' => [ ' [ ['string' => '$b'], ['int' => '$c'], ['string' => '$d'], ['int' => '$e'], ], ], ]; } /** * @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' => 'TypeCoercion', 'error_level' => ['MixedAssignment'], ], 'arrayPropertyAssignment' => [ 'strs = [new stdClass()]; // no issue emitted } }', 'error_message' => 'InvalidPropertyAssignment', ], 'incrementalArrayPropertyAssignment' => [ 'strs[] = new stdClass(); // no issue emitted } }', 'error_message' => 'InvalidPropertyAssignment', ], ]; } }