2016-10-22 23:35:59 +02:00
|
|
|
|
<?php
|
2018-11-06 03:57:36 +01:00
|
|
|
|
namespace Psalm\Internal\Analyzer\Statements\Block;
|
2016-10-22 23:35:59 +02:00
|
|
|
|
|
|
|
|
|
use PhpParser;
|
2018-11-06 03:57:36 +01:00
|
|
|
|
use Psalm\Internal\Analyzer\ClassLikeAnalyzer;
|
|
|
|
|
use Psalm\Internal\Analyzer\ScopeAnalyzer;
|
|
|
|
|
use Psalm\Internal\Analyzer\StatementsAnalyzer;
|
2017-05-19 06:48:26 +02:00
|
|
|
|
use Psalm\CodeLocation;
|
2016-11-02 07:29:00 +01:00
|
|
|
|
use Psalm\Context;
|
2017-12-28 20:40:28 +01:00
|
|
|
|
use Psalm\Issue\InvalidCatch;
|
|
|
|
|
use Psalm\IssueBuffer;
|
2016-10-22 23:35:59 +02:00
|
|
|
|
use Psalm\Type;
|
2017-01-15 01:06:58 +01:00
|
|
|
|
use Psalm\Type\Atomic\TNamedObject;
|
2017-12-28 20:40:28 +01:00
|
|
|
|
use Psalm\Type\Union;
|
2019-06-26 22:52:29 +02:00
|
|
|
|
use function in_array;
|
|
|
|
|
use function array_merge;
|
|
|
|
|
use function array_intersect_key;
|
|
|
|
|
use function array_diff_key;
|
|
|
|
|
use function is_string;
|
|
|
|
|
use function strtolower;
|
|
|
|
|
use function array_map;
|
|
|
|
|
use function version_compare;
|
|
|
|
|
use const PHP_VERSION;
|
2016-10-22 23:35:59 +02:00
|
|
|
|
|
2018-12-02 00:37:49 +01:00
|
|
|
|
/**
|
|
|
|
|
* @internal
|
|
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
|
class TryAnalyzer
|
2016-10-22 23:35:59 +02:00
|
|
|
|
{
|
|
|
|
|
/**
|
2018-11-11 18:01:14 +01:00
|
|
|
|
* @param StatementsAnalyzer $statements_analyzer
|
2016-11-02 07:29:00 +01:00
|
|
|
|
* @param PhpParser\Node\Stmt\TryCatch $stmt
|
|
|
|
|
* @param Context $context
|
2017-05-27 02:16:18 +02:00
|
|
|
|
*
|
2016-11-02 07:29:00 +01:00
|
|
|
|
* @return false|null
|
2016-10-22 23:35:59 +02:00
|
|
|
|
*/
|
2017-01-07 21:09:47 +01:00
|
|
|
|
public static function analyze(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
2016-11-02 07:29:00 +01:00
|
|
|
|
PhpParser\Node\Stmt\TryCatch $stmt,
|
2018-06-17 03:54:44 +02:00
|
|
|
|
Context $context
|
2016-11-02 07:29:00 +01:00
|
|
|
|
) {
|
2017-12-10 23:36:33 +01:00
|
|
|
|
$catch_actions = [];
|
|
|
|
|
$all_catches_leave = true;
|
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
2018-07-13 05:26:08 +02:00
|
|
|
|
|
2017-12-10 23:36:33 +01:00
|
|
|
|
/** @var int $i */
|
|
|
|
|
foreach ($stmt->catches as $i => $catch) {
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$catch_actions[$i] = ScopeAnalyzer::getFinalControlActions(
|
|
|
|
|
$catch->stmts,
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$statements_analyzer->node_data,
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$codebase->config->exit_functions
|
|
|
|
|
);
|
|
|
|
|
$all_catches_leave = $all_catches_leave && !in_array(ScopeAnalyzer::ACTION_NONE, $catch_actions[$i], true);
|
2017-12-10 23:36:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-22 07:13:49 +02:00
|
|
|
|
$existing_thrown_exceptions = $context->possibly_thrown_exceptions;
|
|
|
|
|
|
|
|
|
|
/**
|
2019-04-03 01:42:23 +02:00
|
|
|
|
* @var array<string, array<array-key, CodeLocation>>
|
2018-06-22 07:13:49 +02:00
|
|
|
|
*/
|
|
|
|
|
$context->possibly_thrown_exceptions = [];
|
|
|
|
|
|
2019-11-11 21:17:53 +01:00
|
|
|
|
$old_context = clone $context;
|
|
|
|
|
|
2019-05-20 18:01:18 +02:00
|
|
|
|
if ($all_catches_leave && !$stmt->finally) {
|
2017-12-10 23:36:33 +01:00
|
|
|
|
$try_context = $context;
|
|
|
|
|
} else {
|
|
|
|
|
$try_context = clone $context;
|
2018-01-21 22:24:20 +01:00
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
|
if ($codebase->alter_code) {
|
2018-01-21 22:24:20 +01:00
|
|
|
|
$try_context->branch_point = $try_context->branch_point ?: (int) $stmt->getAttribute('startFilePos');
|
|
|
|
|
}
|
2017-12-10 23:36:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-27 17:23:54 +01:00
|
|
|
|
$assigned_var_ids = $try_context->assigned_var_ids;
|
2017-12-10 23:36:33 +01:00
|
|
|
|
$context->assigned_var_ids = [];
|
|
|
|
|
|
2019-05-20 17:54:21 +02:00
|
|
|
|
$old_referenced_var_ids = $try_context->referenced_var_ids;
|
2018-01-28 23:28:34 +01:00
|
|
|
|
$old_unreferenced_vars = $try_context->unreferenced_vars;
|
2019-11-11 22:52:55 +01:00
|
|
|
|
|
2018-01-28 23:28:34 +01:00
|
|
|
|
$newly_unreferenced_vars = [];
|
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
|
if ($statements_analyzer->analyze($stmt->stmts, $context) === false) {
|
2017-12-10 23:36:33 +01:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-07 19:52:43 +02:00
|
|
|
|
$stmt_control_actions = ScopeAnalyzer::getFinalControlActions(
|
|
|
|
|
$stmt->stmts,
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$statements_analyzer->node_data,
|
2019-07-07 19:52:43 +02:00
|
|
|
|
$codebase->config->exit_functions,
|
2020-01-27 18:17:12 +01:00
|
|
|
|
$context->break_types
|
2019-07-07 19:52:43 +02:00
|
|
|
|
);
|
|
|
|
|
|
2018-02-27 17:23:54 +01:00
|
|
|
|
/** @var array<string, bool> */
|
|
|
|
|
$newly_assigned_var_ids = $context->assigned_var_ids;
|
|
|
|
|
|
|
|
|
|
$context->assigned_var_ids = array_merge(
|
|
|
|
|
$assigned_var_ids,
|
|
|
|
|
$newly_assigned_var_ids
|
|
|
|
|
);
|
2017-12-10 23:36:33 +01:00
|
|
|
|
|
2019-11-09 16:39:21 +01:00
|
|
|
|
$possibly_referenced_var_ids = array_merge(
|
2019-05-20 17:54:21 +02:00
|
|
|
|
$context->referenced_var_ids,
|
|
|
|
|
$old_referenced_var_ids
|
|
|
|
|
);
|
|
|
|
|
|
2017-12-10 23:36:33 +01:00
|
|
|
|
if ($try_context !== $context) {
|
2017-12-11 00:09:38 +01:00
|
|
|
|
foreach ($context->vars_in_scope as $var_id => $type) {
|
|
|
|
|
if (!isset($try_context->vars_in_scope[$var_id])) {
|
2017-12-10 23:36:33 +01:00
|
|
|
|
$try_context->vars_in_scope[$var_id] = clone $type;
|
2017-12-11 00:09:38 +01:00
|
|
|
|
$try_context->vars_in_scope[$var_id]->from_docblock = true;
|
2017-12-10 23:36:33 +01:00
|
|
|
|
} else {
|
|
|
|
|
$try_context->vars_in_scope[$var_id] = Type::combineUnionTypes(
|
2017-12-11 00:09:38 +01:00
|
|
|
|
$try_context->vars_in_scope[$var_id],
|
2017-12-10 23:36:33 +01:00
|
|
|
|
$type
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-12-11 00:34:22 +01:00
|
|
|
|
|
|
|
|
|
$try_context->vars_possibly_in_scope = $context->vars_possibly_in_scope;
|
2020-03-08 19:56:03 +01:00
|
|
|
|
$try_context->possibly_thrown_exceptions = $context->possibly_thrown_exceptions;
|
2018-01-25 07:04:26 +01:00
|
|
|
|
|
2019-05-20 17:54:21 +02:00
|
|
|
|
$context->referenced_var_ids = array_intersect_key(
|
2018-01-25 07:04:26 +01:00
|
|
|
|
$try_context->referenced_var_ids,
|
|
|
|
|
$context->referenced_var_ids
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ($context->collect_references) {
|
2018-01-28 23:28:34 +01:00
|
|
|
|
$newly_unreferenced_vars = array_merge(
|
|
|
|
|
$newly_unreferenced_vars,
|
|
|
|
|
array_diff_key(
|
|
|
|
|
$context->unreferenced_vars,
|
|
|
|
|
$old_unreferenced_vars
|
|
|
|
|
)
|
2018-01-25 07:04:26 +01:00
|
|
|
|
);
|
|
|
|
|
}
|
2017-12-10 23:36:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-17 03:54:44 +02:00
|
|
|
|
$try_leaves_loop = $context->loop_scope
|
|
|
|
|
&& $context->loop_scope->final_actions
|
2018-11-06 03:57:36 +01:00
|
|
|
|
&& !in_array(ScopeAnalyzer::ACTION_NONE, $context->loop_scope->final_actions, true);
|
2016-10-22 23:35:59 +02:00
|
|
|
|
|
2017-12-10 23:36:33 +01:00
|
|
|
|
if (!$all_catches_leave) {
|
2018-02-27 17:23:54 +01:00
|
|
|
|
foreach ($newly_assigned_var_ids as $assigned_var_id => $_) {
|
2017-12-10 23:36:33 +01:00
|
|
|
|
$context->removeVarFromConflictingClauses($assigned_var_id);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2018-02-27 17:23:54 +01:00
|
|
|
|
foreach ($newly_assigned_var_ids as $assigned_var_id => $_) {
|
2017-12-10 23:36:33 +01:00
|
|
|
|
$try_context->removeVarFromConflictingClauses($assigned_var_id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// at this point we have two contexts – $context, in which it is assumed that everything was fine,
|
|
|
|
|
// and $try_context - which allows all variables to have the union of the values before and after
|
|
|
|
|
// the try was applied
|
|
|
|
|
$original_context = clone $try_context;
|
2016-10-22 23:35:59 +02:00
|
|
|
|
|
2019-01-20 15:27:46 +01:00
|
|
|
|
$issues_to_suppress = [
|
|
|
|
|
'RedundantCondition',
|
|
|
|
|
'RedundantConditionGivenDocblockType',
|
|
|
|
|
'TypeDoesNotContainNull',
|
|
|
|
|
'TypeDoesNotContainType',
|
|
|
|
|
];
|
|
|
|
|
|
2019-11-09 18:25:30 +01:00
|
|
|
|
$definitely_newly_assigned_var_ids = $newly_assigned_var_ids;
|
|
|
|
|
|
2017-12-10 23:36:33 +01:00
|
|
|
|
/** @var int $i */
|
|
|
|
|
foreach ($stmt->catches as $i => $catch) {
|
2016-10-22 23:35:59 +02:00
|
|
|
|
$catch_context = clone $original_context;
|
|
|
|
|
|
2019-11-09 18:25:30 +01:00
|
|
|
|
foreach ($catch_context->vars_in_scope as $var_id => $type) {
|
2019-11-11 21:17:53 +01:00
|
|
|
|
if (!isset($old_context->vars_in_scope[$var_id])) {
|
2019-11-11 23:42:46 +01:00
|
|
|
|
$type = clone $type;
|
2019-11-09 18:25:30 +01:00
|
|
|
|
$type->possibly_undefined_from_try = true;
|
2019-11-11 23:42:46 +01:00
|
|
|
|
$catch_context->vars_in_scope[$var_id] = $type;
|
2019-11-11 21:17:53 +01:00
|
|
|
|
} else {
|
|
|
|
|
$catch_context->vars_in_scope[$var_id] = Type::combineUnionTypes(
|
|
|
|
|
$type,
|
|
|
|
|
$old_context->vars_in_scope[$var_id]
|
|
|
|
|
);
|
2019-11-09 18:25:30 +01:00
|
|
|
|
}
|
2019-11-11 22:52:55 +01:00
|
|
|
|
|
|
|
|
|
if (isset($old_context->unreferenced_vars[$var_id])) {
|
|
|
|
|
if (!isset($catch_context->unreferenced_vars[$var_id])) {
|
|
|
|
|
$catch_context->unreferenced_vars[$var_id] = $old_context->unreferenced_vars[$var_id];
|
|
|
|
|
} else {
|
|
|
|
|
$catch_context->unreferenced_vars[$var_id] += $old_context->unreferenced_vars[$var_id];
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-09 18:25:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-04 04:41:45 +01:00
|
|
|
|
$fq_catch_classes = [];
|
2016-10-22 23:35:59 +02:00
|
|
|
|
|
2018-04-17 18:16:25 +02:00
|
|
|
|
$catch_var_name = $catch->var->name;
|
|
|
|
|
|
|
|
|
|
if (!is_string($catch_var_name)) {
|
|
|
|
|
throw new \UnexpectedValueException('Catch var name must be a string');
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-30 05:41:17 +01:00
|
|
|
|
if (!$catch->types) {
|
|
|
|
|
throw new \UnexpectedValueException('Very bad');
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-04 04:41:45 +01:00
|
|
|
|
foreach ($catch->types as $catch_type) {
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$fq_catch_class = ClassLikeAnalyzer::getFQCLNFromNameObject(
|
2016-12-04 04:41:45 +01:00
|
|
|
|
$catch_type,
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getAliases()
|
2016-12-04 04:41:45 +01:00
|
|
|
|
);
|
2016-10-22 23:35:59 +02:00
|
|
|
|
|
2019-06-05 17:33:04 +02:00
|
|
|
|
if ($codebase->alter_code && $fq_catch_class) {
|
|
|
|
|
$codebase->classlikes->handleClassLikeReferenceInMigration(
|
|
|
|
|
$codebase,
|
|
|
|
|
$statements_analyzer,
|
|
|
|
|
$catch_type,
|
|
|
|
|
$fq_catch_class,
|
2019-12-29 00:37:55 +01:00
|
|
|
|
$context->calling_function_id
|
2019-06-05 17:33:04 +02:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-10 23:36:33 +01:00
|
|
|
|
if ($original_context->check_classes) {
|
2018-11-06 03:57:36 +01:00
|
|
|
|
if (ClassLikeAnalyzer::checkFullyQualifiedClassLikeName(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer,
|
2016-12-04 04:41:45 +01:00
|
|
|
|
$fq_catch_class,
|
2018-11-11 18:01:14 +01:00
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $catch_type, $context->include_location),
|
2020-02-11 22:39:33 +01:00
|
|
|
|
$context->self,
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues(),
|
2018-01-02 02:04:03 +01:00
|
|
|
|
false
|
2016-12-04 04:41:45 +01:00
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2016-10-22 23:35:59 +02:00
|
|
|
|
}
|
2016-12-08 23:15:51 +01:00
|
|
|
|
|
2018-02-01 06:50:01 +01:00
|
|
|
|
if (($codebase->classExists($fq_catch_class)
|
2017-12-28 20:40:28 +01:00
|
|
|
|
&& strtolower($fq_catch_class) !== 'exception'
|
2018-02-01 06:50:01 +01:00
|
|
|
|
&& !($codebase->classExtends($fq_catch_class, 'Exception')
|
|
|
|
|
|| $codebase->classImplements($fq_catch_class, 'Throwable')))
|
|
|
|
|
|| ($codebase->interfaceExists($fq_catch_class)
|
2017-12-28 20:40:28 +01:00
|
|
|
|
&& strtolower($fq_catch_class) !== 'throwable'
|
2018-02-01 06:50:01 +01:00
|
|
|
|
&& !$codebase->interfaceExtends($fq_catch_class, 'Throwable'))
|
2017-12-28 20:40:28 +01:00
|
|
|
|
) {
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
|
new InvalidCatch(
|
|
|
|
|
'Class/interface ' . $fq_catch_class . ' cannot be caught',
|
2018-11-11 18:01:14 +01:00
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt),
|
2018-09-21 17:35:51 +02:00
|
|
|
|
$fq_catch_class
|
2017-12-28 20:40:28 +01:00
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2017-12-28 20:40:28 +01:00
|
|
|
|
)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-08 23:15:51 +01:00
|
|
|
|
$fq_catch_classes[] = $fq_catch_class;
|
2016-10-22 23:35:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-22 07:13:49 +02:00
|
|
|
|
if ($catch_context->collect_exceptions) {
|
|
|
|
|
foreach ($fq_catch_classes as $fq_catch_class) {
|
|
|
|
|
$fq_catch_class_lower = strtolower($fq_catch_class);
|
|
|
|
|
|
2020-03-08 19:56:03 +01:00
|
|
|
|
foreach ($catch_context->possibly_thrown_exceptions as $exception_fqcln => $_) {
|
2018-06-22 07:13:49 +02:00
|
|
|
|
$exception_fqcln_lower = strtolower($exception_fqcln);
|
|
|
|
|
|
2020-03-08 19:56:03 +01:00
|
|
|
|
if ($exception_fqcln_lower === $fq_catch_class_lower
|
|
|
|
|
|| ($codebase->classExists($exception_fqcln)
|
|
|
|
|
&& $codebase->classExtendsOrImplements($exception_fqcln, $fq_catch_class))
|
|
|
|
|
|| ($codebase->interfaceExists($exception_fqcln)
|
|
|
|
|
&& $codebase->interfaceExtends($exception_fqcln, $fq_catch_class))
|
2018-06-22 07:13:49 +02:00
|
|
|
|
) {
|
2020-03-08 19:56:03 +01:00
|
|
|
|
unset($original_context->possibly_thrown_exceptions[$exception_fqcln]);
|
2018-06-22 07:13:49 +02:00
|
|
|
|
unset($context->possibly_thrown_exceptions[$exception_fqcln]);
|
2020-01-17 18:05:37 +01:00
|
|
|
|
unset($catch_context->possibly_thrown_exceptions[$exception_fqcln]);
|
2018-06-22 07:13:49 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-17 18:05:37 +01:00
|
|
|
|
|
2020-03-08 19:56:03 +01:00
|
|
|
|
/**
|
|
|
|
|
* @var array<string, array<array-key, CodeLocation>>
|
|
|
|
|
*/
|
|
|
|
|
$catch_context->possibly_thrown_exceptions = [];
|
2018-06-22 07:13:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-17 18:16:25 +02:00
|
|
|
|
$catch_var_id = '$' . $catch_var_name;
|
2017-02-08 00:09:12 +01:00
|
|
|
|
|
2018-02-18 23:55:11 +01:00
|
|
|
|
$catch_context->vars_in_scope[$catch_var_id] = new Union(
|
2016-12-04 04:41:45 +01:00
|
|
|
|
array_map(
|
2016-12-07 01:41:52 +01:00
|
|
|
|
/**
|
|
|
|
|
* @param string $fq_catch_class
|
2017-05-27 02:16:18 +02:00
|
|
|
|
*
|
2016-12-07 20:13:39 +01:00
|
|
|
|
* @return Type\Atomic
|
2016-12-07 01:41:52 +01:00
|
|
|
|
*/
|
2018-02-01 06:50:01 +01:00
|
|
|
|
function ($fq_catch_class) use ($codebase) {
|
2017-12-28 01:49:36 +01:00
|
|
|
|
$catch_class_type = new TNamedObject($fq_catch_class);
|
|
|
|
|
|
|
|
|
|
if (version_compare(PHP_VERSION, '7.0.0dev', '>=')
|
2018-02-01 06:50:01 +01:00
|
|
|
|
&& $codebase->interfaceExists($fq_catch_class)
|
|
|
|
|
&& !$codebase->interfaceExtends($fq_catch_class, 'Throwable')
|
2017-12-28 01:49:36 +01:00
|
|
|
|
) {
|
|
|
|
|
$catch_class_type->addIntersectionType(new TNamedObject('Throwable'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $catch_class_type;
|
2016-12-04 04:41:45 +01:00
|
|
|
|
},
|
|
|
|
|
$fq_catch_classes
|
|
|
|
|
)
|
|
|
|
|
);
|
2016-10-22 23:35:59 +02:00
|
|
|
|
|
2017-10-11 05:09:19 +02:00
|
|
|
|
// discard all clauses because crazy stuff may have happened in try block
|
|
|
|
|
$catch_context->clauses = [];
|
|
|
|
|
|
2017-02-08 00:09:12 +01:00
|
|
|
|
$catch_context->vars_possibly_in_scope[$catch_var_id] = true;
|
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
|
if (!$statements_analyzer->hasVariable($catch_var_id)) {
|
2018-01-28 23:28:34 +01:00
|
|
|
|
$location = new CodeLocation(
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer,
|
2018-04-17 18:16:25 +02:00
|
|
|
|
$catch->var,
|
|
|
|
|
$context->include_location
|
2018-01-28 23:28:34 +01:00
|
|
|
|
);
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->registerVariable(
|
2017-02-08 00:09:12 +01:00
|
|
|
|
$catch_var_id,
|
2018-01-28 23:28:34 +01:00
|
|
|
|
$location,
|
2018-01-21 22:24:20 +01:00
|
|
|
|
$try_context->branch_point
|
2017-02-08 00:09:12 +01:00
|
|
|
|
);
|
2018-06-17 02:01:33 +02:00
|
|
|
|
$catch_context->unreferenced_vars[$catch_var_id] = [$location->getHash() => $location];
|
2017-02-08 00:09:12 +01:00
|
|
|
|
}
|
2016-10-22 23:35:59 +02:00
|
|
|
|
|
2017-02-08 06:28:26 +01:00
|
|
|
|
// this registers the variable to avoid unfair deadcode issues
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$catch_context->hasVariable($catch_var_id, $statements_analyzer);
|
2016-10-22 23:35:59 +02:00
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$suppressed_issues = $statements_analyzer->getSuppressedIssues();
|
2017-12-10 23:36:33 +01:00
|
|
|
|
|
2019-01-20 15:27:46 +01:00
|
|
|
|
foreach ($issues_to_suppress as $issue_to_suppress) {
|
|
|
|
|
if (!in_array($issue_to_suppress, $suppressed_issues, true)) {
|
|
|
|
|
$statements_analyzer->addSuppressedIssues([$issue_to_suppress]);
|
|
|
|
|
}
|
2017-12-10 23:36:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-09 18:25:30 +01:00
|
|
|
|
$old_catch_assigned_var_ids = $catch_context->referenced_var_ids;
|
|
|
|
|
|
|
|
|
|
$catch_context->assigned_var_ids = [];
|
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->analyze($catch->stmts, $catch_context);
|
2016-10-22 23:35:59 +02:00
|
|
|
|
|
2019-05-15 03:22:29 +02:00
|
|
|
|
// recalculate in case there's a no-return clause
|
|
|
|
|
$catch_actions[$i] = ScopeAnalyzer::getFinalControlActions(
|
|
|
|
|
$catch->stmts,
|
2019-11-25 17:44:54 +01:00
|
|
|
|
$statements_analyzer->node_data,
|
2019-05-15 03:22:29 +02:00
|
|
|
|
$codebase->config->exit_functions,
|
2020-01-27 18:17:12 +01:00
|
|
|
|
$context->break_types
|
2019-05-15 03:22:29 +02:00
|
|
|
|
);
|
|
|
|
|
|
2019-01-20 15:27:46 +01:00
|
|
|
|
foreach ($issues_to_suppress as $issue_to_suppress) {
|
|
|
|
|
if (!in_array($issue_to_suppress, $suppressed_issues, true)) {
|
|
|
|
|
$statements_analyzer->removeSuppressedIssues([$issue_to_suppress]);
|
|
|
|
|
}
|
2017-12-10 23:36:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-09 18:25:30 +01:00
|
|
|
|
/** @var array<string, bool> */
|
|
|
|
|
$new_catch_assigned_var_ids = $catch_context->assigned_var_ids;
|
|
|
|
|
|
|
|
|
|
$catch_context->assigned_var_ids += $old_catch_assigned_var_ids;
|
|
|
|
|
|
2019-05-20 17:54:21 +02:00
|
|
|
|
$context->referenced_var_ids = array_intersect_key(
|
2017-11-24 18:17:28 +01:00
|
|
|
|
$catch_context->referenced_var_ids,
|
|
|
|
|
$context->referenced_var_ids
|
|
|
|
|
);
|
2017-02-02 06:45:23 +01:00
|
|
|
|
|
2019-05-20 17:54:21 +02:00
|
|
|
|
$possibly_referenced_var_ids = array_merge(
|
|
|
|
|
$catch_context->referenced_var_ids,
|
|
|
|
|
$possibly_referenced_var_ids
|
|
|
|
|
);
|
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
|
if ($context->collect_references && $catch_actions[$i] !== [ScopeAnalyzer::ACTION_END]) {
|
2018-01-28 23:28:34 +01:00
|
|
|
|
$newly_unreferenced_vars = array_merge(
|
|
|
|
|
$newly_unreferenced_vars,
|
|
|
|
|
array_diff_key(
|
|
|
|
|
$catch_context->unreferenced_vars,
|
|
|
|
|
$old_unreferenced_vars
|
|
|
|
|
)
|
2018-01-25 07:04:26 +01:00
|
|
|
|
);
|
2018-01-28 23:28:34 +01:00
|
|
|
|
|
2018-06-17 02:01:33 +02:00
|
|
|
|
foreach ($catch_context->unreferenced_vars as $var_id => $locations) {
|
2018-01-28 23:28:34 +01:00
|
|
|
|
if (!isset($old_unreferenced_vars[$var_id])
|
2018-02-27 17:23:54 +01:00
|
|
|
|
&& (isset($context->unreferenced_vars[$var_id])
|
|
|
|
|
|| isset($newly_assigned_var_ids[$var_id]))
|
2018-01-28 23:28:34 +01:00
|
|
|
|
) {
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->registerVariableUses($locations);
|
2018-01-28 23:28:34 +01:00
|
|
|
|
} elseif (isset($old_unreferenced_vars[$var_id])
|
2018-06-17 02:01:33 +02:00
|
|
|
|
&& $old_unreferenced_vars[$var_id] !== $locations
|
2018-01-28 23:28:34 +01:00
|
|
|
|
) {
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->registerVariableUses($locations);
|
2019-08-26 17:41:15 +02:00
|
|
|
|
} elseif (isset($newly_unreferenced_vars[$var_id])) {
|
|
|
|
|
$context->unreferenced_vars[$var_id] = $newly_unreferenced_vars[$var_id];
|
2018-01-28 23:28:34 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-25 07:04:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-17 18:05:37 +01:00
|
|
|
|
if ($catch_context->collect_exceptions) {
|
|
|
|
|
$context->mergeExceptions($catch_context);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-11 23:42:46 +01:00
|
|
|
|
if ($catch_actions[$i] !== [ScopeAnalyzer::ACTION_END]
|
|
|
|
|
&& $catch_actions[$i] !== [ScopeAnalyzer::ACTION_CONTINUE]
|
|
|
|
|
&& $catch_actions[$i] !== [ScopeAnalyzer::ACTION_BREAK]
|
|
|
|
|
) {
|
2019-11-09 18:25:30 +01:00
|
|
|
|
$definitely_newly_assigned_var_ids = array_intersect_key(
|
|
|
|
|
$new_catch_assigned_var_ids,
|
|
|
|
|
$definitely_newly_assigned_var_ids
|
|
|
|
|
);
|
|
|
|
|
|
2017-12-11 00:09:38 +01:00
|
|
|
|
foreach ($catch_context->vars_in_scope as $var_id => $type) {
|
2019-07-07 20:00:49 +02:00
|
|
|
|
if ($stmt_control_actions === [ScopeAnalyzer::ACTION_END]) {
|
|
|
|
|
$context->vars_in_scope[$var_id] = $type;
|
|
|
|
|
} elseif (isset($context->vars_in_scope[$var_id])
|
2019-11-11 23:42:46 +01:00
|
|
|
|
&& !$context->vars_in_scope[$var_id]->equals($type)
|
2016-11-02 07:29:00 +01:00
|
|
|
|
) {
|
2017-12-11 00:09:38 +01:00
|
|
|
|
$context->vars_in_scope[$var_id] = Type::combineUnionTypes(
|
|
|
|
|
$context->vars_in_scope[$var_id],
|
2016-11-02 07:29:00 +01:00
|
|
|
|
$type
|
|
|
|
|
);
|
2016-10-22 23:35:59 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-09 21:39:30 +01:00
|
|
|
|
$context->vars_possibly_in_scope = array_merge(
|
|
|
|
|
$catch_context->vars_possibly_in_scope,
|
|
|
|
|
$context->vars_possibly_in_scope
|
|
|
|
|
);
|
2019-11-09 18:25:30 +01:00
|
|
|
|
} else {
|
|
|
|
|
if ($stmt->finally) {
|
|
|
|
|
$context->vars_possibly_in_scope = array_merge(
|
|
|
|
|
$catch_context->vars_possibly_in_scope,
|
|
|
|
|
$context->vars_possibly_in_scope
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-12 00:38:33 +01:00
|
|
|
|
|
|
|
|
|
if ($stmt->finally) {
|
|
|
|
|
$suppressed_issues = $statements_analyzer->getSuppressedIssues();
|
|
|
|
|
|
|
|
|
|
foreach ($issues_to_suppress as $issue_to_suppress) {
|
|
|
|
|
if (!in_array($issue_to_suppress, $suppressed_issues, true)) {
|
|
|
|
|
$statements_analyzer->addSuppressedIssues([$issue_to_suppress]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$statements_analyzer->analyze($stmt->finally->stmts, $catch_context);
|
|
|
|
|
|
|
|
|
|
foreach ($issues_to_suppress as $issue_to_suppress) {
|
|
|
|
|
if (!in_array($issue_to_suppress, $suppressed_issues, true)) {
|
|
|
|
|
$statements_analyzer->removeSuppressedIssues([$issue_to_suppress]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-09 18:25:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($definitely_newly_assigned_var_ids as $var_id => $_) {
|
|
|
|
|
if (isset($context->vars_in_scope[$var_id])) {
|
2019-11-11 23:42:46 +01:00
|
|
|
|
$new_type = clone $context->vars_in_scope[$var_id];
|
|
|
|
|
$new_type->possibly_undefined_from_try = false;
|
|
|
|
|
$context->vars_in_scope[$var_id] = $new_type;
|
2016-10-22 23:35:59 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-17 03:54:44 +02:00
|
|
|
|
if ($context->loop_scope
|
2017-12-03 00:28:18 +01:00
|
|
|
|
&& !$try_leaves_loop
|
2018-11-06 03:57:36 +01:00
|
|
|
|
&& !in_array(ScopeAnalyzer::ACTION_NONE, $context->loop_scope->final_actions, true)
|
2017-12-03 00:28:18 +01:00
|
|
|
|
) {
|
2018-11-06 03:57:36 +01:00
|
|
|
|
$context->loop_scope->final_actions[] = ScopeAnalyzer::ACTION_NONE;
|
2016-10-22 23:35:59 +02:00
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
|
|
2019-05-20 17:54:21 +02:00
|
|
|
|
$newly_referenced_var_ids = array_diff_key(
|
|
|
|
|
$context->referenced_var_ids,
|
|
|
|
|
$old_referenced_var_ids
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ($context->collect_references) {
|
|
|
|
|
foreach ($old_unreferenced_vars as $var_id => $locations) {
|
|
|
|
|
if ((isset($context->unreferenced_vars[$var_id]) && $context->unreferenced_vars[$var_id] !== $locations)
|
|
|
|
|
|| (!isset($newly_referenced_var_ids[$var_id]) && isset($possibly_referenced_var_ids[$var_id]))
|
|
|
|
|
) {
|
|
|
|
|
$statements_analyzer->registerVariableUses($locations);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-09 17:01:48 +01:00
|
|
|
|
|
|
|
|
|
$newly_unreferenced_vars = array_merge(
|
|
|
|
|
$newly_unreferenced_vars,
|
|
|
|
|
array_diff_key(
|
|
|
|
|
$try_context->unreferenced_vars,
|
|
|
|
|
$old_unreferenced_vars
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
foreach ($newly_unreferenced_vars as $var_id => $locations) {
|
|
|
|
|
if (!isset($context->unreferenced_vars[$var_id])) {
|
|
|
|
|
$context->unreferenced_vars[$var_id] = $locations;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-20 17:54:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-10 23:36:33 +01:00
|
|
|
|
if ($stmt->finally) {
|
2019-01-06 18:39:18 +01:00
|
|
|
|
$suppressed_issues = $statements_analyzer->getSuppressedIssues();
|
|
|
|
|
|
2019-01-20 15:27:46 +01:00
|
|
|
|
foreach ($issues_to_suppress as $issue_to_suppress) {
|
|
|
|
|
if (!in_array($issue_to_suppress, $suppressed_issues, true)) {
|
|
|
|
|
$statements_analyzer->addSuppressedIssues([$issue_to_suppress]);
|
|
|
|
|
}
|
2019-01-06 18:39:18 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
|
$statements_analyzer->analyze($stmt->finally->stmts, $context);
|
2019-01-06 18:39:18 +01:00
|
|
|
|
|
2019-01-20 15:27:46 +01:00
|
|
|
|
foreach ($issues_to_suppress as $issue_to_suppress) {
|
|
|
|
|
if (!in_array($issue_to_suppress, $suppressed_issues, true)) {
|
|
|
|
|
$statements_analyzer->removeSuppressedIssues([$issue_to_suppress]);
|
|
|
|
|
}
|
2019-01-06 18:39:18 +01:00
|
|
|
|
}
|
2017-12-10 23:36:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-03 01:42:23 +02:00
|
|
|
|
foreach ($existing_thrown_exceptions as $possibly_thrown_exception => $codelocations) {
|
|
|
|
|
foreach ($codelocations as $hash => $codelocation) {
|
|
|
|
|
$context->possibly_thrown_exceptions[$possibly_thrown_exception][$hash] = $codelocation;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-06-22 07:13:49 +02:00
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
|
return null;
|
2016-10-22 23:35:59 +02:00
|
|
|
|
}
|
|
|
|
|
}
|