1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-15 19:07:00 +01:00
psalm/src/Psalm/Internal/Analyzer/Statements/ThrowAnalyzer.php

76 lines
2.7 KiB
PHP
Raw Normal View History

2018-01-14 18:09:40 +01:00
<?php
2018-11-06 03:57:36 +01:00
namespace Psalm\Internal\Analyzer\Statements;
2018-01-14 18:09:40 +01:00
use PhpParser;
2018-11-06 03:57:36 +01:00
use Psalm\Internal\Analyzer\StatementsAnalyzer;
2020-07-22 01:40:35 +02:00
use Psalm\Internal\Type\Comparator\UnionTypeComparator;
2018-01-14 18:09:40 +01:00
use Psalm\CodeLocation;
use Psalm\Context;
use Psalm\Issue\InvalidThrow;
use Psalm\IssueBuffer;
use Psalm\Type\Atomic\TNamedObject;
use Psalm\Type\Union;
/**
* @internal
*/
2018-11-06 03:57:36 +01:00
class ThrowAnalyzer
2018-01-14 18:09:40 +01:00
{
/**
* @param PhpParser\Node\Stmt\Throw_|PhpParser\Node\Expr\Throw_ $stmt
2018-01-14 18:09:40 +01:00
*/
public static function analyze(
2018-11-11 18:01:14 +01:00
StatementsAnalyzer $statements_analyzer,
PhpParser\Node $stmt,
2018-01-14 18:09:40 +01:00
Context $context
) : bool {
2020-02-22 04:15:25 +01:00
$context->inside_throw = true;
2018-11-11 18:01:14 +01:00
if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->expr, $context) === false) {
2018-01-14 18:09:40 +01:00
return false;
}
2020-02-22 04:15:25 +01:00
$context->inside_throw = false;
2018-01-14 18:09:40 +01:00
if ($context->check_classes
&& ($throw_type = $statements_analyzer->node_data->getType($stmt->expr))
&& !$throw_type->hasMixed()
) {
2018-01-14 18:09:40 +01:00
$exception_type = new Union([new TNamedObject('Exception'), new TNamedObject('Throwable')]);
2018-11-11 18:01:14 +01:00
$file_analyzer = $statements_analyzer->getFileAnalyzer();
$codebase = $statements_analyzer->getCodebase();
2018-01-14 18:09:40 +01:00
foreach ($throw_type->getAtomicTypes() as $throw_type_part) {
$throw_type_candidate = new Union([$throw_type_part]);
2020-07-22 01:40:35 +02:00
if (!UnionTypeComparator::isContainedBy($codebase, $throw_type_candidate, $exception_type)) {
if (IssueBuffer::accepts(
new InvalidThrow(
'Cannot throw ' . $throw_type_part
. ' as it does not extend Exception or implement Throwable',
2018-11-11 18:01:14 +01:00
new CodeLocation($file_analyzer, $stmt),
(string) $throw_type_part
),
2018-11-11 18:01:14 +01:00
$statements_analyzer->getSuppressedIssues()
)) {
return false;
}
} elseif (!$context->isSuppressingExceptions($statements_analyzer)) {
2019-03-24 21:08:05 +01:00
$codelocation = new CodeLocation($file_analyzer, $stmt);
2019-04-03 01:42:23 +02:00
$hash = $codelocation->getHash();
foreach ($throw_type->getAtomicTypes() as $throw_atomic_type) {
if ($throw_atomic_type instanceof TNamedObject) {
2019-04-03 01:42:23 +02:00
$context->possibly_thrown_exceptions[$throw_atomic_type->value][$hash] = $codelocation;
}
}
}
2018-01-14 18:09:40 +01:00
}
}
if ($stmt instanceof PhpParser\Node\Expr\Throw_) {
$statements_analyzer->node_data->setType($stmt, \Psalm\Type::getEmpty());
}
return true;
2018-01-14 18:09:40 +01:00
}
}