Skip to content

Commit

Permalink
Clone isset type before contradicting
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Jan 14, 2020
1 parent 3fd70a9 commit 0f6b61d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ function (\Psalm\Internal\Clause $c) use ($mixed_var_ids) {
if ($stmt_left_type = $statements_analyzer->node_data->getType($stmt->left)) {
$if_return_type_reconciled = AssertionReconciler::reconcile(
'!null',
$stmt_left_type,
clone $stmt_left_type,
'',
$statements_analyzer,
$context->inside_loop,
Expand All @@ -786,7 +786,7 @@ function (\Psalm\Internal\Clause $c) use ($mixed_var_ids) {
$statements_analyzer->getSuppressedIssues()
);

$lhs_type = $if_return_type_reconciled;
$lhs_type = clone $if_return_type_reconciled;
}

$stmt_right_type = null;
Expand Down
31 changes: 23 additions & 8 deletions src/Psalm/Internal/Type/NegatedAssertionReconciler.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Internal\Analyzer\TraitAnalyzer;
use Psalm\Internal\Analyzer\TypeAnalyzer;
use Psalm\Issue\DocblockTypeContradiction;
use Psalm\Issue\ParadoxicalCondition;
use Psalm\Issue\RedundantCondition;
use Psalm\Issue\TypeDoesNotContainType;
Expand Down Expand Up @@ -104,14 +105,28 @@ public static function reconcile(
$failed_reconciliation = 2;

if ($code_location) {
if (IssueBuffer::accepts(
new TypeDoesNotContainType(
'Cannot resolve types for ' . $key . ' and !isset assertion',
$code_location
),
$suppressed_issues
)) {
// fall through
if ($existing_var_type->from_docblock) {
if (IssueBuffer::accepts(
new DocblockTypeContradiction(
'Cannot resolve types for ' . $key . ' with docblock-defined type '
. $existing_var_type . ' and !isset assertion',
$code_location
),
$suppressed_issues
)) {
// fall through
}
} else {
if (IssueBuffer::accepts(
new TypeDoesNotContainType(
'Cannot resolve types for ' . $key . ' with type '
. $existing_var_type . ' and !isset assertion',
$code_location
),
$suppressed_issues
)) {

}
}
}

Expand Down
24 changes: 24 additions & 0 deletions tests/TypeReconciliation/IssetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,30 @@ function foo(?A $a) : string {
return "bar";
}',
],
'issetOnMethodCallInsideFunctionCall' => [
'<?php
class C {
public function foo() : ?string {
return null;
}
}
function foo(C $c) : void {
strlen($c->foo() ?? "");
}'
],
'issetOnMethodCallInsideMethodCall' => [
'<?php
class C {
public function foo() : ?string {
return null;
}
}
function foo(C $c) : void {
new DateTime($c->foo() ?? "");
}'
],
];
}

Expand Down

0 comments on commit 0f6b61d

Please sign in to comment.