Skip to content

Commit

Permalink
Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Feb 3, 2023
1 parent fdbff4d commit 292c986
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,9 @@ public static function analyze(
// this has to go on a separate line because the phar compactor messes with precedence
$scope_to_clone = $if_scope->post_leaving_if_context ?? $post_if_context;
$else_context = clone $scope_to_clone;
$else_context->clauses = Algebra::simplifyCNF(
[...$else_context->clauses, ...$if_scope->negated_clauses],
);

// check the elseifs
foreach ($stmt->elseifs as $elseif) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,10 @@ public static function analyze(
$right_context = clone $left_context;
}

$partitioned_clauses = Context::removeReconciledClauses($left_clauses, $changed_var_ids);
$partitioned_clauses = Context::removeReconciledClauses(
[...$left_context->clauses, ...$left_clauses],
$changed_var_ids
);

$right_context->clauses = $partitioned_clauses[0];

Expand Down
19 changes: 8 additions & 11 deletions src/Psalm/Internal/Type/SimpleAssertionReconciler.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@
use Psalm\Type\Reconciler;
use Psalm\Type\Union;

use function array_key_exists;
use function array_map;
use function array_merge;
use function assert;
Expand Down Expand Up @@ -2780,16 +2779,14 @@ private static function reconcileTruthyOrNonEmpty(
) {
unset($types['array']);
$types [] = new TNonEmptyArray($array_atomic_type->type_params);
} elseif ($array_atomic_type instanceof TKeyedArray) {
if ($array_atomic_type->is_list
&& array_key_exists(0, $array_atomic_type->properties)
&& $array_atomic_type->properties[0]->possibly_undefined
) {
unset($types['array']);
$properties = $array_atomic_type->properties;
$properties[0] = $properties[0]->setPossiblyUndefined(false);
$types [] = $array_atomic_type->setProperties($properties);
}
} elseif ($array_atomic_type instanceof TKeyedArray
&& $array_atomic_type->is_list
&& $array_atomic_type->properties[0]->possibly_undefined
) {
unset($types['array']);
$properties = $array_atomic_type->properties;
$properties[0] = $properties[0]->setPossiblyUndefined(false);
$types [] = $array_atomic_type->setProperties($properties);
}
}

Expand Down
22 changes: 20 additions & 2 deletions tests/TypeReconciliation/TypeAlgebraTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1228,6 +1228,23 @@ function foo(Base $base): void {
}
}'
],
'subclassAfterElseifNegation' => [
'code' => '<?php
abstract class Base {}
class A extends Base {}
class AChild extends A {}
class B extends Base {
public string $s = "";
}
function foo(Base $base): void {
if ($base instanceof A && !($base instanceof AChild)) {
// do nothing
} elseif ($base instanceof B && rand(0, 1)) {
echo $base->s;
}
}'
],
];
}

Expand Down Expand Up @@ -1364,14 +1381,15 @@ function foo(?object $a): void {
],
'repeatedAndConditional' => [
'code' => '<?php
function foo(string $a, string $b): void {
class C {}
function foo(?C $a, ?C $b): void {
if ($a && $b) {
echo "a";
} elseif ($a && $b) {
echo "b";
}
}',
'error_message' => 'ParadoxicalCondition',
'error_message' => 'TypeDoesNotContainType',
],
'andConditionalAfterOrConditional' => [
'code' => '<?php
Expand Down

0 comments on commit 292c986

Please sign in to comment.