Skip to content

Commit

Permalink
Fix regression when identifying clauses in error
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Oct 18, 2019
1 parent 89752b8 commit 2be489c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
40 changes: 36 additions & 4 deletions src/Psalm/Internal/Analyzer/Statements/Block/IfAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
use const ARRAY_FILTER_USE_KEY;
use function array_values;
use function array_keys;
use function array_reduce;
use function array_combine;
use function preg_match;
use function preg_quote;
use function array_unique;
Expand Down Expand Up @@ -172,9 +174,24 @@ function ($clause) {
return !!$clause->possibilities;
}
)) {
$cond_referenced_var_ids = array_intersect_key(
$omit_keys = array_reduce(
$context->clauses,
/**
* @param array<string> $carry
* @return array<string>
*/
function ($carry, Clause $clause) {
return array_merge($carry, array_keys($clause->possibilities));
},
[]
);

$omit_keys = array_combine($omit_keys, $omit_keys);
$omit_keys = array_diff_key($omit_keys, Algebra::getTruthsFromFormula($context->clauses));

$cond_referenced_var_ids = array_diff_key(
$cond_referenced_var_ids,
Algebra::getTruthsFromFormula($context->clauses)
$omit_keys
);
}

Expand Down Expand Up @@ -1031,9 +1048,24 @@ function ($clause) {
return !!$clause->possibilities;
}
)) {
$cond_referenced_var_ids = array_intersect_key(
$omit_keys = array_reduce(
$entry_clauses,
/**
* @param array<string> $carry
* @return array<string>
*/
function ($carry, Clause $clause) {
return array_merge($carry, array_keys($clause->possibilities));
},
[]
);

$omit_keys = array_combine($omit_keys, $omit_keys);
$omit_keys = array_diff_key($omit_keys, Algebra::getTruthsFromFormula($entry_clauses));

$cond_referenced_var_ids = array_diff_key(
$cond_referenced_var_ids,
Algebra::getTruthsFromFormula($entry_clauses)
$omit_keys
);
}
$reconcilable_elseif_types = Algebra::getTruthsFromFormula($elseif_context->clauses);
Expand Down
13 changes: 13 additions & 0 deletions tests/RedundantConditionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,19 @@ function isOne($value): void {
}',
'error_message' => 'RedundantConditionGivenDocblockType',
],
'errorAfterStatementThatCannotBeConvertedToAssertion' => [
'<?php
function a(float $b) : void {
if ($b === 0.0) {
return;
}
$a = new stdClass();
if ($a) {}
}',
'error_message' => 'RedundantCondition',
],
];
}
}

0 comments on commit 2be489c

Please sign in to comment.