1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

fix bugs in tests

This commit is contained in:
kkmuffme 2024-01-12 23:37:45 +01:00
parent 19b1a33a20
commit 93dc2219e0
19 changed files with 121 additions and 42 deletions

View File

@ -663,7 +663,7 @@ class ArrayAccessTest extends TestCase
$x = $d[0];',
'assertions' => [
'$x===' => '"a"|null',
'$x===' => '\'a\'',
],
'ignored_issues' => ['PossiblyUndefinedArrayOffset'],
],
@ -1349,7 +1349,7 @@ class ArrayAccessTest extends TestCase
echo $a[new Foo];',
'error_message' => 'InvalidArrayOffset',
],
'possiblyUndefinedIntArrayOffet' => [
'possiblyUndefinedIntArrayOffset' => [
'code' => '<?php
/** @var array{0?:string} */
$entry = ["a"];
@ -1357,7 +1357,7 @@ class ArrayAccessTest extends TestCase
[$elt] = $entry;',
'error_message' => 'PossiblyUndefinedArrayOffset',
],
'possiblyUndefinedStringArrayOffet' => [
'possiblyUndefinedStringArrayOffset' => [
'code' => '<?php
/** @var array{a?:string} */
$entry = ["a"];
@ -1542,6 +1542,19 @@ class ArrayAccessTest extends TestCase
avg(["a" => 0.5, "b" => 1.5, "c" => new Exception()]);',
'error_message' => 'InvalidArgument',
],
'possiblyUndefinedArrayOffsetKeyedArray' => [
'code' => '<?php
$d = [];
if (!rand(0,1)) {
$d[0] = "a";
}
$x = $d[0];
// should not report TypeDoesNotContainNull
if ($x === null) {}',
'error_message' => 'PossiblyUndefinedArrayOffset',
],
];
}
}

View File

@ -1937,7 +1937,7 @@ class FunctionCallTest extends TestCase
'strposAllowDictionary' => [
'code' => '<?php
function sayHello(string $format): void {
if (strpos("abcdefghijklmno", $format)) {}
if (strpos("abcdefghijklmno", $format) !== false) {}
}',
],
'curlInitIsResourceAllowedIn7x' => [
@ -2138,7 +2138,7 @@ class FunctionCallTest extends TestCase
'strposFirstParamAllowClassString' => [
'code' => '<?php
function sayHello(string $needle): void {
if (strpos(DateTime::class, $needle)) {}
if (strpos(DateTime::class, $needle) !== false) {}
}',
],
'mb_strtolowerProducesStringWithSecondArgument' => [

View File

@ -300,7 +300,7 @@ class ImmutableAnnotationTest extends TestCase
$dto = new DTO("BOOM!");
if ($dto->getError()) {
if ($dto->getError() !== null) {
takesString($dto->getError());
}',
],

View File

@ -138,6 +138,7 @@ class JsonOutputTest extends TestCase
],
'singleIssueForTypeDifference' => [
'code' => '<?php
/** @psalm-suppress RiskyTruthyFalsyComparison */
function fooFoo(?string $a, ?string $b): void {
if ($a || $b) {
if ($a || $b) {}
@ -145,7 +146,7 @@ class JsonOutputTest extends TestCase
}',
'error_count' => 1,
'message' => 'Operand of type non-falsy-string is always truthy',
'line' => 4,
'line' => 5,
'error' => '$b',
],
];

View File

@ -245,7 +245,7 @@ class DoTest extends TestCase
$c = null;
do {
if (!$c) {
if ($c === null || $c === "" || $c === "0") {
foo($c);
} else {
bar($c);

View File

@ -155,7 +155,7 @@ class WhileTest extends TestCase
}
while ($a = foo()) {
if ($a->bar) {}
if ($a->bar !== null) {}
}',
],
'whileTrueWithBreak' => [
@ -271,7 +271,7 @@ class WhileTest extends TestCase
$c = null;
while (rand(0, 1)) {
if (!$c) {
if ($c === null || $c === "" || $c === "0") {
foo($c);
} else {
bar($c);

View File

@ -149,7 +149,7 @@ class MethodCallTest extends TestCase
$obj = new SomeClass();
if ($obj->getInt()) {
if ($obj->getInt() !== null) {
printInt($obj->getInt());
}',
);
@ -185,7 +185,7 @@ class MethodCallTest extends TestCase
$obj = new SomeClass();
if ($obj->getInt()) {
if ($obj->getInt() !== null) {
printInt($obj->getInt());
}',
);
@ -936,7 +936,7 @@ class MethodCallTest extends TestCase
$a = new A();
if ($a->getA()) {
if ($a->getA() !== null) {
echo strlen($a->getA());
}',
],
@ -1007,7 +1007,7 @@ class MethodCallTest extends TestCase
$obj = new SomeClass();
if ($obj->getInt()) {
if ($obj->getInt() !== null) {
printInt($obj->getInt());
}',
],
@ -1031,7 +1031,7 @@ class MethodCallTest extends TestCase
$obj = new SomeClass();
if ($obj->getInt()) {
if ($obj->getInt() !== null) {
printInt($obj->getInt());
}',
],
@ -1631,7 +1631,7 @@ class MethodCallTest extends TestCase
}
function foo(A $a) : void {
if ($a->getA()) {
if ($a->getA() !== null) {
echo strlen($a->getA());
}
}
@ -1697,7 +1697,7 @@ class MethodCallTest extends TestCase
$obj = new SomeClass();
if ($obj->getInt()) {
if ($obj->getInt() !== null) {
printInt($obj->getInt());
}',
'error_message' => 'PossiblyNullArgument',

View File

@ -311,7 +311,7 @@ class MethodSignatureTest extends TestCase
class B extends A {
public function foo(?string $s): string {
return $s ?: "hello";
return $s !== null ? $s : "hello";
}
}
@ -327,7 +327,7 @@ class MethodSignatureTest extends TestCase
class B extends A {
public function foo(string $s = null): string {
return $s ?: "hello";
return $s !== null ? $s : "hello";
}
}
@ -1044,7 +1044,7 @@ class MethodSignatureTest extends TestCase
'code' => '<?php
class A {
public function foo(?string $s): string {
return $s ?: "hello";
return $s !== null ? $s : "hello";
}
}

View File

@ -183,7 +183,7 @@ class PropertyTypeTest extends TestCase
}
function testX(X $x): void {
if ($x->getX()) {
if (is_int($x->getX())) {
XCollector::modify();
if ($x->getX() === null) {}
}
@ -221,7 +221,7 @@ class PropertyTypeTest extends TestCase
}
function testX(X $x): void {
if ($x->getX()) {
if ($x->getX() !== null) {
XCollector::modify();
if ($x->getX() === null) {}
}
@ -255,7 +255,7 @@ class PropertyTypeTest extends TestCase
}
function testX(X $x): void {
if ($x->x) {
if ($x->x !== null) {
XCollector::modify();
if ($x->x === null) {}
}
@ -686,6 +686,8 @@ class PropertyTypeTest extends TestCase
}
echo substr($a->aa, 1);',
'assertions' => [],
'ignored_issues' => ['RiskyTruthyFalsyComparison'],
],
'nullableStaticPropertyWithIfCheck' => [
'code' => '<?php

View File

@ -100,6 +100,8 @@ class ReturnTypeTest extends TestCase
return $str;
}
}',
'assertions' => [],
'ignored_issues' => ['RiskyTruthyFalsyComparison'],
],
'returnTypeNotEmptyCheckInElseIf' => [
'code' => '<?php
@ -118,6 +120,8 @@ class ReturnTypeTest extends TestCase
return $str;
}
}',
'assertions' => [],
'ignored_issues' => ['RiskyTruthyFalsyComparison'],
],
'returnTypeNotEmptyCheckInElse' => [
'code' => '<?php
@ -136,6 +140,8 @@ class ReturnTypeTest extends TestCase
return $str;
}
}',
'assertions' => [],
'ignored_issues' => ['RiskyTruthyFalsyComparison'],
],
'returnTypeAfterIf' => [
'code' => '<?php

View File

@ -547,7 +547,7 @@ class SwitchTypeTest extends TestCase
return "float";
}
if ($fq_const_name && isset($predefined_constants[$fq_const_name])) {
if ($fq_const_name !== null && isset($predefined_constants[$fq_const_name])) {
return "mixed";
}

View File

@ -2878,6 +2878,8 @@ class ClassTemplateTest extends TestCase
): void {
}
}',
'assertions' => [],
'ignored_issues' => ['RiskyTruthyFalsyComparison'],
],
'noCrashTemplateInsideGenerator' => [
'code' => '<?php

View File

@ -298,6 +298,8 @@ class AssignmentInConditionalTest extends TestCase
return $pos;
}',
'assertions' => [],
'ignored_issues' => ['RiskyTruthyFalsyComparison'],
],
'assignmentInIf' => [
'code' => '<?php
@ -416,7 +418,7 @@ class AssignmentInConditionalTest extends TestCase
}
if (rand(0, 10) > 5) {
} elseif (($a = rand(0, 1) ? new A : null) && $a->foo) {}',
} elseif (($a = rand(0, 1) ? new A : null) && is_string($a->foo)) {}',
],
'noParadoxAfterConditionalAssignment' => [
'code' => '<?php
@ -485,7 +487,7 @@ class AssignmentInConditionalTest extends TestCase
return "b";
}',
'error_message' => 'InvalidReturnStatement',
'ignored_issues' => [],
'ignored_issues' => ['RiskyTruthyFalsyComparison'],
'php_version' => '8.0',
],
'assignmentInBranchOfAndReferencedAfterIf' => [

View File

@ -594,6 +594,8 @@ class ConditionalTest extends TestCase
}
return false;
}',
'assertions' => [],
'ignored_issues' => ['RiskyTruthyFalsyComparison'],
],
'numericStringAssertion' => [
'code' => '<?php
@ -1943,6 +1945,8 @@ class ConditionalTest extends TestCase
if ($a && strlen($a) > 5) {}
}
}',
'assertions' => [],
'ignored_issues' => ['RiskyTruthyFalsyComparison'],
],
'arrayUnionTypeSwitching' => [
'code' => '<?php
@ -1958,6 +1962,8 @@ class ConditionalTest extends TestCase
}
}',
'assertions' => [],
'ignored_issues' => ['RiskyTruthyFalsyComparison'],
],
'propertySetOnElementInConditional' => [
'code' => '<?php
@ -2165,6 +2171,8 @@ class ConditionalTest extends TestCase
echo $valuePath;
}
}',
'assertions' => [],
'ignored_issues' => ['RiskyTruthyFalsyComparison'],
],
'issetAssertionOnStaticProperty' => [
'code' => '<?php
@ -2509,6 +2517,8 @@ class ConditionalTest extends TestCase
return [$type];
}',
'assertions' => [],
'ignored_issues' => ['RiskyTruthyFalsyComparison'],
],
'nonEmptyStringAfterLiteralCheck' => [
'code' => '<?php

View File

@ -114,6 +114,8 @@ class EmptyTest extends TestCase
return "an exception";
}',
'assertions' => [],
'ignored_issues' => ['RiskyTruthyFalsyComparison'],
],
'emptyExceptionReconciliationAfterIf' => [
'code' => '<?php
@ -175,6 +177,8 @@ class EmptyTest extends TestCase
foreach ($arr as $item) {
if (empty($item["hide"]) || $item["hide"] === 3) {}
}',
'assertions' => [],
'ignored_issues' => ['RiskyTruthyFalsyComparison'],
],
'alwaysBoolResult' => [
'code' => '<?php
@ -219,7 +223,7 @@ class EmptyTest extends TestCase
if (empty($scopes)){}
}',
'assertions' => [],
'ignored_issues' => ['MixedAssignment', 'MissingParamType', 'MixedArgument'],
'ignored_issues' => ['MixedAssignment', 'MissingParamType', 'MixedArgument', 'RiskyTruthyFalsyComparison'],
],
'multipleEmptiesInCondition' => [
'code' => '<?php
@ -389,6 +393,8 @@ class EmptyTest extends TestCase
echo $arr["a"];
}',
'assertions' => [],
'ignored_issues' => ['RiskyTruthyFalsyComparison'],
],
'reconcileEmptyTwiceWithoutReturn' => [
'code' => '<?php

View File

@ -409,7 +409,7 @@ class IssetTest extends TestCase
$a = isset($_GET["a"]) ? $_GET["a"] : "";
if ($a) {}',
'assertions' => [],
'ignored_issues' => ['MixedAssignment', 'MixedArrayAccess'],
'ignored_issues' => ['MixedAssignment', 'MixedArrayAccess', 'RiskyTruthyFalsyComparison'],
],
'mixedArrayIssetGetStringVar' => [
'code' => '<?php

View File

@ -66,6 +66,10 @@ class RedundantConditionTest extends TestCase
}
return $x;
}',
'assertions' => [],
'ignored_issues' => [
'RiskyTruthyFalsyComparison',
],
],
'noRedundantConditionAfterAssignment' => [
'code' => '<?php
@ -99,7 +103,7 @@ class RedundantConditionTest extends TestCase
switch (get_class($i)) {
case A::class:
if ($i->foo) {}
if ($i->foo !== null) {}
break;
default:
@ -180,7 +184,7 @@ class RedundantConditionTest extends TestCase
}
if ($a) {}',
'assertions' => [],
'ignored_issues' => ['MixedAssignment', 'MixedArrayAccess'],
'ignored_issues' => ['MixedAssignment', 'MixedArrayAccess', 'RiskyTruthyFalsyComparison'],
],
'noComplaintWithIsNumericThenIsEmpty' => [
'code' => '<?php
@ -377,7 +381,7 @@ class RedundantConditionTest extends TestCase
/** @psalm-suppress PossiblyUndefinedGlobalVariable */
$option = $options["option"] ?? false;
if ($option) {}',
if ($option !== false) {}',
'assertions' => [],
'ignored_issues' => ['MixedAssignment', 'MixedArrayAccess'],
],
@ -539,7 +543,7 @@ class RedundantConditionTest extends TestCase
exit;
}
if ($i) {}',
if ($i !== array() && $i !== "" && $i !== "0") {}',
],
'emptyWithoutKnowingArrayType' => [
'code' => '<?php
@ -825,7 +829,7 @@ class RedundantConditionTest extends TestCase
'code' => '<?php
function test(string|int|float|bool $value): bool {
if (is_numeric($value) || $value === true) {
if ($value) {
if ($value === true || (int) $value !== 0) {
return true;
}
}
@ -1068,6 +1072,7 @@ class RedundantConditionTest extends TestCase
return $a;
}',
'error_message' => 'RedundantCondition',
'ignored_issues' => ['RiskyTruthyFalsyComparison'],
],
'refineTypeInMethodCall' => [
'code' => '<?php

View File

@ -53,6 +53,8 @@ class TypeAlgebraTest extends TestCase
if (!$a) return $b;
return $a;
}',
'assertions' => [],
'ignored_issues' => ['RiskyTruthyFalsyComparison'],
],
'twoVarLogicNotNestedWithAllPathsReturning' => [
'code' => '<?php
@ -67,6 +69,8 @@ class TypeAlgebraTest extends TestCase
}
}
}',
'assertions' => [],
'ignored_issues' => ['RiskyTruthyFalsyComparison'],
],
'twoVarLogicNotNestedWithAssignmentBeforeReturn' => [
'code' => '<?php
@ -83,6 +87,8 @@ class TypeAlgebraTest extends TestCase
return $a;
}',
'assertions' => [],
'ignored_issues' => ['RiskyTruthyFalsyComparison'],
],
'invertedTwoVarLogicNotNested' => [
'code' => '<?php
@ -96,6 +102,8 @@ class TypeAlgebraTest extends TestCase
if (!$a) return $b;
return $a;
}',
'assertions' => [],
'ignored_issues' => ['RiskyTruthyFalsyComparison'],
],
'invertedTwoVarLogicNotNestedWithAssignmentBeforeReturn' => [
'code' => '<?php
@ -110,6 +118,8 @@ class TypeAlgebraTest extends TestCase
if (!$a) return $b;
return $a;
}',
'assertions' => [],
'ignored_issues' => ['RiskyTruthyFalsyComparison'],
],
'twoVarLogicNotNestedWithElseifAndNoNegations' => [
'code' => '<?php
@ -125,6 +135,8 @@ class TypeAlgebraTest extends TestCase
if (!$a) return $b;
return $a;
}',
'assertions' => [],
'ignored_issues' => ['RiskyTruthyFalsyComparison'],
],
'threeVarLogicNotNestedWithNoRedefinitionsWithClasses' => [
'code' => '<?php
@ -163,6 +175,8 @@ class TypeAlgebraTest extends TestCase
if (!$a) return $b;
return $a;
}',
'assertions' => [],
'ignored_issues' => ['RiskyTruthyFalsyComparison'],
],
'threeVarLogicNotNestedAndOrWithNoRedefinitions' => [
'code' => '<?php
@ -179,6 +193,8 @@ class TypeAlgebraTest extends TestCase
if (!$a) return $b;
return $a;
}',
'assertions' => [],
'ignored_issues' => ['RiskyTruthyFalsyComparison'],
],
'twoVarLogicNotNestedWithElseifCorrectlyNegatedInElseIf' => [
'code' => '<?php
@ -415,6 +431,8 @@ class TypeAlgebraTest extends TestCase
}
return $arr;
}',
'assertions' => [],
'ignored_issues' => ['RiskyTruthyFalsyComparison'],
],
'lotsaTruthyStatements' => [
'code' => '<?php
@ -998,6 +1016,8 @@ class TypeAlgebraTest extends TestCase
return $b;
}',
'assertions' => [],
'ignored_issues' => ['RiskyTruthyFalsyComparison'],
],
'cancelOutDifferentStatement' => [
'code' => '<?php
@ -1012,6 +1032,8 @@ class TypeAlgebraTest extends TestCase
return $b;
}',
'assertions' => [],
'ignored_issues' => ['RiskyTruthyFalsyComparison'],
],
'moreChecks' => [
'code' => '<?php
@ -1186,6 +1208,8 @@ class TypeAlgebraTest extends TestCase
if ($foo === null) {}
}',
'assertions' => [],
'ignored_issues' => ['RiskyTruthyFalsyComparison'],
],
'compareToIntInsideIfCNF' => [
'code' => '<?php
@ -1198,6 +1222,8 @@ class TypeAlgebraTest extends TestCase
if ($foo === null) {}
}',
'assertions' => [],
'ignored_issues' => ['RiskyTruthyFalsyComparison'],
],
'ternaryAssertionOnBool' => [
'code' => '<?php
@ -1304,6 +1330,7 @@ class TypeAlgebraTest extends TestCase
return $a;
}',
'error_message' => 'NullableReturnStatement',
'ignored_issues' => ['RiskyTruthyFalsyComparison'],
],
'invertedTwoVarLogicNotNestedWithElseif' => [
'code' => '<?php
@ -1320,6 +1347,7 @@ class TypeAlgebraTest extends TestCase
return $a;
}',
'error_message' => 'NullableReturnStatement',
'ignored_issues' => ['RiskyTruthyFalsyComparison'],
],
'threeVarLogicWithElseifAndAnd' => [
'code' => '<?php
@ -1337,6 +1365,7 @@ class TypeAlgebraTest extends TestCase
return $a;
}',
'error_message' => 'TypeDoesNotContainType',
'ignored_issues' => ['RiskyTruthyFalsyComparison'],
],
'twoVarLogicNotNestedWithElseifIncorrectlyReinforcedInIf' => [
'code' => '<?php
@ -1353,6 +1382,7 @@ class TypeAlgebraTest extends TestCase
return $a;
}',
'error_message' => 'RedundantCondition',
'ignored_issues' => ['RiskyTruthyFalsyComparison'],
],
'repeatedIfStatements' => [
'code' => '<?php
@ -1367,6 +1397,7 @@ class TypeAlgebraTest extends TestCase
}
}',
'error_message' => 'TypeDoesNotContainType',
'ignored_issues' => ['RiskyTruthyFalsyComparison'],
],
'repeatedConditionals' => [
'code' => '<?php
@ -1459,6 +1490,7 @@ class TypeAlgebraTest extends TestCase
}
}',
'error_message' => 'RedundantCondition',
'ignored_issues' => ['RiskyTruthyFalsyComparison'],
],
'dependentTypeInvalidated' => [
'code' => '<?php

View File

@ -102,7 +102,7 @@ class UnusedVariableTest extends TestCase
function foo(array $arr) : void {
$a = null;
foreach ($arr as $a) { }
if ($a) {}
if ($a !== null) {}
}',
],
'definedInSecondBranchOfCondition' => [
@ -128,10 +128,10 @@ class UnusedVariableTest extends TestCase
'dummyByRefVar' => [
'code' => '<?php
function foo(string &$a = null, string $b = null): void {
if ($a) {
if ($a !== null) {
echo $a;
}
if ($b) {
if ($b !== null) {
echo $b;
}
}
@ -327,7 +327,7 @@ class UnusedVariableTest extends TestCase
echo $e->getMessage();
}
if ($s) {}
if ($s !== null) {}
}',
],
'throwWithMessageCallAndAssignmentInCatchAndReference' => [
@ -940,7 +940,7 @@ class UnusedVariableTest extends TestCase
if ($foo) {}
} catch (Exception $e) {}
if ($foo) {}',
if ($foo !== false && $foo !== 0) {}',
],
'useTryAssignedVariableInsideFinally' => [
'code' => '<?php
@ -1953,7 +1953,7 @@ class UnusedVariableTest extends TestCase
$arr = str_getcsv($value);
foreach ($arr as &$element) {
$element = $element ?: "foo";
$element = $element !== null ?: "foo";
}
return $arr;
@ -2337,7 +2337,7 @@ class UnusedVariableTest extends TestCase
}
}',
'assertions' => [],
'ignored_issues' => [],
'ignored_issues' => ['RiskyTruthyFalsyComparison'],
'php_version' => '8.0',
],
'concatWithUnknownProperty' => [
@ -3165,7 +3165,7 @@ class UnusedVariableTest extends TestCase
$user = $user_id;
}
if ($user) {
if ($user !== null && $user !== 0) {
$a = 0;
for ($i = 1; $i <= 10; $i++) {
$a += $i;
@ -3185,7 +3185,7 @@ class UnusedVariableTest extends TestCase
$user = $user_id;
}
if ($user) {
if ($user !== null && $user !== 0) {
$a = 0;
foreach ([1, 2, 3] as $i) {
$a += $i;