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 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 array */ public function providerValidCodeParse() { return [ 'nopType' => [ ' [ '$a' => 'int', ], ], 'validDocblockReturn' => [ ' */ 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', ], ], 'allowOptionalParamsToBeEmptyArray' => [ ' [ ' [ ' [ '|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"]();', ], 'slashAfter?' => [ ' [ ' */ function g():Generator { yield new stdClass; } $g = g();', 'assertions' => [ '$g' => 'Generator', ], ], 'returnTypeShouldBeNullable' => [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' */ class Bar { public function foo() : void { $bar = /** @return TA */ function() { return ["hello"]; }; /** @var array */ $bat = [$bar(), $bar()]; foreach ($bat as $b) { echo $b[0]; } } } /** * @psalm-type _A=array{elt:int} * @param _A $p * @return _A */ function f($p) { /** @var _A */ $r = $p; return $r; }', ], 'listUnpackWithDocblock' => [ 'bar();', ], 'spaceInType' => [ ' [ ' \Psalm\Config::REPORT_INFO, 'MissingReturnType' => \Psalm\Config::REPORT_INFO, ] ], ]; } /** * @return array */ public function providerInvalidCodeParse() { return [ 'invalidReturn' => [ ' 'MissingDocblockType', ], 'invalidReturnClass' => [ ' 'InvalidDocblock', ], 'invalidReturnBrackets' => [ ' 'InvalidDocblock', ], 'invalidPropertyClass' => [ ' 'InvalidDocblock', ], 'invalidPropertyBrackets' => [ ' 'InvalidDocblock', ], 'invalidReturnClassWithComma' => [ ' 'InvalidDocblock', ], 'returnClassWithComma' => [ ' 'InvalidDocblock', ], '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', ], 'missingReturnTypeWithBadDocblock' => [ ' 'MissingReturnType', [ 'InvalidDocblock' => \Psalm\Config::REPORT_INFO, ] ], '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', ], 'badStaticVar' => [ ' 'InvalidDocblock', ], 'doubleBar' => [ ' 'InvalidDocblock', ], 'badStringVar' => [ ' 'InvalidDocblock', ], 'badCallableVar' => [ ' 'InvalidDocblock', ], 'hyphenInType' => [ ' 'InvalidDocblock', ], 'badAmpersand' => [ ' 'InvalidDocblock', ], 'invalidTypeAlias' => [ ' */ class A {}', 'error_message' => 'InvalidDocblock', ], 'typeAliasInObjectLike' => [ ' 'InvalidReturnStatement', ], ]; } }