1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-09 06:28:36 +01:00
psalm/src/Psalm/Internal/Analyzer/Statements/Expression/ExitAnalyzer.php

149 lines
4.8 KiB
PHP
Raw Normal View History

2020-06-25 23:17:08 +02:00
<?php
2020-06-25 23:17:08 +02:00
namespace Psalm\Internal\Analyzer\Statements\Expression;
2021-10-31 02:02:16 +02:00
use PhpParser\Node\Expr\Exit_;
2021-06-08 04:55:21 +02:00
use Psalm\CodeLocation;
use Psalm\Context;
use Psalm\Internal\Analyzer\FunctionLikeAnalyzer;
2020-06-25 23:17:08 +02:00
use Psalm\Internal\Analyzer\Statements\Expression\Call\ArgumentAnalyzer;
2021-06-08 04:55:21 +02:00
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
2020-06-25 23:17:08 +02:00
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Internal\Codebase\TaintFlowGraph;
2021-06-08 04:55:21 +02:00
use Psalm\Internal\DataFlow\TaintSink;
2021-10-31 02:02:16 +02:00
use Psalm\Issue\ForbiddenCode;
use Psalm\Issue\ImpureFunctionCall;
use Psalm\IssueBuffer;
2020-06-25 23:17:08 +02:00
use Psalm\Storage\FunctionLikeParameter;
use Psalm\Type;
use Psalm\Type\Atomic\TInt;
use Psalm\Type\Atomic\TString;
2021-12-13 16:28:14 +01:00
use Psalm\Type\TaintKind;
use Psalm\Type\Union;
2020-06-25 23:17:08 +02:00
2022-01-03 07:55:32 +01:00
/**
* @internal
*/
2020-06-25 23:17:08 +02:00
class ExitAnalyzer
{
public static function analyze(
StatementsAnalyzer $statements_analyzer,
2021-10-31 02:02:16 +02:00
Exit_ $stmt,
2020-06-25 23:17:08 +02:00
Context $context
): bool {
$expr_type = null;
2021-10-31 02:02:16 +02:00
$config = $statements_analyzer->getProjectAnalyzer()->getConfig();
$forbidden = null;
if (isset($config->forbidden_functions['exit'])
&& $stmt->getAttribute('kind') === Exit_::KIND_EXIT
) {
$forbidden = 'exit';
} elseif (isset($config->forbidden_functions['die'])
&& $stmt->getAttribute('kind') === Exit_::KIND_DIE
) {
$forbidden = 'die';
}
if ($forbidden) {
IssueBuffer::maybeAdd(
2021-10-31 02:02:16 +02:00
new ForbiddenCode(
'You have forbidden the use of ' . $forbidden,
2022-12-18 17:15:15 +01:00
new CodeLocation($statements_analyzer, $stmt),
2021-10-31 02:02:16 +02:00
),
2022-12-18 17:15:15 +01:00
$statements_analyzer->getSuppressedIssues(),
);
2021-10-31 02:02:16 +02:00
}
2020-06-25 23:17:08 +02:00
if ($stmt->expr) {
$context->inside_call = true;
if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->expr, $context) === false) {
return false;
}
if ($statements_analyzer->data_flow_graph instanceof TaintFlowGraph) {
2020-06-25 23:17:08 +02:00
$call_location = new CodeLocation($statements_analyzer->getSource(), $stmt);
$echo_param_sink = TaintSink::getForMethodArgument(
2020-06-25 23:17:08 +02:00
'exit',
'exit',
0,
null,
2022-12-18 17:15:15 +01:00
$call_location,
2020-06-25 23:17:08 +02:00
);
$echo_param_sink->taints = [
2021-12-13 16:28:14 +01:00
TaintKind::INPUT_HTML,
TaintKind::INPUT_HAS_QUOTES,
TaintKind::USER_SECRET,
2022-12-18 17:15:15 +01:00
TaintKind::SYSTEM_SECRET,
2020-06-25 23:17:08 +02:00
];
$statements_analyzer->data_flow_graph->addSink($echo_param_sink);
2020-06-25 23:17:08 +02:00
}
if ($expr_type = $statements_analyzer->node_data->getType($stmt->expr)) {
$exit_param = new FunctionLikeParameter(
'var',
2022-12-18 17:15:15 +01:00
false,
2020-06-25 23:17:08 +02:00
);
if (ArgumentAnalyzer::verifyType(
$statements_analyzer,
$expr_type,
2021-12-13 16:28:14 +01:00
new Union([new TInt(), new TString()]),
2020-06-25 23:17:08 +02:00
null,
'exit',
null,
2020-06-25 23:17:08 +02:00
0,
new CodeLocation($statements_analyzer->getSource(), $stmt->expr),
$stmt->expr,
$context,
$exit_param,
false,
null,
true,
true,
2022-12-18 17:15:15 +01:00
new CodeLocation($statements_analyzer, $stmt),
2020-06-25 23:17:08 +02:00
) === false) {
return false;
}
}
$context->inside_call = false;
}
if ($expr_type
&& !$expr_type->isInt()
&& !$context->collect_mutations
&& !$context->collect_initializations
) {
if ($context->mutation_free || $context->external_mutation_free) {
2021-10-31 02:02:16 +02:00
$function_name = $stmt->getAttribute('kind') === Exit_::KIND_DIE ? 'die' : 'exit';
IssueBuffer::maybeAdd(
new ImpureFunctionCall(
'Cannot call ' . $function_name . ' with a non-integer argument from a mutation-free context',
2022-12-18 17:15:15 +01:00
new CodeLocation($statements_analyzer, $stmt),
),
2022-12-18 17:15:15 +01:00
$statements_analyzer->getSuppressedIssues(),
);
} elseif ($statements_analyzer->getSource() instanceof FunctionLikeAnalyzer
&& $statements_analyzer->getSource()->track_mutations
) {
$statements_analyzer->getSource()->inferred_has_mutation = true;
$statements_analyzer->getSource()->inferred_impure = true;
}
}
2021-10-13 19:37:47 +02:00
$statements_analyzer->node_data->setType($stmt, Type::getNever());
$context->has_returned = true;
2020-06-25 23:17:08 +02:00
return true;
}
}