1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-14 18:36:58 +01:00
psalm/src/Psalm/Internal/Analyzer/Statements/ThrowAnalyzer.php

62 lines
2.2 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;
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-06 03:57:36 +01:00
StatementsAnalyzer $statements_checker,
2018-01-14 18:09:40 +01:00
PhpParser\Node\Stmt\Throw_ $stmt,
Context $context
) {
2018-11-06 03:57:36 +01:00
if (ExpressionAnalyzer::analyze($statements_checker, $stmt->expr, $context) === false) {
2018-01-14 18:09:40 +01:00
return false;
}
if ($context->check_classes && isset($stmt->expr->inferredType) && !$stmt->expr->inferredType->isMixed()) {
$throw_type = $stmt->expr->inferredType;
$exception_type = new Union([new TNamedObject('Exception'), new TNamedObject('Throwable')]);
2018-11-06 03:57:36 +01:00
$file_checker = $statements_checker->getFileAnalyzer();
$codebase = $statements_checker->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',
new CodeLocation($file_checker, $stmt),
(string) $throw_type_part
),
$statements_checker->getSuppressedIssues()
)) {
return false;
}
} elseif ($context->collect_exceptions) {
foreach ($throw_type->getTypes() as $throw_atomic_type) {
if ($throw_atomic_type instanceof TNamedObject) {
$context->possibly_thrown_exceptions[$throw_atomic_type->value] = true;
}
}
}
2018-01-14 18:09:40 +01:00
}
}
}
}