Skip to content
Permalink
Browse files

Fix #2614 - prevent isset($string)

  • Loading branch information
muglug committed Jan 14, 2020
1 parent ef3c2d3 commit b0073d4558d17aacdfb132a9292a3a7646cf574d
@@ -2032,14 +2032,11 @@ protected static function checkArrayFunctionArgumentsMatch(
break;
}

$array_arg = isset($arg->value) ? $arg->value : null;

/**
* @psalm-suppress PossiblyUndefinedStringArrayOffset
* @var ObjectLike|TArray|TList|null
*/
$array_arg_type = $array_arg
&& ($arg_value_type = $statements_analyzer->node_data->getType($array_arg))
$array_arg_type = ($arg_value_type = $statements_analyzer->node_data->getType($arg->value))
&& ($types = $arg_value_type->getAtomicTypes())
&& isset($types['array'])
? $types['array']
@@ -11,6 +11,7 @@
use Psalm\Internal\Analyzer\TypeAnalyzer;
use Psalm\Issue\ParadoxicalCondition;
use Psalm\Issue\RedundantCondition;
use Psalm\Issue\TypeDoesNotContainType;
use Psalm\IssueBuffer;
use Psalm\Type;
use Psalm\Type\Atomic;
@@ -92,6 +93,34 @@ public static function reconcile(
return Type::getEmpty();
}

if (!$existing_var_type->isNullable()
&& $key
&& strpos($key, '[') === false
&& strpos($key, '->') === false
) {
foreach ($existing_var_type->getAtomicTypes() as $atomic) {
if (!$atomic instanceof TMixed
|| $atomic instanceof Type\Atomic\TNonEmptyMixed
) {
$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
}
}

return Type::getEmpty();
}
}
}

return Type::getNull();
} elseif ($assertion === 'array-key-exists') {
return Type::getEmpty();
@@ -787,6 +787,16 @@ function four(?string $s) : void {
}',
'error_message' => 'TypeDoesNotContainType',
],
'stringIsAlwaysSet' => [
'<?php
function foo(string $s) : string {
if (!isset($s)) {
return "foo";
}
return "bar";
}',
'error_message' => 'TypeDoesNotContainType'
],
];
}
}

0 comments on commit b0073d4

Please sign in to comment.
You can’t perform that action at this time.