1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-29 20:28:59 +01:00

Check from_docblock property to emit the right issue (#4736)

This commit is contained in:
orklah 2020-11-30 03:40:13 +01:00 committed by Daniil Gentili
parent 9789b53617
commit 4ded1080e3
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
3 changed files with 57 additions and 35 deletions

View File

@ -2,6 +2,7 @@
namespace Psalm\Internal\Type;
use Psalm\Issue\RedundantConditionGivenDocblockType;
use function get_class;
use Psalm\CodeLocation;
use Psalm\Issue\ParadoxicalCondition;
@ -637,20 +638,27 @@ class SimpleNegatedAssertionReconciler extends Reconciler
$existing_var_type->addType(new Type\Atomic\TNonEmptyMixed);
}
} elseif ($existing_var_type->isMixed() && !$is_equality) {
if ($code_location
&& $key
&& IssueBuffer::accepts(
new RedundantCondition(
if ($code_location && $key) {
if ($existing_var_type->from_docblock) {
$issue = new RedundantConditionGivenDocblockType(
'Found a redundant condition when evaluating ' . $key
. ' of type ' . $existing_var_type->getId()
. ' and trying to reconcile it with a non-' . $assertion . ' assertion',
. ' of type ' . $existing_var_type->getId()
. ' and trying to reconcile it with a non-' . $assertion . ' assertion',
$code_location,
$existing_var_type->getId() . ' ' . $assertion
),
$suppressed_issues
)
) {
// fall through
);
} else {
$issue = new RedundantCondition(
'Found a redundant condition when evaluating ' . $key
. ' of type ' . $existing_var_type->getId()
. ' and trying to reconcile it with a non-' . $assertion . ' assertion',
$code_location,
$existing_var_type->getId() . ' ' . $assertion
);
}
if (IssueBuffer::accepts($issue, $suppressed_issues)) {
// fall through
}
}
}
@ -668,20 +676,27 @@ class SimpleNegatedAssertionReconciler extends Reconciler
$existing_var_type->addType(new Type\Atomic\TNonEmptyScalar);
}
} elseif ($existing_var_type->isSingle() && !$is_equality) {
if ($code_location
&& $key
&& IssueBuffer::accepts(
new RedundantCondition(
if ($code_location && $key) {
if ($existing_var_type->from_docblock) {
$issue = new RedundantConditionGivenDocblockType(
'Found a redundant condition when evaluating ' . $key
. ' of type ' . $existing_var_type->getId()
. ' and trying to reconcile it with a non-' . $assertion . ' assertion',
. ' of type ' . $existing_var_type->getId()
. ' and trying to reconcile it with a non-' . $assertion . ' assertion',
$code_location,
$existing_var_type->getId() . ' ' . $assertion
),
$suppressed_issues
)
) {
// fall through
);
} else {
$issue = new RedundantCondition(
'Found a redundant condition when evaluating ' . $key
. ' of type ' . $existing_var_type->getId()
. ' and trying to reconcile it with a non-' . $assertion . ' assertion',
$code_location,
$existing_var_type->getId() . ' ' . $assertion
);
}
if (IssueBuffer::accepts($issue, $suppressed_issues)) {
// fall through
}
}
}
@ -705,20 +720,27 @@ class SimpleNegatedAssertionReconciler extends Reconciler
$existing_var_type->addType(new Type\Atomic\TNonEmptyString);
}
} elseif ($existing_var_type->isSingle() && !$is_equality) {
if ($code_location
&& $key
&& IssueBuffer::accepts(
new RedundantCondition(
if ($code_location && $key) {
if ($existing_var_type->from_docblock) {
$issue = new RedundantConditionGivenDocblockType(
'Found a redundant condition when evaluating ' . $key
. ' of type ' . $existing_var_type->getId()
. ' and trying to reconcile it with a non-' . $assertion . ' assertion',
. ' of type ' . $existing_var_type->getId()
. ' and trying to reconcile it with a non-' . $assertion . ' assertion',
$code_location,
$existing_var_type->getId() . ' ' . $assertion
),
$suppressed_issues
)
) {
// fall through
);
} else {
$issue = new RedundantCondition(
'Found a redundant condition when evaluating ' . $key
. ' of type ' . $existing_var_type->getId()
. ' and trying to reconcile it with a non-' . $assertion . ' assertion',
$code_location,
$existing_var_type->getId() . ' ' . $assertion
);
}
if (IssueBuffer::accepts($issue, $suppressed_issues)) {
// fall through
}
}
}

View File

@ -1480,7 +1480,7 @@ class AssertAnnotationTest extends TestCase
if ($bar) {}
}',
'error_message' => 'RedundantCondition - src' . DIRECTORY_SEPARATOR . 'somefile.php:19:29',
'error_message' => 'RedundantConditionGivenDocblockType - src' . DIRECTORY_SEPARATOR . 'somefile.php:19:29',
],
'assertOneOfStrings' => [
'<?php

View File

@ -1882,7 +1882,7 @@ class FunctionCallTest extends TestCase
if ($s) {}
}',
'error_message' => 'RedundantCondition',
'error_message' => 'RedundantConditionGivenDocblockType',
],
'strposNoSetFirstParam' => [
'<?php