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;
|
2020-05-18 21:13:27 +02:00
|
|
|
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\StatementsAnalyzer;
|
2020-10-13 22:49:03 +02:00
|
|
|
use Psalm\Internal\DataFlow\DataFlowNode;
|
2021-03-20 03:41:41 +01:00
|
|
|
use Psalm\Plugin\EventHandler\Event\AddRemoveTaintsEvent;
|
2020-05-18 21:13:27 +02:00
|
|
|
use Psalm\Type;
|
|
|
|
|
|
|
|
class EncapsulatedStringAnalyzer
|
|
|
|
{
|
|
|
|
public static function analyze(
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
|
|
|
PhpParser\Node\Scalar\Encapsed $stmt,
|
|
|
|
Context $context
|
|
|
|
) : bool {
|
2020-06-23 22:38:59 +02:00
|
|
|
$stmt_type = Type::getString();
|
|
|
|
|
2020-11-11 06:38:26 +01:00
|
|
|
$non_empty = false;
|
|
|
|
|
2021-06-14 21:30:25 +02:00
|
|
|
$all_literals = true;
|
|
|
|
|
2020-05-18 21:13:27 +02:00
|
|
|
foreach ($stmt->parts as $part) {
|
2020-11-11 06:38:26 +01:00
|
|
|
if ($part instanceof PhpParser\Node\Scalar\EncapsedStringPart
|
|
|
|
&& $part->value
|
|
|
|
) {
|
|
|
|
$non_empty = true;
|
|
|
|
}
|
|
|
|
|
2020-05-18 21:13:27 +02:00
|
|
|
if (ExpressionAnalyzer::analyze($statements_analyzer, $part, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-23 22:38:59 +02:00
|
|
|
$part_type = $statements_analyzer->node_data->getType($part);
|
|
|
|
|
|
|
|
if ($part_type) {
|
2020-06-29 19:21:33 +02:00
|
|
|
$casted_part_type = CastAnalyzer::castStringAttempt(
|
|
|
|
$statements_analyzer,
|
|
|
|
$context,
|
|
|
|
$part_type,
|
|
|
|
$part
|
|
|
|
);
|
2020-06-23 22:38:59 +02:00
|
|
|
|
2021-06-14 21:30:25 +02:00
|
|
|
if (!$casted_part_type->allLiterals()) {
|
|
|
|
$all_literals = false;
|
|
|
|
}
|
|
|
|
|
2020-10-13 23:28:12 +02:00
|
|
|
if ($statements_analyzer->data_flow_graph
|
2020-07-02 05:23:38 +02:00
|
|
|
&& !\in_array('TaintedInput', $statements_analyzer->getSuppressedIssues())
|
2020-06-23 22:38:59 +02:00
|
|
|
) {
|
|
|
|
$var_location = new CodeLocation($statements_analyzer, $part);
|
|
|
|
|
2020-10-13 22:49:03 +02:00
|
|
|
$new_parent_node = DataFlowNode::getForAssignment('concat', $var_location);
|
2020-10-13 23:28:12 +02:00
|
|
|
$statements_analyzer->data_flow_graph->addNode($new_parent_node);
|
2020-06-23 22:38:59 +02:00
|
|
|
|
2020-09-28 06:45:02 +02:00
|
|
|
$stmt_type->parent_nodes[$new_parent_node->id] = $new_parent_node;
|
2020-06-23 22:38:59 +02:00
|
|
|
|
2021-03-20 03:41:41 +01:00
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
|
|
|
$event = new AddRemoveTaintsEvent($stmt, $context, $statements_analyzer, $codebase);
|
|
|
|
|
|
|
|
$added_taints = $codebase->config->eventDispatcher->dispatchAddTaints($event);
|
|
|
|
$removed_taints = $codebase->config->eventDispatcher->dispatchRemoveTaints($event);
|
|
|
|
|
2020-06-29 15:13:19 +02:00
|
|
|
if ($casted_part_type->parent_nodes) {
|
|
|
|
foreach ($casted_part_type->parent_nodes as $parent_node) {
|
2021-03-20 03:41:41 +01:00
|
|
|
$statements_analyzer->data_flow_graph->addPath(
|
|
|
|
$parent_node,
|
|
|
|
$new_parent_node,
|
|
|
|
'concat',
|
|
|
|
$added_taints,
|
|
|
|
$removed_taints
|
|
|
|
);
|
2020-06-23 22:38:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-18 21:13:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-11 06:38:26 +01:00
|
|
|
if ($non_empty) {
|
2021-06-14 21:30:25 +02:00
|
|
|
if ($all_literals) {
|
|
|
|
$new_type = new Type\Union([new Type\Atomic\TNonspecificNonEmptyLiteralString()]);
|
|
|
|
} else {
|
|
|
|
$new_type = new Type\Union([new Type\Atomic\TNonEmptyString()]);
|
|
|
|
}
|
|
|
|
|
2020-11-11 06:38:26 +01:00
|
|
|
$new_type->parent_nodes = $stmt_type->parent_nodes;
|
|
|
|
$stmt_type = $new_type;
|
|
|
|
}
|
|
|
|
|
2020-06-23 22:38:59 +02:00
|
|
|
$statements_analyzer->node_data->setType($stmt, $stmt_type);
|
2020-05-18 21:13:27 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|