allow_phpstorm_generics = true; $this->addFile( 'somefile.php', 'offsetGet("a"); takesString($s); foreach ($i as $s2) { takesString($s2); } }' ); $this->analyzeFile('somefile.php', new Context()); } /** * @return void */ public function testPhpStormGenericsWithClassProperty() { Config::getInstance()->allow_phpstorm_generics = true; $this->addFile( 'somefile.php', 'bar; } }' ); $this->analyzeFile('somefile.php', new Context()); } /** * @return void */ public function testPhpStormGenericsWithValidIterableArgument() { Config::getInstance()->allow_phpstorm_generics = true; $this->addFile( 'somefile.php', 'analyzeFile('somefile.php', new Context()); } /** * @expectedException \Psalm\Exception\CodeException * @expectedExceptionMessage InvalidScalarArgument * * @return void */ public function testPhpStormGenericsInvalidArgument() { Config::getInstance()->allow_phpstorm_generics = true; $this->addFile( 'somefile.php', 'offsetGet("a"); takesInt($s); }' ); $this->analyzeFile('somefile.php', new Context()); } /** * @expectedException \Psalm\Exception\CodeException * @expectedExceptionMessage PossiblyInvalidMethodCall * * @return void */ public function testPhpStormGenericsNoTypehint() { Config::getInstance()->allow_phpstorm_generics = true; $this->addFile( 'somefile.php', 'offsetGet("a"); }' ); $this->analyzeFile('somefile.php', new Context()); } /** * @expectedException \Psalm\Exception\CodeException * @expectedExceptionMessage InvalidArgument * * @return void */ public function testDontAllowStringConstCoercion() { Config::getInstance()->allow_coercion_from_string_to_class_const = false; $this->addFile( 'somefile.php', 'analyzeFile('somefile.php', new Context()); } /** * @expectedException \Psalm\Exception\CodeException * @expectedExceptionMessage InvalidStringClass * * @return void */ public function testDontAllowStringStandInForNewClass() { Config::getInstance()->allow_string_standin_for_class = false; $this->addFile( 'somefile.php', 'analyzeFile('somefile.php', new Context()); } /** * @expectedException \Psalm\Exception\CodeException * @expectedExceptionMessage InvalidStringClass * * @return void */ public function testDontAllowStringStandInForStaticMethodCall() { Config::getInstance()->allow_string_standin_for_class = false; $this->addFile( 'somefile.php', 'analyzeFile('somefile.php', new Context()); } /** * @expectedException \Psalm\Exception\CodeException * @expectedExceptionMessage InvalidParamDefault * * @return void */ public function testInvalidParamDefault() { $this->addFile( 'somefile.php', 'analyzeFile('somefile.php', new Context()); } /** * @return void */ public function testInvalidParamDefaultButAllowedInConfig() { Config::getInstance()->add_param_default_to_docblock_type = true; $this->addFile( 'somefile.php', 'analyzeFile('somefile.php', new Context()); } /** * @expectedException \Psalm\Exception\CodeException * @expectedExceptionMessage InvalidParamDefault * * @return void */ public function testInvalidTypehintParamDefaultButAllowedInConfig() { Config::getInstance()->add_param_default_to_docblock_type = true; $this->addFile( 'somefile.php', 'analyzeFile('somefile.php', new Context()); } /** * @return void */ public function testPhpDocMethodWhenUndefined() { Config::getInstance()->use_phpdoc_methods_without_call = 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()); } /** * @return void */ public function testCannotOverrideParentRetunTypeWhenIgnoringPhpDocMethod() { Config::getInstance()->use_phpdoc_methods_without_call = false; $this->addFile( 'somefile.php', 'analyzeFile('somefile.php', $context); $this->assertSame('Parent', (string) $context->vars_in_scope['$child']); } /** * @return void */ public function testOverrideParentRetunType() { Config::getInstance()->use_phpdoc_methods_without_call = true; $this->addFile( 'somefile.php', 'analyzeFile('somefile.php', $context); $this->assertSame('Child', (string) $context->vars_in_scope['$child']); } /** * @return array */ public function providerFileCheckerValidCodeParse() { return [ 'nopType' => [ ' [ '$a' => 'int', ], ], 'deprecatedMethod' => [ ' [ ' */ function foo2(): array { return ["hello"]; } /** * @return array */ function foo3(): array { return ["hello"]; }', ], 'reassertWithIs' => [ ' [], 'error_level' => ['RedundantConditionGivenDocblockType'], ], 'checkArrayWithIs' => [ ' [], 'error_level' => ['RedundantConditionGivenDocblockType'], ], 'checkArrayWithIsInsideLoop' => [ '> $data */ function foo($data): void { foreach ($data as $key => $val) { if (!\is_array($data)) { $data = [$key => null]; } else { $data[$key] = !empty($val); } } }', 'assertions' => [], 'error_level' => ['LoopInvalidation', 'MixedArrayOffset', 'RedundantConditionGivenDocblockType'], ], 'goodDocblock' => [ ' [ ' [ 'foo(); $a->bar = 7; takeA($a);', ], 'invalidDocblockParamSuppress' => [ ' [ ' [ ' */ $a = []; $a[0]->getMessage();', ], 'mixedDocblockParamTypeDefinedInParent' => [ ' [ ' [ 'foo(); } }', ], 'psalmVar' => [ ' */ public $foo = []; public function updateFoo(): void { $this->foo[5] = "hello"; } }', ], 'psalmParam' => [ ' $a * @param string[] $a */ function foo(array $a): void { foreach ($a as $key => $value) { takesInt($key); } }', ], 'returnDocblock' => [ ' [ ' new stdClass, "goodbye" => new stdClass]; } $a = null; $b = null; /** * @var string $key * @var stdClass $value */ foreach (foo() as $key => $value) { $a = $key; $b = $value; }', 'assertions' => [ '$a' => 'null|string', '$b' => 'null|stdClass', ], ], 'arrayOfClassConstants' => [ ' $arr */ function takesClassConstants(array $arr) : void {} class A {} class B {} takesClassConstants([A::class, B::class]);', ], 'arrayOfStringClasses' => [ ' $arr */ function takesClassConstants(array $arr) : void {} class A {} class B {} takesClassConstants(["A", "B"]);', 'annotations' => [], 'error_levels' => ['TypeCoercion'], ], 'singleClassConstantAsConstant' => [ ' [ ' [], 'error_levels' => ['TypeCoercion'], ], 'returnClassConstant' => [ ' [ ' [], 'error_levels' => ['LessSpecificReturnStatement', 'MoreSpecificReturnType'], ], 'returnClassConstantArray' => [ ' */ function takesClassConstants() : array { return [A::class, B::class]; }', ], 'returnClassConstantArrayAllowCoercion' => [ ' */ function takesClassConstants() : array { return ["A", "B"]; }', 'annotations' => [], 'error_levels' => ['LessSpecificReturnStatement', 'MoreSpecificReturnType'], ], 'ifClassStringEquals' => [ ' [ ' [ ' [ ' [ '|null), b?:Closure():array, c?:Closure():array, d?:Closure():array, e?:Closure():(array{f:null|string, g:null|string, h:null|string, i:string, j:mixed, k:mixed, l:mixed, m:mixed, n:bool, o?:array{0:string}}|null), p?:Closure():(array{f:null|string, g:null|string, h:null|string, q:string, i:string, j:mixed, k:mixed, l:mixed, m:mixed, n:bool, o?:array{0:string}}|null), r?:Closure():(array|null), s:array} */ $arr = []; $arr["a"]()', ], 'megaClosureAnnotationWithSpacing' => [ '|null), b?: Closure() : array, c?: Closure() : array, d?: Closure() : array, e?: Closure() : (array{ f: null|string, g: null|string, h: null|string, i: string, j: mixed, k: mixed, l: mixed, m: mixed, n: bool, o?: array{0:string} }|null), p?: Closure() : (array{ f: null|string, g: null|string, h: null|string, q: string, i: string, j: mixed, k: mixed, l: mixed, m: mixed, n: bool, o?: array{0:string} }|null), r?: Closure() : (array|null), s: array } */ $arr = []; $arr["a"]()', ], 'magicMethodValidAnnotations' => [ 'getString(); $child->setInteger(4); /** @psalm-suppress MixedAssignment */ $b = $child->setString(5); $c = $child->getBool("hello"); $c = $child->setBool("hello", true); $c = $child->setBool("hello", "true"); $d = $child->getArray(); $child->setArray(["boo"]) $e = $child->getCallable();', 'assertions' => [ '$a' => 'string', '$b' => 'mixed', '$c' => 'bool', '$d' => 'array', '$e' => 'callable():string', ], ], 'namespacedMagicMethodValidAnnotations' => [ 'setBool("hello", true); $c = $child->setBool("hello", "true");', ], 'slashAfter?' => [ ' [ ' */ function g():Generator { yield new stdClass; } $g = g();', 'assertions' => [ '$g' => 'Generator', ], ], 'returnTypeShouldBeNullable' => [ ' [ ' [ ' [ ' 'MissingDocblockType', ], 'invalidReturnClass' => [ ' 'InvalidDocblock', ], 'invalidReturnBrackets' => [ ' 'InvalidDocblock', ], 'invalidPropertyClass' => [ ' 'InvalidDocblock', ], 'invalidPropertyBrackets' => [ ' 'InvalidDocblock', ], 'invalidReturnClassWithComma' => [ ' 'InvalidDocblock', ], 'returnClassWithComma' => [ ' 'InvalidDocblock', ], 'deprecatedMethodWithCall' => [ ' 'DeprecatedMethod', ], 'deprecatedClassWithStaticCall' => [ ' 'DeprecatedClass', ], 'deprecatedClassWithNew' => [ ' 'DeprecatedClass', ], 'deprecatedClassWithExtends' => [ ' 'DeprecatedClass', ], 'deprecatedPropertyGet' => [ 'foo;', 'error_message' => 'DeprecatedProperty', ], 'deprecatedPropertySet' => [ 'foo = 5;', 'error_message' => 'DeprecatedProperty', ], 'missingParamType' => [ ' 'TooManyArguments - src' . DIRECTORY_SEPARATOR . 'somefile.php:8 - Too many arguments for method fooBar ' . '- expecting 0 but saw 1', ], 'missingParamVar' => [ ' 'InvalidDocblock - src' . DIRECTORY_SEPARATOR . 'somefile.php:5 - Badly-formatted @param', ], 'invalidDocblockReturn' => [ ' 'MismatchingDocblockReturnType', ], 'noStringParamType' => [ ' 'MissingParamType - src' . DIRECTORY_SEPARATOR . 'somefile.php:2 - Parameter $a has no provided type,' . ' should be string', 'error_levels' => ['MixedArgument'], ], 'noParamTypeButConcat' => [ ' 'MissingParamType - src' . DIRECTORY_SEPARATOR . 'somefile.php:2 - Parameter $a has no provided type,' . ' should be string', 'error_levels' => ['MixedOperand'], ], 'noParamTypeButAddition' => [ ' 'MissingParamType - src' . DIRECTORY_SEPARATOR . 'somefile.php:2 - Parameter $a has no provided type,' . ' should be int|float', 'error_levels' => ['MixedOperand', 'MixedArgument'], ], 'noParamTypeButDivision' => [ ' 'MissingParamType - src' . DIRECTORY_SEPARATOR . 'somefile.php:2 - Parameter $a has no provided type,' . ' should be int|float', 'error_levels' => ['MixedOperand', 'MixedArgument'], ], 'noParamTypeButTemplatedString' => [ ' 'MissingParamType - src' . DIRECTORY_SEPARATOR . 'somefile.php:2 - Parameter $a has no provided type,' . ' should be string', 'error_levels' => ['MixedOperand'], ], 'noStringIntParamType' => [ ' 'MissingParamType - src' . DIRECTORY_SEPARATOR . 'somefile.php:2 - Parameter $a has no provided type,' . ' should be int|string', 'error_levels' => ['MixedArgument'], ], 'intParamTypeDefinedInParent' => [ ' 'MissingParamType', 'error_levels' => ['MethodSignatureMismatch'], ], 'alreadyHasCheck' => [ ' 'MissingParamType - src' . DIRECTORY_SEPARATOR . 'somefile.php:4 - Parameter $s has no provided type,' . ' could not infer', 'error_levels' => ['MixedArgument'], ], 'isSetBeforeInferrence' => [ 'input' => ' 'MissingParamType - src' . DIRECTORY_SEPARATOR . 'somefile.php:7 - Parameter $s has no provided type,' . ' could not infer', 'error_levels' => ['MixedArgument', 'InvalidReturnType', 'MixedAssignment'], ], 'psalmInvalidVar' => [ ' */ public $foo = []; public function updateFoo(): void { $this->foo["boof"] = "hello"; } }', 'error_message' => 'InvalidPropertyAssignmentValue', ], 'incorrectDocblockOrder' => [ ' 'MissingDocblockType', ], 'badlyFormattedVar' => [ ' 'InvalidDocblock', ], 'badlyWrittenVar' => [ 'conn()->method(); $myVar->otherMethod(); }', 'error_message' => 'MissingDocblockType', ], 'dontOverrideSameType' => [ ' 'InvalidReturnType', ], 'alwaysCheckReturnType' => [ ' 'UndefinedClass', ], 'preventBadBoolean' => [ ' 'UndefinedClass', ], 'preventBadObjectLikeFormat' => [ ' 'InvalidDocblock', ], 'noPhpStormAnnotationsThankYou' => [ ' 'MismatchingDocblockParamType', ], 'noPhpStormAnnotationsPossiblyInvalid' => [ 'offsetGet("a"); }', 'error_message' => 'PossiblyInvalidMethodCall', ], 'arrayOfStringClasses' => [ ' $arr */ function takesClassConstants(array $arr) : void {} class A {} class B {} takesClassConstants(["A", "B"]);', 'error_message' => 'TypeCoercion', ], 'arrayOfNonExistentStringClasses' => [ ' $arr */ function takesClassConstants(array $arr) : void {} takesClassConstants(["A", "B"]);', 'error_message' => 'UndefinedClass', 'error_levels' => ['TypeCoercion'], ], 'singleClassConstantWithInvalidDocblock' => [ ' 'InvalidDocblock', ], 'returnClassConstantDisallowCoercion' => [ ' 'LessSpecificReturnStatement', ], 'returnClassConstantArrayDisallowCoercion' => [ ' */ function takesClassConstants() : array { return ["A", "B"]; }', 'error_message' => 'LessSpecificReturnStatement', ], 'returnClassConstantArrayAllowCoercionWithUndefinedClass' => [ ' */ function takesClassConstants() : array { return ["A", "B"]; }', 'error_message' => 'UndefinedClass', 'error_levels' => ['LessSpecificReturnStatement', 'MoreSpecificReturnType'], ], 'badStaticVar' => [ ' 'InvalidDocblock', ], 'doubleBar' => [ ' 'InvalidDocblock', ], 'badStringVar' => [ ' 'InvalidDocblock', ], 'badCallableVar' => [ ' 'InvalidDocblock', ], 'magicMethodAnnotationWithoutCall' => [ 'getString();', 'error_message' => 'UndefinedMethod', ], 'magicMethodAnnotationWithBadDocblock' => [ ' 'InvalidDocblock', ], 'magicMethodAnnotationWithSealed' => [ 'getString(); $child->foo();', 'error_message' => 'UndefinedMethod - src/somefile.php:14 - Method Child::foo does not exist', ], 'magicMethodAnnotationInvalidArg' => [ 'setString("five");', 'error_message' => 'InvalidScalarArgument', ], 'hyphenInType' => [ ' 'InvalidDocblock', ], 'badAmpersand' => [ ' 'InvalidDocblock', ], ]; } }