file_provider = new \Psalm\Tests\Internal\Provider\FakeFileProvider(); $this->project_analyzer = new \Psalm\Internal\Analyzer\ProjectAnalyzer( new TestConfig(), new \Psalm\Internal\Provider\Providers( $this->file_provider, new \Psalm\Tests\Internal\Provider\FakeParserCacheProvider() ) ); $this->project_analyzer->setPhpVersion('7.3'); $this->project_analyzer->getCodebase()->config->parse_sql = true; } /** * @return iterable,error_levels?:string[]}> */ public function providerValidCodeParse() { return [ 'whileCountUpdate' => [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' 3, "1" => 4, "2" => 5, ]; function takesInt(int $s) : void {} foreach ($foo as $i => $b) { takesInt($i); }', ], 'getValidIntStringOffset' => [ ' 3, "1" => 4, "2" => 5, ]; $a = "2"; echo $foo["2"]; echo $foo[$a];', ], 'checkStringKeyValueAfterKnownIntStringOffset' => [ ' 3, "1" => 4, "2" => 5, ]; $a = "2"; $foo[$a] = 6; function takesInt(int $s) : void {} foreach ($foo as $i => $b) { takesInt($i); }', ], 'regularComparison1' => [ ' [ ' [ ' [ ' [ ' [ ' [], 'error_levels' => ['MixedOperand'], ], 'incrementMixedCall' => [ 'add(function() use (&$i) : void { if (rand(0, 1)) $i++; }); if ($i === 0) {} }', 'assertions' => [], 'error_levels' => ['MissingParamType', 'MixedMethodCall', 'MixedOperand'], ], 'regularValueReconciliation' => [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ 's = $s; } }', ], 'inArrayAssertionWithSwitch' => [ ' [ ' [ ' [ ' [ ' [ ' [ ' 1, A::BAR => 1, A::BAM => 1, ]; if (isset($map[$this->s])) {} } }', ], 'noRedundantConditionWithMixed' => [ ' [], 'error_levels' => ['MissingParamType', 'MixedAssignment'], ], 'numericToStringComparison' => [ ' [ ' [ ' [ ' [ ' 1, 2 => 2, null => "hello", ]; $b = $a[""];', 'assertions' => [ '$b' => 'string', ], ], 'yodaConditionalsShouldHaveSameOutput1' => [ ' false, "to" => false]; public function foo(string ...$things) : void { foreach ($things as $thing) { if ($thing !== "from" && $thing !== "to") { continue; } $this->things[$thing] = !$this->things[$thing]; } } } ', ], 'yodaConditionalsShouldHaveSameOutput2' => [ ' false, "to" => false]; public function foo(string ...$things) : void { foreach ($things as $thing) { if ("from" !== $thing && "to" !== $thing) { continue; } $this->things[$thing] = !$this->things[$thing]; } } } ', ], 'keyOf' => [ ' "a", 2 => "b", 3 => "c" ]; /** * @param key-of $i */ public static function foo(int $i) : void {} } A::foo(1); A::foo(2); A::foo(3);' ], 'valueOf' => [ ' "a", 2 => "b", 3 => "c" ]; /** * @param value-of $j */ public static function bar(string $j) : void {} } A::bar("a"); A::bar("b"); A::bar("c");' ], ]; } /** * @return iterable */ public function providerInvalidCodeParse() { return [ 'neverEqualsType' => [ ' 'TypeDoesNotContainType', ], 'alwaysIdenticalType' => [ ' 'RedundantCondition', ], 'alwaysNotIdenticalType' => [ ' 'RedundantCondition', ], 'neverNotIdenticalType' => [ ' 'TypeDoesNotContainType', ], 'neverEqualsFloatType' => [ ' 'TypeDoesNotContainType', ], 'alwaysIdenticalFloatType' => [ ' 'RedundantCondition', ], 'alwaysNotIdenticalFloatType' => [ ' 'RedundantCondition', ], 'neverNotIdenticalFloatType' => [ ' 'TypeDoesNotContainType', ], 'ifImpossibleString' => [ ' 'TypeDoesNotContainType', ], 'arrayOffsetImpossibleValue' => [ ' 1, "b" => 2, ]; if ($foo["a"] === 2) {}', 'error_message' => 'TypeDoesNotContainType', ], 'impossibleKeyInForeach' => [ ' 3, "1" => 4, "2" => 5, ]; function takesInt(int $s) : void {} foreach ($foo as $i => $b) { if ($i === 3) {} }', 'error_message' => 'TypeDoesNotContainType', ], 'impossibleValueInForeach' => [ ' 3, "1" => 4, "2" => 5, ]; function takesInt(int $s) : void {} foreach ($foo as $i => $b) { if ($b === $i) {} }', 'error_message' => 'TypeDoesNotContainType', ], 'invalidIntStringOffset' => [ ' 3, "1" => 4, "2" => 5, ]; $a = "3"; echo $foo[$a];', 'error_message' => 'InvalidArrayOffset', ], 'noChangeToVariable' => [ ' 'RedundantCondition', ], 'redefinedIntInIfAndImpossbleComparison' => [ ' 'TypeDoesNotContainType', ], 'badIfOrAssertionWithSwitch' => [ ' 'RedundantCondition', ], 'keyOfBadValue' => [ ' "a", 2 => "b", 3 => "c" ]; /** * @param key-of $i */ public static function foo(int $i) : void {} } A::foo(4);', 'error_message' => 'InvalidArgument' ], 'valueOfBadValue' => [ ' "a", 2 => "b", 3 => "c" ]; /** * @param value-of $j */ public static function bar(string $j) : void {} } A::bar("d");', 'error_message' => 'InvalidArgument' ], ]; } }