1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-15 19:07:00 +01:00
psalm/src/Psalm/Internal/Analyzer/AlgebraAnalyzer.php

172 lines
6.3 KiB
PHP
Raw Normal View History

<?php
2018-11-06 03:57:36 +01:00
namespace Psalm\Internal\Analyzer;
use PhpParser;
2017-04-02 21:26:10 +02:00
use Psalm\CodeLocation;
2021-06-08 04:55:21 +02:00
use Psalm\Internal\Algebra;
use Psalm\Internal\Clause;
2017-04-02 21:26:10 +02:00
use Psalm\Issue\ParadoxicalCondition;
use Psalm\Issue\RedundantCondition;
use Psalm\IssueBuffer;
2021-06-08 04:55:21 +02:00
use function array_intersect_key;
use function array_unique;
2021-06-08 04:55:21 +02:00
use function count;
use function preg_match;
/**
* @internal
*/
2018-11-06 03:57:36 +01:00
class AlgebraAnalyzer
{
2017-04-02 21:26:10 +02:00
/**
* This looks to see if there are any clauses in one formula that contradict
* clauses in another formula, or clauses that duplicate previous clauses
2017-04-02 21:26:10 +02:00
*
* e.g.
* if ($a) { }
* elseif ($a) { }
*
2020-10-17 18:36:44 +02:00
* @param list<Clause> $formula_1
* @param list<Clause> $formula_2
2020-11-03 22:44:24 +01:00
* @param array<string, int> $new_assigned_var_ids
2017-04-02 21:26:10 +02:00
*/
public static function checkForParadox(
2020-10-07 18:24:36 +02:00
array $formula_1,
array $formula_2,
2018-11-11 18:01:14 +01:00
StatementsAnalyzer $statements_analyzer,
PhpParser\Node $stmt,
array $new_assigned_var_ids
): void {
2019-01-08 15:57:14 +01:00
try {
2020-10-07 18:24:36 +02:00
$negated_formula2 = Algebra::negateFormula($formula_2);
2019-01-08 15:57:14 +01:00
} catch (\Psalm\Exception\ComplicatedExpressionException $e) {
return;
}
2020-10-07 18:24:36 +02:00
$formula_1_hashes = [];
2018-05-07 20:52:45 +02:00
2020-10-07 18:24:36 +02:00
foreach ($formula_1 as $formula_1_clause) {
$formula_1_hashes[$formula_1_clause->hash] = true;
2018-05-07 20:52:45 +02:00
}
2020-10-07 18:24:36 +02:00
$formula_2_hashes = [];
2018-05-07 20:52:45 +02:00
2020-10-07 18:24:36 +02:00
foreach ($formula_2 as $formula_2_clause) {
$hash = $formula_2_clause->hash;
2018-05-07 20:52:45 +02:00
2020-10-07 18:24:36 +02:00
if (!$formula_2_clause->generated
&& !$formula_2_clause->wedge
&& $formula_2_clause->reconcilable
&& (isset($formula_1_hashes[$hash]) || isset($formula_2_hashes[$hash]))
&& !array_intersect_key($new_assigned_var_ids, $formula_2_clause->possibilities)
2018-05-07 20:52:45 +02:00
) {
IssueBuffer::maybeAdd(
2018-05-07 20:52:45 +02:00
new RedundantCondition(
2020-10-07 18:24:36 +02:00
$formula_2_clause . ' has already been asserted',
2020-09-11 04:44:35 +02:00
new CodeLocation($statements_analyzer, $stmt),
null
2018-05-07 20:52:45 +02:00
),
2018-11-11 18:01:14 +01:00
$statements_analyzer->getSuppressedIssues()
);
2018-05-07 20:52:45 +02:00
}
2020-10-07 18:24:36 +02:00
foreach ($formula_2_clause->possibilities as $key => $values) {
if (!$formula_2_clause->generated
2018-05-07 20:52:45 +02:00
&& count($values) > 1
&& !isset($new_assigned_var_ids[$key])
&& count(array_unique($values)) < count($values)
) {
IssueBuffer::maybeAdd(
2018-05-07 20:52:45 +02:00
new ParadoxicalCondition(
2020-10-07 18:24:36 +02:00
'Found a redundant condition when evaluating assertion (' . $formula_2_clause . ')',
2018-11-11 18:01:14 +01:00
new CodeLocation($statements_analyzer, $stmt)
2018-05-07 20:52:45 +02:00
),
2018-11-11 18:01:14 +01:00
$statements_analyzer->getSuppressedIssues()
);
2018-05-07 20:52:45 +02:00
}
}
2020-10-07 18:24:36 +02:00
$formula_2_hashes[$hash] = true;
2018-05-07 20:52:45 +02:00
}
2017-04-02 21:26:10 +02:00
// remove impossible types
2020-10-07 18:34:28 +02:00
foreach ($negated_formula2 as $negated_clause_2) {
if (count($negated_formula2) === 1) {
2020-10-07 18:34:28 +02:00
foreach ($negated_clause_2->possibilities as $key => $values) {
if (count($values) > 1
&& !isset($new_assigned_var_ids[$key])
2018-05-07 20:52:45 +02:00
&& count(array_unique($values)) < count($values)
) {
IssueBuffer::maybeAdd(
new RedundantCondition(
'Found a redundant condition when evaluating ' . $key,
2020-09-11 04:44:35 +02:00
new CodeLocation($statements_analyzer, $stmt),
null
),
2018-11-11 18:01:14 +01:00
$statements_analyzer->getSuppressedIssues()
);
}
}
}
2020-10-07 18:34:28 +02:00
if (!$negated_clause_2->reconcilable || $negated_clause_2->wedge) {
2017-04-02 21:26:10 +02:00
continue;
}
2020-10-07 18:24:36 +02:00
foreach ($formula_1 as $clause_1) {
2020-10-07 18:34:28 +02:00
if ($negated_clause_2 === $clause_1 || !$clause_1->reconcilable || $clause_1->wedge) {
2017-04-02 21:26:10 +02:00
continue;
}
2020-10-07 18:34:28 +02:00
$negated_clause_2_contains_1_possibilities = true;
2020-10-07 18:24:36 +02:00
foreach ($clause_1->possibilities as $key => $keyed_possibilities) {
2020-10-07 18:34:28 +02:00
if (!isset($negated_clause_2->possibilities[$key])) {
$negated_clause_2_contains_1_possibilities = false;
break;
}
2020-10-07 18:34:28 +02:00
if ($negated_clause_2->possibilities[$key] != $keyed_possibilities) {
$negated_clause_2_contains_1_possibilities = false;
break;
}
foreach ($keyed_possibilities as $possibility) {
if (preg_match('@^!?in-array-@', $possibility)) {
$negated_clause_2_contains_1_possibilities = false;
break;
}
}
}
2020-10-07 18:34:28 +02:00
if ($negated_clause_2_contains_1_possibilities) {
$mini_formula_2 = Algebra::negateFormula([$negated_clause_2]);
2020-10-07 18:00:53 +02:00
2020-10-07 18:41:36 +02:00
if (!$mini_formula_2[0]->wedge) {
if (count($mini_formula_2) > 1) {
2020-10-07 21:36:31 +02:00
$paradox_message = 'Condition ((' . \implode(') && (', $mini_formula_2) . '))'
2020-10-07 18:41:36 +02:00
. ' contradicts a previously-established condition (' . $clause_1 . ')';
} else {
$paradox_message = 'Condition (' . $mini_formula_2[0] . ')'
. ' contradicts a previously-established condition (' . $clause_1 . ')';
}
2020-10-07 18:00:53 +02:00
} else {
2020-10-07 18:34:28 +02:00
$paradox_message = 'Condition not(' . $negated_clause_2 . ')'
. ' contradicts a previously-established condition (' . $clause_1 . ')';
2020-10-07 18:00:53 +02:00
}
IssueBuffer::maybeAdd(
2017-04-02 21:26:10 +02:00
new ParadoxicalCondition(
2020-10-07 18:00:53 +02:00
$paradox_message,
2018-11-11 18:01:14 +01:00
new CodeLocation($statements_analyzer, $stmt)
2017-04-02 21:26:10 +02:00
),
2018-11-11 18:01:14 +01:00
$statements_analyzer->getSuppressedIssues()
);
2017-04-02 21:26:10 +02:00
return;
}
}
}
}
}