2020-05-18 21:13:27 +02:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Internal\Analyzer\Statements\Expression;
|
|
|
|
|
|
|
|
use PhpParser;
|
|
|
|
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\StatementsAnalyzer;
|
2020-06-29 15:13:19 +02:00
|
|
|
use Psalm\Internal\Analyzer\Statements\Expression\Call\Method\MethodCallReturnTypeFetcher;
|
2020-05-18 21:13:27 +02:00
|
|
|
use Psalm\CodeLocation;
|
|
|
|
use Psalm\Context;
|
|
|
|
use Psalm\Issue\InvalidCast;
|
|
|
|
use Psalm\Issue\PossiblyInvalidCast;
|
2020-12-01 23:25:45 +01:00
|
|
|
use Psalm\Issue\RedundantCast;
|
|
|
|
use Psalm\Issue\RedundantCastGivenDocblockType;
|
2020-05-18 21:13:27 +02:00
|
|
|
use Psalm\Issue\UnrecognizedExpression;
|
|
|
|
use Psalm\IssueBuffer;
|
|
|
|
use Psalm\Type;
|
2020-08-30 17:44:14 +02:00
|
|
|
use Psalm\Type\Atomic\TKeyedArray;
|
2020-05-18 21:13:27 +02:00
|
|
|
use Psalm\Type\Atomic\Scalar;
|
|
|
|
use Psalm\Type\Atomic\TArray;
|
2020-05-18 22:02:10 +02:00
|
|
|
use Psalm\Type\Atomic\TFloat;
|
|
|
|
use Psalm\Type\Atomic\TInt;
|
2020-05-18 21:13:27 +02:00
|
|
|
use Psalm\Type\Atomic\TList;
|
|
|
|
use Psalm\Type\Atomic\TMixed;
|
|
|
|
use Psalm\Type\Atomic\TNamedObject;
|
|
|
|
use Psalm\Type\Atomic\TNull;
|
|
|
|
use Psalm\Type\Atomic\TString;
|
2020-11-22 00:11:29 +01:00
|
|
|
use Psalm\Internal\Type\TypeCombiner;
|
2020-05-18 21:13:27 +02:00
|
|
|
use function get_class;
|
|
|
|
use function count;
|
|
|
|
use function array_merge;
|
|
|
|
use function array_values;
|
|
|
|
use function current;
|
|
|
|
|
|
|
|
class CastAnalyzer
|
|
|
|
{
|
|
|
|
public static function analyze(
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
|
|
|
PhpParser\Node\Expr\Cast $stmt,
|
|
|
|
Context $context
|
|
|
|
) : bool {
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\Cast\Int_) {
|
|
|
|
if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->expr, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$as_int = true;
|
|
|
|
$maybe_type = $statements_analyzer->node_data->getType($stmt->expr);
|
|
|
|
|
2020-09-30 18:28:13 +02:00
|
|
|
if ($maybe_type) {
|
2020-11-25 18:04:48 +01:00
|
|
|
if ($maybe_type->isInt()) {
|
|
|
|
if ($maybe_type->from_docblock) {
|
2020-12-01 23:25:45 +01:00
|
|
|
$issue = new RedundantCastGivenDocblockType(
|
2020-11-25 18:04:48 +01:00
|
|
|
'Redundant cast to ' . $maybe_type->getKey(),
|
2020-12-01 23:25:45 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
2020-11-25 18:04:48 +01:00
|
|
|
);
|
|
|
|
} else {
|
2020-12-01 23:25:45 +01:00
|
|
|
$issue = new RedundantCast(
|
2020-11-25 18:04:48 +01:00
|
|
|
'Redundant cast to ' . $maybe_type->getKey(),
|
2020-12-01 23:25:45 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
2020-11-25 18:04:48 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IssueBuffer::accepts($issue, $statements_analyzer->getSuppressedIssues())) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-18 21:13:27 +02:00
|
|
|
$maybe = $maybe_type->getAtomicTypes();
|
|
|
|
|
2020-09-30 18:28:13 +02:00
|
|
|
if (count($maybe) === 1 && current($maybe) instanceof Type\Atomic\TBool) {
|
2020-05-18 21:13:27 +02:00
|
|
|
$as_int = false;
|
|
|
|
$statements_analyzer->node_data->setType($stmt, new Type\Union([
|
|
|
|
new Type\Atomic\TLiteralInt(0),
|
|
|
|
new Type\Atomic\TLiteralInt(1),
|
|
|
|
]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($as_int) {
|
2020-09-30 18:28:13 +02:00
|
|
|
$type = Type::getInt();
|
|
|
|
|
2020-10-13 23:28:12 +02:00
|
|
|
if ($statements_analyzer->data_flow_graph
|
|
|
|
&& $statements_analyzer->data_flow_graph instanceof \Psalm\Internal\Codebase\VariableUseGraph
|
2020-09-30 18:28:13 +02:00
|
|
|
) {
|
|
|
|
$type->parent_nodes = $maybe_type ? $maybe_type->parent_nodes : [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$statements_analyzer->node_data->setType($stmt, $type);
|
2020-05-18 21:13:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\Cast\Double) {
|
|
|
|
if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->expr, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-28 19:06:05 +01:00
|
|
|
$maybe_type = $statements_analyzer->node_data->getType($stmt->expr);
|
|
|
|
|
2020-11-25 18:04:48 +01:00
|
|
|
if ($maybe_type) {
|
|
|
|
if ($maybe_type->isFloat()) {
|
|
|
|
if ($maybe_type->from_docblock) {
|
2020-12-01 23:25:45 +01:00
|
|
|
$issue = new RedundantCastGivenDocblockType(
|
2020-11-25 18:04:48 +01:00
|
|
|
'Redundant cast to ' . $maybe_type->getKey(),
|
2020-12-01 23:25:45 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
2020-11-25 18:04:48 +01:00
|
|
|
);
|
|
|
|
} else {
|
2020-12-01 23:25:45 +01:00
|
|
|
$issue = new RedundantCast(
|
2020-11-25 18:04:48 +01:00
|
|
|
'Redundant cast to ' . $maybe_type->getKey(),
|
2020-12-01 23:25:45 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
2020-11-25 18:04:48 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IssueBuffer::accepts($issue, $statements_analyzer->getSuppressedIssues())) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-28 19:06:05 +01:00
|
|
|
$type = Type::getFloat();
|
|
|
|
|
|
|
|
if ($statements_analyzer->data_flow_graph
|
|
|
|
&& $statements_analyzer->data_flow_graph instanceof \Psalm\Internal\Codebase\VariableUseGraph
|
|
|
|
) {
|
|
|
|
$type->parent_nodes = $maybe_type ? $maybe_type->parent_nodes : [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$statements_analyzer->node_data->setType($stmt, $type);
|
2020-05-18 21:13:27 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\Cast\Bool_) {
|
|
|
|
if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->expr, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-28 19:06:05 +01:00
|
|
|
$maybe_type = $statements_analyzer->node_data->getType($stmt->expr);
|
|
|
|
|
2020-11-25 18:04:48 +01:00
|
|
|
if ($maybe_type) {
|
|
|
|
if ($maybe_type->isBool()) {
|
|
|
|
if ($maybe_type->from_docblock) {
|
2020-12-01 23:25:45 +01:00
|
|
|
$issue = new RedundantCastGivenDocblockType(
|
2020-11-25 18:04:48 +01:00
|
|
|
'Redundant cast to ' . $maybe_type->getKey(),
|
2020-12-01 23:25:45 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
2020-11-25 18:04:48 +01:00
|
|
|
);
|
|
|
|
} else {
|
2020-12-01 23:25:45 +01:00
|
|
|
$issue = new RedundantCast(
|
2020-11-25 18:04:48 +01:00
|
|
|
'Redundant cast to ' . $maybe_type->getKey(),
|
2020-12-01 23:25:45 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
2020-11-25 18:04:48 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IssueBuffer::accepts($issue, $statements_analyzer->getSuppressedIssues())) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-28 19:06:05 +01:00
|
|
|
$type = Type::getBool();
|
|
|
|
|
|
|
|
if ($statements_analyzer->data_flow_graph
|
|
|
|
&& $statements_analyzer->data_flow_graph instanceof \Psalm\Internal\Codebase\VariableUseGraph
|
|
|
|
) {
|
|
|
|
$type->parent_nodes = $maybe_type ? $maybe_type->parent_nodes : [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$statements_analyzer->node_data->setType($stmt, $type);
|
2020-05-18 21:13:27 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\Cast\String_) {
|
|
|
|
if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->expr, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-29 15:13:19 +02:00
|
|
|
$stmt_expr_type = $statements_analyzer->node_data->getType($stmt->expr);
|
|
|
|
|
|
|
|
if ($stmt_expr_type) {
|
2020-11-25 18:04:48 +01:00
|
|
|
if ($stmt_expr_type->isString()) {
|
|
|
|
if ($stmt_expr_type->from_docblock) {
|
2020-12-01 23:25:45 +01:00
|
|
|
$issue = new RedundantCastGivenDocblockType(
|
2020-11-25 18:04:48 +01:00
|
|
|
'Redundant cast to ' . $stmt_expr_type->getKey(),
|
2020-12-01 23:25:45 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
2020-11-25 18:04:48 +01:00
|
|
|
);
|
|
|
|
} else {
|
2020-12-01 23:25:45 +01:00
|
|
|
$issue = new RedundantCast(
|
2020-11-25 18:04:48 +01:00
|
|
|
'Redundant cast to ' . $stmt_expr_type->getKey(),
|
2020-12-01 23:25:45 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
2020-11-25 18:04:48 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IssueBuffer::accepts($issue, $statements_analyzer->getSuppressedIssues())) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-29 19:21:33 +02:00
|
|
|
$stmt_type = self::castStringAttempt(
|
|
|
|
$statements_analyzer,
|
|
|
|
$context,
|
|
|
|
$stmt_expr_type,
|
|
|
|
$stmt->expr,
|
|
|
|
true
|
|
|
|
);
|
2020-05-18 21:13:27 +02:00
|
|
|
} else {
|
|
|
|
$stmt_type = Type::getString();
|
|
|
|
}
|
|
|
|
|
|
|
|
$statements_analyzer->node_data->setType($stmt, $stmt_type);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\Cast\Object_) {
|
2020-09-30 18:28:13 +02:00
|
|
|
$was_inside_use = $context->inside_use;
|
|
|
|
$context->inside_use = true;
|
2020-05-18 21:13:27 +02:00
|
|
|
if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->expr, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-09-30 18:28:13 +02:00
|
|
|
$context->inside_use = $was_inside_use;
|
2020-05-18 21:13:27 +02:00
|
|
|
|
2020-10-28 19:06:05 +01:00
|
|
|
$type = new Type\Union([new TNamedObject('stdClass')]);
|
|
|
|
|
|
|
|
$maybe_type = $statements_analyzer->node_data->getType($stmt->expr);
|
|
|
|
|
|
|
|
if ($statements_analyzer->data_flow_graph
|
|
|
|
&& $statements_analyzer->data_flow_graph instanceof \Psalm\Internal\Codebase\VariableUseGraph
|
|
|
|
) {
|
|
|
|
$type->parent_nodes = $maybe_type ? $maybe_type->parent_nodes : [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$statements_analyzer->node_data->setType($stmt, $type);
|
2020-05-18 21:13:27 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\Cast\Array_) {
|
2020-09-30 18:28:13 +02:00
|
|
|
$was_inside_use = $context->inside_use;
|
|
|
|
$context->inside_use = true;
|
2020-05-18 21:13:27 +02:00
|
|
|
if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->expr, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-09-30 18:28:13 +02:00
|
|
|
$context->inside_use = $was_inside_use;
|
2020-05-18 21:13:27 +02:00
|
|
|
|
|
|
|
$permissible_atomic_types = [];
|
|
|
|
$all_permissible = false;
|
|
|
|
|
|
|
|
if ($stmt_expr_type = $statements_analyzer->node_data->getType($stmt->expr)) {
|
2020-11-25 18:04:48 +01:00
|
|
|
if ($stmt_expr_type->isArray()) {
|
|
|
|
if ($stmt_expr_type->from_docblock) {
|
2020-12-01 23:25:45 +01:00
|
|
|
$issue = new RedundantCastGivenDocblockType(
|
2020-11-25 18:04:48 +01:00
|
|
|
'Redundant cast to ' . $stmt_expr_type->getKey(),
|
2020-12-01 23:25:45 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
2020-11-25 18:04:48 +01:00
|
|
|
);
|
|
|
|
} else {
|
2020-12-01 23:25:45 +01:00
|
|
|
$issue = new RedundantCast(
|
2020-11-25 18:04:48 +01:00
|
|
|
'Redundant cast to ' . $stmt_expr_type->getKey(),
|
2020-12-01 23:25:45 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
2020-11-25 18:04:48 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IssueBuffer::accepts($issue, $statements_analyzer->getSuppressedIssues())) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-18 21:13:27 +02:00
|
|
|
$all_permissible = true;
|
|
|
|
|
|
|
|
foreach ($stmt_expr_type->getAtomicTypes() as $type) {
|
|
|
|
if ($type instanceof Scalar) {
|
2020-08-30 17:44:14 +02:00
|
|
|
$permissible_atomic_types[] = new TKeyedArray([new Type\Union([$type])]);
|
2020-05-18 21:13:27 +02:00
|
|
|
} elseif ($type instanceof TNull) {
|
|
|
|
$permissible_atomic_types[] = new TArray([Type::getEmpty(), Type::getEmpty()]);
|
|
|
|
} elseif ($type instanceof TArray
|
|
|
|
|| $type instanceof TList
|
2020-08-30 17:44:14 +02:00
|
|
|
|| $type instanceof TKeyedArray
|
2020-05-18 21:13:27 +02:00
|
|
|
) {
|
|
|
|
$permissible_atomic_types[] = clone $type;
|
|
|
|
} else {
|
|
|
|
$all_permissible = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($permissible_atomic_types && $all_permissible) {
|
2020-11-22 00:11:29 +01:00
|
|
|
$type = TypeCombiner::combine($permissible_atomic_types);
|
2020-05-18 21:13:27 +02:00
|
|
|
} else {
|
2020-10-28 19:06:05 +01:00
|
|
|
$type = Type::getArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($statements_analyzer->data_flow_graph
|
|
|
|
&& $statements_analyzer->data_flow_graph instanceof \Psalm\Internal\Codebase\VariableUseGraph
|
|
|
|
) {
|
|
|
|
$type->parent_nodes = $stmt_expr_type ? $stmt_expr_type->parent_nodes : [];
|
2020-05-18 21:13:27 +02:00
|
|
|
}
|
|
|
|
|
2020-10-28 19:06:05 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, $type);
|
|
|
|
|
2020-05-18 21:13:27 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-10-04 04:59:43 +02:00
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\Cast\Unset_
|
|
|
|
&& $statements_analyzer->getCodebase()->php_major_version < 8
|
|
|
|
) {
|
2020-05-18 21:13:27 +02:00
|
|
|
if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->expr, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$statements_analyzer->node_data->setType($stmt, Type::getNull());
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UnrecognizedExpression(
|
|
|
|
'Psalm does not understand the cast ' . get_class($stmt),
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function castStringAttempt(
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
|
|
|
Context $context,
|
2020-06-29 19:21:33 +02:00
|
|
|
Type\Union $stmt_type,
|
2020-05-18 21:13:27 +02:00
|
|
|
PhpParser\Node\Expr $stmt,
|
|
|
|
bool $explicit_cast = false
|
|
|
|
) : Type\Union {
|
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
|
|
|
|
|
|
|
$invalid_casts = [];
|
|
|
|
$valid_strings = [];
|
|
|
|
$castable_types = [];
|
|
|
|
|
|
|
|
$atomic_types = $stmt_type->getAtomicTypes();
|
|
|
|
|
2020-06-29 15:13:19 +02:00
|
|
|
$parent_nodes = [];
|
|
|
|
|
2020-10-13 23:28:12 +02:00
|
|
|
if ($statements_analyzer->data_flow_graph) {
|
2020-09-30 18:28:13 +02:00
|
|
|
$parent_nodes = $stmt_type->parent_nodes;
|
|
|
|
}
|
|
|
|
|
2020-05-18 21:13:27 +02:00
|
|
|
while ($atomic_types) {
|
|
|
|
$atomic_type = \array_pop($atomic_types);
|
|
|
|
|
|
|
|
if ($atomic_type instanceof TFloat
|
|
|
|
|| $atomic_type instanceof TInt
|
|
|
|
|| $atomic_type instanceof Type\Atomic\TNumeric
|
|
|
|
) {
|
|
|
|
$castable_types[] = new Type\Atomic\TNumericString();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($atomic_type instanceof TString) {
|
|
|
|
$valid_strings[] = $atomic_type;
|
2020-06-29 15:29:19 +02:00
|
|
|
|
2020-05-18 21:13:27 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-05-19 20:53:06 +02:00
|
|
|
if ($atomic_type instanceof TNull
|
2020-05-19 20:58:53 +02:00
|
|
|
|| $atomic_type instanceof Type\Atomic\TFalse
|
2020-05-19 20:53:06 +02:00
|
|
|
) {
|
2020-05-19 20:58:53 +02:00
|
|
|
$valid_strings[] = new Type\Atomic\TLiteralString('');
|
2020-05-19 20:53:06 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-05-18 21:13:27 +02:00
|
|
|
if ($atomic_type instanceof TMixed
|
|
|
|
|| $atomic_type instanceof Type\Atomic\TResource
|
|
|
|
|| $atomic_type instanceof Type\Atomic\Scalar
|
|
|
|
) {
|
|
|
|
$castable_types[] = new TString();
|
2020-06-29 15:13:19 +02:00
|
|
|
|
2020-05-18 21:13:27 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($atomic_type instanceof TNamedObject
|
|
|
|
|| $atomic_type instanceof Type\Atomic\TObjectWithProperties
|
|
|
|
) {
|
|
|
|
$intersection_types = [$atomic_type];
|
|
|
|
|
|
|
|
if ($atomic_type->extra_types) {
|
|
|
|
$intersection_types = array_merge($intersection_types, $atomic_type->extra_types);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($intersection_types as $intersection_type) {
|
|
|
|
if ($intersection_type instanceof TNamedObject) {
|
|
|
|
$intersection_method_id = new \Psalm\Internal\MethodIdentifier(
|
|
|
|
$intersection_type->value,
|
|
|
|
'__tostring'
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($codebase->methods->methodExists(
|
|
|
|
$intersection_method_id,
|
|
|
|
$context->calling_method_id,
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
|
|
|
)) {
|
|
|
|
$return_type = $codebase->methods->getMethodReturnType(
|
|
|
|
$intersection_method_id,
|
|
|
|
$self_class
|
2020-06-29 15:13:19 +02:00
|
|
|
) ?: Type::getString();
|
|
|
|
|
2020-06-29 18:11:11 +02:00
|
|
|
$declaring_method_id = $codebase->methods->getDeclaringMethodId($intersection_method_id);
|
|
|
|
|
2020-06-29 15:13:19 +02:00
|
|
|
MethodCallReturnTypeFetcher::taintMethodCallResult(
|
|
|
|
$statements_analyzer,
|
|
|
|
$return_type,
|
|
|
|
$stmt,
|
|
|
|
$stmt,
|
|
|
|
$intersection_method_id,
|
2020-06-29 18:11:11 +02:00
|
|
|
$declaring_method_id,
|
2020-06-29 15:13:19 +02:00
|
|
|
$intersection_type->value . '::__toString',
|
|
|
|
$context
|
2020-05-18 21:13:27 +02:00
|
|
|
);
|
|
|
|
|
2020-10-13 23:28:12 +02:00
|
|
|
if ($statements_analyzer->data_flow_graph) {
|
2020-09-28 06:45:02 +02:00
|
|
|
$parent_nodes = array_merge($return_type->parent_nodes, $parent_nodes);
|
2020-06-29 15:29:19 +02:00
|
|
|
}
|
2020-06-29 15:13:19 +02:00
|
|
|
|
|
|
|
$castable_types = array_merge(
|
|
|
|
$castable_types,
|
|
|
|
array_values($return_type->getAtomicTypes())
|
|
|
|
);
|
2020-05-18 21:13:27 +02:00
|
|
|
|
|
|
|
continue 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($intersection_type instanceof Type\Atomic\TObjectWithProperties
|
|
|
|
&& isset($intersection_type->methods['__toString'])
|
|
|
|
) {
|
|
|
|
$castable_types[] = new TString();
|
|
|
|
|
|
|
|
continue 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($atomic_type instanceof Type\Atomic\TTemplateParam) {
|
|
|
|
$atomic_types = array_merge($atomic_types, $atomic_type->as->getAtomicTypes());
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$invalid_casts[] = $atomic_type->getId();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($invalid_casts) {
|
|
|
|
if ($valid_strings || $castable_types) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new PossiblyInvalidCast(
|
|
|
|
$invalid_casts[0] . ' cannot be cast to string',
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidCast(
|
|
|
|
$invalid_casts[0] . ' cannot be cast to string',
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} elseif ($explicit_cast && !$castable_types) {
|
|
|
|
// todo: emit error here
|
|
|
|
}
|
|
|
|
|
|
|
|
$valid_types = array_merge($valid_strings, $castable_types);
|
|
|
|
|
|
|
|
if (!$valid_types) {
|
2020-06-29 15:13:19 +02:00
|
|
|
$str_type = Type::getString();
|
|
|
|
} else {
|
2020-11-22 00:11:29 +01:00
|
|
|
$str_type = \Psalm\Internal\Type\TypeCombiner::combine(
|
2020-06-29 15:13:19 +02:00
|
|
|
$valid_types,
|
|
|
|
$codebase
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-10-13 23:28:12 +02:00
|
|
|
if ($statements_analyzer->data_flow_graph) {
|
2020-06-29 15:13:19 +02:00
|
|
|
$str_type->parent_nodes = $parent_nodes;
|
2020-05-18 21:13:27 +02:00
|
|
|
}
|
|
|
|
|
2020-06-29 15:13:19 +02:00
|
|
|
return $str_type;
|
2020-05-18 21:13:27 +02:00
|
|
|
}
|
|
|
|
}
|