2016-10-22 19:23:18 +02:00
|
|
|
<?php
|
2018-11-06 03:57:36 +01:00
|
|
|
namespace Psalm\Internal\Analyzer\Statements;
|
2016-10-22 19:23:18 +02:00
|
|
|
|
|
|
|
use PhpParser;
|
2018-11-06 03:57:36 +01:00
|
|
|
use Psalm\Codebase;
|
2020-03-22 01:46:38 +01:00
|
|
|
use Psalm\Internal\Analyzer\ClassAnalyzer;
|
2018-11-06 03:57:36 +01:00
|
|
|
use Psalm\Internal\Analyzer\ClassLikeAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\ClosureAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\CommentAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\FunctionLikeAnalyzer;
|
2019-02-06 21:52:43 +01:00
|
|
|
use Psalm\Internal\Analyzer\TraitAnalyzer;
|
2018-11-06 03:57:36 +01:00
|
|
|
use Psalm\Internal\Analyzer\Statements\Expression\ArrayAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\Statements\Expression\AssertionFinder;
|
|
|
|
use Psalm\Internal\Analyzer\Statements\Expression\AssignmentAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\Statements\Expression\BinaryOpAnalyzer;
|
2019-11-08 13:01:34 +01:00
|
|
|
use Psalm\Internal\Analyzer\Statements\Expression\CallAnalyzer;
|
2018-11-06 03:57:36 +01:00
|
|
|
use Psalm\Internal\Analyzer\Statements\Expression\Call\FunctionCallAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\Statements\Expression\Call\MethodCallAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\Statements\Expression\Call\NewAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\Statements\Expression\Call\StaticCallAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\Statements\Expression\Fetch\ArrayFetchAnalyzer;
|
2019-09-14 19:12:54 +02:00
|
|
|
use Psalm\Internal\Analyzer\Statements\Expression\Fetch\ClassConstFetchAnalyzer;
|
2018-11-06 03:57:36 +01:00
|
|
|
use Psalm\Internal\Analyzer\Statements\Expression\Fetch\ConstFetchAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\Statements\Expression\Fetch\PropertyFetchAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\Statements\Expression\Fetch\VariableFetchAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\Statements\Expression\IncludeAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\Statements\Expression\TernaryAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\StatementsAnalyzer;
|
2016-12-04 01:11:30 +01:00
|
|
|
use Psalm\CodeLocation;
|
2016-10-22 19:23:18 +02:00
|
|
|
use Psalm\Config;
|
|
|
|
use Psalm\Context;
|
2017-11-15 03:43:31 +01:00
|
|
|
use Psalm\Exception\DocblockParseException;
|
2018-11-12 16:46:55 +01:00
|
|
|
use Psalm\Internal\FileManipulation\FileManipulationBuffer;
|
2018-02-23 21:39:33 +01:00
|
|
|
use Psalm\FileSource;
|
2019-01-30 17:36:21 +01:00
|
|
|
use Psalm\Issue\DuplicateParam;
|
2016-10-22 19:23:18 +02:00
|
|
|
use Psalm\Issue\ForbiddenCode;
|
2019-07-18 07:31:48 +02:00
|
|
|
use Psalm\Issue\ImpurePropertyAssignment;
|
2017-10-07 17:27:54 +02:00
|
|
|
use Psalm\Issue\InvalidCast;
|
2017-01-16 18:59:09 +01:00
|
|
|
use Psalm\Issue\InvalidClone;
|
2017-11-15 03:43:31 +01:00
|
|
|
use Psalm\Issue\InvalidDocblock;
|
2019-04-13 16:11:25 +02:00
|
|
|
use Psalm\Issue\InvalidOperand;
|
2019-03-17 22:10:51 +01:00
|
|
|
use Psalm\Issue\PossiblyInvalidCast;
|
2019-04-13 16:11:25 +02:00
|
|
|
use Psalm\Issue\PossiblyInvalidOperand;
|
2016-10-22 19:23:18 +02:00
|
|
|
use Psalm\Issue\PossiblyUndefinedVariable;
|
2019-01-04 18:28:00 +01:00
|
|
|
use Psalm\Issue\UndefinedConstant;
|
2016-10-22 19:23:18 +02:00
|
|
|
use Psalm\Issue\UndefinedVariable;
|
2019-09-19 17:59:43 +02:00
|
|
|
use Psalm\Issue\UnnecessaryVarAnnotation;
|
2016-11-06 01:53:39 +01:00
|
|
|
use Psalm\Issue\UnrecognizedExpression;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\IssueBuffer;
|
2019-11-08 13:01:34 +01:00
|
|
|
use Psalm\Storage\FunctionLikeParameter;
|
2016-10-22 19:23:18 +02:00
|
|
|
use Psalm\Type;
|
2017-01-15 01:06:58 +01:00
|
|
|
use Psalm\Type\Atomic\ObjectLike;
|
|
|
|
use Psalm\Type\Atomic\Scalar;
|
|
|
|
use Psalm\Type\Atomic\TArray;
|
2017-05-19 06:48:26 +02:00
|
|
|
use Psalm\Type\Atomic\TFloat;
|
2019-02-22 03:40:06 +01:00
|
|
|
use Psalm\Type\Atomic\TTemplateParam;
|
2017-05-19 06:48:26 +02:00
|
|
|
use Psalm\Type\Atomic\TInt;
|
2020-01-11 17:42:09 +01:00
|
|
|
use Psalm\Type\Atomic\TList;
|
2017-01-15 01:06:58 +01:00
|
|
|
use Psalm\Type\Atomic\TMixed;
|
|
|
|
use Psalm\Type\Atomic\TNamedObject;
|
2018-08-21 04:11:01 +02:00
|
|
|
use Psalm\Type\Atomic\TNull;
|
2017-05-19 06:48:26 +02:00
|
|
|
use Psalm\Type\Atomic\TObject;
|
|
|
|
use Psalm\Type\Atomic\TString;
|
2018-11-18 18:41:47 +01:00
|
|
|
use Psalm\Internal\Type\TypeCombination;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function strpos;
|
|
|
|
use function is_string;
|
|
|
|
use function in_array;
|
|
|
|
use function strtolower;
|
|
|
|
use function get_class;
|
|
|
|
use function count;
|
|
|
|
use function implode;
|
|
|
|
use function is_array;
|
|
|
|
use function array_merge;
|
|
|
|
use function array_values;
|
|
|
|
use function array_map;
|
2020-01-04 22:36:19 +01:00
|
|
|
use function current;
|
2016-10-22 19:23:18 +02:00
|
|
|
|
2018-12-02 00:37:49 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
class ExpressionAnalyzer
|
2016-10-22 19:23:18 +02:00
|
|
|
{
|
|
|
|
/**
|
2018-11-11 18:01:14 +01:00
|
|
|
* @param StatementsAnalyzer $statements_analyzer
|
2016-11-02 07:29:00 +01:00
|
|
|
* @param PhpParser\Node\Expr $stmt
|
|
|
|
* @param Context $context
|
|
|
|
* @param bool $array_assignment
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-11-02 07:29:00 +01:00
|
|
|
* @return false|null
|
2016-10-22 19:23:18 +02:00
|
|
|
*/
|
2017-01-07 21:09:47 +01:00
|
|
|
public static function analyze(
|
2018-11-11 18:01:14 +01:00
|
|
|
StatementsAnalyzer $statements_analyzer,
|
2016-10-22 19:23:18 +02:00
|
|
|
PhpParser\Node\Expr $stmt,
|
|
|
|
Context $context,
|
2018-05-23 05:38:27 +02:00
|
|
|
$array_assignment = false,
|
2019-11-25 17:44:54 +01:00
|
|
|
Context $global_context = null,
|
|
|
|
bool $from_stmt = false
|
2016-10-22 19:23:18 +02:00
|
|
|
) {
|
2018-11-11 18:01:14 +01:00
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
2018-11-06 03:57:36 +01:00
|
|
|
|
2016-10-22 19:23:18 +02:00
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\Variable) {
|
2018-11-06 03:57:36 +01:00
|
|
|
if (VariableFetchAnalyzer::analyze(
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer,
|
2018-01-14 18:09:40 +01:00
|
|
|
$stmt,
|
|
|
|
$context,
|
|
|
|
false,
|
|
|
|
null,
|
|
|
|
$array_assignment
|
|
|
|
) === false
|
|
|
|
) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Assign) {
|
2018-11-06 03:57:36 +01:00
|
|
|
$assignment_type = AssignmentAnalyzer::analyze(
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer,
|
2016-11-01 16:37:58 +01:00
|
|
|
$stmt->var,
|
|
|
|
$stmt->expr,
|
2016-12-04 20:14:00 +01:00
|
|
|
null,
|
2016-11-01 16:37:58 +01:00
|
|
|
$context,
|
2019-06-01 17:53:32 +02:00
|
|
|
$stmt->getDocComment()
|
2016-11-01 16:37:58 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
if ($assignment_type === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-11-01 16:37:58 +01:00
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
if (!$from_stmt) {
|
|
|
|
$statements_analyzer->node_data->setType($stmt, $assignment_type);
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\AssignOp) {
|
2018-11-11 18:01:14 +01:00
|
|
|
if (AssignmentAnalyzer::analyzeAssignmentOperation($statements_analyzer, $stmt, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\MethodCall) {
|
2018-11-11 18:01:14 +01:00
|
|
|
if (MethodCallAnalyzer::analyze($statements_analyzer, $stmt, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\StaticCall) {
|
2018-11-11 18:01:14 +01:00
|
|
|
if (StaticCallAnalyzer::analyze($statements_analyzer, $stmt, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\ConstFetch) {
|
2018-11-11 18:01:14 +01:00
|
|
|
ConstFetchAnalyzer::analyze($statements_analyzer, $stmt, $context);
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Scalar\String_) {
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, Type::getString($stmt->value));
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Scalar\EncapsedStringPart) {
|
2016-10-22 19:23:18 +02:00
|
|
|
// do nothing
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Scalar\MagicConst) {
|
2019-06-14 21:54:15 +02:00
|
|
|
if ($stmt instanceof PhpParser\Node\Scalar\MagicConst\Line) {
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, Type::getInt());
|
2019-06-14 21:54:15 +02:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Scalar\MagicConst\Class_) {
|
|
|
|
if (!$context->self) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UndefinedConstant(
|
|
|
|
'Cannot get __class__ outside a class',
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
2019-01-04 18:28:00 +01:00
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, Type::getClassString());
|
2019-06-14 21:54:15 +02:00
|
|
|
} else {
|
|
|
|
if ($codebase->alter_code) {
|
2019-06-14 21:55:23 +02:00
|
|
|
$codebase->classlikes->handleClassLikeReferenceInMigration(
|
2019-06-14 21:54:15 +02:00
|
|
|
$codebase,
|
|
|
|
$statements_analyzer,
|
|
|
|
$stmt,
|
|
|
|
$context->self,
|
2020-03-26 17:35:27 +01:00
|
|
|
$context->calling_method_id
|
2019-06-14 21:54:15 +02:00
|
|
|
);
|
2019-01-04 18:28:00 +01:00
|
|
|
}
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, Type::getLiteralClassString($context->self));
|
2019-06-14 21:54:15 +02:00
|
|
|
}
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Scalar\MagicConst\Namespace_) {
|
|
|
|
$namespace = $statements_analyzer->getNamespace();
|
|
|
|
if ($namespace === null
|
|
|
|
&& IssueBuffer::accepts(
|
2019-02-18 19:14:08 +01:00
|
|
|
new UndefinedConstant(
|
|
|
|
'Cannot get __namespace__ outside a namespace',
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2019-06-14 21:54:15 +02:00
|
|
|
)
|
|
|
|
) {
|
|
|
|
// fall through
|
|
|
|
}
|
2019-02-18 19:14:08 +01:00
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, Type::getString($namespace));
|
2019-11-30 04:47:20 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Scalar\MagicConst\Method
|
|
|
|
|| $stmt instanceof PhpParser\Node\Scalar\MagicConst\Function_
|
|
|
|
) {
|
|
|
|
$source = $statements_analyzer->getSource();
|
|
|
|
if ($source instanceof FunctionLikeAnalyzer) {
|
2020-02-15 02:54:26 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, Type::getString($source->getId()));
|
2019-11-30 04:47:20 +01:00
|
|
|
} else {
|
|
|
|
$statements_analyzer->node_data->setType($stmt, new Type\Union([new Type\Atomic\TCallableString]));
|
|
|
|
}
|
2019-06-14 21:54:15 +02:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Scalar\MagicConst\File
|
|
|
|
|| $stmt instanceof PhpParser\Node\Scalar\MagicConst\Dir
|
|
|
|
|| $stmt instanceof PhpParser\Node\Scalar\MagicConst\Trait_
|
|
|
|
) {
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, Type::getString());
|
2017-12-06 06:05:01 +01:00
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Scalar\LNumber) {
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, Type::getInt(false, $stmt->value));
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Scalar\DNumber) {
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, Type::getFloat($stmt->value));
|
2017-01-26 04:02:19 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\UnaryMinus ||
|
|
|
|
$stmt instanceof PhpParser\Node\Expr\UnaryPlus
|
|
|
|
) {
|
2018-11-11 18:01:14 +01:00
|
|
|
if (self::analyze($statements_analyzer, $stmt->expr, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-11-07 21:29:47 +01:00
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
if (!($stmt_expr_type = $statements_analyzer->node_data->getType($stmt->expr))) {
|
|
|
|
$statements_analyzer->node_data->setType($stmt, new Type\Union([new TInt, new TFloat]));
|
|
|
|
} elseif ($stmt_expr_type->isMixed()) {
|
|
|
|
$statements_analyzer->node_data->setType($stmt, Type::getMixed());
|
2017-01-26 04:02:19 +01:00
|
|
|
} else {
|
2017-01-26 04:23:06 +01:00
|
|
|
$acceptable_types = [];
|
|
|
|
|
2020-01-04 18:20:26 +01:00
|
|
|
foreach ($stmt_expr_type->getAtomicTypes() as $type_part) {
|
2017-01-26 04:23:06 +01:00
|
|
|
if ($type_part instanceof TInt || $type_part instanceof TFloat) {
|
2018-05-13 00:46:47 +02:00
|
|
|
if ($type_part instanceof Type\Atomic\TLiteralInt
|
|
|
|
&& $stmt instanceof PhpParser\Node\Expr\UnaryMinus
|
|
|
|
) {
|
2018-05-18 17:02:50 +02:00
|
|
|
$type_part->value = -$type_part->value;
|
2018-05-13 00:46:47 +02:00
|
|
|
} elseif ($type_part instanceof Type\Atomic\TLiteralFloat
|
|
|
|
&& $stmt instanceof PhpParser\Node\Expr\UnaryMinus
|
|
|
|
) {
|
2018-05-18 17:02:50 +02:00
|
|
|
$type_part->value = -$type_part->value;
|
2018-05-13 00:46:47 +02:00
|
|
|
}
|
|
|
|
|
2017-01-26 04:23:06 +01:00
|
|
|
$acceptable_types[] = $type_part;
|
|
|
|
} elseif ($type_part instanceof TString) {
|
|
|
|
$acceptable_types[] = new TInt;
|
|
|
|
$acceptable_types[] = new TFloat;
|
2017-01-26 05:50:34 +01:00
|
|
|
} else {
|
|
|
|
$acceptable_types[] = new TInt;
|
2017-01-26 04:02:19 +01:00
|
|
|
}
|
2017-01-26 04:23:06 +01:00
|
|
|
}
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, new Type\Union($acceptable_types));
|
2017-01-26 04:02:19 +01:00
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Isset_) {
|
2018-11-11 18:01:14 +01:00
|
|
|
self::analyzeIsset($statements_analyzer, $stmt, $context);
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, Type::getBool());
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\ClassConstFetch) {
|
2019-09-14 19:12:54 +02:00
|
|
|
if (ClassConstFetchAnalyzer::analyze($statements_analyzer, $stmt, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\PropertyFetch) {
|
2018-11-11 18:01:14 +01:00
|
|
|
if (PropertyFetchAnalyzer::analyzeInstance($statements_analyzer, $stmt, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\StaticPropertyFetch) {
|
2018-11-11 18:01:14 +01:00
|
|
|
if (PropertyFetchAnalyzer::analyzeStatic($statements_analyzer, $stmt, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\BitwiseNot) {
|
2018-11-11 18:01:14 +01:00
|
|
|
if (self::analyze($statements_analyzer, $stmt->expr, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
2019-04-13 16:11:25 +02:00
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
if (!($stmt_expr_type = $statements_analyzer->node_data->getType($stmt->expr))) {
|
|
|
|
$statements_analyzer->node_data->setType($stmt, new Type\Union([new TInt(), new TString()]));
|
|
|
|
} elseif ($stmt_expr_type->isMixed()) {
|
|
|
|
$statements_analyzer->node_data->setType($stmt, Type::getMixed());
|
2019-04-13 16:11:25 +02:00
|
|
|
} else {
|
|
|
|
$acceptable_types = [];
|
|
|
|
$unacceptable_type = null;
|
|
|
|
$has_valid_operand = false;
|
|
|
|
|
2020-01-04 18:20:26 +01:00
|
|
|
foreach ($stmt_expr_type->getAtomicTypes() as $type_string => $type_part) {
|
2019-04-13 16:11:25 +02:00
|
|
|
if ($type_part instanceof TInt || $type_part instanceof TString) {
|
|
|
|
if ($type_part instanceof Type\Atomic\TLiteralInt) {
|
|
|
|
$type_part->value = ~$type_part->value;
|
|
|
|
} elseif ($type_part instanceof Type\Atomic\TLiteralString) {
|
|
|
|
$type_part->value = ~$type_part->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
$acceptable_types[] = $type_part;
|
|
|
|
$has_valid_operand = true;
|
|
|
|
} elseif ($type_part instanceof TFloat) {
|
|
|
|
$type_part = ($type_part instanceof Type\Atomic\TLiteralFloat) ?
|
|
|
|
new Type\Atomic\TLiteralInt(~$type_part->value) :
|
|
|
|
new TInt;
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
$stmt_expr_type->removeType($type_string);
|
|
|
|
$stmt_expr_type->addType($type_part);
|
2019-04-13 16:11:25 +02:00
|
|
|
|
|
|
|
$acceptable_types[] = $type_part;
|
|
|
|
$has_valid_operand = true;
|
|
|
|
} elseif (!$unacceptable_type) {
|
|
|
|
$unacceptable_type = $type_part;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-30 05:41:17 +01:00
|
|
|
if ($unacceptable_type || !$acceptable_types) {
|
2019-04-13 16:11:25 +02:00
|
|
|
$message = 'Cannot negate a non-numeric non-string type ' . $unacceptable_type;
|
|
|
|
if ($has_valid_operand) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new PossiblyInvalidOperand(
|
|
|
|
$message,
|
|
|
|
new CodeLocation($statements_analyzer, $stmt)
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidOperand(
|
|
|
|
$message,
|
|
|
|
new CodeLocation($statements_analyzer, $stmt)
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, Type::getMixed());
|
2019-04-13 16:11:25 +02:00
|
|
|
} else {
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, new Type\Union($acceptable_types));
|
2019-04-13 16:11:25 +02:00
|
|
|
}
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\BinaryOp) {
|
2018-11-06 03:57:36 +01:00
|
|
|
if (BinaryOpAnalyzer::analyze(
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer,
|
2017-02-13 00:06:18 +01:00
|
|
|
$stmt,
|
2019-12-16 14:32:00 +01:00
|
|
|
$context,
|
|
|
|
0,
|
|
|
|
$from_stmt
|
2017-02-13 00:06:18 +01:00
|
|
|
) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-12-17 06:48:31 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\PostInc ||
|
|
|
|
$stmt instanceof PhpParser\Node\Expr\PostDec ||
|
|
|
|
$stmt instanceof PhpParser\Node\Expr\PreInc ||
|
|
|
|
$stmt instanceof PhpParser\Node\Expr\PreDec
|
|
|
|
) {
|
2019-08-31 00:06:45 +02:00
|
|
|
$was_inside_assignment = $context->inside_assignment;
|
|
|
|
$context->inside_assignment = true;
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
if (self::analyze($statements_analyzer, $stmt->var, $context) === false) {
|
2019-08-31 00:06:45 +02:00
|
|
|
if (!$was_inside_assignment) {
|
|
|
|
$context->inside_assignment = false;
|
|
|
|
}
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-12-25 00:44:11 +01:00
|
|
|
|
2019-08-31 00:06:45 +02:00
|
|
|
if (!$was_inside_assignment) {
|
|
|
|
$context->inside_assignment = false;
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
if ($stmt_var_type = $statements_analyzer->node_data->getType($stmt->var)) {
|
2018-06-18 19:16:51 +02:00
|
|
|
$return_type = null;
|
|
|
|
|
|
|
|
$fake_right_expr = new PhpParser\Node\Scalar\LNumber(1, $stmt->getAttributes());
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($fake_right_expr, Type::getInt());
|
2018-06-18 19:16:51 +02:00
|
|
|
|
2019-03-25 02:22:52 +01:00
|
|
|
BinaryOpAnalyzer::analyzeNonDivArithmeticOp(
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer,
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data,
|
2018-06-18 19:16:51 +02:00
|
|
|
$stmt->var,
|
|
|
|
$fake_right_expr,
|
|
|
|
$stmt,
|
|
|
|
$return_type,
|
|
|
|
$context
|
|
|
|
);
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
$stmt_type = clone $stmt_var_type;
|
|
|
|
|
|
|
|
$statements_analyzer->node_data->setType($stmt, $stmt_type);
|
|
|
|
$stmt_type->from_calculation = true;
|
2018-04-25 05:12:01 +02:00
|
|
|
|
2020-01-04 18:20:26 +01:00
|
|
|
foreach ($stmt_type->getAtomicTypes() as $atomic_type) {
|
2018-05-08 23:42:02 +02:00
|
|
|
if ($atomic_type instanceof Type\Atomic\TLiteralInt) {
|
2019-11-25 17:44:54 +01:00
|
|
|
$stmt_type->addType(new Type\Atomic\TInt);
|
2018-05-08 23:42:02 +02:00
|
|
|
} elseif ($atomic_type instanceof Type\Atomic\TLiteralFloat) {
|
2019-11-25 17:44:54 +01:00
|
|
|
$stmt_type->addType(new Type\Atomic\TFloat);
|
2018-05-03 19:56:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-25 05:12:01 +02:00
|
|
|
$var_id = self::getArrayVarId($stmt->var, null);
|
|
|
|
|
2019-08-30 18:36:35 +02:00
|
|
|
if ($var_id && $context->mutation_free && strpos($var_id, '->')) {
|
2019-07-18 07:31:48 +02:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new ImpurePropertyAssignment(
|
2019-08-30 18:36:35 +02:00
|
|
|
'Cannot assign to a property from a mutation-free context',
|
2019-07-18 07:31:48 +02:00
|
|
|
new CodeLocation($statements_analyzer, $stmt->var)
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-25 05:12:01 +02:00
|
|
|
if ($var_id && isset($context->vars_in_scope[$var_id])) {
|
2019-11-25 17:44:54 +01:00
|
|
|
$context->vars_in_scope[$var_id] = $stmt_type;
|
2018-06-17 05:40:25 +02:00
|
|
|
|
2020-03-28 21:30:56 +01:00
|
|
|
if ($codebase->find_unused_variables && $stmt->var instanceof PhpParser\Node\Expr\Variable) {
|
2018-11-11 18:01:14 +01:00
|
|
|
$location = new CodeLocation($statements_analyzer, $stmt->var);
|
2018-06-17 05:40:25 +02:00
|
|
|
$context->assigned_var_ids[$var_id] = true;
|
|
|
|
$context->possibly_assigned_var_ids[$var_id] = true;
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->registerVariableAssignment(
|
2018-06-17 05:40:25 +02:00
|
|
|
$var_id,
|
|
|
|
$location
|
|
|
|
);
|
|
|
|
$context->unreferenced_vars[$var_id] = [$location->getHash() => $location];
|
|
|
|
}
|
2020-01-09 17:51:49 +01:00
|
|
|
|
|
|
|
// removes dependent vars from $context
|
|
|
|
$context->removeDescendents(
|
|
|
|
$var_id,
|
|
|
|
$context->vars_in_scope[$var_id],
|
|
|
|
$return_type,
|
|
|
|
$statements_analyzer
|
|
|
|
);
|
2018-04-25 05:12:01 +02:00
|
|
|
}
|
2016-12-25 00:44:11 +01:00
|
|
|
} else {
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, Type::getMixed());
|
2016-12-25 00:44:11 +01:00
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\New_) {
|
2018-11-11 18:01:14 +01:00
|
|
|
if (NewAnalyzer::analyze($statements_analyzer, $stmt, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Array_) {
|
2018-11-11 18:01:14 +01:00
|
|
|
if (ArrayAnalyzer::analyze($statements_analyzer, $stmt, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Scalar\Encapsed) {
|
2018-11-11 18:01:14 +01:00
|
|
|
if (self::analyzeEncapsulatedString($statements_analyzer, $stmt, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\FuncCall) {
|
2018-11-06 03:57:36 +01:00
|
|
|
if (FunctionCallAnalyzer::analyze(
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer,
|
2017-07-29 21:05:06 +02:00
|
|
|
$stmt,
|
|
|
|
$context
|
|
|
|
) === false
|
|
|
|
) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Ternary) {
|
2018-11-11 18:01:14 +01:00
|
|
|
if (TernaryAnalyzer::analyze($statements_analyzer, $stmt, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\BooleanNot) {
|
2018-11-11 18:01:14 +01:00
|
|
|
if (self::analyzeBooleanNot($statements_analyzer, $stmt, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Empty_) {
|
2018-11-11 18:01:14 +01:00
|
|
|
self::analyzeEmpty($statements_analyzer, $stmt, $context);
|
2019-09-01 06:56:46 +02:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Closure
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\ArrowFunction
|
|
|
|
) {
|
2018-11-11 18:01:14 +01:00
|
|
|
$closure_analyzer = new ClosureAnalyzer($stmt, $statements_analyzer);
|
2016-10-22 19:23:18 +02:00
|
|
|
|
2019-09-01 06:56:46 +02:00
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\Closure
|
|
|
|
&& self::analyzeClosureUses($statements_analyzer, $stmt, $context) === false
|
|
|
|
) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-01-17 00:33:04 +01:00
|
|
|
$use_context = new Context($context->self);
|
2019-09-09 18:11:04 +02:00
|
|
|
$use_context->mutation_free = $context->mutation_free;
|
|
|
|
$use_context->external_mutation_free = $context->external_mutation_free;
|
|
|
|
$use_context->pure = $context->pure;
|
2016-10-22 19:23:18 +02:00
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
if (!$statements_analyzer->isStatic()) {
|
2017-01-12 06:54:41 +01:00
|
|
|
if ($context->collect_mutations &&
|
|
|
|
$context->self &&
|
2018-02-01 06:50:01 +01:00
|
|
|
$codebase->classExtends(
|
2017-01-12 06:54:41 +01:00
|
|
|
$context->self,
|
2018-11-11 18:01:14 +01:00
|
|
|
(string)$statements_analyzer->getFQCLN()
|
2017-01-02 21:31:18 +01:00
|
|
|
)
|
2017-01-12 06:54:41 +01:00
|
|
|
) {
|
2019-11-11 16:11:42 +01:00
|
|
|
/** @psalm-suppress PossiblyUndefinedStringArrayOffset */
|
2017-01-12 06:54:41 +01:00
|
|
|
$use_context->vars_in_scope['$this'] = clone $context->vars_in_scope['$this'];
|
|
|
|
} elseif ($context->self) {
|
2017-01-15 01:06:58 +01:00
|
|
|
$use_context->vars_in_scope['$this'] = new Type\Union([new TNamedObject($context->self)]);
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($context->vars_in_scope as $var => $type) {
|
|
|
|
if (strpos($var, '$this->') === 0) {
|
|
|
|
$use_context->vars_in_scope[$var] = clone $type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-22 01:46:38 +01:00
|
|
|
if ($context->self) {
|
|
|
|
$self_class_storage = $codebase->classlike_storage_provider->get($context->self);
|
|
|
|
|
|
|
|
ClassAnalyzer::addContextProperties(
|
|
|
|
$statements_analyzer,
|
|
|
|
$self_class_storage,
|
|
|
|
$use_context,
|
2020-03-22 01:53:46 +01:00
|
|
|
$context->self,
|
2020-03-22 01:46:38 +01:00
|
|
|
$statements_analyzer->getParentFQCLN()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-01-28 18:43:19 +01:00
|
|
|
foreach ($context->vars_possibly_in_scope as $var => $_) {
|
2016-10-22 19:23:18 +02:00
|
|
|
if (strpos($var, '$this->') === 0) {
|
|
|
|
$use_context->vars_possibly_in_scope[$var] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-10 21:36:35 +01:00
|
|
|
$byref_uses = [];
|
|
|
|
|
2019-09-01 06:56:46 +02:00
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\Closure) {
|
|
|
|
foreach ($stmt->uses as $use) {
|
|
|
|
if (!is_string($use->var->name)) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-04-17 18:16:25 +02:00
|
|
|
|
2019-09-01 06:56:46 +02:00
|
|
|
$use_var_id = '$' . $use->var->name;
|
2019-03-10 21:36:35 +01:00
|
|
|
|
2019-09-01 06:56:46 +02:00
|
|
|
if ($use->byRef) {
|
|
|
|
$byref_uses[$use_var_id] = true;
|
|
|
|
}
|
2019-03-10 21:36:35 +01:00
|
|
|
|
2019-09-01 06:56:46 +02:00
|
|
|
// insert the ref into the current context if passed by ref, as whatever we're passing
|
|
|
|
// the closure to could execute it straight away.
|
|
|
|
if (!$context->hasVariable($use_var_id, $statements_analyzer) && $use->byRef) {
|
|
|
|
$context->vars_in_scope[$use_var_id] = Type::getMixed();
|
|
|
|
}
|
|
|
|
|
|
|
|
$use_context->vars_in_scope[$use_var_id] =
|
|
|
|
$context->hasVariable($use_var_id, $statements_analyzer) && !$use->byRef
|
|
|
|
? clone $context->vars_in_scope[$use_var_id]
|
|
|
|
: Type::getMixed();
|
|
|
|
|
|
|
|
$use_context->vars_possibly_in_scope[$use_var_id] = true;
|
2016-10-30 03:17:46 +01:00
|
|
|
}
|
2019-09-01 06:56:46 +02:00
|
|
|
} else {
|
|
|
|
$traverser = new PhpParser\NodeTraverser;
|
2016-10-30 03:17:46 +01:00
|
|
|
|
2020-03-15 04:54:42 +01:00
|
|
|
$short_closure_visitor = new \Psalm\Internal\PhpVisitor\ShortClosureVisitor();
|
2016-10-30 03:17:46 +01:00
|
|
|
|
2019-09-01 06:56:46 +02:00
|
|
|
$traverser->addVisitor($short_closure_visitor);
|
|
|
|
$traverser->traverse($stmt->getStmts());
|
|
|
|
|
|
|
|
foreach ($short_closure_visitor->getUsedVariables() as $use_var_id => $_) {
|
|
|
|
$use_context->vars_in_scope[$use_var_id] =
|
|
|
|
$context->hasVariable($use_var_id, $statements_analyzer)
|
|
|
|
? clone $context->vars_in_scope[$use_var_id]
|
|
|
|
: Type::getMixed();
|
|
|
|
|
|
|
|
$use_context->vars_possibly_in_scope[$use_var_id] = true;
|
|
|
|
}
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
|
2020-03-26 17:35:27 +01:00
|
|
|
$use_context->calling_method_id = $context->calling_method_id;
|
2019-04-16 22:07:48 +02:00
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
$closure_analyzer->analyze($use_context, $statements_analyzer->node_data, $context, false, $byref_uses);
|
2016-10-22 19:23:18 +02:00
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
if (!$statements_analyzer->node_data->getType($stmt)) {
|
|
|
|
$statements_analyzer->node_data->setType($stmt, Type::getClosure());
|
2016-12-07 20:13:39 +01:00
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\ArrayDimFetch) {
|
2018-11-06 03:57:36 +01:00
|
|
|
if (ArrayFetchAnalyzer::analyze(
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer,
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmt,
|
2017-11-19 18:33:43 +01:00
|
|
|
$context
|
2016-11-02 07:29:00 +01:00
|
|
|
) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Cast\Int_) {
|
2018-11-11 18:01:14 +01:00
|
|
|
if (self::analyze($statements_analyzer, $stmt->expr, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-01-04 22:36:19 +01:00
|
|
|
$as_int = true;
|
|
|
|
$maybe_type = $statements_analyzer->node_data->getType($stmt->expr);
|
|
|
|
|
|
|
|
if (null !== $maybe_type) {
|
|
|
|
$maybe = $maybe_type->getAtomicTypes();
|
|
|
|
|
|
|
|
if (1 === count($maybe) && current($maybe) instanceof Type\Atomic\TBool) {
|
|
|
|
$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) {
|
|
|
|
$statements_analyzer->node_data->setType($stmt, Type::getInt());
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Cast\Double) {
|
2018-11-11 18:01:14 +01:00
|
|
|
if (self::analyze($statements_analyzer, $stmt->expr, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, Type::getFloat());
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Cast\Bool_) {
|
2018-11-11 18:01:14 +01:00
|
|
|
if (self::analyze($statements_analyzer, $stmt->expr, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, Type::getBool());
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Cast\String_) {
|
2018-11-11 18:01:14 +01:00
|
|
|
if (self::analyze($statements_analyzer, $stmt->expr, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
if ($statements_analyzer->node_data->getType($stmt->expr)) {
|
2020-01-30 04:28:40 +01:00
|
|
|
$stmt_type = self::castStringAttempt($statements_analyzer, $stmt->expr, true);
|
|
|
|
} else {
|
|
|
|
$stmt_type = Type::getString();
|
2017-10-07 17:27:54 +02:00
|
|
|
}
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, $stmt_type);
|
2019-08-04 16:37:36 +02:00
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
if (($stmt_expr_type = $statements_analyzer->node_data->getType($stmt->expr))
|
|
|
|
&& $stmt_expr_type->tainted
|
|
|
|
) {
|
|
|
|
$stmt_type->tainted = $stmt_expr_type->tainted;
|
|
|
|
$stmt_type->sources = $stmt_expr_type->sources;
|
2019-08-04 16:37:36 +02:00
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Cast\Object_) {
|
2018-11-11 18:01:14 +01:00
|
|
|
if (self::analyze($statements_analyzer, $stmt->expr, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, new Type\Union([new TNamedObject('stdClass')]));
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Cast\Array_) {
|
2018-11-11 18:01:14 +01:00
|
|
|
if (self::analyze($statements_analyzer, $stmt->expr, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-04-21 20:06:47 +02:00
|
|
|
$permissible_atomic_types = [];
|
2017-04-21 22:33:18 +02:00
|
|
|
$all_permissible = false;
|
2017-04-21 20:06:47 +02:00
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
if ($stmt_expr_type = $statements_analyzer->node_data->getType($stmt->expr)) {
|
2017-04-21 22:33:18 +02:00
|
|
|
$all_permissible = true;
|
|
|
|
|
2020-01-04 18:20:26 +01:00
|
|
|
foreach ($stmt_expr_type->getAtomicTypes() as $type) {
|
2017-04-21 20:06:47 +02:00
|
|
|
if ($type instanceof Scalar) {
|
2018-08-21 04:11:01 +02:00
|
|
|
$permissible_atomic_types[] = new ObjectLike([new Type\Union([$type])]);
|
|
|
|
} elseif ($type instanceof TNull) {
|
|
|
|
$permissible_atomic_types[] = new TArray([Type::getEmpty(), Type::getEmpty()]);
|
2020-01-11 17:42:09 +01:00
|
|
|
} elseif ($type instanceof TArray
|
|
|
|
|| $type instanceof TList
|
|
|
|
|| $type instanceof ObjectLike
|
|
|
|
) {
|
2018-08-21 04:11:01 +02:00
|
|
|
$permissible_atomic_types[] = clone $type;
|
2017-04-21 20:06:47 +02:00
|
|
|
} else {
|
|
|
|
$all_permissible = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-30 05:41:17 +01:00
|
|
|
if ($permissible_atomic_types && $all_permissible) {
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType(
|
|
|
|
$stmt,
|
|
|
|
TypeCombination::combineTypes($permissible_atomic_types)
|
|
|
|
);
|
2017-04-21 20:06:47 +02:00
|
|
|
} else {
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, Type::getArray());
|
2017-04-21 20:06:47 +02:00
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Cast\Unset_) {
|
2018-11-11 18:01:14 +01:00
|
|
|
if (self::analyze($statements_analyzer, $stmt->expr, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, Type::getNull());
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Clone_) {
|
2018-11-11 18:01:14 +01:00
|
|
|
self::analyzeClone($statements_analyzer, $stmt, $context);
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Instanceof_) {
|
2018-11-11 18:01:14 +01:00
|
|
|
if (self::analyze($statements_analyzer, $stmt->expr, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-27 16:07:31 +02:00
|
|
|
if ($stmt->class instanceof PhpParser\Node\Expr) {
|
2018-11-11 18:01:14 +01:00
|
|
|
if (self::analyze($statements_analyzer, $stmt->class, $context) === false) {
|
2018-06-27 16:07:31 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} elseif (!in_array(strtolower($stmt->class->parts[0]), ['self', 'static', 'parent'], true)
|
2016-11-02 07:29:00 +01:00
|
|
|
) {
|
2016-10-22 19:23:18 +02:00
|
|
|
if ($context->check_classes) {
|
2018-11-06 03:57:36 +01:00
|
|
|
$fq_class_name = ClassLikeAnalyzer::getFQCLNFromNameObject(
|
2016-10-22 19:23:18 +02:00
|
|
|
$stmt->class,
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getAliases()
|
2016-10-22 19:23:18 +02:00
|
|
|
);
|
|
|
|
|
2019-07-01 15:55:39 +02:00
|
|
|
if ($codebase->store_node_types
|
|
|
|
&& $fq_class_name
|
|
|
|
&& !$context->collect_initializations
|
|
|
|
&& !$context->collect_mutations
|
|
|
|
) {
|
2019-06-30 03:32:26 +02:00
|
|
|
$codebase->analyzer->addNodeReference(
|
|
|
|
$statements_analyzer->getFilePath(),
|
|
|
|
$stmt->class,
|
|
|
|
$codebase->classlikes->classOrInterfaceExists($fq_class_name)
|
|
|
|
? $fq_class_name
|
|
|
|
: '*' . implode('\\', $stmt->class->parts)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
if (ClassLikeAnalyzer::checkFullyQualifiedClassLikeName(
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer,
|
2016-11-07 23:29:51 +01:00
|
|
|
$fq_class_name,
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt->class),
|
2020-02-11 22:39:33 +01:00
|
|
|
$context->self,
|
2020-03-26 17:35:27 +01:00
|
|
|
$context->calling_method_id,
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues(),
|
2018-01-02 02:04:03 +01:00
|
|
|
false
|
2016-11-02 07:29:00 +01:00
|
|
|
) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
2019-02-24 07:33:25 +01:00
|
|
|
|
2019-06-04 22:36:32 +02:00
|
|
|
if ($codebase->alter_code) {
|
|
|
|
$codebase->classlikes->handleClassLikeReferenceInMigration(
|
|
|
|
$codebase,
|
|
|
|
$statements_analyzer,
|
|
|
|
$stmt->class,
|
|
|
|
$fq_class_name,
|
2020-03-26 17:35:27 +01:00
|
|
|
$context->calling_method_id
|
2019-06-04 22:36:32 +02:00
|
|
|
);
|
|
|
|
}
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
}
|
2016-11-05 22:57:14 +01:00
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, Type::getBool());
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Exit_) {
|
2017-02-12 01:42:12 +01:00
|
|
|
if ($stmt->expr) {
|
2019-08-13 19:15:23 +02:00
|
|
|
$context->inside_call = true;
|
2018-11-11 18:01:14 +01:00
|
|
|
if (self::analyze($statements_analyzer, $stmt->expr, $context) === false) {
|
2017-02-12 01:42:12 +01:00
|
|
|
return false;
|
|
|
|
}
|
2019-08-13 19:15:23 +02:00
|
|
|
$context->inside_call = false;
|
2017-02-12 01:42:12 +01:00
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Include_) {
|
2018-11-11 18:01:14 +01:00
|
|
|
IncludeAnalyzer::analyze($statements_analyzer, $stmt, $context, $global_context);
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Eval_) {
|
2016-10-22 19:23:18 +02:00
|
|
|
$context->check_classes = false;
|
|
|
|
$context->check_variables = false;
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
if (self::analyze($statements_analyzer, $stmt->expr, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\AssignRef) {
|
2018-11-11 18:01:14 +01:00
|
|
|
if (AssignmentAnalyzer::analyzeAssignmentRef($statements_analyzer, $stmt, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\ErrorSuppress) {
|
2019-10-22 16:40:37 +02:00
|
|
|
$context->error_suppressing = true;
|
2018-11-11 18:01:14 +01:00
|
|
|
if (self::analyze($statements_analyzer, $stmt->expr, $context) === false) {
|
2018-03-13 04:48:10 +01:00
|
|
|
return false;
|
|
|
|
}
|
2019-10-22 16:40:37 +02:00
|
|
|
$context->error_suppressing = false;
|
2019-11-25 17:44:54 +01:00
|
|
|
|
|
|
|
$expr_type = $statements_analyzer->node_data->getType($stmt->expr);
|
|
|
|
|
|
|
|
if ($expr_type) {
|
|
|
|
$statements_analyzer->node_data->setType($stmt, $expr_type);
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\ShellExec) {
|
2016-10-22 19:23:18 +02:00
|
|
|
if (IssueBuffer::accepts(
|
2016-12-04 01:11:30 +01:00
|
|
|
new ForbiddenCode(
|
|
|
|
'Use of shell_exec',
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
2016-12-04 01:11:30 +01:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2016-10-22 19:23:18 +02:00
|
|
|
)) {
|
2019-03-23 15:58:26 +01:00
|
|
|
// continue
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Print_) {
|
2020-03-11 22:41:05 +01:00
|
|
|
$was_inside_call = $context->inside_call;
|
|
|
|
$context->inside_call = true;
|
2019-11-08 13:01:34 +01:00
|
|
|
if (self::analyzePrint($statements_analyzer, $stmt, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
2020-03-11 22:41:05 +01:00
|
|
|
$context->inside_call = $was_inside_call;
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Yield_) {
|
2018-11-11 18:01:14 +01:00
|
|
|
self::analyzeYield($statements_analyzer, $stmt, $context);
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\YieldFrom) {
|
2018-11-11 18:01:14 +01:00
|
|
|
self::analyzeYieldFrom($statements_analyzer, $stmt, $context);
|
2018-10-07 02:11:19 +02:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Error) {
|
|
|
|
// do nothing
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-11-06 01:53:39 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UnrecognizedExpression(
|
|
|
|
'Psalm does not understand ' . get_class($stmt),
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
2016-11-06 01:53:39 +01:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2016-11-06 01:53:39 +01:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
|
2018-06-26 00:02:05 +02:00
|
|
|
if (!$context->inside_conditional
|
|
|
|
&& ($stmt instanceof PhpParser\Node\Expr\BinaryOp
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\Instanceof_
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\Assign
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\BooleanNot
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\Empty_
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\Isset_
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\FuncCall)
|
|
|
|
) {
|
2019-11-25 17:44:54 +01:00
|
|
|
$assertions = $statements_analyzer->node_data->getAssertions($stmt);
|
|
|
|
|
|
|
|
if ($assertions === null) {
|
|
|
|
AssertionFinder::scrapeAssertions(
|
|
|
|
$stmt,
|
|
|
|
$context->self,
|
|
|
|
$statements_analyzer,
|
|
|
|
$codebase
|
|
|
|
);
|
|
|
|
}
|
2018-06-26 00:02:05 +02:00
|
|
|
}
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
$plugin_classes = $codebase->config->after_expression_checks;
|
2016-12-04 01:11:30 +01:00
|
|
|
|
2018-02-12 04:49:19 +01:00
|
|
|
if ($plugin_classes) {
|
2017-11-07 20:46:53 +01:00
|
|
|
$file_manipulations = [];
|
2016-12-08 21:57:18 +01:00
|
|
|
|
2018-02-12 04:49:19 +01:00
|
|
|
foreach ($plugin_classes as $plugin_fq_class_name) {
|
2018-11-06 03:57:36 +01:00
|
|
|
if ($plugin_fq_class_name::afterExpressionAnalysis(
|
2016-12-04 01:11:30 +01:00
|
|
|
$stmt,
|
|
|
|
$context,
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer,
|
2018-11-06 03:57:36 +01:00
|
|
|
$codebase,
|
2017-11-07 20:46:53 +01:00
|
|
|
$file_manipulations
|
2016-12-04 01:11:30 +01:00
|
|
|
) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
2017-11-07 20:46:53 +01:00
|
|
|
|
|
|
|
if ($file_manipulations) {
|
2018-11-11 18:01:14 +01:00
|
|
|
FileManipulationBuffer::add($statements_analyzer->getFilePath(), $file_manipulations);
|
2017-11-07 20:46:53 +01:00
|
|
|
}
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
|
|
|
|
return null;
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-11-11 18:01:14 +01:00
|
|
|
* @param StatementsAnalyzer $statements_analyzer
|
2016-11-02 07:29:00 +01:00
|
|
|
* @param PhpParser\Node\Expr $stmt
|
|
|
|
* @param Type\Union $by_ref_type
|
|
|
|
* @param Context $context
|
2018-02-18 23:55:11 +01:00
|
|
|
* @param bool $constrain_type
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-10-22 19:23:18 +02:00
|
|
|
* @return void
|
|
|
|
*/
|
2016-11-02 07:29:00 +01:00
|
|
|
public static function assignByRefParam(
|
2018-11-11 18:01:14 +01:00
|
|
|
StatementsAnalyzer $statements_analyzer,
|
2016-11-02 07:29:00 +01:00
|
|
|
PhpParser\Node\Expr $stmt,
|
|
|
|
Type\Union $by_ref_type,
|
2019-03-03 21:11:09 +01:00
|
|
|
Type\Union $by_ref_out_type,
|
2017-10-28 21:33:29 +02:00
|
|
|
Context $context,
|
2019-02-20 23:43:12 +01:00
|
|
|
bool $constrain_type = true,
|
|
|
|
bool $prevent_null = false
|
2016-11-02 07:29:00 +01:00
|
|
|
) {
|
2020-01-22 15:57:59 +01:00
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\PropertyFetch && $stmt->name instanceof PhpParser\Node\Identifier) {
|
|
|
|
$prop_name = $stmt->name->name;
|
|
|
|
|
|
|
|
Expression\Assignment\PropertyAssignmentAnalyzer::analyzeInstance(
|
|
|
|
$statements_analyzer,
|
|
|
|
$stmt,
|
|
|
|
$prop_name,
|
|
|
|
null,
|
|
|
|
$by_ref_out_type,
|
|
|
|
$context
|
|
|
|
);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
$var_id = self::getVarId(
|
|
|
|
$stmt,
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getFQCLN(),
|
|
|
|
$statements_analyzer
|
2016-11-02 07:29:00 +01:00
|
|
|
);
|
2016-10-22 19:23:18 +02:00
|
|
|
|
2016-12-24 19:23:22 +01:00
|
|
|
if ($var_id) {
|
2018-12-08 19:18:55 +01:00
|
|
|
if (!$by_ref_type->hasMixed() && $constrain_type) {
|
2018-11-06 03:57:36 +01:00
|
|
|
$context->byref_constraints[$var_id] = new \Psalm\Internal\ReferenceConstraint($by_ref_type);
|
2017-02-23 06:25:28 +01:00
|
|
|
}
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
if (!$context->hasVariable($var_id, $statements_analyzer)) {
|
2016-12-24 19:23:22 +01:00
|
|
|
$context->vars_possibly_in_scope[$var_id] = true;
|
2017-02-08 00:09:12 +01:00
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
if (!$statements_analyzer->hasVariable($var_id)) {
|
|
|
|
$location = new CodeLocation($statements_analyzer, $stmt);
|
|
|
|
$statements_analyzer->registerVariable($var_id, $location, null);
|
2018-01-28 23:28:34 +01:00
|
|
|
|
2019-02-20 23:43:12 +01:00
|
|
|
if ($constrain_type
|
|
|
|
&& $prevent_null
|
|
|
|
&& !$by_ref_type->isMixed()
|
|
|
|
&& !$by_ref_type->isNullable()
|
|
|
|
&& !strpos($var_id, '->')
|
|
|
|
&& !strpos($var_id, '::')
|
|
|
|
) {
|
|
|
|
if (IssueBuffer::accepts(
|
2019-04-26 00:02:19 +02:00
|
|
|
new \Psalm\Issue\NullReference(
|
2019-02-20 23:43:12 +01:00
|
|
|
'Not expecting null argument passed by reference',
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-28 21:30:56 +01:00
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
|
|
|
|
|
|
|
if ($codebase->find_unused_variables) {
|
2018-06-17 02:01:33 +02:00
|
|
|
$context->unreferenced_vars[$var_id] = [$location->getHash() => $location];
|
2018-01-28 23:28:34 +01:00
|
|
|
}
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$context->hasVariable($var_id, $statements_analyzer);
|
2017-02-08 00:09:12 +01:00
|
|
|
}
|
2019-03-04 02:16:11 +01:00
|
|
|
} elseif ($var_id === '$this') {
|
|
|
|
// don't allow changing $this
|
|
|
|
return;
|
2016-12-24 19:23:22 +01:00
|
|
|
} else {
|
|
|
|
$existing_type = $context->vars_in_scope[$var_id];
|
2017-04-02 23:37:56 +02:00
|
|
|
|
2018-11-21 18:38:43 +01:00
|
|
|
// removes dependent vars from $context
|
2017-04-02 23:37:56 +02:00
|
|
|
$context->removeDescendents(
|
|
|
|
$var_id,
|
|
|
|
$existing_type,
|
|
|
|
$by_ref_type,
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer
|
2017-04-02 23:37:56 +02:00
|
|
|
);
|
|
|
|
|
2018-01-09 21:44:31 +01:00
|
|
|
if ($existing_type->getId() !== 'array<empty, empty>') {
|
2019-03-04 00:21:12 +01:00
|
|
|
$context->vars_in_scope[$var_id] = clone $by_ref_out_type;
|
2018-11-21 18:38:43 +01:00
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
if (!($stmt_type = $statements_analyzer->node_data->getType($stmt))
|
|
|
|
|| $stmt_type->isEmpty()
|
|
|
|
) {
|
|
|
|
$statements_analyzer->node_data->setType($stmt, clone $by_ref_type);
|
2018-11-21 18:38:43 +01:00
|
|
|
}
|
2017-05-25 04:07:49 +02:00
|
|
|
|
2016-12-24 19:23:22 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2017-02-23 06:25:28 +01:00
|
|
|
|
2019-12-07 21:58:05 +01:00
|
|
|
$context->assigned_var_ids[$var_id] = true;
|
|
|
|
|
2019-03-03 21:11:09 +01:00
|
|
|
$context->vars_in_scope[$var_id] = $by_ref_out_type;
|
2016-10-22 19:23:18 +02:00
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
if (!($stmt_type = $statements_analyzer->node_data->getType($stmt)) || $stmt_type->isEmpty()) {
|
|
|
|
$statements_analyzer->node_data->setType($stmt, clone $by_ref_type);
|
2018-11-21 18:38:43 +01:00
|
|
|
}
|
|
|
|
}
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-14 18:09:40 +01:00
|
|
|
* @param PhpParser\Node\Expr $stmt
|
|
|
|
* @param string|null $this_class_name
|
2018-02-23 21:39:33 +01:00
|
|
|
* @param FileSource|null $source
|
2018-01-14 18:09:40 +01:00
|
|
|
* @param int|null &$nesting
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2018-01-14 18:09:40 +01:00
|
|
|
* @return string|null
|
2016-10-22 19:23:18 +02:00
|
|
|
*/
|
2018-01-14 18:09:40 +01:00
|
|
|
public static function getVarId(
|
|
|
|
PhpParser\Node\Expr $stmt,
|
|
|
|
$this_class_name,
|
2018-02-23 21:39:33 +01:00
|
|
|
FileSource $source = null,
|
2018-01-14 18:09:40 +01:00
|
|
|
&$nesting = null
|
2016-10-22 19:23:18 +02:00
|
|
|
) {
|
2018-01-14 18:09:40 +01:00
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\Variable && is_string($stmt->name)) {
|
|
|
|
return '$' . $stmt->name;
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\StaticPropertyFetch
|
2018-04-17 18:16:25 +02:00
|
|
|
&& $stmt->name instanceof PhpParser\Node\Identifier
|
2018-01-14 18:09:40 +01:00
|
|
|
&& $stmt->class instanceof PhpParser\Node\Name
|
|
|
|
) {
|
|
|
|
if (count($stmt->class->parts) === 1
|
|
|
|
&& in_array(strtolower($stmt->class->parts[0]), ['self', 'static', 'parent'], true)
|
|
|
|
) {
|
|
|
|
if (!$this_class_name) {
|
|
|
|
$fq_class_name = $stmt->class->parts[0];
|
|
|
|
} else {
|
|
|
|
$fq_class_name = $this_class_name;
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2018-01-14 18:09:40 +01:00
|
|
|
$fq_class_name = $source
|
2018-11-06 03:57:36 +01:00
|
|
|
? ClassLikeAnalyzer::getFQCLNFromNameObject(
|
2018-01-14 18:09:40 +01:00
|
|
|
$stmt->class,
|
|
|
|
$source->getAliases()
|
|
|
|
)
|
|
|
|
: implode('\\', $stmt->class->parts);
|
2017-02-11 05:35:34 +01:00
|
|
|
}
|
|
|
|
|
2018-04-17 18:16:25 +02:00
|
|
|
return $fq_class_name . '::$' . $stmt->name->name;
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
2016-10-22 19:23:18 +02:00
|
|
|
|
2018-04-17 18:16:25 +02:00
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\PropertyFetch && $stmt->name instanceof PhpParser\Node\Identifier) {
|
2018-01-14 18:09:40 +01:00
|
|
|
$object_id = self::getVarId($stmt->var, $this_class_name, $source);
|
2017-03-20 07:05:58 +01:00
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
if (!$object_id) {
|
|
|
|
return null;
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
|
2018-04-17 18:16:25 +02:00
|
|
|
return $object_id . '->' . $stmt->name->name;
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\ArrayDimFetch && $nesting !== null) {
|
|
|
|
++$nesting;
|
2017-05-25 04:07:49 +02:00
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
return self::getVarId($stmt->var, $this_class_name, $source, $nesting);
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-14 18:09:40 +01:00
|
|
|
* @param PhpParser\Node\Expr $stmt
|
|
|
|
* @param string|null $this_class_name
|
2018-02-23 21:39:33 +01:00
|
|
|
* @param FileSource|null $source
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2018-01-14 18:09:40 +01:00
|
|
|
* @return string|null
|
2016-10-22 19:23:18 +02:00
|
|
|
*/
|
2018-01-14 18:09:40 +01:00
|
|
|
public static function getRootVarId(
|
|
|
|
PhpParser\Node\Expr $stmt,
|
|
|
|
$this_class_name,
|
2018-02-23 21:39:33 +01:00
|
|
|
FileSource $source = null
|
2016-10-22 19:23:18 +02:00
|
|
|
) {
|
2018-01-14 18:09:40 +01:00
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\Variable
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\StaticPropertyFetch
|
2017-02-17 02:43:23 +01:00
|
|
|
) {
|
2018-01-14 18:09:40 +01:00
|
|
|
return self::getVarId($stmt, $this_class_name, $source);
|
|
|
|
}
|
2017-11-28 06:46:41 +01:00
|
|
|
|
2018-04-17 18:16:25 +02:00
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\PropertyFetch && $stmt->name instanceof PhpParser\Node\Identifier) {
|
2018-01-14 18:09:40 +01:00
|
|
|
$property_root = self::getRootVarId($stmt->var, $this_class_name, $source);
|
2017-11-28 06:46:41 +01:00
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
if ($property_root) {
|
2018-04-17 18:16:25 +02:00
|
|
|
return $property_root . '->' . $stmt->name->name;
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
2016-10-22 19:23:18 +02:00
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\ArrayDimFetch) {
|
|
|
|
return self::getRootVarId($stmt->var, $this_class_name, $source);
|
|
|
|
}
|
2017-11-28 06:46:41 +01:00
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
return null;
|
|
|
|
}
|
2017-11-28 06:46:41 +01:00
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
/**
|
|
|
|
* @param PhpParser\Node\Expr $stmt
|
|
|
|
* @param string|null $this_class_name
|
2018-02-23 21:39:33 +01:00
|
|
|
* @param FileSource|null $source
|
2018-01-14 18:09:40 +01:00
|
|
|
*
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
public static function getArrayVarId(
|
|
|
|
PhpParser\Node\Expr $stmt,
|
|
|
|
$this_class_name,
|
2018-02-23 21:39:33 +01:00
|
|
|
FileSource $source = null
|
2018-01-14 18:09:40 +01:00
|
|
|
) {
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\Assign) {
|
|
|
|
return self::getArrayVarId($stmt->var, $this_class_name, $source);
|
|
|
|
}
|
2017-12-04 00:33:54 +01:00
|
|
|
|
2018-02-17 17:24:08 +01:00
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\ArrayDimFetch) {
|
2018-01-14 18:09:40 +01:00
|
|
|
$root_var_id = self::getArrayVarId($stmt->var, $this_class_name, $source);
|
2017-12-04 00:33:54 +01:00
|
|
|
|
2018-02-17 17:24:08 +01:00
|
|
|
$offset = null;
|
|
|
|
|
|
|
|
if ($root_var_id) {
|
|
|
|
if ($stmt->dim instanceof PhpParser\Node\Scalar\String_
|
|
|
|
|| $stmt->dim instanceof PhpParser\Node\Scalar\LNumber
|
|
|
|
) {
|
|
|
|
$offset = $stmt->dim instanceof PhpParser\Node\Scalar\String_
|
|
|
|
? '\'' . $stmt->dim->value . '\''
|
|
|
|
: $stmt->dim->value;
|
|
|
|
} elseif ($stmt->dim instanceof PhpParser\Node\Expr\Variable
|
|
|
|
&& is_string($stmt->dim->name)
|
|
|
|
) {
|
|
|
|
$offset = '$' . $stmt->dim->name;
|
2018-05-31 22:49:01 +02:00
|
|
|
} elseif ($stmt->dim instanceof PhpParser\Node\Expr\ConstFetch) {
|
|
|
|
$offset = implode('\\', $stmt->dim->name->parts);
|
2019-10-02 01:31:08 +02:00
|
|
|
} elseif ($stmt->dim instanceof PhpParser\Node\Expr\PropertyFetch) {
|
|
|
|
$object_id = self::getArrayVarId($stmt->dim->var, $this_class_name, $source);
|
|
|
|
|
|
|
|
if ($object_id && $stmt->dim->name instanceof PhpParser\Node\Identifier) {
|
|
|
|
$offset = $object_id . '->' . $stmt->dim->name;
|
|
|
|
}
|
2019-12-04 18:23:26 +01:00
|
|
|
} elseif ($stmt->dim instanceof PhpParser\Node\Expr\ClassConstFetch
|
|
|
|
&& $stmt->dim->name instanceof PhpParser\Node\Identifier
|
|
|
|
&& $stmt->dim->class instanceof PhpParser\Node\Name
|
|
|
|
&& $stmt->dim->class->parts[0] === 'static'
|
|
|
|
) {
|
|
|
|
$offset = 'static::' . $stmt->dim->name;
|
2019-11-25 17:44:54 +01:00
|
|
|
} elseif ($stmt->dim
|
|
|
|
&& $source instanceof StatementsAnalyzer
|
2020-04-13 03:47:50 +02:00
|
|
|
&& ($stmt_dim_type = $source->node_data->getType($stmt->dim))
|
|
|
|
&& (!$stmt->dim instanceof PhpParser\Node\Expr\ClassConstFetch
|
|
|
|
|| !$stmt->dim->name instanceof PhpParser\Node\Identifier
|
|
|
|
|| $stmt->dim->name->name !== 'class'
|
|
|
|
)
|
|
|
|
) {
|
2019-11-25 17:44:54 +01:00
|
|
|
if ($stmt_dim_type->isSingleStringLiteral()) {
|
|
|
|
$offset = '\'' . $stmt_dim_type->getSingleStringLiteral()->value . '\'';
|
|
|
|
} elseif ($stmt_dim_type->isSingleIntLiteral()) {
|
|
|
|
$offset = $stmt_dim_type->getSingleIntLiteral()->value;
|
2019-02-07 22:24:31 +01:00
|
|
|
}
|
|
|
|
} elseif ($stmt->dim instanceof PhpParser\Node\Expr\ClassConstFetch
|
|
|
|
&& $stmt->dim->name instanceof PhpParser\Node\Identifier
|
|
|
|
) {
|
|
|
|
/** @var string|null */
|
|
|
|
$resolved_name = $stmt->dim->class->getAttribute('resolvedName');
|
|
|
|
|
|
|
|
if ($resolved_name) {
|
2020-02-18 00:19:33 +01:00
|
|
|
$offset = $resolved_name . '::' . $stmt->dim->name;
|
2019-02-07 22:24:31 +01:00
|
|
|
}
|
2018-02-17 17:24:08 +01:00
|
|
|
}
|
|
|
|
|
2019-12-13 21:51:43 +01:00
|
|
|
return $offset !== null ? $root_var_id . '[' . $offset . ']' : null;
|
2018-02-17 17:24:08 +01:00
|
|
|
}
|
2018-01-14 18:09:40 +01:00
|
|
|
}
|
2016-12-28 20:20:16 +01:00
|
|
|
|
2018-05-09 03:21:22 +02:00
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\PropertyFetch) {
|
2018-04-06 22:23:10 +02:00
|
|
|
$object_id = self::getArrayVarId($stmt->var, $this_class_name, $source);
|
|
|
|
|
|
|
|
if (!$object_id) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-05-09 03:21:22 +02:00
|
|
|
if ($stmt->name instanceof PhpParser\Node\Identifier) {
|
|
|
|
return $object_id . '->' . $stmt->name;
|
2019-11-25 17:44:54 +01:00
|
|
|
} elseif ($source instanceof StatementsAnalyzer
|
|
|
|
&& ($stmt_name_type = $source->node_data->getType($stmt->name))
|
|
|
|
&& $stmt_name_type->isSingleStringLiteral()
|
|
|
|
) {
|
|
|
|
return $object_id . '->' . $stmt_name_type->getSingleStringLiteral()->value;
|
2018-05-09 03:21:22 +02:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2018-04-06 22:23:10 +02:00
|
|
|
}
|
|
|
|
|
2019-01-13 17:54:39 +01:00
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\ClassConstFetch
|
|
|
|
&& $stmt->name instanceof PhpParser\Node\Identifier
|
|
|
|
) {
|
|
|
|
/** @var string|null */
|
|
|
|
$resolved_name = $stmt->class->getAttribute('resolvedName');
|
|
|
|
|
|
|
|
if ($resolved_name) {
|
2019-05-27 19:33:14 +02:00
|
|
|
if (($resolved_name === 'self' || $resolved_name === 'static') && $this_class_name) {
|
|
|
|
$resolved_name = $this_class_name;
|
|
|
|
}
|
|
|
|
|
2019-01-13 17:54:39 +01:00
|
|
|
return $resolved_name . '::' . $stmt->name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-28 19:25:44 +02:00
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\MethodCall
|
|
|
|
&& $stmt->name instanceof PhpParser\Node\Identifier
|
|
|
|
&& !$stmt->args
|
|
|
|
) {
|
|
|
|
$config = \Psalm\Config::getInstance();
|
|
|
|
|
2019-12-12 15:32:33 +01:00
|
|
|
if ($config->memoize_method_calls || isset($stmt->pure)) {
|
2018-04-28 19:25:44 +02:00
|
|
|
$lhs_var_name = self::getArrayVarId(
|
|
|
|
$stmt->var,
|
|
|
|
$this_class_name,
|
|
|
|
$source
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!$lhs_var_name) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $lhs_var_name . '->' . strtolower($stmt->name->name) . '()';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
return self::getVarId($stmt, $this_class_name, $source);
|
|
|
|
}
|
2016-10-22 19:23:18 +02:00
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
/**
|
|
|
|
* @param Type\Union $return_type
|
2018-01-26 19:51:00 +01:00
|
|
|
* @param string|null $self_class
|
2019-06-24 21:08:55 +02:00
|
|
|
* @param string|Type\Atomic\TNamedObject|Type\Atomic\TTemplateParam|null $static_class_type
|
2018-01-14 18:09:40 +01:00
|
|
|
*
|
|
|
|
* @return Type\Union
|
|
|
|
*/
|
|
|
|
public static function fleshOutType(
|
2018-11-06 03:57:36 +01:00
|
|
|
Codebase $codebase,
|
2018-01-14 18:09:40 +01:00
|
|
|
Type\Union $return_type,
|
2019-05-25 17:51:09 +02:00
|
|
|
?string $self_class,
|
|
|
|
$static_class_type,
|
2019-06-07 00:46:40 +02:00
|
|
|
?string $parent_class,
|
2020-03-29 19:37:37 +02:00
|
|
|
bool $evaluate_class_constants = true,
|
|
|
|
bool $evaluate_conditional_types = false,
|
2020-03-24 23:14:10 +01:00
|
|
|
bool $final = false
|
2018-01-14 18:09:40 +01:00
|
|
|
) {
|
|
|
|
$return_type = clone $return_type;
|
2016-10-22 19:23:18 +02:00
|
|
|
|
2018-01-14 18:09:40 +01:00
|
|
|
$new_return_type_parts = [];
|
2016-10-22 19:23:18 +02:00
|
|
|
|
2020-01-04 18:20:26 +01:00
|
|
|
foreach ($return_type->getAtomicTypes() as $return_type_part) {
|
2019-05-28 16:44:04 +02:00
|
|
|
$parts = self::fleshOutAtomicType(
|
2018-11-06 03:57:36 +01:00
|
|
|
$codebase,
|
2017-07-29 21:05:06 +02:00
|
|
|
$return_type_part,
|
2018-01-26 19:51:00 +01:00
|
|
|
$self_class,
|
2019-05-25 17:51:09 +02:00
|
|
|
$static_class_type,
|
2019-06-07 00:46:40 +02:00
|
|
|
$parent_class,
|
2020-03-29 19:37:37 +02:00
|
|
|
$evaluate_class_constants,
|
|
|
|
$evaluate_conditional_types,
|
2020-03-24 23:14:10 +01:00
|
|
|
$final
|
2017-07-29 21:05:06 +02:00
|
|
|
);
|
2019-05-28 16:44:04 +02:00
|
|
|
|
|
|
|
if (is_array($parts)) {
|
|
|
|
$new_return_type_parts = array_merge($new_return_type_parts, $parts);
|
|
|
|
} else {
|
|
|
|
$new_return_type_parts[] = $parts;
|
|
|
|
}
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
|
2017-01-15 23:52:01 +01:00
|
|
|
$fleshed_out_type = new Type\Union($new_return_type_parts);
|
|
|
|
|
|
|
|
$fleshed_out_type->from_docblock = $return_type->from_docblock;
|
2017-05-10 18:49:59 +02:00
|
|
|
$fleshed_out_type->ignore_nullable_issues = $return_type->ignore_nullable_issues;
|
2018-01-25 00:52:58 +01:00
|
|
|
$fleshed_out_type->ignore_falsable_issues = $return_type->ignore_falsable_issues;
|
2018-03-17 21:53:11 +01:00
|
|
|
$fleshed_out_type->possibly_undefined = $return_type->possibly_undefined;
|
2018-01-12 05:18:13 +01:00
|
|
|
$fleshed_out_type->by_ref = $return_type->by_ref;
|
2018-08-07 20:43:05 +02:00
|
|
|
$fleshed_out_type->initialized = $return_type->initialized;
|
2019-05-06 22:38:08 +02:00
|
|
|
$fleshed_out_type->had_template = $return_type->had_template;
|
2019-08-04 16:37:36 +02:00
|
|
|
$fleshed_out_type->sources = $return_type->sources;
|
|
|
|
$fleshed_out_type->tainted = $return_type->tainted;
|
2017-01-15 23:52:01 +01:00
|
|
|
|
|
|
|
return $fleshed_out_type;
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-02 04:27:52 +01:00
|
|
|
* @param Type\Atomic &$return_type
|
2018-01-26 19:51:00 +01:00
|
|
|
* @param string|null $self_class
|
2019-06-24 21:08:55 +02:00
|
|
|
* @param string|Type\Atomic\TNamedObject|Type\Atomic\TTemplateParam|null $static_class_type
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2020-01-30 05:41:17 +01:00
|
|
|
* @return Type\Atomic|non-empty-array<int, Type\Atomic>
|
2016-10-22 19:23:18 +02:00
|
|
|
*/
|
2018-01-26 19:51:00 +01:00
|
|
|
private static function fleshOutAtomicType(
|
2018-11-06 03:57:36 +01:00
|
|
|
Codebase $codebase,
|
2018-11-23 19:24:35 +01:00
|
|
|
Type\Atomic &$return_type,
|
2019-05-25 17:51:09 +02:00
|
|
|
?string $self_class,
|
|
|
|
$static_class_type,
|
2019-06-07 00:46:40 +02:00
|
|
|
?string $parent_class,
|
2020-03-29 19:37:37 +02:00
|
|
|
bool $evaluate_class_constants = true,
|
|
|
|
bool $evaluate_conditional_types = false,
|
2020-03-24 23:14:10 +01:00
|
|
|
bool $final = false
|
2017-07-29 21:05:06 +02:00
|
|
|
) {
|
2018-12-08 20:10:06 +01:00
|
|
|
if ($return_type instanceof TNamedObject
|
2019-02-22 03:40:06 +01:00
|
|
|
|| $return_type instanceof TTemplateParam
|
2018-12-08 20:10:06 +01:00
|
|
|
) {
|
2018-11-25 00:31:00 +01:00
|
|
|
if ($return_type->extra_types) {
|
|
|
|
$new_intersection_types = [];
|
|
|
|
|
|
|
|
foreach ($return_type->extra_types as &$extra_type) {
|
|
|
|
self::fleshOutAtomicType(
|
|
|
|
$codebase,
|
|
|
|
$extra_type,
|
|
|
|
$self_class,
|
2019-05-25 17:51:09 +02:00
|
|
|
$static_class_type,
|
2019-06-07 00:46:40 +02:00
|
|
|
$parent_class,
|
2020-03-29 19:37:37 +02:00
|
|
|
$evaluate_class_constants,
|
|
|
|
$evaluate_conditional_types
|
2018-11-25 00:31:00 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
if ($extra_type instanceof TNamedObject && $extra_type->extra_types) {
|
|
|
|
$new_intersection_types = array_merge(
|
|
|
|
$new_intersection_types,
|
|
|
|
$extra_type->extra_types
|
|
|
|
);
|
|
|
|
$extra_type->extra_types = [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($new_intersection_types) {
|
|
|
|
$return_type->extra_types = array_merge($return_type->extra_types, $new_intersection_types);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-08 20:10:06 +01:00
|
|
|
if ($return_type instanceof TNamedObject) {
|
|
|
|
$return_type_lc = strtolower($return_type->value);
|
2018-01-21 19:38:51 +01:00
|
|
|
|
2020-03-29 19:37:37 +02:00
|
|
|
if ($static_class_type && ($return_type_lc === 'static' || $return_type_lc === '$this')) {
|
2018-12-08 20:10:06 +01:00
|
|
|
if (is_string($static_class_type)) {
|
|
|
|
$return_type->value = $static_class_type;
|
|
|
|
} else {
|
2019-11-12 14:01:22 +01:00
|
|
|
if ($return_type instanceof Type\Atomic\TGenericObject
|
2019-12-09 16:58:09 +01:00
|
|
|
&& $static_class_type instanceof Type\Atomic\TGenericObject
|
2019-11-12 14:01:22 +01:00
|
|
|
) {
|
|
|
|
$return_type->value = $static_class_type->value;
|
|
|
|
} else {
|
|
|
|
$return_type = clone $static_class_type;
|
|
|
|
}
|
2018-12-08 20:10:06 +01:00
|
|
|
}
|
2020-03-24 23:00:20 +01:00
|
|
|
|
2020-03-24 23:14:10 +01:00
|
|
|
if (!$final && $return_type instanceof TNamedObject) {
|
2020-03-24 23:00:20 +01:00
|
|
|
$return_type->was_static = true;
|
|
|
|
}
|
2020-03-28 00:17:44 +01:00
|
|
|
} elseif ($return_type->was_static
|
|
|
|
&& ($static_class_type instanceof Type\Atomic\TNamedObject
|
|
|
|
|| $static_class_type instanceof Type\Atomic\TTemplateParam)
|
|
|
|
) {
|
2020-03-28 05:08:31 +01:00
|
|
|
$return_type = clone $return_type;
|
|
|
|
$cloned_static = clone $static_class_type;
|
|
|
|
$extra_static = $cloned_static->extra_types ?: [];
|
|
|
|
$cloned_static->extra_types = null;
|
2020-03-28 13:14:35 +01:00
|
|
|
|
|
|
|
if ($cloned_static->getKey(false) !== $return_type->getKey(false)) {
|
|
|
|
$return_type->extra_types[$static_class_type->getKey()] = clone $cloned_static;
|
|
|
|
}
|
2020-03-28 05:08:31 +01:00
|
|
|
|
|
|
|
foreach ($extra_static as $extra_static_type) {
|
2020-03-28 13:14:35 +01:00
|
|
|
if ($extra_static_type->getKey(false) !== $return_type->getKey(false)) {
|
|
|
|
$return_type->extra_types[$extra_static_type->getKey()] = clone $extra_static_type;
|
|
|
|
}
|
2020-03-28 05:08:31 +01:00
|
|
|
}
|
2020-03-29 19:37:37 +02:00
|
|
|
} elseif ($self_class && $return_type_lc === 'self') {
|
2019-02-17 19:14:59 +01:00
|
|
|
$return_type->value = $self_class;
|
2020-03-29 19:37:37 +02:00
|
|
|
} elseif ($parent_class && $return_type_lc === 'parent') {
|
2019-05-25 17:51:09 +02:00
|
|
|
$return_type->value = $parent_class;
|
2018-11-22 07:03:32 +01:00
|
|
|
} else {
|
2018-12-08 20:10:06 +01:00
|
|
|
$return_type->value = $codebase->classlikes->getUnAliasedName($return_type->value);
|
2018-11-22 07:03:32 +01:00
|
|
|
}
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-09 06:29:16 +01:00
|
|
|
if ($return_type instanceof Type\Atomic\TClassString
|
|
|
|
&& $return_type->as_type
|
|
|
|
) {
|
2020-03-09 23:59:56 +01:00
|
|
|
$new_as_type = clone $return_type->as_type;
|
2020-03-09 06:32:27 +01:00
|
|
|
|
2020-03-09 06:29:16 +01:00
|
|
|
self::fleshOutAtomicType(
|
|
|
|
$codebase,
|
2020-03-09 06:32:27 +01:00
|
|
|
$new_as_type,
|
2020-03-09 06:29:16 +01:00
|
|
|
$self_class,
|
|
|
|
$static_class_type,
|
|
|
|
$parent_class,
|
2020-03-29 19:37:37 +02:00
|
|
|
$evaluate_class_constants,
|
|
|
|
$evaluate_conditional_types,
|
2020-03-24 23:14:10 +01:00
|
|
|
$final
|
2020-03-09 06:29:16 +01:00
|
|
|
);
|
|
|
|
|
2020-03-09 06:32:27 +01:00
|
|
|
if ($new_as_type instanceof TNamedObject) {
|
|
|
|
$return_type->as_type = $new_as_type;
|
|
|
|
$return_type->as = $return_type->as_type->value;
|
|
|
|
}
|
2020-03-09 06:29:16 +01:00
|
|
|
}
|
|
|
|
|
2018-05-21 06:46:56 +02:00
|
|
|
if ($return_type instanceof Type\Atomic\TScalarClassConstant) {
|
2018-08-09 02:59:21 +02:00
|
|
|
if ($return_type->fq_classlike_name === 'self' && $self_class) {
|
|
|
|
$return_type->fq_classlike_name = $self_class;
|
|
|
|
}
|
|
|
|
|
2020-03-29 19:37:37 +02:00
|
|
|
if ($evaluate_class_constants && $codebase->classOrInterfaceExists($return_type->fq_classlike_name)) {
|
2018-08-09 05:12:12 +02:00
|
|
|
if (strtolower($return_type->const_name) === 'class') {
|
|
|
|
return new Type\Atomic\TLiteralClassString($return_type->fq_classlike_name);
|
|
|
|
}
|
|
|
|
|
2020-02-07 16:54:57 +01:00
|
|
|
if (strpos($return_type->const_name, '*') !== false) {
|
|
|
|
$class_storage = $codebase->classlike_storage_provider->get($return_type->fq_classlike_name);
|
|
|
|
|
|
|
|
$matching_constants = \array_keys($class_storage->class_constant_locations);
|
|
|
|
|
|
|
|
$const_name_part = \substr($return_type->const_name, 0, -1);
|
|
|
|
|
|
|
|
if ($const_name_part) {
|
|
|
|
$matching_constants = \array_filter(
|
|
|
|
$matching_constants,
|
|
|
|
function ($constant_name) use ($const_name_part) {
|
|
|
|
return $constant_name !== $const_name_part
|
|
|
|
&& \strpos($constant_name, $const_name_part) === 0;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$matching_constants = [$return_type->const_name];
|
2019-12-10 22:16:44 +01:00
|
|
|
}
|
2018-05-21 06:46:56 +02:00
|
|
|
|
2020-02-07 16:54:57 +01:00
|
|
|
$matching_constant_types = [];
|
2018-05-21 06:46:56 +02:00
|
|
|
|
2020-02-07 16:54:57 +01:00
|
|
|
foreach ($matching_constants as $matching_constant) {
|
|
|
|
try {
|
|
|
|
$class_constant = $codebase->classlikes->getConstantForClass(
|
|
|
|
$return_type->fq_classlike_name,
|
|
|
|
$matching_constant,
|
|
|
|
\ReflectionProperty::IS_PRIVATE
|
|
|
|
);
|
|
|
|
} catch (\Psalm\Exception\CircularReferenceException $e) {
|
|
|
|
$class_constant = null;
|
2018-05-21 06:46:56 +02:00
|
|
|
}
|
2020-02-07 16:54:57 +01:00
|
|
|
|
|
|
|
if ($class_constant) {
|
|
|
|
if ($class_constant->isSingle()) {
|
|
|
|
$class_constant = clone $class_constant;
|
|
|
|
|
|
|
|
$matching_constant_types = \array_merge(
|
|
|
|
\array_values($class_constant->getAtomicTypes()),
|
|
|
|
$matching_constant_types
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($matching_constant_types) {
|
|
|
|
return $matching_constant_types;
|
2018-05-21 06:46:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-15 17:30:49 +01:00
|
|
|
return $return_type;
|
2018-05-21 06:46:56 +02:00
|
|
|
}
|
|
|
|
|
2019-05-28 16:44:04 +02:00
|
|
|
if ($return_type instanceof Type\Atomic\TKeyOfClassConstant
|
|
|
|
|| $return_type instanceof Type\Atomic\TValueOfClassConstant
|
|
|
|
) {
|
|
|
|
if ($return_type->fq_classlike_name === 'self' && $self_class) {
|
|
|
|
$return_type->fq_classlike_name = $self_class;
|
|
|
|
}
|
|
|
|
|
2020-03-29 19:37:37 +02:00
|
|
|
if ($evaluate_class_constants && $codebase->classOrInterfaceExists($return_type->fq_classlike_name)) {
|
2019-12-10 22:16:44 +01:00
|
|
|
try {
|
|
|
|
$class_constant_type = $codebase->classlikes->getConstantForClass(
|
|
|
|
$return_type->fq_classlike_name,
|
|
|
|
$return_type->const_name,
|
|
|
|
\ReflectionProperty::IS_PRIVATE
|
|
|
|
);
|
|
|
|
} catch (\Psalm\Exception\CircularReferenceException $e) {
|
|
|
|
$class_constant_type = null;
|
|
|
|
}
|
2019-05-28 16:44:04 +02:00
|
|
|
|
2019-09-14 20:26:31 +02:00
|
|
|
if ($class_constant_type) {
|
2020-01-04 18:20:26 +01:00
|
|
|
foreach ($class_constant_type->getAtomicTypes() as $const_type_atomic) {
|
2019-05-28 16:44:04 +02:00
|
|
|
if ($const_type_atomic instanceof Type\Atomic\ObjectLike
|
|
|
|
|| $const_type_atomic instanceof Type\Atomic\TArray
|
|
|
|
) {
|
|
|
|
if ($const_type_atomic instanceof Type\Atomic\ObjectLike) {
|
|
|
|
$const_type_atomic = $const_type_atomic->getGenericArrayType();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($return_type instanceof Type\Atomic\TKeyOfClassConstant) {
|
2020-01-04 18:20:26 +01:00
|
|
|
return array_values($const_type_atomic->type_params[0]->getAtomicTypes());
|
2019-05-28 16:44:04 +02:00
|
|
|
}
|
|
|
|
|
2020-01-04 18:20:26 +01:00
|
|
|
return array_values($const_type_atomic->type_params[1]->getAtomicTypes());
|
2019-05-28 16:44:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $return_type;
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:19:54 +01:00
|
|
|
if ($return_type instanceof Type\Atomic\TArray
|
|
|
|
|| $return_type instanceof Type\Atomic\TGenericObject
|
|
|
|
|| $return_type instanceof Type\Atomic\TIterable
|
|
|
|
) {
|
2016-10-22 19:23:18 +02:00
|
|
|
foreach ($return_type->type_params as &$type_param) {
|
2018-01-26 19:51:00 +01:00
|
|
|
$type_param = self::fleshOutType(
|
2018-11-06 03:57:36 +01:00
|
|
|
$codebase,
|
2018-01-26 19:51:00 +01:00
|
|
|
$type_param,
|
|
|
|
$self_class,
|
2019-05-25 17:51:09 +02:00
|
|
|
$static_class_type,
|
2020-03-24 23:14:10 +01:00
|
|
|
$parent_class,
|
2020-03-29 19:37:37 +02:00
|
|
|
$evaluate_class_constants,
|
|
|
|
$evaluate_conditional_types,
|
2020-03-24 23:14:10 +01:00
|
|
|
$final
|
2018-01-26 19:51:00 +01:00
|
|
|
);
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
2018-03-07 19:54:46 +01:00
|
|
|
} elseif ($return_type instanceof Type\Atomic\ObjectLike) {
|
|
|
|
foreach ($return_type->properties as &$property_type) {
|
|
|
|
$property_type = self::fleshOutType(
|
2018-11-06 03:57:36 +01:00
|
|
|
$codebase,
|
2018-03-07 19:54:46 +01:00
|
|
|
$property_type,
|
|
|
|
$self_class,
|
2019-05-25 17:51:09 +02:00
|
|
|
$static_class_type,
|
2020-03-24 23:14:10 +01:00
|
|
|
$parent_class,
|
2020-03-29 19:37:37 +02:00
|
|
|
$evaluate_class_constants,
|
|
|
|
$evaluate_conditional_types,
|
2020-03-24 23:14:10 +01:00
|
|
|
$final
|
2018-03-07 19:54:46 +01:00
|
|
|
);
|
|
|
|
}
|
2019-11-05 19:08:12 +01:00
|
|
|
} elseif ($return_type instanceof Type\Atomic\TList) {
|
|
|
|
$return_type->type_param = self::fleshOutType(
|
|
|
|
$codebase,
|
|
|
|
$return_type->type_param,
|
|
|
|
$self_class,
|
|
|
|
$static_class_type,
|
2020-03-24 23:14:10 +01:00
|
|
|
$parent_class,
|
2020-03-29 19:37:37 +02:00
|
|
|
$evaluate_class_constants,
|
|
|
|
$evaluate_conditional_types,
|
2020-03-24 23:14:10 +01:00
|
|
|
$final
|
2019-11-05 19:08:12 +01:00
|
|
|
);
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2019-03-08 00:02:31 +01:00
|
|
|
if ($return_type instanceof Type\Atomic\TCallable) {
|
|
|
|
if ($return_type->params) {
|
|
|
|
foreach ($return_type->params as $param) {
|
|
|
|
if ($param->type) {
|
|
|
|
$param->type = self::fleshOutType(
|
|
|
|
$codebase,
|
|
|
|
$param->type,
|
|
|
|
$self_class,
|
2019-05-25 17:51:09 +02:00
|
|
|
$static_class_type,
|
2020-03-24 23:14:10 +01:00
|
|
|
$parent_class,
|
2020-03-29 19:37:37 +02:00
|
|
|
$evaluate_class_constants,
|
|
|
|
$evaluate_conditional_types,
|
2020-03-24 23:14:10 +01:00
|
|
|
$final
|
2019-03-08 00:02:31 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($return_type->return_type) {
|
|
|
|
$return_type->return_type = self::fleshOutType(
|
|
|
|
$codebase,
|
|
|
|
$return_type->return_type,
|
|
|
|
$self_class,
|
2019-05-25 17:51:09 +02:00
|
|
|
$static_class_type,
|
2020-03-24 23:14:10 +01:00
|
|
|
$parent_class,
|
2020-03-29 19:37:37 +02:00
|
|
|
$evaluate_class_constants,
|
|
|
|
$evaluate_conditional_types,
|
2020-03-24 23:14:10 +01:00
|
|
|
$final
|
2019-03-08 00:02:31 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-29 19:37:37 +02:00
|
|
|
if ($return_type instanceof Type\Atomic\TConditional) {
|
|
|
|
if ($evaluate_conditional_types) {
|
|
|
|
$all_conditional_return_types = [];
|
2020-03-29 16:19:09 +02:00
|
|
|
|
2020-03-29 19:37:37 +02:00
|
|
|
foreach ($return_type->if_type->getAtomicTypes() as $if_atomic_type) {
|
|
|
|
$candidate = self::fleshOutAtomicType(
|
|
|
|
$codebase,
|
|
|
|
$if_atomic_type,
|
|
|
|
$self_class,
|
|
|
|
$static_class_type,
|
|
|
|
$parent_class,
|
|
|
|
$evaluate_class_constants,
|
|
|
|
$evaluate_conditional_types,
|
|
|
|
$final
|
2020-03-29 16:19:09 +02:00
|
|
|
);
|
|
|
|
|
2020-03-29 19:37:37 +02:00
|
|
|
if (is_array($candidate)) {
|
|
|
|
$all_conditional_return_types = array_merge(
|
|
|
|
$all_conditional_return_types,
|
|
|
|
$candidate
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$all_conditional_return_types[] = $candidate;
|
|
|
|
}
|
|
|
|
}
|
2020-03-29 16:19:09 +02:00
|
|
|
|
2020-03-29 19:37:37 +02:00
|
|
|
foreach ($return_type->else_type->getAtomicTypes() as $else_atomic_type) {
|
|
|
|
$candidate = self::fleshOutAtomicType(
|
|
|
|
$codebase,
|
|
|
|
$else_atomic_type,
|
|
|
|
$self_class,
|
|
|
|
$static_class_type,
|
|
|
|
$parent_class,
|
|
|
|
$evaluate_class_constants,
|
|
|
|
$evaluate_conditional_types,
|
|
|
|
$final
|
2020-03-29 16:19:09 +02:00
|
|
|
);
|
2020-03-29 19:37:37 +02:00
|
|
|
|
|
|
|
if (is_array($candidate)) {
|
|
|
|
$all_conditional_return_types = array_merge(
|
|
|
|
$all_conditional_return_types,
|
|
|
|
$candidate
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$all_conditional_return_types[] = $candidate;
|
|
|
|
}
|
2020-03-29 16:19:09 +02:00
|
|
|
}
|
|
|
|
|
2020-03-29 19:37:37 +02:00
|
|
|
foreach ($all_conditional_return_types as $i => $conditional_return_type) {
|
|
|
|
if ($conditional_return_type instanceof Type\Atomic\TVoid
|
|
|
|
&& count($all_conditional_return_types) > 1
|
|
|
|
) {
|
|
|
|
$all_conditional_return_types[$i] = new Type\Atomic\TNull();
|
|
|
|
$all_conditional_return_types[$i]->from_docblock = true;
|
|
|
|
}
|
2020-03-29 16:19:09 +02:00
|
|
|
}
|
2020-03-29 19:37:37 +02:00
|
|
|
|
|
|
|
return $all_conditional_return_types;
|
2020-03-29 16:19:09 +02:00
|
|
|
}
|
|
|
|
|
2020-03-29 19:37:37 +02:00
|
|
|
$return_type->conditional_type = self::fleshOutType(
|
|
|
|
$codebase,
|
|
|
|
$return_type->conditional_type,
|
|
|
|
$self_class,
|
|
|
|
$static_class_type,
|
|
|
|
$parent_class,
|
|
|
|
$evaluate_class_constants,
|
|
|
|
$evaluate_conditional_types,
|
|
|
|
$final
|
|
|
|
);
|
|
|
|
|
|
|
|
$return_type->if_type = self::fleshOutType(
|
|
|
|
$codebase,
|
|
|
|
$return_type->if_type,
|
|
|
|
$self_class,
|
|
|
|
$static_class_type,
|
|
|
|
$parent_class,
|
|
|
|
$evaluate_class_constants,
|
|
|
|
$evaluate_conditional_types,
|
|
|
|
$final
|
|
|
|
);
|
|
|
|
|
|
|
|
$return_type->else_type = self::fleshOutType(
|
|
|
|
$codebase,
|
|
|
|
$return_type->else_type,
|
|
|
|
$self_class,
|
|
|
|
$static_class_type,
|
|
|
|
$parent_class,
|
|
|
|
$evaluate_class_constants,
|
|
|
|
$evaluate_conditional_types,
|
|
|
|
$final
|
|
|
|
);
|
2020-03-29 16:19:09 +02:00
|
|
|
}
|
|
|
|
|
2016-11-01 19:14:35 +01:00
|
|
|
return $return_type;
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-11-11 18:01:14 +01:00
|
|
|
* @param StatementsAnalyzer $statements_analyzer
|
2016-11-02 07:29:00 +01:00
|
|
|
* @param PhpParser\Node\Expr\Closure $stmt
|
|
|
|
* @param Context $context
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-11-02 07:29:00 +01:00
|
|
|
* @return false|null
|
2016-10-22 19:23:18 +02:00
|
|
|
*/
|
2017-01-07 21:09:47 +01:00
|
|
|
protected static function analyzeClosureUses(
|
2018-11-11 18:01:14 +01:00
|
|
|
StatementsAnalyzer $statements_analyzer,
|
2016-11-02 07:29:00 +01:00
|
|
|
PhpParser\Node\Expr\Closure $stmt,
|
|
|
|
Context $context
|
|
|
|
) {
|
2019-01-30 17:36:21 +01:00
|
|
|
$param_names = array_map(
|
|
|
|
function (PhpParser\Node\Param $p) : string {
|
|
|
|
if (!$p->var instanceof PhpParser\Node\Expr\Variable
|
|
|
|
|| !is_string($p->var->name)
|
|
|
|
) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
return $p->var->name;
|
|
|
|
},
|
|
|
|
$stmt->params
|
|
|
|
);
|
|
|
|
|
2016-11-01 19:14:35 +01:00
|
|
|
foreach ($stmt->uses as $use) {
|
2018-05-08 23:42:02 +02:00
|
|
|
if (!is_string($use->var->name)) {
|
2018-04-17 18:16:25 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$use_var_id = '$' . $use->var->name;
|
|
|
|
|
2019-01-30 17:36:21 +01:00
|
|
|
if (in_array($use->var->name, $param_names)) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new DuplicateParam(
|
|
|
|
'Closure use duplicates param name ' . $use_var_id,
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $use->var)
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
if (!$context->hasVariable($use_var_id, $statements_analyzer)) {
|
2018-05-08 23:43:26 +02:00
|
|
|
if ($use_var_id === '$argv' || $use_var_id === '$argc') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-11-01 19:14:35 +01:00
|
|
|
if ($use->byRef) {
|
2017-02-08 00:09:12 +01:00
|
|
|
$context->vars_in_scope[$use_var_id] = Type::getMixed();
|
|
|
|
$context->vars_possibly_in_scope[$use_var_id] = true;
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
if (!$statements_analyzer->hasVariable($use_var_id)) {
|
|
|
|
$statements_analyzer->registerVariable(
|
2018-01-21 22:24:20 +01:00
|
|
|
$use_var_id,
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer, $use->var),
|
2018-01-21 22:24:20 +01:00
|
|
|
null
|
|
|
|
);
|
2017-02-08 00:09:12 +01:00
|
|
|
}
|
2017-05-25 04:07:49 +02:00
|
|
|
|
2016-10-22 19:23:18 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-08 23:43:26 +02:00
|
|
|
if (!isset($context->vars_possibly_in_scope[$use_var_id])) {
|
2016-11-01 19:14:35 +01:00
|
|
|
if ($context->check_variables) {
|
2017-12-06 06:35:41 +01:00
|
|
|
if (IssueBuffer::accepts(
|
2016-11-02 07:29:00 +01:00
|
|
|
new UndefinedVariable(
|
2017-02-08 00:09:12 +01:00
|
|
|
'Cannot find referenced variable ' . $use_var_id,
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $use->var)
|
2017-12-06 06:35:41 +01:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2017-12-06 06:35:41 +01:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-10-22 19:23:18 +02:00
|
|
|
|
2018-05-08 23:43:26 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$first_appearance = $statements_analyzer->getFirstAppearance($use_var_id);
|
2018-05-08 23:43:26 +02:00
|
|
|
|
|
|
|
if ($first_appearance) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new PossiblyUndefinedVariable(
|
|
|
|
'Possibly undefined variable ' . $use_var_id . ', first seen on line ' .
|
|
|
|
$first_appearance->getLineNumber(),
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $use->var)
|
2018-05-08 23:43:26 +02:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-05-08 23:43:26 +02:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($context->check_variables) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UndefinedVariable(
|
|
|
|
'Cannot find referenced variable ' . $use_var_id,
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $use->var)
|
2018-05-08 23:43:26 +02:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-05-08 23:43:26 +02:00
|
|
|
)) {
|
|
|
|
return false;
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
2018-05-08 23:43:26 +02:00
|
|
|
|
|
|
|
continue;
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
2018-05-12 00:09:11 +02:00
|
|
|
} elseif ($use->byRef) {
|
2019-09-17 00:42:44 +02:00
|
|
|
$context->remove($use_var_id);
|
|
|
|
|
|
|
|
$context->vars_in_scope[$use_var_id] = Type::getMixed();
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
|
|
|
}
|
2016-11-01 19:14:35 +01:00
|
|
|
|
2019-11-08 13:01:34 +01:00
|
|
|
/**
|
|
|
|
* @param StatementsAnalyzer $statements_analyzer
|
|
|
|
* @param PhpParser\Node\Expr\Print_ $stmt
|
|
|
|
* @param Context $context
|
|
|
|
*
|
|
|
|
* @return false|null
|
|
|
|
*/
|
|
|
|
protected static function analyzePrint(
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
|
|
|
PhpParser\Node\Expr\Print_ $stmt,
|
|
|
|
Context $context
|
|
|
|
) {
|
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
|
|
|
|
|
|
|
if (self::analyze($statements_analyzer, $stmt->expr, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
if ($stmt_expr_type = $statements_analyzer->node_data->getType($stmt->expr)) {
|
2019-11-08 13:01:34 +01:00
|
|
|
if (CallAnalyzer::checkFunctionArgumentType(
|
|
|
|
$statements_analyzer,
|
2019-11-25 17:44:54 +01:00
|
|
|
$stmt_expr_type,
|
2019-11-08 13:01:34 +01:00
|
|
|
Type::getString(),
|
|
|
|
null,
|
|
|
|
'print',
|
|
|
|
0,
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt->expr),
|
|
|
|
$stmt->expr,
|
|
|
|
$context,
|
|
|
|
new FunctionLikeParameter('var', false),
|
|
|
|
false,
|
2020-03-17 22:34:45 +01:00
|
|
|
null,
|
2019-11-08 13:01:34 +01:00
|
|
|
false,
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, Type::getInt(false, 1));
|
2019-11-08 13:01:34 +01:00
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-10-22 19:23:18 +02:00
|
|
|
/**
|
2018-11-11 18:01:14 +01:00
|
|
|
* @param StatementsAnalyzer $statements_analyzer
|
2016-11-02 07:29:00 +01:00
|
|
|
* @param PhpParser\Node\Expr\Yield_ $stmt
|
|
|
|
* @param Context $context
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-11-02 07:29:00 +01:00
|
|
|
* @return false|null
|
2016-10-22 19:23:18 +02:00
|
|
|
*/
|
2017-01-07 21:09:47 +01:00
|
|
|
protected static function analyzeYield(
|
2018-11-11 18:01:14 +01:00
|
|
|
StatementsAnalyzer $statements_analyzer,
|
2016-11-02 07:29:00 +01:00
|
|
|
PhpParser\Node\Expr\Yield_ $stmt,
|
|
|
|
Context $context
|
|
|
|
) {
|
2019-06-01 17:53:32 +02:00
|
|
|
$doc_comment = $stmt->getDocComment();
|
2017-03-02 04:27:52 +01:00
|
|
|
|
2018-02-08 05:33:31 +01:00
|
|
|
$var_comments = [];
|
|
|
|
$var_comment_type = null;
|
2017-05-25 07:32:34 +02:00
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
2018-11-06 03:57:36 +01:00
|
|
|
|
2019-06-01 17:53:32 +02:00
|
|
|
if ($doc_comment) {
|
2017-11-15 03:43:31 +01:00
|
|
|
try {
|
2018-11-06 03:57:36 +01:00
|
|
|
$var_comments = CommentAnalyzer::getTypeFromComment(
|
2019-06-01 17:53:32 +02:00
|
|
|
$doc_comment,
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer,
|
2019-06-01 18:25:57 +02:00
|
|
|
$statements_analyzer->getAliases()
|
2017-11-15 03:43:31 +01:00
|
|
|
);
|
|
|
|
} catch (DocblockParseException $e) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidDocblock(
|
|
|
|
(string)$e->getMessage(),
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
2017-11-15 03:43:31 +01:00
|
|
|
)
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
2017-05-25 07:32:34 +02:00
|
|
|
|
2018-02-08 05:33:31 +01:00
|
|
|
foreach ($var_comments as $var_comment) {
|
2019-08-21 17:25:08 +02:00
|
|
|
if (!$var_comment->type) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
$comment_type = ExpressionAnalyzer::fleshOutType(
|
|
|
|
$codebase,
|
2018-01-02 02:04:03 +01:00
|
|
|
$var_comment->type,
|
2018-01-26 19:51:00 +01:00
|
|
|
$context->self,
|
2019-05-25 17:51:09 +02:00
|
|
|
$context->self ? new Type\Atomic\TNamedObject($context->self) : null,
|
|
|
|
$statements_analyzer->getParentFQCLN()
|
2017-10-07 16:22:52 +02:00
|
|
|
);
|
|
|
|
|
2019-09-26 21:08:05 +02:00
|
|
|
$type_location = null;
|
|
|
|
|
|
|
|
if ($var_comment->type_start
|
|
|
|
&& $var_comment->type_end
|
|
|
|
&& $var_comment->line_number
|
|
|
|
) {
|
|
|
|
$type_location = new CodeLocation\DocblockTypeLocation(
|
|
|
|
$statements_analyzer,
|
|
|
|
$var_comment->type_start,
|
|
|
|
$var_comment->type_end,
|
|
|
|
$var_comment->line_number
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-02-08 05:33:31 +01:00
|
|
|
if (!$var_comment->var_id) {
|
|
|
|
$var_comment_type = $comment_type;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-09-19 17:59:43 +02:00
|
|
|
if ($codebase->find_unused_variables
|
2019-09-26 21:08:05 +02:00
|
|
|
&& $type_location
|
2019-09-19 17:59:43 +02:00
|
|
|
&& isset($context->vars_in_scope[$var_comment->var_id])
|
|
|
|
&& $context->vars_in_scope[$var_comment->var_id]->getId() === $comment_type->getId()
|
|
|
|
) {
|
2019-09-26 21:53:41 +02:00
|
|
|
$project_analyzer = $statements_analyzer->getProjectAnalyzer();
|
|
|
|
|
2019-09-26 21:08:05 +02:00
|
|
|
if ($codebase->alter_code
|
|
|
|
&& isset($project_analyzer->getIssuesToFix()['UnnecessaryVarAnnotation'])
|
|
|
|
) {
|
|
|
|
FileManipulationBuffer::addVarAnnotationToRemove($type_location);
|
|
|
|
} elseif (IssueBuffer::accepts(
|
2019-09-19 17:59:43 +02:00
|
|
|
new UnnecessaryVarAnnotation(
|
|
|
|
'The @var annotation for ' . $var_comment->var_id . ' is unnecessary',
|
2019-09-26 21:08:05 +02:00
|
|
|
$type_location
|
2019-12-02 21:24:01 +01:00
|
|
|
),
|
|
|
|
[],
|
|
|
|
true
|
2019-09-19 17:59:43 +02:00
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-07 16:22:52 +02:00
|
|
|
$context->vars_in_scope[$var_comment->var_id] = $comment_type;
|
2017-05-25 07:32:34 +02:00
|
|
|
}
|
2017-03-02 04:27:52 +01:00
|
|
|
}
|
2016-10-22 19:23:18 +02:00
|
|
|
|
|
|
|
if ($stmt->key) {
|
2019-08-13 19:15:23 +02:00
|
|
|
$context->inside_call = true;
|
2018-11-11 18:01:14 +01:00
|
|
|
if (self::analyze($statements_analyzer, $stmt->key, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
2019-08-13 19:15:23 +02:00
|
|
|
$context->inside_call = false;
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt->value) {
|
2019-08-13 19:15:23 +02:00
|
|
|
$context->inside_call = true;
|
2018-11-11 18:01:14 +01:00
|
|
|
if (self::analyze($statements_analyzer, $stmt->value, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
2019-08-13 19:15:23 +02:00
|
|
|
$context->inside_call = false;
|
2016-10-22 19:23:18 +02:00
|
|
|
|
2018-02-08 05:33:31 +01:00
|
|
|
if ($var_comment_type) {
|
2020-04-05 06:10:50 +02:00
|
|
|
$expression_type = clone $var_comment_type;
|
2019-11-25 17:44:54 +01:00
|
|
|
} elseif ($stmt_var_type = $statements_analyzer->node_data->getType($stmt->value)) {
|
2020-04-03 04:38:10 +02:00
|
|
|
$expression_type = clone $stmt_var_type;
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2020-04-03 04:38:10 +02:00
|
|
|
$expression_type = Type::getMixed();
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2020-04-03 04:38:10 +02:00
|
|
|
$expression_type = Type::getEmpty();
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
|
2020-04-03 04:38:10 +02:00
|
|
|
foreach ($expression_type->getAtomicTypes() as $expression_atomic_type) {
|
|
|
|
if ($expression_atomic_type instanceof Type\Atomic\TNamedObject) {
|
|
|
|
$classlike_storage = $codebase->classlike_storage_provider->get($expression_atomic_type->value);
|
|
|
|
|
|
|
|
if ($classlike_storage->yield) {
|
|
|
|
if ($expression_atomic_type instanceof Type\Atomic\TGenericObject) {
|
|
|
|
$yield_type = PropertyFetchAnalyzer::localizePropertyType(
|
|
|
|
$codebase,
|
2020-04-05 05:54:41 +02:00
|
|
|
clone $classlike_storage->yield,
|
2020-04-03 04:38:10 +02:00
|
|
|
$expression_atomic_type,
|
|
|
|
$classlike_storage,
|
|
|
|
$classlike_storage
|
|
|
|
);
|
2020-04-05 05:54:41 +02:00
|
|
|
} else {
|
|
|
|
$yield_type = Type::getMixed();
|
2020-04-03 04:38:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$expression_type->substitute($expression_type, $yield_type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$statements_analyzer->node_data->setType($stmt, $expression_type);
|
|
|
|
|
2019-02-06 21:52:43 +01:00
|
|
|
$source = $statements_analyzer->getSource();
|
|
|
|
|
|
|
|
if ($source instanceof FunctionLikeAnalyzer
|
|
|
|
&& !($source->getSource() instanceof TraitAnalyzer)
|
|
|
|
) {
|
2019-03-04 00:21:12 +01:00
|
|
|
$source->examineParamTypes($statements_analyzer, $context, $codebase, $stmt);
|
2019-02-06 21:52:43 +01:00
|
|
|
|
|
|
|
$storage = $source->getFunctionLikeStorage($statements_analyzer);
|
|
|
|
|
|
|
|
if ($storage->return_type) {
|
2020-01-04 18:20:26 +01:00
|
|
|
foreach ($storage->return_type->getAtomicTypes() as $atomic_return_type) {
|
2020-04-03 04:38:10 +02:00
|
|
|
if ($atomic_return_type instanceof Type\Atomic\TNamedObject
|
2019-02-06 21:52:43 +01:00
|
|
|
&& $atomic_return_type->value === 'Generator'
|
|
|
|
) {
|
2020-04-03 04:38:10 +02:00
|
|
|
if ($atomic_return_type instanceof Type\Atomic\TGenericObject) {
|
|
|
|
if (!$atomic_return_type->type_params[2]->isVoid()) {
|
|
|
|
$statements_analyzer->node_data->setType(
|
|
|
|
$stmt,
|
|
|
|
Type::combineUnionTypes(
|
2020-04-05 06:10:50 +02:00
|
|
|
clone $atomic_return_type->type_params[2],
|
2020-04-03 04:38:10 +02:00
|
|
|
$expression_type,
|
|
|
|
$codebase
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$statements_analyzer->node_data->setType(
|
|
|
|
$stmt,
|
|
|
|
Type::combineUnionTypes(
|
|
|
|
Type::getMixed(),
|
|
|
|
$expression_type
|
|
|
|
)
|
|
|
|
);
|
2019-02-06 21:52:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-11-11 18:01:14 +01:00
|
|
|
* @param StatementsAnalyzer $statements_analyzer
|
2016-11-02 07:29:00 +01:00
|
|
|
* @param PhpParser\Node\Expr\YieldFrom $stmt
|
|
|
|
* @param Context $context
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-11-02 07:29:00 +01:00
|
|
|
* @return false|null
|
2016-10-22 19:23:18 +02:00
|
|
|
*/
|
2017-01-07 21:09:47 +01:00
|
|
|
protected static function analyzeYieldFrom(
|
2018-11-11 18:01:14 +01:00
|
|
|
StatementsAnalyzer $statements_analyzer,
|
2016-11-02 07:29:00 +01:00
|
|
|
PhpParser\Node\Expr\YieldFrom $stmt,
|
|
|
|
Context $context
|
|
|
|
) {
|
2020-02-18 01:21:04 +01:00
|
|
|
$was_inside_call = $context->inside_call;
|
|
|
|
|
|
|
|
$context->inside_call = true;
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
if (self::analyze($statements_analyzer, $stmt->expr, $context) === false) {
|
2020-02-18 01:21:04 +01:00
|
|
|
$context->inside_call = $was_inside_call;
|
|
|
|
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
if ($stmt_expr_type = $statements_analyzer->node_data->getType($stmt->expr)) {
|
2018-07-13 23:34:44 +02:00
|
|
|
$yield_from_type = null;
|
|
|
|
|
2020-01-04 18:20:26 +01:00
|
|
|
foreach ($stmt_expr_type->getAtomicTypes() as $atomic_type) {
|
2019-02-09 21:48:15 +01:00
|
|
|
if ($yield_from_type === null) {
|
|
|
|
if ($atomic_type instanceof Type\Atomic\TGenericObject
|
|
|
|
&& strtolower($atomic_type->value) === 'generator'
|
|
|
|
&& isset($atomic_type->type_params[3])
|
|
|
|
) {
|
|
|
|
$yield_from_type = clone $atomic_type->type_params[3];
|
|
|
|
} elseif ($atomic_type instanceof Type\Atomic\TArray) {
|
|
|
|
$yield_from_type = clone $atomic_type->type_params[1];
|
|
|
|
} elseif ($atomic_type instanceof Type\Atomic\ObjectLike) {
|
|
|
|
$yield_from_type = $atomic_type->getGenericValueType();
|
|
|
|
}
|
2018-07-13 23:34:44 +02:00
|
|
|
} else {
|
|
|
|
$yield_from_type = Type::getMixed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-15 04:29:15 +01:00
|
|
|
// this should be whatever the generator above returns, but *not* the return type
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, $yield_from_type ?: Type::getMixed());
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
|
2020-02-18 01:21:04 +01:00
|
|
|
$context->inside_call = $was_inside_call;
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
2018-11-11 18:01:14 +01:00
|
|
|
* @param StatementsAnalyzer $statements_analyzer
|
2016-11-02 07:29:00 +01:00
|
|
|
* @param PhpParser\Node\Expr\BooleanNot $stmt
|
|
|
|
* @param Context $context
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-11-02 07:29:00 +01:00
|
|
|
* @return false|null
|
|
|
|
*/
|
2017-01-07 21:09:47 +01:00
|
|
|
protected static function analyzeBooleanNot(
|
2018-11-11 18:01:14 +01:00
|
|
|
StatementsAnalyzer $statements_analyzer,
|
2016-11-02 07:29:00 +01:00
|
|
|
PhpParser\Node\Expr\BooleanNot $stmt,
|
|
|
|
Context $context
|
|
|
|
) {
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, Type::getBool());
|
2016-12-28 22:29:50 +01:00
|
|
|
|
2019-03-16 02:50:16 +01:00
|
|
|
$inside_negation = $context->inside_negation;
|
|
|
|
|
|
|
|
$context->inside_negation = !$inside_negation;
|
2019-02-26 07:03:33 +01:00
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
if (self::analyze($statements_analyzer, $stmt->expr, $context) === false) {
|
2016-12-28 22:29:50 +01:00
|
|
|
return false;
|
|
|
|
}
|
2019-03-16 02:50:16 +01:00
|
|
|
|
|
|
|
$context->inside_negation = $inside_negation;
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
2018-11-11 18:01:14 +01:00
|
|
|
* @param StatementsAnalyzer $statements_analyzer
|
2016-11-02 07:29:00 +01:00
|
|
|
* @param PhpParser\Node\Expr\Empty_ $stmt
|
|
|
|
* @param Context $context
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-12-05 18:14:10 +01:00
|
|
|
* @return void
|
2016-11-02 07:29:00 +01:00
|
|
|
*/
|
2017-01-07 21:09:47 +01:00
|
|
|
protected static function analyzeEmpty(
|
2018-11-11 18:01:14 +01:00
|
|
|
StatementsAnalyzer $statements_analyzer,
|
2016-11-02 07:29:00 +01:00
|
|
|
PhpParser\Node\Expr\Empty_ $stmt,
|
|
|
|
Context $context
|
|
|
|
) {
|
2018-11-11 18:01:14 +01:00
|
|
|
self::analyzeIssetVar($statements_analyzer, $stmt->expr, $context);
|
2019-11-06 22:14:46 +01:00
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
if (($stmt_expr_type = $statements_analyzer->node_data->getType($stmt->expr))
|
|
|
|
&& $stmt_expr_type->hasBool()
|
|
|
|
&& $stmt_expr_type->isSingle()
|
|
|
|
&& !$stmt_expr_type->from_docblock
|
2019-11-06 22:14:46 +01:00
|
|
|
) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new \Psalm\Issue\InvalidArgument(
|
|
|
|
'Calling empty on a boolean value is almost certainly unintended',
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt->expr),
|
|
|
|
'empty'
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, Type::getBool());
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-11-11 18:01:14 +01:00
|
|
|
* @param StatementsAnalyzer $statements_analyzer
|
2016-11-02 07:29:00 +01:00
|
|
|
* @param PhpParser\Node\Scalar\Encapsed $stmt
|
|
|
|
* @param Context $context
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-11-02 07:29:00 +01:00
|
|
|
* @return false|null
|
2016-10-22 19:23:18 +02:00
|
|
|
*/
|
2017-01-07 21:09:47 +01:00
|
|
|
protected static function analyzeEncapsulatedString(
|
2018-11-11 18:01:14 +01:00
|
|
|
StatementsAnalyzer $statements_analyzer,
|
2016-11-02 07:29:00 +01:00
|
|
|
PhpParser\Node\Scalar\Encapsed $stmt,
|
|
|
|
Context $context
|
|
|
|
) {
|
2016-10-22 19:23:18 +02:00
|
|
|
foreach ($stmt->parts as $part) {
|
2018-11-11 18:01:14 +01:00
|
|
|
if (self::analyze($statements_analyzer, $part, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
2017-09-03 01:48:59 +02:00
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
if ($statements_analyzer->node_data->getType($part)) {
|
2019-03-17 21:31:56 +01:00
|
|
|
self::castStringAttempt($statements_analyzer, $part);
|
|
|
|
}
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, Type::getString());
|
2017-05-25 04:07:49 +02:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
|
2019-03-17 21:31:56 +01:00
|
|
|
private static function castStringAttempt(
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
2020-01-30 04:28:40 +01:00
|
|
|
PhpParser\Node\Expr $stmt,
|
|
|
|
bool $explicit_cast = false
|
|
|
|
) : Type\Union {
|
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
if (!($stmt_type = $statements_analyzer->node_data->getType($stmt))) {
|
2020-01-30 04:28:40 +01:00
|
|
|
return Type::getString();
|
2019-03-17 21:31:56 +01:00
|
|
|
}
|
|
|
|
|
2019-03-17 22:10:51 +01:00
|
|
|
$invalid_casts = [];
|
2020-01-30 04:28:40 +01:00
|
|
|
$valid_strings = [];
|
|
|
|
$castable_types = [];
|
2019-03-17 22:10:51 +01:00
|
|
|
|
2020-02-21 21:17:35 +01:00
|
|
|
$atomic_types = $stmt_type->getAtomicTypes();
|
|
|
|
|
|
|
|
while ($atomic_types) {
|
2020-02-21 21:44:26 +01:00
|
|
|
$atomic_type = \array_pop($atomic_types);
|
2020-02-21 21:17:35 +01:00
|
|
|
|
2020-01-30 04:28:40 +01:00
|
|
|
if ($atomic_type instanceof TString) {
|
|
|
|
$valid_strings[] = $atomic_type;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($atomic_type instanceof TMixed
|
|
|
|
|| $atomic_type instanceof Type\Atomic\TResource
|
|
|
|
|| $atomic_type instanceof Type\Atomic\TNull
|
|
|
|
|| $atomic_type instanceof Type\Atomic\Scalar
|
2019-03-17 21:31:56 +01:00
|
|
|
) {
|
2020-01-30 04:28:40 +01:00
|
|
|
$castable_types[] = new TString();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($atomic_type instanceof TNamedObject
|
2020-02-15 02:54:26 +01:00
|
|
|
&& $codebase->methods->methodExists(
|
|
|
|
new \Psalm\Internal\MethodIdentifier(
|
|
|
|
$atomic_type->value,
|
|
|
|
'__tostring'
|
|
|
|
)
|
|
|
|
)
|
2020-01-30 04:28:40 +01:00
|
|
|
) {
|
2020-01-30 04:31:23 +01:00
|
|
|
$return_type = $codebase->methods->getMethodReturnType(
|
2020-02-15 02:54:26 +01:00
|
|
|
new \Psalm\Internal\MethodIdentifier(
|
|
|
|
$atomic_type->value,
|
|
|
|
'__tostring'
|
|
|
|
),
|
2020-01-30 04:31:23 +01:00
|
|
|
$self_class
|
|
|
|
);
|
2020-01-30 04:28:40 +01:00
|
|
|
|
|
|
|
if ($return_type) {
|
|
|
|
$castable_types = array_merge(
|
|
|
|
$castable_types,
|
|
|
|
array_values($return_type->getAtomicTypes())
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$castable_types[] = new TString();
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
2019-03-17 22:10:51 +01:00
|
|
|
}
|
2020-01-30 04:28:40 +01:00
|
|
|
|
|
|
|
if ($atomic_type instanceof Type\Atomic\TObjectWithProperties
|
|
|
|
&& isset($atomic_type->methods['__toString'])
|
|
|
|
) {
|
|
|
|
$castable_types[] = new TString();
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-02-21 21:17:35 +01:00
|
|
|
if ($atomic_type instanceof Type\Atomic\TTemplateParam) {
|
|
|
|
$atomic_types = array_merge($atomic_types, $atomic_type->as->getAtomicTypes());
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-01-30 04:28:40 +01:00
|
|
|
$invalid_casts[] = $atomic_type->getId();
|
2019-03-17 22:10:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($invalid_casts) {
|
2020-01-30 04:28:40 +01:00
|
|
|
if ($valid_strings || $castable_types) {
|
2019-03-17 22:10:51 +01:00
|
|
|
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 {
|
2019-03-17 21:31:56 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidCast(
|
2019-03-17 22:10:51 +01:00
|
|
|
$invalid_casts[0] . ' cannot be cast to string',
|
2019-03-17 21:31:56 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
2020-01-30 04:28:40 +01:00
|
|
|
} elseif ($explicit_cast && !$castable_types) {
|
|
|
|
// todo: emit error here
|
2019-03-17 21:31:56 +01:00
|
|
|
}
|
2020-01-30 04:28:40 +01:00
|
|
|
|
2020-01-30 05:41:17 +01:00
|
|
|
$valid_types = array_merge($valid_strings, $castable_types);
|
|
|
|
|
|
|
|
if (!$valid_types) {
|
|
|
|
return Type::getString();
|
|
|
|
}
|
|
|
|
|
2020-01-30 04:28:40 +01:00
|
|
|
return \Psalm\Internal\Type\TypeCombination::combineTypes(
|
2020-01-30 05:41:17 +01:00
|
|
|
$valid_types,
|
2020-01-30 04:28:40 +01:00
|
|
|
$codebase
|
|
|
|
);
|
2019-03-17 21:31:56 +01:00
|
|
|
}
|
|
|
|
|
2017-01-15 19:58:20 +01:00
|
|
|
/**
|
2018-11-11 18:01:14 +01:00
|
|
|
* @param StatementsAnalyzer $statements_analyzer
|
2017-01-15 19:58:20 +01:00
|
|
|
* @param PhpParser\Node\Expr\Isset_ $stmt
|
|
|
|
* @param Context $context
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-01-15 19:58:20 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected static function analyzeIsset(
|
2018-11-11 18:01:14 +01:00
|
|
|
StatementsAnalyzer $statements_analyzer,
|
2017-01-15 19:58:20 +01:00
|
|
|
PhpParser\Node\Expr\Isset_ $stmt,
|
|
|
|
Context $context
|
|
|
|
) {
|
|
|
|
foreach ($stmt->vars as $isset_var) {
|
2018-04-17 18:16:25 +02:00
|
|
|
if ($isset_var instanceof PhpParser\Node\Expr\PropertyFetch
|
|
|
|
&& $isset_var->var instanceof PhpParser\Node\Expr\Variable
|
|
|
|
&& $isset_var->var->name === 'this'
|
|
|
|
&& $isset_var->name instanceof PhpParser\Node\Identifier
|
2017-01-15 19:58:20 +01:00
|
|
|
) {
|
2018-04-17 18:16:25 +02:00
|
|
|
$var_id = '$this->' . $isset_var->name->name;
|
2017-04-28 22:41:54 +02:00
|
|
|
|
|
|
|
if (!isset($context->vars_in_scope[$var_id])) {
|
|
|
|
$context->vars_in_scope[$var_id] = Type::getMixed();
|
|
|
|
$context->vars_possibly_in_scope[$var_id] = true;
|
|
|
|
}
|
2017-01-15 19:58:20 +01:00
|
|
|
}
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
self::analyzeIssetVar($statements_analyzer, $isset_var, $context);
|
2017-01-15 19:58:20 +01:00
|
|
|
}
|
2018-04-09 16:20:13 +02:00
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, Type::getBool());
|
2017-01-15 19:58:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-11-11 18:01:14 +01:00
|
|
|
* @param StatementsAnalyzer $statements_analyzer
|
2017-01-15 19:58:20 +01:00
|
|
|
* @param PhpParser\Node\Expr $stmt
|
|
|
|
* @param Context $context
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2018-01-10 01:33:39 +01:00
|
|
|
* @return false|null
|
2017-01-15 19:58:20 +01:00
|
|
|
*/
|
|
|
|
protected static function analyzeIssetVar(
|
2018-11-11 18:01:14 +01:00
|
|
|
StatementsAnalyzer $statements_analyzer,
|
2017-01-15 19:58:20 +01:00
|
|
|
PhpParser\Node\Expr $stmt,
|
|
|
|
Context $context
|
|
|
|
) {
|
2018-05-01 04:13:13 +02:00
|
|
|
|
2018-01-10 01:33:39 +01:00
|
|
|
$context->inside_isset = true;
|
2017-01-15 19:58:20 +01:00
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
if (self::analyze($statements_analyzer, $stmt, $context) === false) {
|
2018-01-10 01:33:39 +01:00
|
|
|
return false;
|
2017-01-15 19:58:20 +01:00
|
|
|
}
|
2018-01-10 01:33:39 +01:00
|
|
|
|
|
|
|
$context->inside_isset = false;
|
2017-01-15 19:58:20 +01:00
|
|
|
}
|
|
|
|
|
2017-01-16 18:59:09 +01:00
|
|
|
/**
|
2018-11-11 18:01:14 +01:00
|
|
|
* @param StatementsAnalyzer $statements_analyzer
|
2017-01-16 18:59:09 +01:00
|
|
|
* @param PhpParser\Node\Expr\Clone_ $stmt
|
|
|
|
* @param Context $context
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-01-16 18:59:09 +01:00
|
|
|
* @return false|null
|
|
|
|
*/
|
|
|
|
protected static function analyzeClone(
|
2018-11-11 18:01:14 +01:00
|
|
|
StatementsAnalyzer $statements_analyzer,
|
2017-01-16 18:59:09 +01:00
|
|
|
PhpParser\Node\Expr\Clone_ $stmt,
|
|
|
|
Context $context
|
|
|
|
) {
|
2018-11-11 18:01:14 +01:00
|
|
|
if (self::analyze($statements_analyzer, $stmt->expr, $context) === false) {
|
2017-01-16 18:59:09 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
$stmt_expr_type = $statements_analyzer->node_data->getType($stmt->expr);
|
|
|
|
|
|
|
|
if ($stmt_expr_type) {
|
|
|
|
$clone_type = $stmt_expr_type;
|
2019-04-11 20:53:21 +02:00
|
|
|
|
2019-09-09 17:14:40 +02:00
|
|
|
$immutable_cloned = false;
|
|
|
|
|
2020-01-04 18:20:26 +01:00
|
|
|
foreach ($clone_type->getAtomicTypes() as $clone_type_part) {
|
2018-04-20 16:52:23 +02:00
|
|
|
if (!$clone_type_part instanceof TNamedObject
|
|
|
|
&& !$clone_type_part instanceof TObject
|
|
|
|
&& !$clone_type_part instanceof TMixed
|
2019-02-22 03:40:06 +01:00
|
|
|
&& !$clone_type_part instanceof TTemplateParam
|
2017-01-19 21:00:05 +01:00
|
|
|
) {
|
2019-04-11 20:53:21 +02:00
|
|
|
if ($clone_type_part instanceof Type\Atomic\TFalse
|
|
|
|
&& $clone_type->ignore_falsable_issues
|
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($clone_type_part instanceof Type\Atomic\TNull
|
|
|
|
&& $clone_type->ignore_nullable_issues
|
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-01-16 18:59:09 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidClone(
|
|
|
|
'Cannot clone ' . $clone_type_part,
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $stmt)
|
2017-01-16 18:59:09 +01:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2017-01-16 18:59:09 +01:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2019-09-09 17:14:40 +02:00
|
|
|
|
2020-03-14 06:09:12 +01:00
|
|
|
if ($clone_type_part instanceof TNamedObject) {
|
|
|
|
$immutable_cloned = true;
|
2019-09-09 17:14:40 +02:00
|
|
|
}
|
2017-01-16 18:59:09 +01:00
|
|
|
}
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, $stmt_expr_type);
|
2019-09-09 17:14:40 +02:00
|
|
|
|
|
|
|
if ($immutable_cloned) {
|
2019-11-25 17:44:54 +01:00
|
|
|
$stmt_expr_type = clone $stmt_expr_type;
|
|
|
|
$statements_analyzer->node_data->setType($stmt, $stmt_expr_type);
|
2020-03-14 06:09:12 +01:00
|
|
|
$stmt_expr_type->reference_free = true;
|
|
|
|
$stmt_expr_type->allow_mutations = true;
|
2019-09-09 17:14:40 +02:00
|
|
|
}
|
2017-01-16 18:59:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-22 19:23:18 +02:00
|
|
|
/**
|
2016-11-07 23:29:51 +01:00
|
|
|
* @param string $fq_class_name
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-05-25 04:07:49 +02:00
|
|
|
* @return bool
|
2016-10-22 19:23:18 +02:00
|
|
|
*/
|
2016-11-07 23:29:51 +01:00
|
|
|
public static function isMock($fq_class_name)
|
2016-10-22 19:23:18 +02:00
|
|
|
{
|
2018-12-18 05:29:27 +01:00
|
|
|
return in_array(strtolower($fq_class_name), Config::getInstance()->getMockClasses(), true);
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
}
|