project_analyzer->getCodebase(); $codebase->reportUnusedVariables(); } public function testLessSpecificImplementedReturnTypeWithDocblockOnMultipleLines(): void { $this->expectException(CodeException::class); $this->expectExceptionMessage('LessSpecificImplementedReturnType - somefile.php:5:'); $this->addFile( 'somefile.php', 'analyzeFile('somefile.php', new Context()); } public function testLessSpecificImplementedReturnTypeWithDocblockOnMultipleLinesWithMultipleClasses(): void { $this->expectException(CodeException::class); $this->expectExceptionMessage('LessSpecificImplementedReturnType - somefile.php:15:'); $this->addFile( 'somefile.php', 'analyzeFile('somefile.php', new Context()); } public function testLessSpecificImplementedReturnTypeWithDescription(): void { $this->expectException(CodeException::class); $this->expectExceptionMessage('LessSpecificImplementedReturnType - somefile.php:7:'); $this->addFile( 'somefile.php', 'analyzeFile('somefile.php', new Context()); } public function testInvalidParamDefault(): void { $this->expectException(CodeException::class); $this->expectExceptionMessage('InvalidParamDefault'); $this->addFile( 'somefile.php', 'analyzeFile('somefile.php', new Context()); } public function testInvalidParamDefaultButAllowedInConfig(): void { Config::getInstance()->add_param_default_to_docblock_type = true; $this->addFile( 'somefile.php', 'analyzeFile('somefile.php', new Context()); } public function testInvalidTypehintParamDefaultButAllowedInConfig(): void { $this->expectException(CodeException::class); $this->expectExceptionMessage('InvalidParamDefault'); Config::getInstance()->add_param_default_to_docblock_type = true; $this->addFile( 'somefile.php', 'analyzeFile('somefile.php', new Context()); } /** * @return iterable,ignored_issues?:list,php_version?:string}> */ public function providerValidCodeParse(): iterable { return [ 'nopType' => [ 'code' => ' [ '$_a' => 'int', ], ], 'validDocblockReturn' => [ 'code' => ' */ function foo2(): array { return ["hello"]; } /** * @return array */ function foo3(): array { return ["hello"]; }', ], 'reassertWithIs' => [ 'code' => ' [], 'ignored_issues' => ['RedundantConditionGivenDocblockType'], ], 'checkArrayWithIs' => [ 'code' => ' [], 'ignored_issues' => ['RedundantConditionGivenDocblockType'], ], 'goodDocblock' => [ 'code' => ' [ 'code' => ' [ 'code' => 'foo(); $a->bar = 7; takeA($a);', ], 'invalidDocblockParamSuppress' => [ 'code' => ' [ 'code' => ' [ 'code' => ' */ $a = []; echo $a[0]->getMessage();', ], 'ignoreVarDocblock' => [ 'code' => ' * @ignore-var */ $a = []; $a[0]->getMessage();', 'assertions' => [], 'ignored_issues' => ['EmptyArrayAccess', 'MixedMethodCall'], ], 'psalmIgnoreVarDocblock' => [ 'code' => ' * @psalm-ignore-var */ $a = []; $a[0]->getMessage();', 'assertions' => [], 'ignored_issues' => ['EmptyArrayAccess', 'MixedMethodCall'], ], 'mixedDocblockParamTypeDefinedInParent' => [ 'code' => ' [ 'code' => ' [ 'code' => 'foo(); } }', ], 'psalmVar' => [ 'code' => ' */ public $foo = []; public function updateFoo(): void { $this->foo[5] = "hello"; } }', ], 'psalmParam' => [ 'code' => ' $a * @param string[] $a */ function foo(array $a): void { foreach ($a as $key => $_value) { takesInt($key); } }', ], 'returnDocblock' => [ 'code' => ' [ 'code' => ' 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' => [ 'code' => ' [ 'code' => ' [ 'code' => ' [ 'code' => '|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' => [ 'code' => '|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 * } * * Some text */ $arr = []; $arr["a"]();', ], 'multipeLineGenericArray' => [ 'code' => ', * array * > * * @psalm-type RuleArray = array{ * rule: string, * controller?: class-string<\Exception>, * redirect?: string, * code?: int, * type?: string, * middleware?: MiddlewareArray * } * * Foo Bar */ class A {}', ], 'builtInClassInAShape' => [ 'code' => ' [ 'code' => ' [ 'code' => ' [ 'code' => ' [ 'code' => ' [ '$a' => 'string', '$b' => 'string', '$c' => 'string', ], ], 'valueReturnType' => [ 'code' => ' [ 'code' => ' [ 'code' => ' [ 'code' => ' [ 'code' => ' [ 'code' => ' */ 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) { /** * @psalm-suppress UnnecessaryVarAnnotation * @var _A */ $r = $p; return $r; }', ], 'listUnpackWithDocblock' => [ 'code' => 'bar();', ], 'spaceInType' => [ 'code' => ' [ 'code' => ' [], 'ignored_issues' => ['InvalidDocblock', 'MissingReturnType'], ], 'objectWithPropertiesAnnotation' => [ 'code' => 'foo; } $s = new \stdClass(); $s->foo = "hello"; foo($s); class A { /** @var string */ public $foo = "hello"; } foo(new A);', ], 'refineTypeInNestedCall' => [ 'code' => ' $arr */ foreach (array_filter(array_keys($arr), function (string $key) : bool { return strpos($key, "BAR") === 0; }) as $envVar) { yield $envVar => [getenv($envVar)]; } }', ], 'allowAnnotationOnServer' => [ 'code' => ' $_SERVER */ foreach (array_filter(array_keys($_SERVER), function (string $key) : bool { return strpos($key, "BAR") === 0; }) as $envVar) { yield $envVar => [getenv($envVar)]; } }', ], 'annotationOnForeachItems' => [ 'code' => ' $_) {} if (is_null($item)) {} } function bat(array $arr) : void { $item = null; /** * @psalm-suppress MixedArrayAccess * @var string $item */ foreach ($arr as list($item)) {} if (is_null($item)) {} } function baz(array $arr) : void { $item = null; /** * @psalm-suppress MixedArrayAccess * @var string $item */ foreach ($arr as list($item => $_)) {} if (is_null($item)) {} }', 'assertions' => [], 'ignored_issues' => [ 'MixedAssignment', ], ], 'extraneousDocblockParamName' => [ 'code' => ' [ 'code' => ' $arr */ function foo(array $arr) : void { foreach ($arr as $a) {} echo $a; } foo(["a", "b", "c"]); /** @param array $arr */ function bar(array $arr) : void { if (!$arr) { return; } foo($arr); }', ], 'nonEmptyArrayInNamespace' => [ 'code' => ' $arr */ function foo(array $arr) : void { foreach ($arr as $a) {} echo $a; } foo(["a", "b", "c"]); /** @param array $arr */ function bar(array $arr) : void { if (!$arr) { return; } foo($arr); }', ], 'noExceptionOnIntersection' => [ 'code' => ' [ 'code' => 'foo(); $a->bar(); }', ], 'allowClosingComma' => [ 'code' => ' "", "bar" => "", "baz" => ""];', ], 'returnNumber' => [ 'code' => ' [ 'code' => ' [ 'code' => ' [ 'code' => ', 1:list} */ $bar = [[], []]; foo($bar);' ], 'allowResourceInList' => [ 'code' => ' $_s */ function foo(array $_s) : void { }' ], 'possiblyUndefinedObjectProperty' => [ 'code' => 'value ?? "");' ], 'throwSelf' => [ 'code' => ' [ 'code' => ' 1, "b" => "two"]; }' ], 'falsableFunctionAllowedWhenBooleanExpected' => [ 'code' => ' [ 'code' => ' [], 'ignored_issues' => [], 'php_version' => '7.4' ], 'arrayWithKeySlashesAndNewline' => [ 'code' => ' "literal"];', 'assertions' => [ '$_arr' => 'array{\'foo\\\\bar\nbaz\': string}' ] ], 'doubleSpaceBeforeAt' => [ 'code' => ' [ 'code' => ' [ 'code' => ' [ 'code' => 'format("Y");' ], 'intMaskWithClassConstants' => [ 'code' => ' $flags */ function takesFlags(int $flags) : void { echo $flags; } takesFlags(FileFlag::MODIFIED | FileFlag::NEW);' ], 'intMaskWithZero' => [ 'code' => ' $_flags */ function takesFlags(int $_flags): void {} takesFlags(0); ' ], 'intMaskOfWithClassWildcard' => [ 'code' => ' $flags */ function takesFlags(int $flags) : void { echo $flags; } takesFlags(FileFlag::MODIFIED | FileFlag::NEW);' ], 'intMaskOfWithZero' => [ 'code' => ' $_flags */ function takesFlags(int $_flags): void {} takesFlags(0); ' ], 'emptyStringFirst' => [ 'code' => ' [ 'code' => ' [ 'code' => 'config;} } class A extends Vendor { /** * @var string[] * @psalm-suppress NonInvariantDocblockPropertyType */ public array $config = []; } $a = new Vendor(); $_b = new A(); echo (string)($a->getConfig()[0]??"");' ], 'promotedPropertiesDocumentationEitherForParamOrForProperty' => [ 'code' => ' [ 'code' => ' */ private iterable $strings, ) { } }' ], ]; } /** * @return iterable}> */ public function providerInvalidCodeParse(): iterable { return [ 'invalidClassMethodReturn' => [ 'code' => ' 'MissingDocblockType', ], 'invalidClassMethodReturnBrackets' => [ 'code' => ' 'InvalidDocblock', ], 'invalidInterfaceMethodReturn' => [ 'code' => ' 'MissingDocblockType', ], 'invalidInterfaceMethodReturnBrackets' => [ 'code' => ' 'InvalidDocblock', ], 'invalidPropertyBrackets' => [ 'code' => ' 'InvalidDocblock', ], 'invalidReturnClassWithComma' => [ 'code' => ' 'InvalidDocblock', ], 'returnClassWithComma' => [ 'code' => ' 'InvalidDocblock', ], 'missingParamType' => [ 'code' => ' 'TooManyArguments - src' . DIRECTORY_SEPARATOR . 'somefile.php:8:21 - Too many arguments for fooBar ' . '- expecting 0 but saw 1', ], 'missingParamVar' => [ 'code' => ' 'InvalidDocblock - src' . DIRECTORY_SEPARATOR . 'somefile.php:5:21 - Badly-formatted @param', ], 'invalidSlashWithString' => [ 'code' => ' 'InvalidDocblock', ], 'missingReturnTypeWithBadDocblock' => [ 'code' => ' 'MissingReturnType', 'ignored_issues' => ['InvalidDocblock'], ], 'invalidDocblockReturn' => [ 'code' => ' 'MismatchingDocblockReturnType', ], 'intParamTypeDefinedInParent' => [ 'code' => ' 'MissingParamType', 'ignored_issues' => ['MethodSignatureMismatch'], ], 'psalmInvalidVar' => [ 'code' => ' */ public $foo = []; public function updateFoo(): void { $this->foo["boof"] = "hello"; } }', 'error_message' => 'InvalidPropertyAssignmentValue', ], 'incorrectDocblockOrder' => [ 'code' => ' 'MissingDocblockType', ], 'badlyFormattedVar' => [ 'code' => ' 'InvalidDocblock', ], 'badlyWrittenVar' => [ 'code' => 'conn()->method(); $myVar->otherMethod(); }', 'error_message' => 'MissingDocblockType', ], 'dontOverrideSameType' => [ 'code' => ' 'InvalidReturnType', ], 'alwaysCheckReturnType' => [ 'code' => ' 'UndefinedClass', ], 'preventBadBoolean' => [ 'code' => ' 'UndefinedClass', ], 'undefinedDocblockClassCall' => [ 'code' => 'foo()->bar(); } } ', 'error_message' => 'UndefinedDocblockClass', ], 'noPhpStormAnnotationsThankYou' => [ 'code' => ' 'MismatchingDocblockParamType', ], 'noPhpStormAnnotationsPossiblyInvalid' => [ 'code' => 'offsetGet("a"); }', 'error_message' => 'PossiblyInvalidMethodCall', ], 'doubleBar' => [ 'code' => ' 'InvalidDocblock', ], 'badStringVar' => [ 'code' => ' 'InvalidDocblock', ], 'badCallableVar' => [ 'code' => ' 'InvalidDocblock', ], 'hyphenInType' => [ 'code' => ' 'InvalidDocblock', ], 'badAmpersand' => [ 'code' => ' 'InvalidDocblock', ], 'invalidTypeAlias' => [ 'code' => ' */ class A {}', 'error_message' => 'InvalidDocblock', ], 'typeAliasInTKeyedArray' => [ 'code' => ' 'InvalidReturnStatement', ], 'noCrashOnHalfDoneArrayPropertyType' => [ 'code' => ' 'InvalidDocblock', ], 'noCrashOnHalfDoneTKeyedArrayPropertyType' => [ 'code' => ' 'InvalidDocblock', ], 'noCrashOnInvalidClassTemplateAsType' => [ 'code' => ' 'InvalidDocblock', ], 'noCrashOnInvalidFunctionTemplateAsType' => [ 'code' => ' 'InvalidDocblock', ], 'returnTypeNewLineIsIgnored' => [ 'code' => ' 'MissingReturnType', ], 'objectWithPropertiesAnnotationNoMatchingProperty' => [ 'code' => 'foo; } class A {} foo(new A);', 'error_message' => 'InvalidArgument', ], 'badVar' => [ 'code' => ' 'UndefinedDocblockClass', ], 'badPsalmType' => [ 'code' => ' 'InvalidDocblock', ], 'mismatchingDocblockParamName' => [ 'code' => ' 'InvalidDocblockParamName - src' . DIRECTORY_SEPARATOR . 'somefile.php:2:41', ], 'nonEmptyArrayCalledWithEmpty' => [ 'code' => ' $arr */ function foo(array $arr) : void { foreach ($arr as $a) {} echo $a; } foo([]);', 'error_message' => 'InvalidArgument', ], 'nonEmptyArrayCalledWithEmptyInNamespace' => [ 'code' => ' $arr */ function foo(array $arr) : void { foreach ($arr as $a) {} echo $a; } foo([]);', 'error_message' => 'InvalidArgument', ], 'nonEmptyArrayCalledWithArray' => [ 'code' => ' $arr */ function foo(array $arr) : void { foreach ($arr as $a) {} echo $a; } /** @param array $arr */ function bar(array $arr) { foo($arr); }', 'error_message' => 'ArgumentTypeCoercion', ], 'spreadOperatorArrayAnnotationBadArg' => [ 'code' => ' 'InvalidScalarArgument', ], 'spreadOperatorArrayAnnotationBadSpreadArg' => [ 'code' => ' 'InvalidScalarArgument', ], 'spreadOperatorByRefAnnotationBadCall1' => [ 'code' => ' 'InvalidArgument', ], 'spreadOperatorByRefAnnotationBadCall2' => [ 'code' => ' 'InvalidArgument', ], 'spreadOperatorByRefAnnotationBadCall3' => [ 'code' => ' 'InvalidArgument', ], 'identifyReturnType' => [ 'code' => ' 'InvalidReturnType - src' . DIRECTORY_SEPARATOR . 'somefile.php:2:33', ], 'invalidParamDocblockAsterisk' => [ 'code' => ' 'MissingDocblockType', ], 'canNeverReturnDeclaredType' => [ 'code' => ' 'InvalidReturnStatement - src' . DIRECTORY_SEPARATOR . 'somefile.php:6:32', ], 'falsableWithExpectedTypeTrue' => [ 'code' => ' 'FalsableReturnStatement - src' . DIRECTORY_SEPARATOR . 'somefile.php:6:32', ], 'DuplicatedParam' => [ 'code' => ' 'InvalidDocblock - src' . DIRECTORY_SEPARATOR . 'somefile.php:6:21 - Found duplicated @param or prefixed @param tag in docblock for bar', ], 'DuplicatedReturn' => [ 'code' => ' 'InvalidDocblock - src' . DIRECTORY_SEPARATOR . 'somefile.php:6:21 - Found duplicated @return or prefixed @return tag in docblock for bar', ], 'missingClassForTKeyedArray' => [ 'code' => ' 'ImplementedReturnTypeMismatch' ], 'unexpectedImportType' => [ 'code' => ' 'PossiblyInvalidDocblockTag', ], 'unexpectedVarOnFunction' => [ 'code' => ' 'PossiblyInvalidDocblockTag', ], 'unterminatedParentheses' => [ 'code' => ' 'InvalidDocblock', ], 'emptyParentheses' => [ 'code' => ' 'InvalidDocblock', ], 'unbalancedParentheses' => [ 'code' => " 'InvalidDocblock', ], 'promotedPropertiesDocumentationFailsWhenSendingBadTypeAgainstParam' => [ 'code' => ' 'InvalidArgument', ], 'promotedPropertiesDocumentationFailsWhenSendingBadTypeAgainstProperty' => [ 'code' => ' 'InvalidArgument', ], 'promotedPropertyDuplicateDoc' => [ 'code' => ' 'InvalidDocblock', ], 'promotedPropertyWithParamDocblockAndSignatureType' => [ 'code' => 'licenseType = "dudidu"; echo $a->licenseType;', 'error_message' => 'InvalidArgument', ], ]; } }