2020-05-18 21:13:27 +02:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Internal\Analyzer\Statements\Expression;
|
|
|
|
|
|
|
|
use PhpParser;
|
2021-06-08 04:55:21 +02:00
|
|
|
use Psalm\CodeLocation;
|
|
|
|
use Psalm\Context;
|
2021-07-27 14:22:06 +02:00
|
|
|
use Psalm\Internal\Analyzer\FunctionLikeAnalyzer;
|
2020-05-18 21:13:27 +02:00
|
|
|
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\StatementsAnalyzer;
|
2020-09-30 18:28:13 +02:00
|
|
|
use Psalm\Internal\Codebase\TaintFlowGraph;
|
2021-06-08 04:55:21 +02:00
|
|
|
use Psalm\Internal\DataFlow\TaintSink;
|
2020-05-18 21:13:27 +02:00
|
|
|
use Psalm\Issue\ForbiddenCode;
|
2021-07-27 14:22:06 +02:00
|
|
|
use Psalm\Issue\ImpureFunctionCall;
|
2020-05-18 21:13:27 +02:00
|
|
|
use Psalm\IssueBuffer;
|
|
|
|
use Psalm\Storage\FunctionLikeParameter;
|
|
|
|
use Psalm\Type;
|
|
|
|
|
|
|
|
class PrintAnalyzer
|
|
|
|
{
|
|
|
|
public static function analyze(
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
|
|
|
PhpParser\Node\Expr\Print_ $stmt,
|
|
|
|
Context $context
|
|
|
|
) : bool {
|
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
|
|
|
|
|
|
|
if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->expr, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-13 23:28:12 +02:00
|
|
|
if ($statements_analyzer->data_flow_graph instanceof TaintFlowGraph) {
|
2020-06-24 23:23:16 +02:00
|
|
|
$call_location = new CodeLocation($statements_analyzer->getSource(), $stmt);
|
|
|
|
|
2020-09-21 05:59:52 +02:00
|
|
|
$print_param_sink = TaintSink::getForMethodArgument(
|
2020-06-24 23:23:16 +02:00
|
|
|
'print',
|
|
|
|
'print',
|
|
|
|
0,
|
|
|
|
null,
|
|
|
|
$call_location
|
|
|
|
);
|
|
|
|
|
|
|
|
$print_param_sink->taints = [
|
|
|
|
Type\TaintKind::INPUT_HTML,
|
2021-06-10 23:43:04 +02:00
|
|
|
Type\TaintKind::INPUT_HAS_QUOTES,
|
2020-06-24 23:23:16 +02:00
|
|
|
Type\TaintKind::USER_SECRET,
|
|
|
|
Type\TaintKind::SYSTEM_SECRET
|
|
|
|
];
|
|
|
|
|
2020-10-13 23:28:12 +02:00
|
|
|
$statements_analyzer->data_flow_graph->addSink($print_param_sink);
|
2020-06-24 23:23:16 +02:00
|
|
|
}
|
|
|
|
|
2020-05-18 21:13:27 +02:00
|
|
|
if ($stmt_expr_type = $statements_analyzer->node_data->getType($stmt->expr)) {
|
2020-05-19 04:57:00 +02:00
|
|
|
if (Call\ArgumentAnalyzer::verifyType(
|
2020-05-18 21:13:27 +02:00
|
|
|
$statements_analyzer,
|
|
|
|
$stmt_expr_type,
|
|
|
|
Type::getString(),
|
|
|
|
null,
|
|
|
|
'print',
|
2020-11-18 15:59:54 +01:00
|
|
|
null,
|
2020-05-18 21:13:27 +02:00
|
|
|
0,
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt->expr),
|
|
|
|
$stmt->expr,
|
|
|
|
$context,
|
|
|
|
new FunctionLikeParameter('var', false),
|
|
|
|
false,
|
|
|
|
null,
|
2020-05-22 04:47:58 +02:00
|
|
|
true,
|
2020-05-18 21:13:27 +02:00
|
|
|
true,
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
|
|
|
) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($codebase->config->forbidden_functions['print'])) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new ForbiddenCode(
|
|
|
|
'You have forbidden the use of print',
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-27 14:22:06 +02:00
|
|
|
if (!$context->collect_initializations && !$context->collect_mutations) {
|
|
|
|
if ($context->mutation_free || $context->external_mutation_free) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new ImpureFunctionCall(
|
|
|
|
'Cannot call print from a mutation-free context',
|
|
|
|
new CodeLocation($statements_analyzer, $stmt)
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
} elseif ($statements_analyzer->getSource() instanceof FunctionLikeAnalyzer
|
|
|
|
&& $statements_analyzer->getSource()->track_mutations
|
|
|
|
) {
|
|
|
|
$statements_analyzer->getSource()->inferred_has_mutation = true;
|
|
|
|
$statements_analyzer->getSource()->inferred_impure = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-18 21:13:27 +02:00
|
|
|
$statements_analyzer->node_data->setType($stmt, Type::getInt(false, 1));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|