use_phpdoc_method_without_magic_or_parent = true; $this->addFile( 'somefile.php', 'getString(); $child->setInteger(4); /** @psalm-suppress MixedAssignment */ $b = $child->setString(5); $c = $child->getBool("hello"); $d = $child->getArray(); $e = $child->getCallable();', ); $this->analyzeFile('somefile.php', new Context()); } public function testPhpDocMethodWhenTemplated(): void { Config::getInstance()->use_phpdoc_method_without_magic_or_parent = true; $this->addFile( 'somefile.php', 'analyzeFile('somefile.php', new Context()); } public function testAnnotationWithoutCallConfig(): void { $this->expectExceptionMessage('UndefinedMethod'); $this->expectException(CodeException::class); Config::getInstance()->use_phpdoc_method_without_magic_or_parent = false; $this->addFile( 'somefile.php', 'getString();', ); $context = new Context(); $this->analyzeFile('somefile.php', $context); } public function testOverrideParentClassRetunType(): void { Config::getInstance()->use_phpdoc_method_without_magic_or_parent = true; $this->addFile( 'somefile.php', 'analyzeFile('somefile.php', $context); $this->assertSame('Child', (string) $context->vars_in_scope['$child']); } public function testOverrideExceptionMethodReturn(): void { Config::getInstance()->use_phpdoc_method_without_magic_or_parent = true; $this->addFile( 'somefile.php', 'getCode(); }', ); $context = new Context(); $this->analyzeFile('somefile.php', $context); } public function providerValidCodeParse(): iterable { return [ 'validSimpleAnnotations' => [ 'code' => 'getString(); $child->setInteger(4); /** @psalm-suppress MixedAssignment */ $b = $child->setString(5); $c = $child->getBool("hello"); $d = $child->getArray(); $e = $child->getCallable(); $child->setMixed("hello"); $child->setMixed(4); $child->setImplicitMixed("hello"); $child->setImplicitMixed(4);', 'assertions' => [ '$a' => 'string', '$b' => 'mixed', '$c' => 'bool', '$d' => 'array', '$e' => 'callable():string', ], ], 'validAnnotationWithDefault' => [ 'code' => 'setArray(["boo"]); $child->setArray(["boo"], 8);', ], 'validAnnotationWithByRefParam' => [ 'code' => 'configure("foo", $array);', ], 'validAnnotationWithNonEmptyDefaultArray' => [ 'code' => 'setArray(["boo"]); $child->setArray(["boo"]);', ], 'validAnnotationWithNonEmptyDefaultOldStyleArray' => [ 'code' => 'setArray(["boo"]); $child->setArray(["boo"]);', ], 'validStaticAnnotationWithDefault' => [ 'code' => ' [ '$a' => 'string', ], ], 'validAnnotationWithVariadic' => [ 'code' => 'setInts(1, 2, 3, 4);', ], 'validUnionAnnotations' => [ 'code' => 'setBool("hello", true); $c = $child->setBool("hello", "true"); $child->setAnotherArray(["boo"]);', 'assertions' => [ '$b' => 'bool', '$c' => 'bool', ], ], 'namespacedValidAnnotations' => [ 'code' => 'setBool("hello", true); $c = $child->setBool("hello", "true");', ], 'globalMethod' => [ 'code' => ' [ 'code' => 'work()); } }', ], 'magicMethodOverridesParentWithMoreSpecificType' => [ 'code' => ' [ 'code' => 'hasMany("User", ["id" => "user_id"]) ->viaTable("account_to_user", ["account_id" => "id"]); return $query; } }', ], 'magicMethodReturnSelf' => [ 'code' => 'getThis();', 'assertions' => [ '$a' => 'C', '$b' => 'C', ], ], 'allowMagicMethodStatic' => [ 'code' => 'getStatic(); $d = (new D)->getStatic();', 'assertions' => [ '$c' => 'C', '$d' => 'D', ], ], 'validSimplePsalmAnnotations' => [ 'code' => 'getString(); $child->setInteger(4);', 'assertions' => [ '$a' => 'string', ], ], 'overrideMethodAnnotations' => [ 'code' => 'getString(); $child->setInteger(4);', 'assertions' => [ '$a' => 'string', ], ], 'alwaysAllowAnnotationOnInterface' => [ 'code' => 'sayHello();', ], 'inheritInterfacePseudoMethodsFromParent' => [ 'code' => 'getClassMetadata()); test(concreteEm()->getClassMetadata()); test2(em()->getOtherMetadata()); test2(concreteEm()->getOtherMetadata());', ], 'fullyQualifiedParam' => [ 'code' => 'setInteger(function() : void {}); }', ], 'allowMethodsNamedBooleanAndInteger' => [ 'code' => 'boolean(5); $child->integer(5);', ], 'overrideWithSelfBeforeMethodName' => [ 'code' => ' [ 'code' => ' [ 'code' => 'foo();', ], 'allowFinalOverrider' => [ 'code' => ' [ 'code' => ' getAll():\IteratorAggregate */ class Foo { private \IteratorAggregate $items; /** * @psalm-suppress MixedReturnTypeCoercion */ public function getAll(): \IteratorAggregate { return $this->items; } public function __construct(\IteratorAggregate $foos) { $this->items = $foos; } } /** * @psalm-suppress MixedReturnTypeCoercion * @method \IteratorAggregate getAll():\IteratorAggregate */ class Bar { private \IteratorAggregate $items; /** * @psalm-suppress MixedReturnTypeCoercion */ public function getAll(): \IteratorAggregate { return $this->items; } public function __construct(\IteratorAggregate $foos) { $this->items = $foos; } }', ], 'parseFloatInDefault' => [ 'code' => 'randomInt(); }', ], 'negativeInDefault' => [ 'code' => 'foo();', ], 'namespacedNegativeInDefault' => [ 'code' => 'foo(); }', ], 'namespacedUnion' => [ 'code' => 'bar(new \DateTime(), new Cache());', ], 'magicMethodInheritance' => [ 'code' => 'foo()); consumeInt($b->bar());', ], 'magicMethodInheritanceOnInterface' => [ 'code' => 'foo());', ], 'magicStaticMethodInheritance' => [ 'code' => ' [ 'code' => ' [ 'code' => 'create([])); $d = new BlahModel(); consumeBlah($d->create([]));', ], 'returnThisShouldKeepGenerics' => [ 'code' => ' $a */ $a = new A(); $b = $a->foo(); /** @var I $i */ $c = $i->foo();', 'assertions' => [ '$b' => 'A&static', '$c' => 'I&static', ], ], 'genericsOfInheritedMethodsShouldBeResolved' => [ 'code' => ' */ class A implements I { public function __call(string $name, array $args) {} } /** * @template E * @extends I */ interface I2 extends I {} class B {} /** * @template E * @method E get() */ class C { public function __call(string $name, array $args) {} } /** * @template E * @extends C */ class D extends C {} /** @var A $a */ $a = new A(); $b = $a->get(); /** @var I2 $i */ $c = $i->get(); /** @var D $d */ $d = new D(); $e = $d->get();', 'assertions' => [ '$b' => 'B', '$c' => 'B', '$e' => 'B', ], ], 'arrayAsMethodName' => [ 'code' => <<<'PHP' [ 'code' => ' 'InvalidDocblock', ], 'annotationWithByRefParam' => [ 'code' => ' 'InvalidDocblock', ], 'annotationWithSealed' => [ 'code' => 'getString(); $child->foo();', 'error_message' => 'UndefinedMagicMethod - src' . DIRECTORY_SEPARATOR . 'somefile.php:13:29 - Magic method Child::foo does not exist', ], 'annotationInvalidArg' => [ 'code' => 'setString("five");', 'error_message' => 'InvalidArgument', ], 'unionAnnotationInvalidArg' => [ 'code' => 'setBool("hello", 5);', 'error_message' => 'InvalidArgument', ], 'validAnnotationWithInvalidVariadicCall' => [ 'code' => 'setInts([1, 2, 3]);', 'error_message' => 'InvalidArgument', ], 'magicMethodOverridesParentWithDifferentReturnType' => [ 'code' => ' 'ImplementedReturnTypeMismatch - src' . DIRECTORY_SEPARATOR . 'somefile.php:11:33', ], 'magicMethodOverridesParentWithDifferentParamType' => [ 'code' => ' 'ImplementedParamTypeMismatch - src' . DIRECTORY_SEPARATOR . 'somefile.php:11:33', ], 'parseBadMethodAnnotation' => [ 'code' => ' 'InvalidDocblock', ], 'methodwithDash' => [ 'code' => ' 'InvalidDocblock', ], 'methodWithAmpersandAndSpace' => [ 'code' => ' 'InvalidDocblock', ], 'inheritSealedMethods' => [ 'code' => 'foo();', 'error_message' => 'UndefinedMagicMethod', ], 'lonelyMethod' => [ 'code' => ' 'InvalidDocblock', ], 'magicParentCallShouldNotPolluteContext' => [ 'code' => ' 'UndefinedVariable', ], ]; } public function testSealAllMethodsWithoutFoo(): void { Config::getInstance()->seal_all_methods = true; $this->addFile( 'somefile.php', 'foo(); ', ); $error_message = 'UndefinedMagicMethod'; $this->expectException(CodeException::class); $this->expectExceptionMessage($error_message); $this->analyzeFile('somefile.php', new Context()); } public function testSealAllMethodsWithFoo(): void { Config::getInstance()->seal_all_methods = true; $this->addFile( 'somefile.php', 'foo(); ', ); $this->analyzeFile('somefile.php', new Context()); } public function testSealAllMethodsWithFooInSubclass(): void { Config::getInstance()->seal_all_methods = true; $this->addFile( 'somefile.php', 'foo(); ', ); $this->analyzeFile('somefile.php', new Context()); } public function testSealAllMethodsWithFooAnnotated(): void { Config::getInstance()->seal_all_methods = true; $this->addFile( 'somefile.php', 'foo(); ', ); $this->analyzeFile('somefile.php', new Context()); } public function testSealAllMethodsSetToFalse(): void { Config::getInstance()->seal_all_methods = false; $this->addFile( 'somefile.php', 'foo(); ', ); $this->analyzeFile('somefile.php', new Context()); } public function testIntersectionTypeWhenMagicMethodDoesNotExistButIsProvidedBySecondType(): void { $this->addFile( 'somefile.php', 'otherMethod(); ', ); $this->analyzeFile('somefile.php', new Context()); } public function testIntersectionTypeWhenMethodDoesNotExistOnEither(): void { $this->addFile( 'somefile.php', 'nonExistantMethod(); ', ); $error_message = 'UndefinedMagicMethod'; $this->expectException(CodeException::class); $this->expectExceptionMessage($error_message); $this->analyzeFile('somefile.php', new Context()); } }