file_provider = new Provider\FakeFileProvider(); $this->project_analyzer = new \Psalm\Internal\Analyzer\ProjectAnalyzer( new TestConfig(), new \Psalm\Internal\Provider\Providers( $this->file_provider, new Provider\FakeParserCacheProvider() ) ); $this->project_analyzer->setPhpVersion('7.3'); $this->project_analyzer->getCodebase()->reportUnusedVariables(); } /** * @dataProvider providerValidCodeParse * * @param string $code * @param array $error_levels * * @return void */ public function testValidCode($code, array $error_levels = []) { $test_name = $this->getTestName(); if (strpos($test_name, 'SKIPPED-') !== false) { $this->markTestSkipped('Skipped due to a bug.'); } $file_path = self::$src_dir_path . 'somefile.php'; $this->addFile( $file_path, $code ); foreach ($error_levels as $error_level) { $this->project_analyzer->getCodebase()->config->setCustomErrorLevel($error_level, Config::REPORT_SUPPRESS); } $context = new Context(); $context->collect_references = true; $this->analyzeFile($file_path, $context); } /** * @dataProvider providerInvalidCodeParse * * @param string $code * @param string $error_message * @param array $error_levels * * @return void */ public function testInvalidCode($code, $error_message, $error_levels = []) { if (strpos($this->getTestName(), 'SKIPPED-') !== false) { $this->markTestSkipped(); } $this->expectException(\Psalm\Exception\CodeException::class); $this->expectExceptionMessageRegExp('/\b' . preg_quote($error_message, '/') . '\b/'); $file_path = self::$src_dir_path . 'somefile.php'; foreach ($error_levels as $error_level) { $this->project_analyzer->getCodebase()->config->setCustomErrorLevel($error_level, Config::REPORT_SUPPRESS); } $this->addFile( $file_path, $code ); $context = new Context(); $context->collect_references = true; $this->analyzeFile($file_path, $context); } /** * @return array */ public function providerValidCodeParse() { return [ 'arrayOffset' => [ ' [ ' [ ' [ 'PossiblyUndefinedVariable', 'MixedArrayAccess', 'MixedOperand', 'MixedAssignment', 'InvalidStringClass', ], ], 'varDefinedInIfWithReference' => [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' 3);', ], 'loopTypeChangedInIfAndContinueWithReference' => [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ 'getMessage(); } }', ], 'throwWithMessageCallAndAssignmentAndReference' => [ 'getMessage(); } if ($s) {} }', ], 'throwWithMessageCallAndAssignmentInCatchAndReference' => [ 'getMessage(); $s = "hello"; } if ($s) {} }', ], 'throwWithMessageCallAndAssignmentInTryAndCatchAndReference' => [ 'getMessage(); $s = "hello"; } if ($s) {} }', ], 'throwWithMessageCallAndNestedAssignmentInTryAndCatchAndReference' => [ 'getMessage(); $t = "hello"; } if ($t) { $s = $t; } } if ($s) {} }', ], 'throwWithReturnInOneCatch' => [ 'getMessage(); $s = false; } catch (Exception $e) { return; } if ($s) {} }', ], 'loopWithIfRedefinition' => [ ' [ 'passedByRef($b); }', ], 'usedMethodCallVariable' => [ '$methodName()] = true; } return $ret; }', 'error_levels' => [ 'MixedAssignment', 'MixedMethodCall', 'MixedArrayOffset', 'MixedTypeCoercion', ], ], 'globalVariableUsage' => [ ' [ ' [ ' [ ' 0) { return "value"; } throw new Exception("fail"); } function main() : void { try { $s = example_string(); if (!$s) { echo "Failed to get string\n"; } } catch (Exception $e) { $s = "fallback"; } printf("s is %s\n", $s); }', ], 'loopTypeChangedInIfAndBreakWithReference' => [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' $options */ public function __construct(array $options) { $this->setOptions($options); } /** * @param array $options */ protected function setOptions(array $options): void { foreach ($options as $key => $value) { $normalized = ucfirst($key); $method = "set" . $normalized; if (method_exists($this, $method)) { $this->$method($value); } } } } new A(["bar" => "bat"]);', ], 'instanceofVarUse' => [ ' [ ' 0); echo $i;', ], 'callableReferencesItself' => [ ' [ ' $type */ function bar(string $type) : ArrayObject { $data = [["foo"], ["bar"]]; return new $type($data[0]); }', ], 'byRefVariableUsedInAddition' => [ ' [ ' [ 'bar([$b]); }', ], 'paramUsedInsideLoop' => [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' $a */ function getLastNum(array $a): int { foreach ($a as $num) { $last = $num; } return $last; }' ], 'usedStrtolowerInArray' => [ ' $row */ function foo(array $row, string $s) : array { $row["a" . strtolower($s)] += 1; return $row; }', ], 'pureWithReflectionMethodSetValue' => [ 'setValue([get_class($mock) => "hello"]); }' ], 'defineBeforeAssignmentInConditional' => [ ' [ ' [ 'bar($i); }', ], 'noUnusedVariableAfterRedeclaredInCatch' => [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' */ public function providerInvalidCodeParse() { return [ 'simpleUnusedVariable' => [ ' 'UnusedVariable', ], 'unusedVarWithAdditionOp' => [ ' 'UnusedVariable', ], 'unusedVarWithConditionalAdditionOp' => [ ' 'UnusedVariable', ], 'unusedVarWithConditionalAddition' => [ ' 'UnusedVariable', ], 'unusedVarWithIncrement' => [ ' 'UnusedVariable', ], 'unusedVarWithConditionalIncrement' => [ ' 'UnusedVariable', ], 'ifInBothBranchesWithoutReference' => [ ' 'UnusedVariable', ], 'varInNestedAssignmentWithoutReference' => [ ' 'UnusedVariable', ], 'varInSecondNestedAssignmentWithoutReference' => [ ' 'UnusedVariable', ], 'varReassignedInBothBranchesOfIf' => [ ' 'UnusedVariable', ], 'varReassignedInNestedBranchesOfIf' => [ ' 'UnusedVariable', ], 'ifVarReassignedInBranchWithNoUse' => [ ' 'UnusedVariable', ], 'elseVarReassignedInBranchAndNoReference' => [ ' 'UnusedVariable', ], 'switchVarReassignedInBranch' => [ ' 'UnusedVariable', ], 'switchVarReassignedInBranchWithDefault' => [ ' 'UnusedVariable', ], 'switchVarReassignedInAllBranches' => [ ' 'UnusedVariable', ], 'unusedListVar' => [ ' 'UnusedVariable', ], 'unusedPreForVar' => [ ' 'UnusedVariable', ], 'unusedIfInReturnBlock' => [ ' 'UnusedVariable', ], 'unusedIfVarInBranch' => [ ' 'UnusedVariable', ], 'throwWithMessageCallAndAssignmentAndNoReference' => [ 'getMessage(); } }', 'error_message' => 'UnusedVariable', ], 'throwWithMessageCallAndAssignmentInCatchAndNoReference' => [ 'getMessage(); $s = "hello"; } }', 'error_message' => 'UnusedVariable', ], 'throwWithMessageCallAndNestedAssignmentInTryAndCatchAndNoReference' => [ 'getMessage(); $t = "hello"; } if ($t) { $s = $t; } } }', 'error_message' => 'UnusedVariable', ], 'throwWithReturnInOneCatchAndNoReference' => [ 'getMessage(); $s = false; } catch (Exception $e) { return; } }', 'error_message' => 'UnusedVariable', ], 'loopTypeChangedInIfWithoutReference' => [ ' 'UnusedVariable', ], 'loopTypeChangedInIfAndContinueWithoutReference' => [ ' 'UnusedVariable', ], 'loopReassignedInIfAndContinueWithoutReferenceAfter' => [ ' 'UnusedVariable', ], 'loopReassignedInIfAndContinueWithoutReference' => [ ' 'UnusedVariable', ], 'unusedConditionalCode' => [ ' 'UnusedVariable', ], 'varDefinedInIfWithoutReference' => [ ' 'UnusedVariable', ], 'SKIPPED-byrefInForeachLoopWithoutReference' => [ ' 'UnusedVariable', ], 'loopSetIfNullWithBreakWithoutReference' => [ ' 'UnusedVariable', ], 'loopSetIfNullWithBreakWithoutReference2' => [ ' 'UnusedVariable', ], 'loopSetIfNullWithContinueWithoutReference' => [ ' 'UnusedVariable', ], 'loopAssignmentAfterReferenceWithBreak' => [ ' 'UnusedVariable', ], 'loopAssignmentAfterReferenceWithBreakInIf' => [ ' 'UnusedVariable', ], 'switchVarConditionalAssignmentWithoutReference' => [ ' 'UnusedVariable', ], 'switchInIf' => [ ' 'UnusedVariable', ], 'reusedKeyVar' => [ ' "foo.foo"]; foreach ($arr as $key => $v) { list($key) = explode(".", $v); echo $key; }', 'error_message' => 'UnusedVariable', ], 'detectUnusedVarBeforeTryInsideForeach' => [ ' 'UnusedVariable', ], 'detectUnusedVariableInsideIfLoop' => [ ' 'UnusedVariable', ], 'detectUnusedVariableInsideIfElseLoop' => [ ' 'UnusedVariable', ], 'detectUnusedVariableInsideIfElseifLoop' => [ ' 'UnusedVariable', ], 'detectUnusedVariableInsideIfLoopWithEchoInside' => [ ' 'UnusedVariable', ], 'detectUnusedVariableInsideLoopAfterAssignment' => [ ' 'UnusedVariable', ], 'detectUnusedVariableInsideLoopAfterAssignmentWithAddition' => [ ' 'UnusedVariable', ], 'detectUnusedVariableInsideLoopCalledInFunction' => [ ' 'UnusedVariable', ], 'detectUnusedVariableReassignedInIfFollowedByTryInsideForLoop' => [ ' 'UnusedVariable', ], 'detectUnusedVariableReassignedInIfFollowedByTryInsideForeachLoop' => [ ' 'UnusedVariable', ], 'detectUselessArrayAssignment' => [ ' 'UnusedVariable', ], 'detectUnusedSecondAssignmentBeforeTry' => [ ' 'UnusedVariable', ], 'detectRedundancyAfterLoopWithContinue' => [ ' 'UnusedVariable', ], 'setInLoopThatsAlwaysEnteredButNotReferenced' => [ ' $a */ function getLastNum(array $a): int { foreach ($a as $num) { $last = $num; } return 4; }', 'error_message' => 'UnusedVariable', ], 'defineInBothBranchesOfConditional' => [ ' 'UnusedVariable', ], 'knownVarType' => [ ' 'UnnecessaryVarAnnotation', ], 'knownVarTypeWithName' => [ ' 'UnnecessaryVarAnnotation', ], 'knownForeachVarType' => [ ' 'UnnecessaryVarAnnotation', ], ]; } }