1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Fix return type checks

This commit is contained in:
Matthew Brown 2020-02-22 11:28:24 -05:00
parent 202fb7026a
commit 952045e2ba
6 changed files with 41 additions and 21 deletions

View File

@ -2016,22 +2016,15 @@ class AssertionFinder
return;
}
$always_contains = TypeAnalyzer::isContainedBy(
$codebase,
$expected_type,
$first_var_type
);
$never_contains = !TypeAnalyzer::isContainedBy(
if (!TypeAnalyzer::isContainedBy(
$codebase,
$first_var_type,
$expected_type
);
)) {
return;
}
/** @psalm-suppress ParadoxicalCondition */
if (($always_contains && !$negate)
|| ($never_contains && $negate)
) {
if (!$negate) {
if ($first_var_type->from_docblock) {
if (IssueBuffer::accepts(
new RedundantConditionGivenDocblockType(
@ -2053,9 +2046,7 @@ class AssertionFinder
// fall through
}
}
} elseif (($always_contains && $negate)
|| ($never_contains && !$negate)
) {
} else {
if ($first_var_type->from_docblock) {
if (IssueBuffer::accepts(
new DocblockTypeContradiction(

View File

@ -1047,7 +1047,7 @@ class TypeAnalyzer
}
if ($container_type_part instanceof TNumeric &&
($input_type_part->isNumericType() || $input_type_part instanceof TString)
$input_type_part->isNumericType()
) {
return true;
}

View File

@ -188,7 +188,7 @@ class TypeCombinationTest extends TestCase
],
],
'arrayMixedOrStringKeys' => [
'array<int|mixed|string, string>',
'array<array-key, string>',
[
'array<int|string,string>',
'array<mixed,string>',

View File

@ -816,7 +816,7 @@ class TypeParseTest extends TestCase
*/
public function testVeryLargeType()
{
$very_large_type = 'array{a: Closure():(array<mixed, mixed>|null), b?: Closure():array<mixed, mixed>, c?: Closure():array<mixed, mixed>, d?: Closure():array<mixed, mixed>, e?: Closure():(array{f: null|string, g: null|string, h: null|string, i: string, j: mixed, k: mixed, l: mixed, m: mixed, n: bool, o?: array{0: string}}|null), p?: Closure():(array{f: null|string, g: null|string, h: null|string, i: string, j: mixed, k: mixed, l: mixed, m: mixed, n: bool, o?: array{0: string}}|null), q: string, r?: Closure():(array<mixed, mixed>|null), s: array<mixed, mixed>}|null';
$very_large_type = 'array{a: Closure():(array<array-key, mixed>|null), b?: Closure():array<array-key, mixed>, c?: Closure():array<array-key, mixed>, d?: Closure():array<array-key, mixed>, e?: Closure():(array{f: null|string, g: null|string, h: null|string, i: string, j: mixed, k: mixed, l: mixed, m: mixed, n: bool, o?: array{0: string}}|null), p?: Closure():(array{f: null|string, g: null|string, h: null|string, i: string, j: mixed, k: mixed, l: mixed, m: mixed, n: bool, o?: array{0: string}}|null), q: string, r?: Closure():(array<array-key, mixed>|null), s: array<array-key, mixed>}|null';
$this->assertSame(
$very_large_type,

View File

@ -59,9 +59,7 @@ class ReconcilerTest extends \Psalm\Tests\TestCase
$reconciled->getId()
);
if (is_array($reconciled->getAtomicTypes())) {
$this->assertContainsOnlyInstancesOf('Psalm\Type\Atomic', $reconciled->getAtomicTypes());
}
$this->assertContainsOnlyInstancesOf('Psalm\Type\Atomic', $reconciled->getAtomicTypes());
}
/**

View File

@ -773,6 +773,16 @@ class RedundantConditionTest extends \Psalm\Tests\TestCase
'$fp' => 'closed-resource',
]
],
'allowCheckOnReturnTypeUnion' => [
'<?php
/** @return int|string */
function returnsInt() {
return rand(0, 1) ? 1 : "hello";
}
if (is_int(returnsInt())) {}
if (!is_int(returnsInt())) {}',
],
];
}
@ -1260,6 +1270,27 @@ class RedundantConditionTest extends \Psalm\Tests\TestCase
if (is_int(returnsInt())) {}',
'error_message' => 'RedundantCondition',
],
'preventAlwaysReturningSpecificInt' => [
'<?php
/**
* @return 3|4
*/
function returnsInt(): int {
return rand(0, 1) ? 3 : 4;
}
if (is_int(returnsInt())) {}',
'error_message' => 'RedundantConditionGivenDocblockType',
],
'preventNotAlwaysReturningInt' => [
'<?php
function returnsInt(): int {
return 3;
}
if (!is_int(returnsInt())) {}',
'error_message' => 'TypeDoesNotContainType',
],
];
}
}