Skip to content
Permalink
Browse files

Allow catch to assume no try statements completed

  • Loading branch information
muglug committed Nov 11, 2019
1 parent 658f86c commit d15989c975c40df398c6bf3652d58d5a736fff24
@@ -61,6 +61,8 @@ public static function analyze(
*/
$context->possibly_thrown_exceptions = [];
$old_context = clone $context;
if ($all_catches_leave && !$stmt->finally) {
$try_context = $context;
} else {
@@ -78,8 +80,6 @@ public static function analyze(
$old_unreferenced_vars = $try_context->unreferenced_vars;
$newly_unreferenced_vars = [];
$old_context_vars = $context->vars_in_scope;
if ($statements_analyzer->analyze($stmt->stmts, $context) === false) {
return false;
}
@@ -168,8 +168,13 @@ public static function analyze(
$catch_context = clone $original_context;
foreach ($catch_context->vars_in_scope as $var_id => $type) {
if (!isset($old_context_vars[$var_id])) {
if (!isset($old_context->vars_in_scope[$var_id])) {
$type->possibly_undefined_from_try = true;
} else {
$catch_context->vars_in_scope[$var_id] = Type::combineUnionTypes(
$type,
$old_context->vars_in_scope[$var_id]
);
}
}
@@ -394,7 +399,7 @@ function ($fq_catch_class) use ($codebase) {
);
foreach ($catch_context->vars_in_scope as $var_id => $type) {
if (!isset($old_context_vars[$var_id])) {
if (!isset($old_context->vars_in_scope[$var_id])) {
$type->possibly_undefined_from_try = false;
}
@@ -62,9 +62,9 @@ protected function execute(InputInterface $i, OutputInterface $o)
$plugin_name = $i->getArgument('pluginName');
try {
assert(is_string($plugin_name));
assert(is_string($plugin_name));
try {
$plugin_class = $plugin_list->resolvePluginClass($plugin_name);
} catch (InvalidArgumentException $e) {
$io->error('Unknown plugin class ' . $plugin_name);
@@ -62,9 +62,9 @@ protected function execute(InputInterface $i, OutputInterface $o)
$plugin_name = $i->getArgument('pluginName');
try {
assert(is_string($plugin_name));
assert(is_string($plugin_name));
try {
$plugin_class = $plugin_list->resolvePluginClass($plugin_name);
} catch (InvalidArgumentException $e) {
$io->error('Unknown plugin class ' . $plugin_name);
@@ -367,6 +367,28 @@ public static function possiblyThrows(): bool {
}',
'error_message' => 'PossiblyUndefinedGlobalVariable',
],
'possiblyNullReturnInTry' => [
'<?php
function foo() : string {
$a = null;
try {
$a = dangerous();
} catch (Exception $e) {
return $a;
}
return $a;
}
function dangerous() : string {
if (rand(0, 1)) {
throw new \Exception("bad");
}
return "hello";
}',
'error_message' => 'NullableReturnStatement'
],
];
}
}

0 comments on commit d15989c

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