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); } $this->analyzeFile($file_path, new 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 ); $this->analyzeFile($file_path, new 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"]]; /** @psalm-suppress UnsafeInstantiation */ 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' => [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' $test */ function foo(array $test) : void { foreach($test as $key => $_testValue) { echo $key; } }' ], 'usedAfterMixedVariableAssignment' => [ ' [ ' [ ' [ ' [ 'foo(new Exception($m)); }', ], 'validMixedAnnotation' => [ ' [ 'value; $update = $value; } }' ], 'createdAndUsedInCondition' => [ 'foo()) {} return; } if (!($a = getA()) || $a->foo()) {}' ], 'usedInUndefinedFunction' => [ ' [ ' "b", "c" => "d"]; foreach ($variables as $name => $value) { ${$name} = $value; }' ], 'usedLoopVariable' => [ ' [ ' [ ' [ ' [1], "b" => [2] ]; foreach (["a"] as $e){ takes_ref($a[$e]); } function takes_ref(array &$p): void { echo implode(",", $p); }' ], 'doWhileWithBreak' => [ ' [ '= $index = nextNumber($index)) { // ... } } function nextNumber(int $eee): int { return $eee + 1; }' ], 'usedParamInWhileIndirectly' => [ '= $index = nextNumber($index)) { // ... } } function nextNumber(int $i): int { return $i + 1; }' ], 'doArrayIncrement' => [ ' $keys * @param int $key */ function error2(array $keys, int $key): int { if ($key === 1) {} do { $nextKey = $keys[++$key] ?? null; } while ($nextKey === null); return $nextKey; }' ], ]; } /** * @return array */ 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', ], ]; } }