1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-16 03:17:02 +01:00
psalm/src/Psalm/Internal/Analyzer/Statements/ThrowAnalyzer.php

68 lines
2.4 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;
use Psalm\Internal\Analyzer\TypeAnalyzer;
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
{
/**
* @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
) {
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;
}
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->getTypes() as $throw_type_part) {
$throw_type_candidate = new Union([$throw_type_part]);
2018-11-06 03:57:36 +01:00
if (!TypeAnalyzer::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->getTypes() 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
}
}
}
}