1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-15 10:57:08 +01:00
psalm/src/Psalm/Internal/Analyzer/Statements/ExpressionAnalyzer.php

407 lines
13 KiB
PHP
Raw Normal View History

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\Internal\Analyzer\ClosureAnalyzer;
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\StatementsAnalyzer;
use Psalm\CodeLocation;
2016-10-22 19:23:18 +02:00
use Psalm\Config;
use Psalm\Context;
use Psalm\Internal\FileManipulation\FileManipulationBuffer;
2016-10-22 19:23:18 +02:00
use Psalm\Issue\ForbiddenCode;
2016-11-06 01:53:39 +01:00
use Psalm\Issue\UnrecognizedExpression;
2016-11-02 07:29:00 +01:00
use Psalm\IssueBuffer;
2016-10-22 19:23:18 +02:00
use Psalm\Type;
use function in_array;
use function strtolower;
use function get_class;
2016-10-22 19:23:18 +02:00
/**
* @internal
*/
2018-11-06 03:57:36 +01:00
class ExpressionAnalyzer
2016-10-22 19:23:18 +02: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,
2020-05-18 21:13:27 +02:00
bool $array_assignment = false,
?Context $global_context = null,
bool $from_stmt = false
2020-05-18 21:13:27 +02:00
) : bool {
2018-11-11 18:01:14 +01:00
$codebase = $statements_analyzer->getCodebase();
2018-11-06 03:57:36 +01:00
2020-05-18 21:13:27 +02:00
if (self::handleExpression(
$statements_analyzer,
$stmt,
$context,
$array_assignment,
$global_context,
$from_stmt
) === false
) {
2020-05-18 21:13:27 +02:00
return false;
2016-10-22 19:23:18 +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)
) {
$assertions = $statements_analyzer->node_data->getAssertions($stmt);
if ($assertions === null) {
2020-05-18 21:13:27 +02:00
Expression\AssertionFinder::scrapeAssertions(
$stmt,
$context->self,
$statements_analyzer,
$codebase,
false,
true,
false
);
}
2020-05-18 21:13:27 +02:00
}
2016-10-22 19:23:18 +02:00
2020-05-18 21:13:27 +02:00
$plugin_classes = $codebase->config->after_expression_checks;
2018-05-08 23:43:26 +02:00
2020-05-18 21:13:27 +02:00
if ($plugin_classes) {
$file_manipulations = [];
2018-05-08 23:43:26 +02:00
2020-05-18 21:13:27 +02:00
foreach ($plugin_classes as $plugin_fq_class_name) {
if ($plugin_fq_class_name::afterExpressionAnalysis(
$stmt,
$context,
$statements_analyzer,
$codebase,
$file_manipulations
) === false) {
return false;
2016-11-01 19:14:35 +01:00
}
2020-05-18 21:13:27 +02:00
}
2020-05-18 21:13:27 +02:00
if ($file_manipulations) {
FileManipulationBuffer::add($statements_analyzer->getFilePath(), $file_manipulations);
2016-10-22 19:23:18 +02:00
}
}
2016-11-01 19:14:35 +01:00
2020-05-18 21:13:27 +02:00
return true;
2016-11-02 07:29:00 +01:00
}
2016-11-01 19:14:35 +01:00
2020-05-18 21:13:27 +02:00
private static function handleExpression(
StatementsAnalyzer $statements_analyzer,
2020-05-18 21:13:27 +02:00
PhpParser\Node\Expr $stmt,
Context $context,
bool $array_assignment,
?Context $global_context,
bool $from_stmt
) : bool {
if ($stmt instanceof PhpParser\Node\Expr\Variable) {
return Expression\Fetch\VariableFetchAnalyzer::analyze(
$statements_analyzer,
$stmt,
$context,
false,
null,
$array_assignment
);
}
2020-05-18 21:13:27 +02:00
if ($stmt instanceof PhpParser\Node\Expr\Assign) {
$assignment_type = Expression\AssignmentAnalyzer::analyze(
$statements_analyzer,
2020-05-18 21:13:27 +02:00
$stmt->var,
$stmt->expr,
2020-03-17 22:34:45 +01:00
null,
2020-05-18 21:13:27 +02:00
$context,
$stmt->getDocComment()
);
if ($assignment_type === false) {
return false;
}
2020-05-18 21:13:27 +02:00
if (!$from_stmt) {
$statements_analyzer->node_data->setType($stmt, $assignment_type);
}
2020-05-18 21:13:27 +02:00
return true;
}
2020-05-18 21:13:27 +02:00
if ($stmt instanceof PhpParser\Node\Expr\AssignOp) {
return Expression\AssignmentAnalyzer::analyzeAssignmentOperation($statements_analyzer, $stmt, $context);
}
2020-05-18 21:13:27 +02:00
if ($stmt instanceof PhpParser\Node\Expr\MethodCall) {
return MethodCallAnalyzer::analyze($statements_analyzer, $stmt, $context);
}
2020-05-18 21:13:27 +02:00
if ($stmt instanceof PhpParser\Node\Expr\StaticCall) {
return StaticCallAnalyzer::analyze($statements_analyzer, $stmt, $context);
}
2017-03-02 04:27:52 +01:00
2020-05-18 21:13:27 +02:00
if ($stmt instanceof PhpParser\Node\Expr\ConstFetch) {
Expression\Fetch\ConstFetchAnalyzer::analyze($statements_analyzer, $stmt, $context);
2020-05-18 21:13:27 +02:00
return true;
}
2018-11-06 03:57:36 +01:00
2020-05-18 21:13:27 +02:00
if ($stmt instanceof PhpParser\Node\Scalar\String_) {
$statements_analyzer->node_data->setType($stmt, Type::getString($stmt->value));
2020-05-18 21:13:27 +02:00
return true;
}
2020-05-18 21:13:27 +02:00
if ($stmt instanceof PhpParser\Node\Scalar\EncapsedStringPart) {
return true;
}
2020-05-18 21:13:27 +02:00
if ($stmt instanceof PhpParser\Node\Scalar\MagicConst) {
Expression\MagicConstAnalyzer::analyze($statements_analyzer, $stmt, $context);
2020-05-18 21:13:27 +02:00
return true;
}
2020-05-18 21:13:27 +02:00
if ($stmt instanceof PhpParser\Node\Scalar\LNumber) {
$statements_analyzer->node_data->setType($stmt, Type::getInt(false, $stmt->value));
2020-05-18 21:13:27 +02:00
return true;
2017-03-02 04:27:52 +01:00
}
2016-10-22 19:23:18 +02:00
2020-05-18 21:13:27 +02:00
if ($stmt instanceof PhpParser\Node\Scalar\DNumber) {
$statements_analyzer->node_data->setType($stmt, Type::getFloat($stmt->value));
2016-10-22 19:23:18 +02:00
2020-05-18 21:13:27 +02:00
return true;
2016-10-22 19:23:18 +02:00
}
2016-11-02 07:29:00 +01:00
2020-05-18 21:13:27 +02:00
if ($stmt instanceof PhpParser\Node\Expr\UnaryMinus || $stmt instanceof PhpParser\Node\Expr\UnaryPlus) {
return Expression\UnaryPlusMinusAnalyzer::analyze($statements_analyzer, $stmt, $context);
}
2020-05-18 21:13:27 +02:00
if ($stmt instanceof PhpParser\Node\Expr\Isset_) {
Expression\IssetAnalyzer::analyze($statements_analyzer, $stmt, $context);
$statements_analyzer->node_data->setType($stmt, Type::getBool());
2019-02-06 21:52:43 +01:00
2020-05-18 21:13:27 +02:00
return true;
2019-02-06 21:52:43 +01:00
}
2020-05-18 21:13:27 +02:00
if ($stmt instanceof PhpParser\Node\Expr\ClassConstFetch) {
return Expression\Fetch\ClassConstFetchAnalyzer::analyze($statements_analyzer, $stmt, $context);
}
2016-10-22 19:23:18 +02:00
2020-05-18 21:13:27 +02:00
if ($stmt instanceof PhpParser\Node\Expr\PropertyFetch) {
return Expression\Fetch\InstancePropertyFetchAnalyzer::analyze(
2020-05-18 21:13:27 +02:00
$statements_analyzer,
$stmt,
$context,
$array_assignment
2020-05-18 21:13:27 +02:00
);
}
2020-05-18 21:13:27 +02:00
if ($stmt instanceof PhpParser\Node\Expr\StaticPropertyFetch) {
return Expression\Fetch\StaticPropertyFetchAnalyzer::analyze(
2020-05-18 21:13:27 +02:00
$statements_analyzer,
$stmt,
$context
);
}
2020-05-18 21:13:27 +02:00
if ($stmt instanceof PhpParser\Node\Expr\BitwiseNot) {
return Expression\BitwiseNotAnalyzer::analyze($statements_analyzer, $stmt, $context);
}
2020-05-18 21:13:27 +02:00
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp) {
return Expression\BinaryOpAnalyzer::analyze(
$statements_analyzer,
$stmt,
$context,
0,
$from_stmt
);
2016-10-22 19:23:18 +02:00
}
2020-05-18 21:13:27 +02:00
if ($stmt instanceof PhpParser\Node\Expr\PostInc
|| $stmt instanceof PhpParser\Node\Expr\PostDec
|| $stmt instanceof PhpParser\Node\Expr\PreInc
|| $stmt instanceof PhpParser\Node\Expr\PreDec
) {
Expression\IncDecExpressionAnalyzer::analyze($statements_analyzer, $stmt, $context);
2020-05-18 21:13:27 +02:00
return true;
2016-10-22 19:23:18 +02:00
}
2016-11-02 07:29:00 +01:00
2020-05-18 21:13:27 +02:00
if ($stmt instanceof PhpParser\Node\Expr\New_) {
return NewAnalyzer::analyze($statements_analyzer, $stmt, $context);
}
2020-05-18 21:13:27 +02:00
if ($stmt instanceof PhpParser\Node\Expr\Array_) {
return Expression\ArrayAnalyzer::analyze($statements_analyzer, $stmt, $context);
}
2016-10-22 19:23:18 +02:00
2020-05-18 21:13:27 +02:00
if ($stmt instanceof PhpParser\Node\Scalar\Encapsed) {
return Expression\EncapsulatedStringAnalyzer::analyze($statements_analyzer, $stmt, $context);
}
2016-12-28 22:29:50 +01:00
2020-05-18 21:13:27 +02:00
if ($stmt instanceof PhpParser\Node\Expr\FuncCall) {
return FunctionCallAnalyzer::analyze(
$statements_analyzer,
$stmt,
$context
);
}
2019-03-16 02:50:16 +01:00
2020-05-18 21:13:27 +02:00
if ($stmt instanceof PhpParser\Node\Expr\Ternary) {
return Expression\TernaryAnalyzer::analyze($statements_analyzer, $stmt, $context);
}
2020-05-18 21:13:27 +02:00
if ($stmt instanceof PhpParser\Node\Expr\BooleanNot) {
return Expression\BooleanNotAnalyzer::analyze($statements_analyzer, $stmt, $context);
2016-12-28 22:29:50 +01:00
}
2019-03-16 02:50:16 +01:00
2020-05-18 21:13:27 +02:00
if ($stmt instanceof PhpParser\Node\Expr\Empty_) {
Expression\EmptyAnalyzer::analyze($statements_analyzer, $stmt, $context);
2016-10-22 19:23:18 +02:00
2020-05-18 21:13:27 +02:00
return true;
}
2020-05-18 21:13:27 +02:00
if ($stmt instanceof PhpParser\Node\Expr\Closure
|| $stmt instanceof PhpParser\Node\Expr\ArrowFunction
) {
ClosureAnalyzer::analyzeExpression($statements_analyzer, $stmt, $context);
2020-05-18 21:13:27 +02:00
return true;
2016-10-22 19:23:18 +02:00
}
2020-05-18 21:13:27 +02:00
if ($stmt instanceof PhpParser\Node\Expr\ArrayDimFetch) {
return Expression\Fetch\ArrayFetchAnalyzer::analyze(
$statements_analyzer,
$stmt,
$context
);
}
2016-10-22 19:23:18 +02:00
2020-05-18 21:13:27 +02:00
if ($stmt instanceof PhpParser\Node\Expr\Cast) {
return Expression\CastAnalyzer::analyze($statements_analyzer, $stmt, $context);
}
2020-05-18 21:13:27 +02:00
if ($stmt instanceof PhpParser\Node\Expr\Clone_) {
return Expression\CloneAnalyzer::analyze($statements_analyzer, $stmt, $context);
}
2020-05-18 21:13:27 +02:00
if ($stmt instanceof PhpParser\Node\Expr\Instanceof_) {
return Expression\InstanceofAnalyzer::analyze($statements_analyzer, $stmt, $context);
}
2020-05-18 21:13:27 +02:00
if ($stmt instanceof PhpParser\Node\Expr\Exit_) {
2020-06-25 23:17:08 +02:00
return Expression\ExitAnalyzer::analyze($statements_analyzer, $stmt, $context);
2020-05-18 21:13:27 +02:00
}
2020-05-18 21:13:27 +02:00
if ($stmt instanceof PhpParser\Node\Expr\Include_) {
return Expression\IncludeAnalyzer::analyze($statements_analyzer, $stmt, $context, $global_context);
}
2020-05-18 21:13:27 +02:00
if ($stmt instanceof PhpParser\Node\Expr\Eval_) {
2020-05-23 06:03:29 +02:00
Expression\EvalAnalyzer::analyze($statements_analyzer, $stmt, $context);
return true;
2020-05-18 21:13:27 +02:00
}
2020-05-18 21:13:27 +02:00
if ($stmt instanceof PhpParser\Node\Expr\AssignRef) {
return Expression\AssignmentAnalyzer::analyzeAssignmentRef($statements_analyzer, $stmt, $context);
}
if ($stmt instanceof PhpParser\Node\Expr\ErrorSuppress) {
$context->error_suppressing = true;
if (self::analyze($statements_analyzer, $stmt->expr, $context) === false) {
return false;
}
2020-05-18 21:13:27 +02:00
$context->error_suppressing = false;
2020-05-18 21:13:27 +02:00
$expr_type = $statements_analyzer->node_data->getType($stmt->expr);
2020-05-18 21:13:27 +02:00
if ($expr_type) {
$statements_analyzer->node_data->setType($stmt, $expr_type);
}
2020-05-18 21:13:27 +02:00
return true;
}
2020-05-18 21:13:27 +02:00
if ($stmt instanceof PhpParser\Node\Expr\ShellExec) {
if (IssueBuffer::accepts(
new ForbiddenCode(
'Use of shell_exec',
new CodeLocation($statements_analyzer->getSource(), $stmt)
),
$statements_analyzer->getSuppressedIssues()
)) {
// continue
}
2020-05-18 21:13:27 +02:00
return true;
}
2020-05-18 21:13:27 +02:00
if ($stmt instanceof PhpParser\Node\Expr\Print_) {
$was_inside_call = $context->inside_call;
$context->inside_call = true;
if (Expression\PrintAnalyzer::analyze($statements_analyzer, $stmt, $context) === false) {
return false;
}
2020-05-18 21:13:27 +02:00
$context->inside_call = $was_inside_call;
2020-05-18 21:13:27 +02:00
return true;
}
2018-04-09 16:20:13 +02:00
2020-05-18 21:13:27 +02:00
if ($stmt instanceof PhpParser\Node\Expr\Yield_) {
return Expression\YieldAnalyzer::analyze($statements_analyzer, $stmt, $context);
}
2020-05-18 21:13:27 +02:00
if ($stmt instanceof PhpParser\Node\Expr\YieldFrom) {
return Expression\YieldFromAnalyzer::analyze($statements_analyzer, $stmt, $context);
}
if ($stmt instanceof PhpParser\Node\Expr\Match_) {
return Expression\MatchAnalyzer::analyze($statements_analyzer, $stmt, $context);
}
if ($stmt instanceof PhpParser\Node\Expr\Throw_) {
return ThrowAnalyzer::analyze($statements_analyzer, $stmt, $context);
}
if ($stmt instanceof PhpParser\Node\Expr\NullsafePropertyFetch
|| $stmt instanceof PhpParser\Node\Expr\NullsafeMethodCall
) {
return Expression\NullsafeAnalyzer::analyze($statements_analyzer, $stmt, $context);
}
2020-05-18 21:13:27 +02:00
if ($stmt instanceof PhpParser\Node\Expr\Error) {
// do nothing
return true;
}
2020-05-18 21:13:27 +02:00
if (IssueBuffer::accepts(
new UnrecognizedExpression(
'Psalm does not understand ' . get_class($stmt),
new CodeLocation($statements_analyzer->getSource(), $stmt)
),
$statements_analyzer->getSuppressedIssues()
)) {
// fall through
}
2020-05-18 21:13:27 +02:00
return false;
}
public static function isMock(string $fq_class_name): bool
2016-10-22 19:23:18 +02:00
{
return in_array(strtolower($fq_class_name), Config::getInstance()->getMockClasses(), true);
2016-10-22 19:23:18 +02:00
}
}