2016-10-22 23:35:59 +02:00
|
|
|
|
<?php
|
|
|
|
|
namespace Psalm\Checker\Statements\Block;
|
|
|
|
|
|
|
|
|
|
use PhpParser;
|
|
|
|
|
use Psalm\Checker\ClassLikeChecker;
|
2017-12-28 01:49:36 +01:00
|
|
|
|
use Psalm\Checker\InterfaceChecker;
|
2016-10-22 23:35:59 +02:00
|
|
|
|
use Psalm\Checker\ScopeChecker;
|
|
|
|
|
use Psalm\Checker\StatementsChecker;
|
2017-05-19 06:48:26 +02:00
|
|
|
|
use Psalm\CodeLocation;
|
2016-11-02 07:29:00 +01:00
|
|
|
|
use Psalm\Context;
|
2017-12-03 00:28:18 +01:00
|
|
|
|
use Psalm\Scope\LoopScope;
|
2016-10-22 23:35:59 +02:00
|
|
|
|
use Psalm\Type;
|
2017-01-15 01:06:58 +01:00
|
|
|
|
use Psalm\Type\Atomic\TNamedObject;
|
2016-10-22 23:35:59 +02:00
|
|
|
|
|
|
|
|
|
class TryChecker
|
|
|
|
|
{
|
|
|
|
|
/**
|
2016-11-02 07:29:00 +01:00
|
|
|
|
* @param StatementsChecker $statements_checker
|
|
|
|
|
* @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(
|
2016-11-02 07:29:00 +01:00
|
|
|
|
StatementsChecker $statements_checker,
|
|
|
|
|
PhpParser\Node\Stmt\TryCatch $stmt,
|
|
|
|
|
Context $context,
|
2017-12-03 00:28:18 +01:00
|
|
|
|
LoopScope $loop_scope = null
|
2016-11-02 07:29:00 +01:00
|
|
|
|
) {
|
2017-12-10 23:36:33 +01:00
|
|
|
|
$catch_actions = [];
|
|
|
|
|
$all_catches_leave = true;
|
|
|
|
|
|
|
|
|
|
/** @var int $i */
|
|
|
|
|
foreach ($stmt->catches as $i => $catch) {
|
|
|
|
|
$catch_actions[$i] = ScopeChecker::getFinalControlActions($catch->stmts);
|
|
|
|
|
$all_catches_leave = $all_catches_leave && !in_array(ScopeChecker::ACTION_NONE, $catch_actions[$i], true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($all_catches_leave) {
|
|
|
|
|
$try_context = $context;
|
|
|
|
|
} else {
|
|
|
|
|
$try_context = clone $context;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$assigned_var_ids = $context->assigned_var_ids;
|
|
|
|
|
$context->assigned_var_ids = [];
|
|
|
|
|
|
|
|
|
|
if ($statements_checker->analyze($stmt->stmts, $context, $loop_scope) === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$context->assigned_var_ids = $assigned_var_ids;
|
|
|
|
|
|
|
|
|
|
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;
|
2017-12-10 23:36:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-03 00:28:18 +01:00
|
|
|
|
$try_leaves_loop = $loop_scope
|
|
|
|
|
&& $loop_scope->final_actions
|
|
|
|
|
&& !in_array(ScopeChecker::ACTION_NONE, $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) {
|
|
|
|
|
foreach ($assigned_var_ids as $assigned_var_id => $_) {
|
|
|
|
|
$context->removeVarFromConflictingClauses($assigned_var_id);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
foreach ($assigned_var_ids as $assigned_var_id => $_) {
|
|
|
|
|
$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
|
|
|
|
|
2017-12-28 01:49:36 +01:00
|
|
|
|
$project_checker = $statements_checker->getFileChecker()->project_checker;
|
|
|
|
|
|
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;
|
|
|
|
|
|
2016-12-04 04:41:45 +01:00
|
|
|
|
$fq_catch_classes = [];
|
2016-10-22 23:35:59 +02:00
|
|
|
|
|
2016-12-04 04:41:45 +01:00
|
|
|
|
foreach ($catch->types as $catch_type) {
|
2016-12-08 23:15:51 +01:00
|
|
|
|
$fq_catch_class = ClassLikeChecker::getFQCLNFromNameObject(
|
2016-12-04 04:41:45 +01:00
|
|
|
|
$catch_type,
|
2017-07-25 22:11:02 +02:00
|
|
|
|
$statements_checker->getAliases()
|
2016-12-04 04:41:45 +01:00
|
|
|
|
);
|
2016-10-22 23:35:59 +02:00
|
|
|
|
|
2017-12-10 23:36:33 +01:00
|
|
|
|
if ($original_context->check_classes) {
|
2016-12-04 04:41:45 +01:00
|
|
|
|
if (ClassLikeChecker::checkFullyQualifiedClassLikeName(
|
2017-07-29 21:05:06 +02:00
|
|
|
|
$statements_checker->getFileChecker()->project_checker,
|
2016-12-04 04:41:45 +01:00
|
|
|
|
$fq_catch_class,
|
2017-06-21 20:22:52 +02:00
|
|
|
|
new CodeLocation($statements_checker->getSource(), $catch_type, $context->include_location),
|
2016-12-04 04:41:45 +01:00
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
|
) === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2016-10-22 23:35:59 +02:00
|
|
|
|
}
|
2016-12-08 23:15:51 +01:00
|
|
|
|
|
|
|
|
|
$fq_catch_classes[] = $fq_catch_class;
|
2016-10-22 23:35:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-08 00:09:12 +01:00
|
|
|
|
$catch_var_id = '$' . $catch->var;
|
|
|
|
|
|
|
|
|
|
$catch_context->vars_in_scope[$catch_var_id] = new Type\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
|
|
|
|
*/
|
2017-12-28 01:49:36 +01:00
|
|
|
|
function ($fq_catch_class) use ($project_checker) {
|
|
|
|
|
$catch_class_type = new TNamedObject($fq_catch_class);
|
|
|
|
|
|
|
|
|
|
if (version_compare(PHP_VERSION, '7.0.0dev', '>=')
|
|
|
|
|
&& InterfaceChecker::interfaceExists($project_checker, $fq_catch_class)
|
|
|
|
|
&& !InterfaceChecker::interfaceExtends($project_checker, $fq_catch_class, 'Throwable')
|
|
|
|
|
) {
|
|
|
|
|
$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;
|
|
|
|
|
|
|
|
|
|
if (!$statements_checker->hasVariable($catch_var_id)) {
|
|
|
|
|
$statements_checker->registerVariable(
|
|
|
|
|
$catch_var_id,
|
2017-06-21 20:22:52 +02:00
|
|
|
|
new CodeLocation($statements_checker, $catch, $context->include_location, true)
|
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
|
|
|
|
|
$catch_context->hasVariable($catch_var_id);
|
2016-10-22 23:35:59 +02:00
|
|
|
|
|
2017-12-10 23:36:33 +01:00
|
|
|
|
$suppressed_issues = $statements_checker->getSuppressedIssues();
|
|
|
|
|
|
|
|
|
|
if (!in_array('RedundantCondition', $suppressed_issues, true)) {
|
|
|
|
|
$statements_checker->addSuppressedIssues(['RedundantCondition']);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-03 00:28:18 +01:00
|
|
|
|
$statements_checker->analyze($catch->stmts, $catch_context, $loop_scope);
|
2016-10-22 23:35:59 +02:00
|
|
|
|
|
2017-12-10 23:36:33 +01:00
|
|
|
|
if (!in_array('RedundantCondition', $suppressed_issues, true)) {
|
|
|
|
|
$statements_checker->removeSuppressedIssues(['RedundantCondition']);
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-24 18:17:28 +01:00
|
|
|
|
$context->referenced_var_ids = array_merge(
|
|
|
|
|
$catch_context->referenced_var_ids,
|
|
|
|
|
$context->referenced_var_ids
|
|
|
|
|
);
|
2017-02-02 06:45:23 +01:00
|
|
|
|
|
2017-12-10 23:36:33 +01:00
|
|
|
|
if ($catch_actions[$i] !== [ScopeChecker::ACTION_END]) {
|
2017-12-11 00:09:38 +01:00
|
|
|
|
foreach ($catch_context->vars_in_scope as $var_id => $type) {
|
|
|
|
|
if ($catch->var !== $var_id &&
|
|
|
|
|
$context->hasVariable($var_id) &&
|
|
|
|
|
$context->vars_in_scope[$var_id]->getId() !== $type->getId()
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
|
$context->vars_possibly_in_scope = array_merge(
|
|
|
|
|
$catch_context->vars_possibly_in_scope,
|
|
|
|
|
$context->vars_possibly_in_scope
|
|
|
|
|
);
|
2016-10-22 23:35:59 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-03 00:28:18 +01:00
|
|
|
|
if ($loop_scope
|
|
|
|
|
&& !$try_leaves_loop
|
|
|
|
|
&& !in_array(ScopeChecker::ACTION_NONE, $loop_scope->final_actions, true)
|
|
|
|
|
) {
|
|
|
|
|
$loop_scope->final_actions[] = ScopeChecker::ACTION_NONE;
|
2016-10-22 23:35:59 +02:00
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
|
|
2017-12-10 23:36:33 +01:00
|
|
|
|
if ($stmt->finally) {
|
|
|
|
|
$statements_checker->analyze($stmt->finally->stmts, $context, $loop_scope);
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
|
return null;
|
2016-10-22 23:35:59 +02:00
|
|
|
|
}
|
|
|
|
|
}
|