file_provider = new FakeFileProvider(); $this->project_analyzer = new ProjectAnalyzer( new TestConfig(), new Providers( $this->file_provider, new FakeParserCacheProvider() ) ); $this->project_analyzer->setPhpVersion('7.3', 'tests'); } /** * @return iterable,error_levels?:string[]}> */ public function providerValidCodeParse(): iterable { 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', 'MixedAssignment'], ], 'incrementMixedCall' => [ 'add(function() use (&$i) : void { if (rand(0, 1)) $i++; }); if ($i === 0) {} }', 'assertions' => [], 'error_levels' => ['MissingParamType', 'MixedMethodCall', 'MixedOperand', 'MixedAssignment'], ], 'regularValueReconciliation' => [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ 's = $s; } }', ], 'inArrayAssertionWithSwitch' => [ ' [ ' [ ' [ ' [ ' [ ' [ ' 1, A::BAR => 1, A::BAM => 1, ]; if ($this->s !== null && 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]; } } } ', ], 'supportSingleLiteralType' => [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' 0)) { echo $i; }', ], 'refinePositiveInt' => [ ' 0) {}' ], 'assignOpThenCheck' => [ ' 0]; if (rand(0, 1)) { $data["e"]++; } if ($data["e"] > 0) {}' ], 'compareToNullImplicitly' => [ ' [ '= 0; $i--) { $ret = ($ret === 10) ? 1 : $ret + 1; } return $ret; }' ], 'inArrayPreserveNull' => [ ' [ ' 1)){} }' ], 'returnFromUnionLiteral' => [ ' [ ' [ ' */ $case_actions = []; if (!in_array(A::ACTION_ONE, $case_actions, true)) {} }' ], 'checkIdenticalArray' => [ ' \is_string($value)); if ($array === $filtered) { foreach ($array as $obj) { echo strlen($obj); } } }', [], [], '7.4' ], 'zeroIsNonEmptyString' => [ ' [ ' [ ' [ ' [ ' [ '$a===' => '5', ], ], ]; } /** * @return iterable */ public function providerInvalidCodeParse(): iterable { return [ 'neverEqualsType' => [ ' 'TypeDoesNotContainType', ], 'alwaysIdenticalType' => [ ' 'RedundantCondition', ], 'alwaysNotIdenticalType' => [ ' 'RedundantCondition', ], 'neverNotIdenticalType' => [ ' 'TypeDoesNotContainType', ], 'neverEqualsFloatType' => [ ' 'TypeDoesNotContainType', ], 'alwaysIdenticalFloatType' => [ ' 'RedundantCondition', ], 'alwaysNotIdenticalFloatType' => [ ' 'RedundantCondition', ], 'inArrayRemoveNull' => [ ' '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', ], 'casedComparison' => [ ' 'TypeDoesNotContainType', ], 'compareValueTwice' => [ ' 0 && $i > 0) {}', 'error_message' => 'RedundantCondition', ], 'numericStringCoerceToLiteral' => [ ' 'ArgumentTypeCoercion' ], 'stringCoercedToNonEmptyString' => [ ' 'ArgumentTypeCoercion', ], ]; } }