2017-03-18 18:37:00 +01:00
|
|
|
<?php
|
2018-11-06 03:57:36 +01:00
|
|
|
namespace Psalm\Internal\Analyzer;
|
2017-03-18 18:37:00 +01:00
|
|
|
|
|
|
|
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;
|
2017-11-28 06:46:41 +01:00
|
|
|
use Psalm\Issue\RedundantCondition;
|
2017-05-19 06:48:26 +02:00
|
|
|
use Psalm\IssueBuffer;
|
2021-06-08 04:55:21 +02:00
|
|
|
|
2019-06-26 22:52:29 +02:00
|
|
|
use function array_intersect_key;
|
|
|
|
use function array_unique;
|
2021-06-08 04:55:21 +02:00
|
|
|
use function count;
|
2021-09-06 22:05:56 +02:00
|
|
|
use function preg_match;
|
2017-03-18 18:37:00 +01:00
|
|
|
|
2018-12-02 00:37:49 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
class AlgebraAnalyzer
|
2017-03-18 18:37:00 +01:00
|
|
|
{
|
2017-04-02 21:26:10 +02:00
|
|
|
/**
|
|
|
|
* This looks to see if there are any clauses in one formula that contradict
|
2017-11-28 06:46:41 +01:00
|
|
|
* 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,
|
2017-11-29 04:17:03 +01:00
|
|
|
PhpParser\Node $stmt,
|
|
|
|
array $new_assigned_var_ids
|
2020-10-12 21:02:52 +02:00
|
|
|
): 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;
|
|
|
|
}
|
2017-08-08 00:38:38 +02:00
|
|
|
|
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
|
|
|
) {
|
2021-11-29 20:54:17 +01: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()
|
2021-11-29 20:54:17 +01:00
|
|
|
);
|
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)
|
|
|
|
) {
|
2021-11-29 20:54:17 +01:00
|
|
|
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()
|
2021-11-29 20:54:17 +01:00
|
|
|
);
|
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) {
|
2017-12-18 06:47:36 +01:00
|
|
|
if (count($negated_formula2) === 1) {
|
2020-10-07 18:34:28 +02:00
|
|
|
foreach ($negated_clause_2->possibilities as $key => $values) {
|
2017-12-18 06:39:00 +01:00
|
|
|
if (count($values) > 1
|
|
|
|
&& !isset($new_assigned_var_ids[$key])
|
2018-05-07 20:52:45 +02:00
|
|
|
&& count(array_unique($values)) < count($values)
|
2017-12-18 06:39:00 +01:00
|
|
|
) {
|
2021-11-29 20:54:17 +01:00
|
|
|
IssueBuffer::maybeAdd(
|
2017-12-18 06:39:00 +01:00
|
|
|
new RedundantCondition(
|
|
|
|
'Found a redundant condition when evaluating ' . $key,
|
2020-09-11 04:44:35 +02:00
|
|
|
new CodeLocation($statements_analyzer, $stmt),
|
|
|
|
null
|
2017-12-18 06:39:00 +01:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2021-11-29 20:54:17 +01:00
|
|
|
);
|
2017-11-28 06:46:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
2017-08-08 00:38:38 +02:00
|
|
|
|
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;
|
2017-08-08 00:38:38 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-10-07 18:34:28 +02:00
|
|
|
if ($negated_clause_2->possibilities[$key] != $keyed_possibilities) {
|
|
|
|
$negated_clause_2_contains_1_possibilities = false;
|
2017-08-08 00:38:38 +02:00
|
|
|
break;
|
|
|
|
}
|
2021-09-06 22:05:56 +02:00
|
|
|
foreach ($keyed_possibilities as $possibility) {
|
|
|
|
if (preg_match('@^!?in-array-@', $possibility)) {
|
|
|
|
$negated_clause_2_contains_1_possibilities = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-08-08 00:38:38 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-11-29 20:54:17 +01: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()
|
2021-11-29 20:54:17 +01:00
|
|
|
);
|
2017-04-02 21:26:10 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-18 18:37:00 +01:00
|
|
|
}
|
|
|
|
}
|