2018-05-03 19:56:30 +02:00
|
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
|
2020-08-08 05:22:30 +02:00
|
|
|
|
namespace Psalm\Tests\TypeReconciliation;
|
2018-05-03 19:56:30 +02:00
|
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
|
use Psalm\Internal\Analyzer\ProjectAnalyzer;
|
2021-07-02 01:10:21 +02:00
|
|
|
|
use Psalm\Internal\Provider\FakeFileProvider;
|
2021-12-03 20:11:20 +01:00
|
|
|
|
use Psalm\Internal\Provider\Providers;
|
2020-08-23 16:32:07 +02:00
|
|
|
|
use Psalm\Internal\RuntimeCaches;
|
2021-12-03 20:11:20 +01:00
|
|
|
|
use Psalm\Tests\Internal\Provider\FakeParserCacheProvider;
|
|
|
|
|
use Psalm\Tests\TestCase;
|
|
|
|
|
use Psalm\Tests\TestConfig;
|
|
|
|
|
use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait;
|
|
|
|
|
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
|
2020-08-23 16:32:07 +02:00
|
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
|
class ValueTest extends TestCase
|
2018-05-03 19:56:30 +02:00
|
|
|
|
{
|
2021-12-03 20:11:20 +01:00
|
|
|
|
use InvalidCodeAnalysisTestTrait;
|
|
|
|
|
use ValidCodeAnalysisTestTrait;
|
2018-05-03 19:56:30 +02:00
|
|
|
|
|
2021-12-05 18:51:26 +01:00
|
|
|
|
public function setUp(): void
|
2019-04-22 16:01:25 +02:00
|
|
|
|
{
|
2020-08-23 16:32:07 +02:00
|
|
|
|
RuntimeCaches::clearAll();
|
2019-04-22 16:01:25 +02:00
|
|
|
|
|
2021-07-02 01:10:21 +02:00
|
|
|
|
$this->file_provider = new FakeFileProvider();
|
2019-04-22 16:01:25 +02:00
|
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
|
$this->project_analyzer = new ProjectAnalyzer(
|
|
|
|
|
new TestConfig(),
|
|
|
|
|
new Providers(
|
2019-04-22 16:01:25 +02:00
|
|
|
|
$this->file_provider,
|
2021-12-03 20:11:20 +01:00
|
|
|
|
new FakeParserCacheProvider()
|
2019-04-22 16:01:25 +02:00
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
2021-11-27 01:06:33 +01:00
|
|
|
|
$this->project_analyzer->setPhpVersion('7.3', 'tests');
|
2019-04-22 16:01:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-03 19:56:30 +02:00
|
|
|
|
/**
|
2022-01-13 19:49:37 +01:00
|
|
|
|
* @return iterable<string,array{code:string,assertions?:array<string,string>,ignored_issues?:array<string>}>
|
2018-05-03 19:56:30 +02:00
|
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
|
public function providerValidCodeParse(): iterable
|
2018-05-03 19:56:30 +02:00
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'whileCountUpdate' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2018-05-03 19:56:30 +02:00
|
|
|
|
$array = [1, 2, 3];
|
|
|
|
|
while (rand(1, 10) === 1) {
|
|
|
|
|
$array[] = 4;
|
|
|
|
|
$array[] = 5;
|
|
|
|
|
$array[] = 6;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (count($array) === 7) {}',
|
|
|
|
|
],
|
|
|
|
|
'tryCountCatch' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2018-05-03 19:56:30 +02:00
|
|
|
|
$errors = [];
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
|
throw new Exception("bad");
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception $e) {
|
|
|
|
|
$errors[] = $e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (count($errors) !== 0) {
|
|
|
|
|
echo "Errors";
|
|
|
|
|
}',
|
|
|
|
|
],
|
|
|
|
|
'ternaryDifferentString' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2018-05-03 19:56:30 +02:00
|
|
|
|
$foo = rand(0, 1) ? "bar" : "bat";
|
|
|
|
|
|
|
|
|
|
if ($foo === "bar") {}
|
|
|
|
|
|
|
|
|
|
if ($foo !== "bar") {}
|
|
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
|
$foo = "baz";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($foo === "baz") {}
|
|
|
|
|
|
|
|
|
|
if ($foo !== "bat") {}',
|
|
|
|
|
],
|
|
|
|
|
'ifDifferentString' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2018-05-03 19:56:30 +02:00
|
|
|
|
$foo = "bar";
|
|
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
|
$foo = "bat";
|
|
|
|
|
} elseif (rand(0, 1)) {
|
|
|
|
|
$foo = "baz";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$bar = "bar";
|
|
|
|
|
$baz = "baz";
|
|
|
|
|
|
|
|
|
|
if ($foo === "bar") {}
|
|
|
|
|
if ($foo !== "bar") {}
|
|
|
|
|
if ($foo === "baz") {}
|
|
|
|
|
if ($foo === $bar) {}
|
|
|
|
|
if ($foo !== $bar) {}
|
|
|
|
|
if ($foo === $baz) {}',
|
|
|
|
|
],
|
|
|
|
|
'ifThisOrThat' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2018-05-03 19:56:30 +02:00
|
|
|
|
$foo = "bar";
|
|
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
|
$foo = "bat";
|
|
|
|
|
} elseif (rand(0, 1)) {
|
|
|
|
|
$foo = "baz";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($foo === "baz" || $foo === "bar") {}',
|
|
|
|
|
],
|
|
|
|
|
'ifDifferentNullableString' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2018-05-03 19:56:30 +02:00
|
|
|
|
$foo = null;
|
|
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
|
$foo = "bar";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$bar = "bar";
|
|
|
|
|
|
|
|
|
|
if ($foo === "bar") {}
|
|
|
|
|
if ($foo !== "bar") {}',
|
|
|
|
|
],
|
|
|
|
|
'whileIncremented' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2018-05-03 19:56:30 +02:00
|
|
|
|
$i = 1;
|
|
|
|
|
$j = 2;
|
|
|
|
|
while (rand(0, 1)) {
|
|
|
|
|
if ($i === $j) {}
|
|
|
|
|
$i++;
|
2019-03-23 19:27:54 +01:00
|
|
|
|
}',
|
2018-05-03 19:56:30 +02:00
|
|
|
|
],
|
|
|
|
|
'checkStringKeyValue' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2018-05-03 19:56:30 +02:00
|
|
|
|
$foo = [
|
|
|
|
|
"0" => 3,
|
|
|
|
|
"1" => 4,
|
|
|
|
|
"2" => 5,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
function takesInt(int $s) : void {}
|
|
|
|
|
|
|
|
|
|
foreach ($foo as $i => $b) {
|
|
|
|
|
takesInt($i);
|
|
|
|
|
}',
|
|
|
|
|
],
|
|
|
|
|
'getValidIntStringOffset' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2018-05-03 19:56:30 +02:00
|
|
|
|
$foo = [
|
|
|
|
|
"0" => 3,
|
|
|
|
|
"1" => 4,
|
|
|
|
|
"2" => 5,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$a = "2";
|
|
|
|
|
|
|
|
|
|
echo $foo["2"];
|
|
|
|
|
echo $foo[$a];',
|
|
|
|
|
],
|
|
|
|
|
'checkStringKeyValueAfterKnownIntStringOffset' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2018-05-03 19:56:30 +02:00
|
|
|
|
$foo = [
|
|
|
|
|
"0" => 3,
|
|
|
|
|
"1" => 4,
|
|
|
|
|
"2" => 5,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$a = "2";
|
|
|
|
|
$foo[$a] = 6;
|
|
|
|
|
|
|
|
|
|
function takesInt(int $s) : void {}
|
|
|
|
|
|
|
|
|
|
foreach ($foo as $i => $b) {
|
|
|
|
|
takesInt($i);
|
|
|
|
|
}',
|
|
|
|
|
],
|
2018-05-07 22:00:56 +02:00
|
|
|
|
'regularComparison1' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2018-05-07 22:00:56 +02:00
|
|
|
|
function foo(string $s1, string $s2, ?int $i) : string {
|
2018-05-07 20:52:45 +02:00
|
|
|
|
if ($s1 !== $s2) {
|
|
|
|
|
return $s1;
|
|
|
|
|
}
|
2018-05-07 22:00:56 +02:00
|
|
|
|
|
|
|
|
|
return $s2;
|
|
|
|
|
}',
|
|
|
|
|
],
|
|
|
|
|
'regularComparison2' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2018-05-07 22:00:56 +02:00
|
|
|
|
function foo(string $s1, string $s2) : string {
|
|
|
|
|
if ($s1 !== "hello") {
|
|
|
|
|
if ($s1 !== "goodbye") {
|
|
|
|
|
return $s1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $s2;
|
|
|
|
|
}',
|
|
|
|
|
],
|
|
|
|
|
'regularComparison3' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2018-05-07 22:00:56 +02:00
|
|
|
|
class A {
|
|
|
|
|
const B = 1;
|
|
|
|
|
const C = 2;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
function foo(string $s1, string $s2, ?int $i) : string {
|
|
|
|
|
if ($i !== A::B && $i !== A::C) {}
|
|
|
|
|
|
2018-05-07 20:52:45 +02:00
|
|
|
|
return $s2;
|
|
|
|
|
}',
|
|
|
|
|
],
|
2018-05-07 22:00:56 +02:00
|
|
|
|
'regularComparisonOnPossiblyNull' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2018-05-07 22:00:56 +02:00
|
|
|
|
/** @psalm-ignore-nullable-return */
|
|
|
|
|
function generate() : ?string {
|
|
|
|
|
return rand(0, 1000) ? "hello" : null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function foo() : string {
|
|
|
|
|
$str = generate();
|
|
|
|
|
|
|
|
|
|
if ($str[0] === "h") {
|
|
|
|
|
return $str;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return "hello";
|
|
|
|
|
}',
|
|
|
|
|
],
|
2018-05-08 23:42:02 +02:00
|
|
|
|
'incrementAndCheck' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2018-05-08 23:42:02 +02:00
|
|
|
|
$i = 0;
|
|
|
|
|
if (rand(0, 1)) $i++;
|
2019-03-23 19:27:54 +01:00
|
|
|
|
if ($i === 1) {}',
|
2018-05-08 23:42:02 +02:00
|
|
|
|
],
|
|
|
|
|
'incrementInClosureAndCheck' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2018-05-08 23:42:02 +02:00
|
|
|
|
$i = 0;
|
|
|
|
|
$a = function() use (&$i) : void {
|
2020-07-31 18:44:01 +02:00
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
|
$i++;
|
|
|
|
|
}
|
2018-05-08 23:42:02 +02:00
|
|
|
|
};
|
|
|
|
|
$a();
|
|
|
|
|
if ($i === 0) {}',
|
2018-06-18 19:16:51 +02:00
|
|
|
|
'assertions' => [],
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'ignored_issues' => ['MixedOperand', 'MixedAssignment'],
|
2018-05-08 23:42:02 +02:00
|
|
|
|
],
|
2018-05-09 00:11:10 +02:00
|
|
|
|
'incrementMixedCall' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2018-05-09 00:11:10 +02:00
|
|
|
|
function foo($f) : void {
|
|
|
|
|
$i = 0;
|
|
|
|
|
$f->add(function() use (&$i) : void {
|
|
|
|
|
if (rand(0, 1)) $i++;
|
|
|
|
|
});
|
|
|
|
|
if ($i === 0) {}
|
|
|
|
|
}',
|
|
|
|
|
'assertions' => [],
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'ignored_issues' => ['MissingParamType', 'MixedMethodCall', 'MixedOperand', 'MixedAssignment'],
|
2018-05-09 00:11:10 +02:00
|
|
|
|
],
|
2018-05-13 00:46:47 +02:00
|
|
|
|
'regularValueReconciliation' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2018-05-13 00:46:47 +02:00
|
|
|
|
$s = rand(0, 1) ? "a" : "b";
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
|
$s = "c";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($s === "a" || $s === "b") {
|
|
|
|
|
if ($s === "a") {}
|
2019-03-23 19:27:54 +01:00
|
|
|
|
}',
|
2018-05-13 00:46:47 +02:00
|
|
|
|
],
|
2018-05-18 17:02:50 +02:00
|
|
|
|
'moreValueReconciliation' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2018-05-18 17:02:50 +02:00
|
|
|
|
$a = rand(0, 1) ? "a" : "b";
|
|
|
|
|
$b = rand(0, 1) ? "a" : "b";
|
|
|
|
|
|
|
|
|
|
$s = rand(0, 1) ? $a : $b;
|
|
|
|
|
if (rand(0, 1)) $s = "c";
|
|
|
|
|
|
|
|
|
|
if ($s === $a) {
|
|
|
|
|
} elseif ($s === $b) {}',
|
|
|
|
|
],
|
2018-05-13 00:46:47 +02:00
|
|
|
|
'negativeInts' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2018-05-13 00:46:47 +02:00
|
|
|
|
class C {
|
|
|
|
|
const A = 1;
|
|
|
|
|
const B = -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const A = 1;
|
|
|
|
|
const B = -1;
|
|
|
|
|
|
|
|
|
|
$i = rand(0, 1) ? A : B;
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
|
$i = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($i === A) {
|
|
|
|
|
echo "here";
|
|
|
|
|
} elseif ($i === B) {
|
|
|
|
|
echo "here";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$i = rand(0, 1) ? C::A : C::B;
|
|
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
|
$i = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($i === C::A) {
|
|
|
|
|
echo "here";
|
|
|
|
|
} elseif ($i === C::B) {
|
|
|
|
|
echo "here";
|
|
|
|
|
}',
|
|
|
|
|
],
|
2018-05-18 17:02:50 +02:00
|
|
|
|
'falsyReconciliation' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2018-05-18 17:02:50 +02:00
|
|
|
|
$s = rand(0, 1) ? 200 : null;
|
2019-03-23 19:27:54 +01:00
|
|
|
|
if (!$s) {}',
|
2018-05-18 17:02:50 +02:00
|
|
|
|
],
|
|
|
|
|
'redefinedIntInIfAndPossibleComparison' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2018-05-18 17:02:50 +02:00
|
|
|
|
$s = rand(0, 1) ? 0 : 1;
|
|
|
|
|
|
|
|
|
|
if ($s && rand(0, 1)) {
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
|
$s = 2;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($s == 2) {}',
|
|
|
|
|
],
|
2018-05-22 00:33:39 +02:00
|
|
|
|
'noEmpties' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2018-05-22 00:33:39 +02:00
|
|
|
|
$context = \'a\';
|
|
|
|
|
while ( true ) {
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
|
exit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$context = \'b\';
|
|
|
|
|
} elseif (rand(0, 1)) {
|
|
|
|
|
if ($context !== \'c\' && $context !== \'b\') {
|
|
|
|
|
exit;
|
|
|
|
|
}
|
2018-05-18 17:02:50 +02:00
|
|
|
|
|
2018-05-22 00:33:39 +02:00
|
|
|
|
$context = \'c\';
|
|
|
|
|
}
|
|
|
|
|
}',
|
|
|
|
|
],
|
2018-06-08 19:53:42 +02:00
|
|
|
|
'ifOrAssertionWithSwitch' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2018-06-08 19:53:42 +02:00
|
|
|
|
function foo(string $s) : void {
|
|
|
|
|
switch ($s) {
|
|
|
|
|
case "a":
|
|
|
|
|
case "b":
|
|
|
|
|
case "c":
|
|
|
|
|
if ($s === "a" || $s === "b") {
|
|
|
|
|
throw new \InvalidArgumentException;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}',
|
|
|
|
|
],
|
|
|
|
|
'inArrayAssertionProperty' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2018-06-08 19:53:42 +02:00
|
|
|
|
class Foo
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* @psalm-var "a"|"b"
|
|
|
|
|
*/
|
|
|
|
|
private $s;
|
|
|
|
|
|
|
|
|
|
public function __construct(string $s)
|
|
|
|
|
{
|
|
|
|
|
if (!in_array($s, ["a", "b"], true)) {
|
|
|
|
|
throw new \InvalidArgumentException;
|
|
|
|
|
}
|
|
|
|
|
$this->s = $s;
|
|
|
|
|
}
|
|
|
|
|
}',
|
|
|
|
|
],
|
|
|
|
|
'inArrayAssertionWithSwitch' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2018-06-08 19:53:42 +02:00
|
|
|
|
function foo(string $s) : void {
|
|
|
|
|
switch ($s) {
|
|
|
|
|
case "a":
|
|
|
|
|
case "b":
|
|
|
|
|
case "c":
|
|
|
|
|
if (in_array($s, ["a", "b"], true)) {
|
|
|
|
|
throw new \InvalidArgumentException;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}',
|
|
|
|
|
],
|
2018-06-12 17:19:35 +02:00
|
|
|
|
'removeLiteralStringForNotIsString' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2018-06-12 17:19:35 +02:00
|
|
|
|
function takesInt(int $i) : void {}
|
|
|
|
|
|
|
|
|
|
$f = ["a", "b", "c"];
|
|
|
|
|
$f[rand(0, 2)] = 5;
|
|
|
|
|
|
|
|
|
|
$i = rand(0, 2);
|
|
|
|
|
if (isset($f[$i]) && !is_string($f[$i])) {
|
|
|
|
|
takesInt($f[$i]);
|
2019-03-23 19:27:54 +01:00
|
|
|
|
}',
|
2018-06-12 17:19:35 +02:00
|
|
|
|
],
|
|
|
|
|
'removeLiteralIntForNotIsInt' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2018-06-12 17:19:35 +02:00
|
|
|
|
function takesString(string $i) : void {}
|
|
|
|
|
|
|
|
|
|
$f = [0, 1, 2];
|
|
|
|
|
$f[rand(0, 2)] = "hello";
|
|
|
|
|
|
|
|
|
|
$i = rand(0, 2);
|
|
|
|
|
if (isset($f[$i]) && !is_int($f[$i])) {
|
|
|
|
|
takesString($f[$i]);
|
2019-03-23 19:27:54 +01:00
|
|
|
|
}',
|
2018-06-12 17:19:35 +02:00
|
|
|
|
],
|
|
|
|
|
'removeLiteralFloatForNotIsFloat' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2018-06-12 17:19:35 +02:00
|
|
|
|
function takesString(string $i) : void {}
|
|
|
|
|
|
|
|
|
|
$f = [1.1, 1.2, 1.3];
|
|
|
|
|
$f[rand(0, 2)] = "hello";
|
|
|
|
|
|
|
|
|
|
$i = rand(0, 2);
|
|
|
|
|
if (isset($f[$i]) && !is_float($f[$i])) {
|
|
|
|
|
takesString($f[$i]);
|
2019-03-23 19:27:54 +01:00
|
|
|
|
}',
|
2018-06-12 17:19:35 +02:00
|
|
|
|
],
|
2018-09-09 19:01:16 +02:00
|
|
|
|
'coerceFromMixed' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2018-09-09 19:01:16 +02:00
|
|
|
|
function type(int $b): void {}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param mixed $a
|
|
|
|
|
*/
|
|
|
|
|
function foo($a) : void {
|
|
|
|
|
if ($a === 1 || $a === 2) {
|
|
|
|
|
type($a);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (in_array($a, [1, 2], true)) {
|
|
|
|
|
type($a);
|
|
|
|
|
}
|
|
|
|
|
}',
|
|
|
|
|
],
|
|
|
|
|
'coerceFromString' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2018-09-09 19:01:16 +02:00
|
|
|
|
/** @param "a"|"b" $b */
|
|
|
|
|
function type(string $b): void {}
|
|
|
|
|
|
|
|
|
|
function foo(string $a) : void {
|
|
|
|
|
if ($a === "a" || $a === "b") {
|
|
|
|
|
type($a);
|
|
|
|
|
}
|
|
|
|
|
}',
|
|
|
|
|
],
|
2018-11-02 04:31:40 +01:00
|
|
|
|
'coercePossibleOffset' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2018-11-02 04:31:40 +01:00
|
|
|
|
class A {
|
|
|
|
|
const FOO = "foo";
|
|
|
|
|
const BAR = "bar";
|
|
|
|
|
const BAT = "bat";
|
|
|
|
|
const BAM = "bam";
|
|
|
|
|
|
|
|
|
|
/** @var self::FOO|self::BAR|self::BAT|null $s */
|
|
|
|
|
public $s;
|
|
|
|
|
|
|
|
|
|
public function isFooOrBar() : void {
|
|
|
|
|
$map = [
|
|
|
|
|
A::FOO => 1,
|
|
|
|
|
A::BAR => 1,
|
|
|
|
|
A::BAM => 1,
|
|
|
|
|
];
|
|
|
|
|
|
2020-02-13 23:58:15 +01:00
|
|
|
|
if ($this->s !== null && isset($map[$this->s])) {}
|
2018-11-02 04:31:40 +01:00
|
|
|
|
}
|
2019-03-23 19:27:54 +01:00
|
|
|
|
}',
|
2018-11-02 04:31:40 +01:00
|
|
|
|
],
|
2018-12-08 19:18:55 +01:00
|
|
|
|
'noRedundantConditionWithMixed' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2018-12-08 19:18:55 +01:00
|
|
|
|
function foo($a) : void {
|
|
|
|
|
if ($a == "a") {
|
|
|
|
|
} else {
|
|
|
|
|
if ($a == "b" && rand(0, 1)) {}
|
|
|
|
|
}
|
|
|
|
|
}',
|
2019-03-01 21:55:20 +01:00
|
|
|
|
'assertions' => [],
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'ignored_issues' => ['MissingParamType', 'MixedAssignment'],
|
2018-12-08 19:18:55 +01:00
|
|
|
|
],
|
2019-03-12 06:26:19 +01:00
|
|
|
|
'numericToStringComparison' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2019-03-12 06:26:19 +01:00
|
|
|
|
/** @psalm-suppress MissingParamType */
|
|
|
|
|
function foo($s) : void {
|
|
|
|
|
if (is_numeric($s)) {
|
|
|
|
|
if ($s === 1) {}
|
|
|
|
|
}
|
|
|
|
|
}',
|
|
|
|
|
],
|
2019-03-16 17:40:19 +01:00
|
|
|
|
'newlineIssue' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2019-03-16 17:40:19 +01:00
|
|
|
|
$a = "foo";
|
|
|
|
|
$b = "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
";
|
|
|
|
|
|
|
|
|
|
$c = $a;
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
|
$c = $b;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-23 19:27:54 +01:00
|
|
|
|
if ($c === $b) {}',
|
2019-03-16 17:40:19 +01:00
|
|
|
|
],
|
2019-03-21 21:57:42 +01:00
|
|
|
|
'don’tChangeType' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2019-03-21 21:57:42 +01:00
|
|
|
|
$x = 0;
|
|
|
|
|
$y = rand(0, 1);
|
|
|
|
|
$x++;
|
|
|
|
|
if ($x !== $y) {
|
|
|
|
|
chr($x);
|
|
|
|
|
}',
|
|
|
|
|
],
|
2019-03-21 22:26:10 +01:00
|
|
|
|
'don’tChangeTypeInElse' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2019-03-21 22:26:10 +01:00
|
|
|
|
/** @var 0|string */
|
|
|
|
|
$x = 0;
|
|
|
|
|
$y = rand(0, 1) ? 0 : 1;
|
|
|
|
|
if ($x !== $y) {
|
|
|
|
|
} else {
|
|
|
|
|
if (!is_string($x)) {
|
|
|
|
|
chr($x);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @var int|string */
|
|
|
|
|
$x = 0;
|
|
|
|
|
if ($x !== $y) {
|
|
|
|
|
} else {
|
|
|
|
|
if (!is_string($x)) {
|
|
|
|
|
chr($x);
|
|
|
|
|
}
|
2019-03-23 19:27:54 +01:00
|
|
|
|
}',
|
2019-03-21 22:26:10 +01:00
|
|
|
|
],
|
2019-03-22 21:45:17 +01:00
|
|
|
|
'convertNullArrayKeyToEmptyString' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2019-03-22 21:45:17 +01:00
|
|
|
|
$a = [
|
|
|
|
|
1 => 1,
|
|
|
|
|
2 => 2,
|
|
|
|
|
null => "hello",
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$b = $a[""];',
|
|
|
|
|
'assertions' => [
|
|
|
|
|
'$b' => 'string',
|
|
|
|
|
],
|
|
|
|
|
],
|
2019-05-02 06:50:35 +02:00
|
|
|
|
'yodaConditionalsShouldHaveSameOutput1' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2019-05-02 06:50:35 +02:00
|
|
|
|
class Foo {
|
|
|
|
|
/**
|
|
|
|
|
* @var array{from:bool, to:bool}
|
|
|
|
|
*/
|
|
|
|
|
protected $things = ["from" => 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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2019-05-02 06:50:35 +02:00
|
|
|
|
class Foo {
|
|
|
|
|
/**
|
|
|
|
|
* @var array{from:bool, to:bool}
|
|
|
|
|
*/
|
|
|
|
|
protected $things = ["from" => 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];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
',
|
|
|
|
|
],
|
2019-11-21 16:44:24 +01:00
|
|
|
|
'supportSingleLiteralType' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2019-11-21 16:44:24 +01:00
|
|
|
|
class A {
|
|
|
|
|
/**
|
|
|
|
|
* @var string
|
|
|
|
|
* @psalm-var "easy"
|
|
|
|
|
*/
|
|
|
|
|
private $type = "easy";
|
|
|
|
|
}'
|
|
|
|
|
],
|
2021-07-08 13:08:35 +02:00
|
|
|
|
'supportMultipleValues' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2021-07-08 13:08:35 +02:00
|
|
|
|
class A {
|
|
|
|
|
/**
|
|
|
|
|
* @var 0|-1|1
|
|
|
|
|
*/
|
|
|
|
|
private $type = -1;
|
|
|
|
|
}'
|
|
|
|
|
],
|
2020-01-04 22:36:19 +01:00
|
|
|
|
'typecastTrueToInt' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2020-01-04 22:36:19 +01:00
|
|
|
|
/**
|
|
|
|
|
* @param 0|1 $int
|
|
|
|
|
*/
|
|
|
|
|
function foo(int $int) : void {
|
|
|
|
|
echo (string) $int;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foo((int) true);',
|
|
|
|
|
],
|
|
|
|
|
'typecastFalseToInt' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2020-01-04 22:36:19 +01:00
|
|
|
|
/**
|
|
|
|
|
* @param 0|1 $int
|
|
|
|
|
*/
|
|
|
|
|
function foo(int $int) : void {
|
|
|
|
|
echo (string) $int;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foo((int) false);',
|
|
|
|
|
],
|
|
|
|
|
'typecastedBoolToInt' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2020-01-04 22:36:19 +01:00
|
|
|
|
/**
|
|
|
|
|
* @param 0|1 $int
|
|
|
|
|
*/
|
|
|
|
|
function foo(int $int) : void {
|
|
|
|
|
echo (string) $int;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foo((int) ((bool) 2));',
|
|
|
|
|
],
|
2020-01-30 03:25:44 +01:00
|
|
|
|
'notEqualToEachOther' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2020-01-30 03:25:44 +01:00
|
|
|
|
function example(object $a, object $b): bool {
|
|
|
|
|
if ($a !== $b && \get_class($a) === \get_class($b)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}'
|
|
|
|
|
],
|
2020-02-22 06:16:15 +01:00
|
|
|
|
'numericStringValue' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2020-02-22 06:16:15 +01:00
|
|
|
|
/** @psalm-return numeric-string */
|
|
|
|
|
function makeNumeric() : string {
|
|
|
|
|
return "12.34";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @psalm-param numeric-string $string */
|
|
|
|
|
function consumeNumeric(string $string) : void {
|
|
|
|
|
\error_log($string);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
consumeNumeric("12.34");'
|
|
|
|
|
],
|
2020-02-26 23:28:31 +01:00
|
|
|
|
'resolveScalarClassConstant' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2020-02-26 23:28:31 +01:00
|
|
|
|
class PaymentFailure {
|
|
|
|
|
const NO_CLIENT = "no_client";
|
|
|
|
|
const NO_CARD = "no_card";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return PaymentFailure::NO_CARD|PaymentFailure::NO_CLIENT
|
|
|
|
|
*/
|
|
|
|
|
function something() {
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
|
return PaymentFailure::NO_CARD;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return PaymentFailure::NO_CLIENT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function blah(): void {
|
|
|
|
|
$test = something();
|
|
|
|
|
if ($test === PaymentFailure::NO_CLIENT) {}
|
|
|
|
|
}'
|
|
|
|
|
],
|
2020-05-18 14:52:37 +02:00
|
|
|
|
'removeNullAfterLessThanZero' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2020-05-18 14:52:37 +02:00
|
|
|
|
function fcn(?int $val): int {
|
|
|
|
|
if ($val < 0) {
|
|
|
|
|
return $val;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 5;
|
|
|
|
|
}',
|
|
|
|
|
],
|
2020-05-18 19:42:47 +02:00
|
|
|
|
'numericStringCastFromInt' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2020-05-18 19:42:47 +02:00
|
|
|
|
/**
|
|
|
|
|
* @return numeric-string
|
|
|
|
|
*/
|
|
|
|
|
function makeNumStringFromInt(int $v) {
|
|
|
|
|
return (string) $v;
|
|
|
|
|
}',
|
|
|
|
|
],
|
|
|
|
|
'numericStringCastFromFloat' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2020-05-18 19:42:47 +02:00
|
|
|
|
/**
|
|
|
|
|
* @return numeric-string
|
|
|
|
|
*/
|
|
|
|
|
function makeNumStringFromFloat(float $v) {
|
|
|
|
|
return (string) $v;
|
|
|
|
|
}'
|
2020-07-27 01:09:26 +02:00
|
|
|
|
],
|
|
|
|
|
'compareNegatedValue' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2020-07-27 01:09:26 +02:00
|
|
|
|
$i = rand(-1, 5);
|
|
|
|
|
|
|
|
|
|
if (!($i > 0)) {
|
|
|
|
|
echo $i;
|
|
|
|
|
}',
|
|
|
|
|
],
|
2020-07-30 17:25:47 +02:00
|
|
|
|
'refinePositiveInt' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2020-07-30 17:25:47 +02:00
|
|
|
|
$f = rand(0, 1) ? -1 : 1;
|
|
|
|
|
if ($f > 0) {}'
|
|
|
|
|
],
|
2020-07-31 17:26:54 +02:00
|
|
|
|
'assignOpThenCheck' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2020-07-31 17:26:54 +02:00
|
|
|
|
$data = ["e" => 0];
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
|
$data["e"]++;
|
|
|
|
|
}
|
|
|
|
|
if ($data["e"] > 0) {}'
|
|
|
|
|
],
|
2020-08-26 23:58:01 +02:00
|
|
|
|
'compareToNullImplicitly' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2020-08-26 23:58:01 +02:00
|
|
|
|
final class Foo {
|
|
|
|
|
public const VALUE_ANY = null;
|
|
|
|
|
public const VALUE_ONE = "one";
|
|
|
|
|
|
|
|
|
|
/** @return self::VALUE_* */
|
|
|
|
|
public static function getValues() {
|
|
|
|
|
return rand(0, 1) ? null : self::VALUE_ONE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$data = Foo::getValues();
|
|
|
|
|
|
|
|
|
|
if ($data === Foo::VALUE_ANY) {
|
|
|
|
|
$data = "default";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
echo strlen($data);'
|
|
|
|
|
],
|
2020-08-31 16:02:23 +02:00
|
|
|
|
'negateValueInUnion' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2020-08-31 16:02:23 +02:00
|
|
|
|
function f(): int {
|
|
|
|
|
$ret = 0;
|
|
|
|
|
for ($i = 20; $i >= 0; $i--) {
|
|
|
|
|
$ret = ($ret === 10) ? 1 : $ret + 1;
|
|
|
|
|
}
|
|
|
|
|
return $ret;
|
|
|
|
|
}'
|
|
|
|
|
],
|
2020-09-20 00:12:14 +02:00
|
|
|
|
'inArrayPreserveNull' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2020-09-20 00:12:14 +02:00
|
|
|
|
function x(?string $foo): void {
|
|
|
|
|
if (!in_array($foo, ["foo", "bar", null], true)) {
|
|
|
|
|
throw new Exception();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($foo) {}
|
|
|
|
|
}',
|
|
|
|
|
],
|
2020-10-12 05:16:43 +02:00
|
|
|
|
'allowCheckOnPositiveNumericInverse' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2020-10-12 05:16:43 +02:00
|
|
|
|
function foo(int $a): void {
|
|
|
|
|
if (false === ($a > 1)){}
|
|
|
|
|
}'
|
|
|
|
|
],
|
2020-11-19 20:32:49 +01:00
|
|
|
|
'returnFromUnionLiteral' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2020-11-19 20:32:49 +01:00
|
|
|
|
/**
|
2021-08-03 18:34:35 +02:00
|
|
|
|
* @return array{"a1", "a2"}
|
2020-11-19 20:32:49 +01:00
|
|
|
|
*/
|
|
|
|
|
function getSupportedConsts() {
|
|
|
|
|
return ["a1", "a2"];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function foo(mixed $file) : string {
|
|
|
|
|
if (in_array($file, getSupportedConsts(), true)) {
|
|
|
|
|
return $file;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return "";
|
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'assertions' => [],
|
|
|
|
|
'ignored_issues' => [],
|
|
|
|
|
'php_version' => '8.0'
|
2020-11-19 20:32:49 +01:00
|
|
|
|
],
|
|
|
|
|
'returnFromUnionLiteralNegated' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2020-11-19 20:32:49 +01:00
|
|
|
|
/**
|
2021-08-03 18:34:35 +02:00
|
|
|
|
* @return array{"a1", "a2"}
|
2020-11-19 20:32:49 +01:00
|
|
|
|
*/
|
|
|
|
|
function getSupportedConsts() {
|
|
|
|
|
return ["a1", "a2"];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function foo(mixed $file) : string {
|
|
|
|
|
if (!in_array($file, getSupportedConsts(), true)) {
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $file;
|
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'assertions' => [],
|
|
|
|
|
'ignored_issues' => [],
|
|
|
|
|
'php_version' => '8.0'
|
2020-11-19 20:32:49 +01:00
|
|
|
|
],
|
|
|
|
|
'inArrayInsideLoop' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2020-11-19 20:32:49 +01:00
|
|
|
|
class A {
|
|
|
|
|
const ACTION_ONE = "one";
|
|
|
|
|
const ACTION_TWO = "two";
|
|
|
|
|
const ACTION_THREE = "two";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (rand(0, 1)) {
|
|
|
|
|
/** @var list<A::ACTION_*> */
|
|
|
|
|
$case_actions = [];
|
|
|
|
|
|
|
|
|
|
if (!in_array(A::ACTION_ONE, $case_actions, true)) {}
|
|
|
|
|
}'
|
|
|
|
|
],
|
2021-01-11 23:14:23 +01:00
|
|
|
|
'checkIdenticalArray' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2021-01-11 23:14:23 +01:00
|
|
|
|
/** @psalm-suppress MixedAssignment */
|
|
|
|
|
$array = json_decode(file_get_contents(\'php://stdin\'));
|
|
|
|
|
|
|
|
|
|
if (is_array($array)) {
|
|
|
|
|
$filtered = array_filter($array, fn ($value) => \is_string($value));
|
|
|
|
|
|
|
|
|
|
if ($array === $filtered) {
|
|
|
|
|
foreach ($array as $obj) {
|
|
|
|
|
echo strlen($obj);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'assertions' => [],
|
|
|
|
|
'ignored_issues' => [],
|
|
|
|
|
'php_version' => '7.4'
|
2021-01-11 23:14:23 +01:00
|
|
|
|
],
|
2021-02-03 23:19:48 +01:00
|
|
|
|
'zeroIsNonEmptyString' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2021-02-03 23:19:48 +01:00
|
|
|
|
/**
|
|
|
|
|
* @param non-empty-string $s
|
|
|
|
|
*/
|
|
|
|
|
function foo(string $s) : void {}
|
|
|
|
|
|
|
|
|
|
foo("0");',
|
|
|
|
|
],
|
|
|
|
|
'notLiteralEmptyCanBeNotEmptyString' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2021-02-03 23:19:48 +01:00
|
|
|
|
/**
|
|
|
|
|
* @param non-empty-string $s
|
|
|
|
|
*/
|
|
|
|
|
function foo(string $s) : void {}
|
|
|
|
|
|
|
|
|
|
function takesString(string $s) : void {
|
|
|
|
|
if ($s !== "") {
|
|
|
|
|
foo($s);
|
|
|
|
|
}
|
|
|
|
|
}',
|
|
|
|
|
],
|
|
|
|
|
'nonEmptyStringCanBeStringZero' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2021-02-03 23:19:48 +01:00
|
|
|
|
/**
|
|
|
|
|
* @param non-empty-string $s
|
|
|
|
|
*/
|
|
|
|
|
function foo(string $s) : void {
|
|
|
|
|
if ($s === "0") {}
|
|
|
|
|
if (empty($s)) {}
|
|
|
|
|
}',
|
|
|
|
|
],
|
2018-05-03 19:56:30 +02:00
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-01-13 19:49:37 +01:00
|
|
|
|
* @return iterable<string,array{code:string,error_message:string}>
|
2018-05-03 19:56:30 +02:00
|
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
|
public function providerInvalidCodeParse(): iterable
|
2018-05-03 19:56:30 +02:00
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'neverEqualsType' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2018-05-03 19:56:30 +02:00
|
|
|
|
$a = 4;
|
|
|
|
|
if ($a === 5) {
|
|
|
|
|
// do something
|
|
|
|
|
}',
|
|
|
|
|
'error_message' => 'TypeDoesNotContainType',
|
|
|
|
|
],
|
|
|
|
|
'alwaysIdenticalType' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2018-05-03 19:56:30 +02:00
|
|
|
|
$a = 4;
|
|
|
|
|
if ($a === 4) {
|
|
|
|
|
// do something
|
|
|
|
|
}',
|
|
|
|
|
'error_message' => 'RedundantCondition',
|
|
|
|
|
],
|
|
|
|
|
'alwaysNotIdenticalType' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2018-05-03 19:56:30 +02:00
|
|
|
|
$a = 4;
|
|
|
|
|
if ($a !== 5) {
|
|
|
|
|
// do something
|
|
|
|
|
}',
|
|
|
|
|
'error_message' => 'RedundantCondition',
|
|
|
|
|
],
|
|
|
|
|
'neverNotIdenticalType' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2018-05-03 19:56:30 +02:00
|
|
|
|
$a = 4;
|
|
|
|
|
if ($a !== 4) {
|
|
|
|
|
// do something
|
|
|
|
|
}',
|
2018-05-18 17:02:50 +02:00
|
|
|
|
'error_message' => 'TypeDoesNotContainType',
|
2018-05-03 19:56:30 +02:00
|
|
|
|
],
|
2018-06-10 16:48:19 +02:00
|
|
|
|
'neverEqualsFloatType' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2018-06-10 16:48:19 +02:00
|
|
|
|
$a = 4.0;
|
|
|
|
|
if ($a === 4.1) {
|
|
|
|
|
// do something
|
|
|
|
|
}',
|
|
|
|
|
'error_message' => 'TypeDoesNotContainType',
|
|
|
|
|
],
|
|
|
|
|
'alwaysIdenticalFloatType' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2018-06-10 16:48:19 +02:00
|
|
|
|
$a = 4.1;
|
|
|
|
|
if ($a === 4.1) {
|
|
|
|
|
// do something
|
|
|
|
|
}',
|
|
|
|
|
'error_message' => 'RedundantCondition',
|
|
|
|
|
],
|
|
|
|
|
'alwaysNotIdenticalFloatType' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2018-06-10 16:48:19 +02:00
|
|
|
|
$a = 4.0;
|
|
|
|
|
if ($a !== 4.1) {
|
|
|
|
|
// do something
|
|
|
|
|
}',
|
|
|
|
|
'error_message' => 'RedundantCondition',
|
|
|
|
|
],
|
2021-09-04 18:28:34 +02:00
|
|
|
|
'inArrayRemoveNull' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2021-09-04 18:28:34 +02:00
|
|
|
|
function x(?string $foo, string $bar): void {
|
|
|
|
|
if (!in_array($foo, [$bar], true)) {
|
|
|
|
|
throw new Exception();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (is_string($foo)) {}
|
|
|
|
|
}',
|
|
|
|
|
'error_message' => 'RedundantCondition',
|
|
|
|
|
],
|
2018-06-10 16:48:19 +02:00
|
|
|
|
'neverNotIdenticalFloatType' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2018-06-10 16:48:19 +02:00
|
|
|
|
$a = 4.1;
|
|
|
|
|
if ($a !== 4.1) {
|
|
|
|
|
// do something
|
|
|
|
|
}',
|
|
|
|
|
'error_message' => 'TypeDoesNotContainType',
|
|
|
|
|
],
|
2018-05-03 19:56:30 +02:00
|
|
|
|
'ifImpossibleString' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2018-05-03 19:56:30 +02:00
|
|
|
|
$foo = rand(0, 1) ? "bar" : "bat";
|
|
|
|
|
|
|
|
|
|
if ($foo === "baz") {}',
|
|
|
|
|
'error_message' => 'TypeDoesNotContainType',
|
|
|
|
|
],
|
|
|
|
|
'arrayOffsetImpossibleValue' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2018-05-03 19:56:30 +02:00
|
|
|
|
$foo = [
|
|
|
|
|
"a" => 1,
|
|
|
|
|
"b" => 2,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if ($foo["a"] === 2) {}',
|
|
|
|
|
'error_message' => 'TypeDoesNotContainType',
|
|
|
|
|
],
|
|
|
|
|
'impossibleKeyInForeach' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2018-05-03 19:56:30 +02:00
|
|
|
|
$foo = [
|
|
|
|
|
"0" => 3,
|
|
|
|
|
"1" => 4,
|
|
|
|
|
"2" => 5,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
function takesInt(int $s) : void {}
|
|
|
|
|
|
|
|
|
|
foreach ($foo as $i => $b) {
|
|
|
|
|
if ($i === 3) {}
|
|
|
|
|
}',
|
|
|
|
|
'error_message' => 'TypeDoesNotContainType',
|
|
|
|
|
],
|
|
|
|
|
'impossibleValueInForeach' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2018-05-03 19:56:30 +02:00
|
|
|
|
$foo = [
|
|
|
|
|
"0" => 3,
|
|
|
|
|
"1" => 4,
|
|
|
|
|
"2" => 5,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
function takesInt(int $s) : void {}
|
|
|
|
|
|
|
|
|
|
foreach ($foo as $i => $b) {
|
|
|
|
|
if ($b === $i) {}
|
|
|
|
|
}',
|
|
|
|
|
'error_message' => 'TypeDoesNotContainType',
|
|
|
|
|
],
|
|
|
|
|
'invalidIntStringOffset' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2018-05-03 19:56:30 +02:00
|
|
|
|
$foo = [
|
|
|
|
|
"0" => 3,
|
|
|
|
|
"1" => 4,
|
|
|
|
|
"2" => 5,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$a = "3";
|
|
|
|
|
|
|
|
|
|
echo $foo[$a];',
|
|
|
|
|
'error_message' => 'InvalidArrayOffset',
|
|
|
|
|
],
|
2018-05-12 00:09:11 +02:00
|
|
|
|
'noChangeToVariable' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2018-05-12 00:09:11 +02:00
|
|
|
|
$i = 0;
|
|
|
|
|
|
|
|
|
|
$a = function() use ($i) : void {
|
|
|
|
|
$i++;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$a();
|
|
|
|
|
|
|
|
|
|
if ($i === 0) {}',
|
|
|
|
|
'error_message' => 'RedundantCondition',
|
|
|
|
|
],
|
2018-05-18 17:02:50 +02:00
|
|
|
|
'redefinedIntInIfAndImpossbleComparison' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2018-05-18 17:02:50 +02:00
|
|
|
|
$s = rand(0, 1) ? 0 : 1;
|
|
|
|
|
|
|
|
|
|
if ($s && rand(0, 1)) {
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
|
$s = 2;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($s == 3) {}',
|
|
|
|
|
'error_message' => 'TypeDoesNotContainType',
|
|
|
|
|
],
|
2018-06-08 19:53:42 +02:00
|
|
|
|
'badIfOrAssertionWithSwitch' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2018-06-08 19:53:42 +02:00
|
|
|
|
function foo(string $s) : void {
|
|
|
|
|
switch ($s) {
|
|
|
|
|
case "a":
|
|
|
|
|
case "b":
|
|
|
|
|
case "c":
|
|
|
|
|
if ($s === "a" || $s === "b") {
|
|
|
|
|
throw new \InvalidArgumentException;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($s === "c") {}
|
|
|
|
|
}
|
|
|
|
|
}',
|
|
|
|
|
'error_message' => 'RedundantCondition',
|
|
|
|
|
],
|
2019-05-28 21:46:56 +02:00
|
|
|
|
'casedComparison' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2019-05-28 21:46:56 +02:00
|
|
|
|
if ("C" === "c") {}',
|
|
|
|
|
'error_message' => 'TypeDoesNotContainType',
|
2019-07-05 22:24:00 +02:00
|
|
|
|
],
|
2020-07-27 00:29:17 +02:00
|
|
|
|
'compareValueTwice' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2020-07-27 00:29:17 +02:00
|
|
|
|
$i = rand(-1, 5);
|
|
|
|
|
|
|
|
|
|
if ($i > 0 && $i > 0) {}',
|
|
|
|
|
'error_message' => 'RedundantCondition',
|
|
|
|
|
],
|
2020-05-25 04:42:08 +02:00
|
|
|
|
'numericStringCoerceToLiteral' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2020-05-25 04:42:08 +02:00
|
|
|
|
/** @param "0"|"1" $s */
|
|
|
|
|
function foo(string $s) : void {}
|
|
|
|
|
|
|
|
|
|
function bar(string $s) : void {
|
|
|
|
|
if (is_numeric($s)) {
|
|
|
|
|
foo($s);
|
|
|
|
|
}
|
|
|
|
|
}',
|
|
|
|
|
'error_message' => 'ArgumentTypeCoercion'
|
|
|
|
|
],
|
2021-02-03 23:19:48 +01:00
|
|
|
|
'stringCoercedToNonEmptyString' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
|
'code' => '<?php
|
2021-02-03 23:19:48 +01:00
|
|
|
|
/**
|
|
|
|
|
* @param non-empty-string $name
|
|
|
|
|
*/
|
|
|
|
|
function sayHello(string $name) : void {}
|
|
|
|
|
|
|
|
|
|
function takeInput(string $name) : void {
|
|
|
|
|
sayHello($name);
|
|
|
|
|
}',
|
|
|
|
|
'error_message' => 'ArgumentTypeCoercion',
|
|
|
|
|
],
|
2018-05-03 19:56:30 +02:00
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|