Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Nov 28, 2023
1 parent 5e39860 commit f9f4c48
Show file tree
Hide file tree
Showing 24 changed files with 62 additions and 82 deletions.
3 changes: 0 additions & 3 deletions src/Psalm/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -543,9 +543,7 @@ public function decrementReferenceCount(string $ref_id): void
/**
* @param Clause[] $clauses
* @param array<string, bool> $changed_var_ids
*
* @return array{list<Clause>, list<Clause>}
*
* @psalm-pure
*/
public static function removeReconciledClauses(array $clauses, array $changed_var_ids): array
Expand Down Expand Up @@ -574,7 +572,6 @@ public static function removeReconciledClauses(array $clauses, array $changed_va

/**
* @param Clause[] $clauses
*
* @return list<Clause>
*/
public static function filterClauses(
Expand Down
24 changes: 8 additions & 16 deletions src/Psalm/Internal/Algebra.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ public static function negateTypes(array $all_types): array
* (!$a) && (!$b) && ($a || $b || $c) => $c
*
* @param list<Clause> $clauses
*
* @return list<Clause>
*
* @psalm-pure
*/
public static function simplifyCNF(array $clauses): array
Expand Down Expand Up @@ -209,7 +207,7 @@ public static function simplifyCNF(array $clauses): array
} else {
$updated_clause = $clause_b->addPossibilities(
$clause_var,
$clause_var_possibilities
$clause_var_possibilities,
);

$cloned_clauses[$updated_clause->hash] = $updated_clause;
Expand Down Expand Up @@ -257,15 +255,15 @@ public static function simplifyCNF(array $clauses): array
for ($k = $i + 1; $k < $clause_count; $k++) {
$clause_b = $clauses[$k];
$common_keys = array_keys(
array_intersect_key($clause_a->possibilities, $clause_b->possibilities)
array_intersect_key($clause_a->possibilities, $clause_b->possibilities),
);
if ($common_keys) {
$common_negated_keys = [];
foreach ($common_keys as $common_key) {
if (count($clause_a->possibilities[$common_key]) === 1
&& count($clause_b->possibilities[$common_key]) === 1
&& reset($clause_a->possibilities[$common_key])->isNegationOf(
reset($clause_b->possibilities[$common_key])
reset($clause_b->possibilities[$common_key]),
)
) {
$common_negated_keys[] = $common_key;
Expand Down Expand Up @@ -313,7 +311,7 @@ public static function simplifyCNF(array $clauses): array
false,
true,
true,
[]
[],
));

unset($simplified_clauses[$conflict_clause->hash]);
Expand All @@ -335,7 +333,6 @@ public static function simplifyCNF(array $clauses): array
* @param list<Clause> $clauses
* @param array<string, bool> $cond_referenced_var_ids
* @param array<string, array<int, array<int, Assertion>>> $active_truths
*
* @return array<string, list<list<Assertion>>>
*/
public static function getTruthsFromFormula(
Expand Down Expand Up @@ -472,9 +469,7 @@ public static function getTruthsFromFormula(

/**
* @param non-empty-list<Clause> $clauses
*
* @return list<Clause>
*
* @psalm-pure
*/
public static function groupImpossibilities(array $clauses): array
Expand All @@ -495,7 +490,7 @@ public static function groupImpossibilities(array $clauses): array
$seed_clause = new Clause(
[$var => [(string)$impossible_type => $impossible_type]],
$clause->creating_conditional_id,
$clause->creating_object_id
$clause->creating_object_id,
);

$seed_clauses[] = $seed_clause;
Expand Down Expand Up @@ -568,7 +563,7 @@ public static function groupImpossibilities(array $clauses): array
false,
true,
true,
[]
[],
);

$new_clauses[] = $new_clause;
Expand All @@ -591,9 +586,7 @@ public static function groupImpossibilities(array $clauses): array
/**
* @param list<Clause> $left_clauses
* @param list<Clause> $right_clauses
*
* @return list<Clause>
*
* @psalm-pure
*/
public static function combineOredClauses(
Expand Down Expand Up @@ -686,7 +679,7 @@ public static function combineOredClauses(
|| $left_clause->generated
|| count($left_clauses) > 1
|| count($right_clauses) > 1,
[]
[],
);
}
}
Expand Down Expand Up @@ -714,14 +707,13 @@ public static function combineOredClauses(
* (!$a || !$c || !$f)
*
* @param list<Clause> $clauses
*
* @return non-empty-list<Clause>
*/
public static function negateFormula(array $clauses): array
{
$clauses = array_filter(
$clauses,
static fn(Clause $clause): bool => $clause->reconcilable
static fn(Clause $clause): bool => $clause->reconcilable,
);

if (!$clauses) {
Expand Down
24 changes: 12 additions & 12 deletions src/Psalm/Internal/Algebra/FormulaGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,8 @@ public static function getFormula(
$this_class_name,
$source,
$codebase,
!$inside_negation
)
!$inside_negation,
),
);
}

Expand All @@ -235,8 +235,8 @@ public static function getFormula(
$source,
$codebase,
!$inside_negation,
$cache
)
$cache,
),
);
}

Expand All @@ -255,8 +255,8 @@ public static function getFormula(
$source,
$codebase,
!$inside_negation,
$cache
)
$cache,
),
);
}

Expand Down Expand Up @@ -318,8 +318,8 @@ public static function getFormula(
$source,
$codebase,
!$inside_negation,
$cache
)
$cache,
),
);
}

Expand All @@ -338,8 +338,8 @@ public static function getFormula(
$source,
$codebase,
!$inside_negation,
$cache
)
$cache,
),
);
}

Expand Down Expand Up @@ -455,8 +455,8 @@ public static function getFormula(
new Clause(
[$conditional_ref => ['truthy' => new Truthy()]],
$conditional_object_id,
$creating_object_id
)
$creating_object_id,
),
];
}
}
2 changes: 1 addition & 1 deletion src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public function analyze(
NodeDataProvider $type_provider,
?Context $global_context = null,
bool $add_mutations = false,
array &$byref_vars = []
array &$byref_vars = [],
): ?bool {
$storage = $this->storage;

Expand Down
8 changes: 4 additions & 4 deletions src/Psalm/Internal/Analyzer/Statements/Block/DoAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ static function (Clause $c) use ($mixed_var_ids): bool {
}

return true;
}
)
},
),
);

if (!$while_clauses) {
Expand Down Expand Up @@ -117,8 +117,8 @@ static function (Clause $c) use ($mixed_var_ids): bool {

$negated_while_types = Algebra::getTruthsFromFormula(
Algebra::simplifyCNF(
[...$context->clauses, ...$negated_while_clauses]
)
[...$context->clauses, ...$negated_while_clauses],
),
);

if ($negated_while_types) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static function analyze(
static fn(Clause $c): bool => count($c->possibilities) > 1
|| $c->wedge
|| !isset($changed_var_ids[array_keys($c->possibilities)[0]])
)
),
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static function analyze(
}

$else_context->clauses = Algebra::simplifyCNF(
[...$else_context->clauses, ...$if_scope->negated_clauses]
[...$else_context->clauses, ...$if_scope->negated_clauses],
);

$else_types = Algebra::getTruthsFromFormula($else_context->clauses);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public static function analyze(
array_filter(
$elseif_context_clauses,
static fn(Clause $c): bool => !in_array($c->hash, $reconciled_expression_clauses, true)
)
),
);
}

Expand All @@ -158,7 +158,7 @@ public static function analyze(
try {
if (array_filter(
$entry_clauses,
static fn(Clause $clause): bool => (bool) $clause->possibilities
static fn(Clause $clause): bool => (bool) $clause->possibilities,
)) {
$omit_keys = array_reduce(
$entry_clauses,
Expand Down Expand Up @@ -186,7 +186,7 @@ public static function analyze(
$active_elseif_types,
);
$negated_elseif_types = Algebra::getTruthsFromFormula(
Algebra::negateFormula($elseif_clauses)
Algebra::negateFormula($elseif_clauses),
);
} catch (ComplicatedExpressionException) {
$reconcilable_elseif_types = [];
Expand Down Expand Up @@ -405,7 +405,7 @@ public static function analyze(

try {
$if_scope->negated_clauses = Algebra::simplifyCNF(
[...$if_scope->negated_clauses, ...Algebra::negateFormula($elseif_clauses)]
[...$if_scope->negated_clauses, ...Algebra::negateFormula($elseif_clauses)],
);
} catch (ComplicatedExpressionException) {
$if_scope->negated_clauses = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public static function analyze(

if (array_filter(
$outer_context->clauses,
static fn(Clause $clause): bool => (bool) $clause->possibilities
static fn(Clause $clause): bool => (bool) $clause->possibilities,
)) {
$omit_keys = array_reduce(
$outer_context->clauses,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,6 @@ private static function updateLoopScopeContexts(

/**
* @param list<Clause> $pre_condition_clauses
*
* @return list<string>
*/
private static function applyPreConditionToLoopContext(
Expand Down Expand Up @@ -596,7 +595,7 @@ private static function applyPreConditionToLoopContext(
$always_assigned_before_loop_body_vars = Context::getNewOrUpdatedVarIds($outer_context, $loop_context);

$loop_context->clauses = Algebra::simplifyCNF(
[...$outer_context->clauses, ...$pre_condition_clauses]
[...$outer_context->clauses, ...$pre_condition_clauses],
);

$active_while_types = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public static function analyze(

if (!$has_default && $switch_scope->negated_clauses && $switch_var_id) {
$entry_clauses = Algebra::simplifyCNF(
[...$original_context->clauses, ...$switch_scope->negated_clauses]
[...$original_context->clauses, ...$switch_scope->negated_clauses],
);

$reconcilable_if_types = Algebra::getTruthsFromFormula($entry_clauses);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ public static function analyze(

if ($switch_scope->negated_clauses && count($switch_scope->negated_clauses) < 50) {
$entry_clauses = Algebra::simplifyCNF(
[...$original_context->clauses, ...$switch_scope->negated_clauses]
[...$original_context->clauses, ...$switch_scope->negated_clauses],
);
} else {
$entry_clauses = $original_context->clauses;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public static function analyze(
array_filter(
$negated_left_clauses,
static fn(Clause $c): bool => !in_array($c->hash, $reconciled_expression_clauses)
)
),
);

if (count($negated_left_clauses) === 1
Expand All @@ -194,7 +194,7 @@ public static function analyze(
}

$clauses_for_right_analysis = Algebra::simplifyCNF(
[...$context->clauses, ...$negated_left_clauses]
[...$context->clauses, ...$negated_left_clauses],
);

$active_negated_type_assertions = [];
Expand Down Expand Up @@ -297,7 +297,7 @@ public static function analyze(
)[0];

$combined_right_clauses = Algebra::simplifyCNF(
[...$clauses_for_right_analysis, ...$right_clauses]
[...$clauses_for_right_analysis, ...$right_clauses],
);

$active_right_type_assertions = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -848,8 +848,8 @@ public static function applyAssertionsToContext(
$arg_value,
$context->self,
$statements_analyzer,
$codebase
)
$codebase,
),
);
} elseif ($single_rule instanceof IsType
&& $single_rule->type instanceof TTrue
Expand All @@ -870,11 +870,11 @@ public static function applyAssertionsToContext(
}

$simplified_clauses = Algebra::simplifyCNF(
[...$context->clauses, ...$assert_clauses]
[...$context->clauses, ...$assert_clauses],
);

$assert_type_assertions = Algebra::getTruthsFromFormula(
$simplified_clauses
$simplified_clauses,
);

$type_assertions = [...$type_assertions, ...$assert_type_assertions];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1747,15 +1747,11 @@ private static function handleArrayAccessOnKeyedArray(
}
}

/**
* @param Type\Union|Type\MutableUnion $array_type
* @param Type\Union|Type\MutableUnion $offset_type
*/
public static function validateArrayOffset(
StatementsAnalyzer $statements_analyzer,
Expr $stmt,
$array_type,
$offset_type
Type\Union|Type\MutableUnion $array_type,
Type\Union|Type\MutableUnion $offset_type,
): void {
$all_arrays_shaped = true;
$exact_keys = null;
Expand Down
Loading

0 comments on commit f9f4c48

Please sign in to comment.