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;
|
|
|
|
|
2018-12-02 00:37:49 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
class ThrowAnalyzer
|
2018-01-14 18:09:40 +01:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @return false|null
|
|
|
|
*/
|
|
|
|
public static function analyze(
|
2018-11-11 18:01:14 +01:00
|
|
|
StatementsAnalyzer $statements_analyzer,
|
2018-01-14 18:09:40 +01:00
|
|
|
PhpParser\Node\Stmt\Throw_ $stmt,
|
|
|
|
Context $context
|
|
|
|
) {
|
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
|
|
|
|
2019-11-25 17:44:54 +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
|
|
|
|
2020-01-04 18:20:26 +01:00
|
|
|
foreach ($throw_type->getAtomicTypes() as $throw_type_part) {
|
2018-09-21 17:35:51 +02:00
|
|
|
$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)) {
|
2018-09-21 17:35:51 +02:00
|
|
|
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),
|
2018-09-21 17:35:51 +02:00
|
|
|
(string) $throw_type_part
|
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-09-21 17:35:51 +02:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-03-29 00:50:29 +01:00
|
|
|
} 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();
|
2020-01-04 18:20:26 +01:00
|
|
|
foreach ($throw_type->getAtomicTypes() as $throw_atomic_type) {
|
2018-09-21 17:35:51 +02:00
|
|
|
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-09-21 17:35:51 +02:00
|
|
|
}
|
2018-06-22 07:13:49 +02:00
|
|
|
}
|
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|