1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-15 19:07:00 +01:00
psalm/src/Psalm/Checker/Statements/ExpressionChecker.php

1311 lines
49 KiB
PHP
Raw Normal View History

2016-10-22 19:23:18 +02:00
<?php
namespace Psalm\Checker\Statements;
use PhpParser;
use Psalm\Checker\ClassChecker;
use Psalm\Checker\ClassLikeChecker;
use Psalm\Checker\ClosureChecker;
use Psalm\Checker\CommentChecker;
use Psalm\Checker\MethodChecker;
2016-11-01 16:37:58 +01:00
use Psalm\Checker\Statements\Expression\AssignmentChecker;
2016-11-01 19:14:35 +01:00
use Psalm\Checker\Statements\Expression\CallChecker;
use Psalm\Checker\Statements\Expression\FetchChecker;
2016-11-02 07:29:00 +01:00
use Psalm\Checker\StatementsChecker;
2016-10-22 19:23:18 +02:00
use Psalm\Checker\TypeChecker;
use Psalm\Config;
use Psalm\Context;
use Psalm\Issue\ForbiddenCode;
use Psalm\Issue\InvalidStaticVariable;
use Psalm\Issue\PossiblyUndefinedVariable;
use Psalm\Issue\UndefinedVariable;
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;
class ExpressionChecker
{
2016-11-02 07:29:00 +01:00
/**
* @var array<string,array<int,string>>
*/
2016-10-22 19:23:18 +02:00
protected static $reflection_functions = [];
/**
2016-11-02 07:29:00 +01:00
* @param StatementsChecker $statements_checker
* @param PhpParser\Node\Expr $stmt
* @param Context $context
* @param bool $array_assignment
* @param Type\Union|null $assignment_key_type
* @param Type\Union|null $assignment_value_type
* @param string|null $assignment_key_value
* @return false|null
2016-10-22 19:23:18 +02:00
*/
public static function check(
StatementsChecker $statements_checker,
PhpParser\Node\Expr $stmt,
Context $context,
$array_assignment = false,
Type\Union $assignment_key_type = null,
Type\Union $assignment_value_type = null,
$assignment_key_value = null
) {
if ($stmt instanceof PhpParser\Node\Expr\Variable) {
2016-11-05 02:14:04 +01:00
if (self::checkVariable($statements_checker, $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) {
2016-11-01 16:37:58 +01:00
$assignment_type = AssignmentChecker::check(
$statements_checker,
$stmt->var,
$stmt->expr,
$context,
(string)$stmt->getDocComment()
);
if ($assignment_type === false) {
2016-10-22 19:23:18 +02:00
return false;
}
2016-11-01 16:37:58 +01:00
$stmt->inferredType = $assignment_type;
2016-11-02 07:29:00 +01:00
} elseif ($stmt instanceof PhpParser\Node\Expr\AssignOp) {
2016-11-01 16:37:58 +01:00
if (AssignmentChecker::checkAssignmentOperation($statements_checker, $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) {
2016-11-01 19:14:35 +01:00
if (CallChecker::checkMethodCall($statements_checker, $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) {
2016-11-01 19:14:35 +01:00
if (CallChecker::checkStaticCall($statements_checker, $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) {
2016-11-01 19:14:35 +01:00
if (FetchChecker::checkConstFetch($statements_checker, $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\String_) {
2016-10-22 19:23:18 +02:00
$stmt->inferredType = Type::getString();
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) {
2016-10-22 19:23:18 +02:00
// do nothing
2016-11-02 07:29:00 +01:00
} elseif ($stmt instanceof PhpParser\Node\Scalar\LNumber) {
2016-10-22 19:23:18 +02:00
$stmt->inferredType = Type::getInt();
2016-11-02 07:29:00 +01:00
} elseif ($stmt instanceof PhpParser\Node\Scalar\DNumber) {
2016-10-22 19:23:18 +02:00
$stmt->inferredType = Type::getFloat();
2016-11-02 07:29:00 +01:00
} elseif ($stmt instanceof PhpParser\Node\Expr\UnaryMinus) {
2016-10-22 19:23:18 +02:00
if (self::check($statements_checker, $stmt->expr, $context) === false) {
return false;
}
$stmt->inferredType = $stmt->expr->inferredType;
2016-11-02 07:29:00 +01:00
} elseif ($stmt instanceof PhpParser\Node\Expr\UnaryPlus) {
2016-10-22 19:23:18 +02:00
if (self::check($statements_checker, $stmt->expr, $context) === false) {
return false;
}
2016-11-07 21:29:47 +01:00
$stmt->inferredType = isset($stmt->expr->inferredType) ? $stmt->expr->inferredType : null;
2016-11-02 07:29:00 +01:00
} elseif ($stmt instanceof PhpParser\Node\Expr\Isset_) {
2016-10-22 19:23:18 +02:00
foreach ($stmt->vars as $isset_var) {
if ($isset_var instanceof PhpParser\Node\Expr\PropertyFetch &&
$isset_var->var instanceof PhpParser\Node\Expr\Variable &&
$isset_var->var->name === 'this' &&
is_string($isset_var->name)
) {
$var_id = '$this->' . $isset_var->name;
$context->vars_in_scope[$var_id] = Type::getMixed();
$context->vars_possibly_in_scope[$var_id] = true;
}
}
$stmt->inferredType = Type::getBool();
2016-11-02 07:29:00 +01:00
} elseif ($stmt instanceof PhpParser\Node\Expr\ClassConstFetch) {
2016-11-01 19:14:35 +01:00
if (FetchChecker::checkClassConstFetch($statements_checker, $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) {
2016-11-01 19:14:35 +01:00
if (FetchChecker::checkPropertyFetch($statements_checker, $stmt, $context, $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\StaticPropertyFetch) {
2016-11-01 19:14:35 +01:00
if (FetchChecker::checkStaticPropertyFetch($statements_checker, $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) {
2016-10-22 19:23:18 +02:00
if (self::check($statements_checker, $stmt->expr, $context) === false) {
return false;
}
2016-11-02 07:29:00 +01:00
} elseif ($stmt instanceof PhpParser\Node\Expr\BinaryOp) {
2016-10-22 19:23:18 +02:00
if (self::checkBinaryOp($statements_checker, $stmt, $context) === false) {
return false;
}
2016-11-02 07:29:00 +01:00
} elseif ($stmt instanceof PhpParser\Node\Expr\PostInc) {
2016-10-22 19:23:18 +02:00
if (self::check($statements_checker, $stmt->var, $context) === false) {
return false;
}
2016-11-02 07:29:00 +01:00
} elseif ($stmt instanceof PhpParser\Node\Expr\PostDec) {
2016-10-22 19:23:18 +02:00
if (self::check($statements_checker, $stmt->var, $context) === false) {
return false;
}
2016-11-02 07:29:00 +01:00
} elseif ($stmt instanceof PhpParser\Node\Expr\PreInc) {
2016-10-22 19:23:18 +02:00
if (self::check($statements_checker, $stmt->var, $context) === false) {
return false;
}
2016-11-02 07:29:00 +01:00
} elseif ($stmt instanceof PhpParser\Node\Expr\PreDec) {
2016-10-22 19:23:18 +02:00
if (self::check($statements_checker, $stmt->var, $context) === false) {
return false;
}
2016-11-02 07:29:00 +01:00
} elseif ($stmt instanceof PhpParser\Node\Expr\New_) {
2016-11-01 19:14:35 +01:00
if (CallChecker::checkNew($statements_checker, $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_) {
2016-10-22 19:23:18 +02:00
if (self::checkArray($statements_checker, $stmt, $context) === false) {
return false;
}
2016-11-02 07:29:00 +01:00
} elseif ($stmt instanceof PhpParser\Node\Scalar\Encapsed) {
2016-10-22 19:23:18 +02:00
if (self::checkEncapsulatedString($statements_checker, $stmt, $context) === false) {
return false;
}
2016-11-02 07:29:00 +01:00
} elseif ($stmt instanceof PhpParser\Node\Expr\FuncCall) {
2016-11-01 19:14:35 +01:00
if (CallChecker::checkFunctionCall($statements_checker, $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) {
2016-10-22 19:23:18 +02:00
if (self::checkTernary($statements_checker, $stmt, $context) === false) {
return false;
}
2016-11-02 07:29:00 +01:00
} elseif ($stmt instanceof PhpParser\Node\Expr\BooleanNot) {
2016-10-22 19:23:18 +02:00
if (self::checkBooleanNot($statements_checker, $stmt, $context) === false) {
return false;
}
2016-11-02 07:29:00 +01:00
} elseif ($stmt instanceof PhpParser\Node\Expr\Empty_) {
2016-10-22 19:23:18 +02:00
if (self::checkEmpty($statements_checker, $stmt, $context) === false) {
return false;
}
2016-11-02 07:29:00 +01:00
} elseif ($stmt instanceof PhpParser\Node\Expr\Closure) {
2016-10-22 19:23:18 +02:00
$closure_checker = new ClosureChecker($stmt, $statements_checker->getSource());
if (self::checkClosureUses($statements_checker, $stmt, $context) === false) {
return false;
}
$use_context = new Context($statements_checker->getFileName(), $context->self);
if (!$statements_checker->isStatic()) {
$this_class = ClassLikeChecker::getThisClass();
2016-11-02 07:29:00 +01:00
$this_class = $this_class &&
2016-11-08 01:16:51 +01:00
ClassChecker::classExtends($this_class, $statements_checker->getFQCLN())
2016-10-22 19:23:18 +02:00
? $this_class
: $context->self;
if ($this_class) {
$use_context->vars_in_scope['$this'] = new Type\Union([new Type\Atomic($this_class)]);
}
}
foreach ($context->vars_in_scope as $var => $type) {
if (strpos($var, '$this->') === 0) {
$use_context->vars_in_scope[$var] = clone $type;
}
}
foreach ($context->vars_possibly_in_scope as $var => $type) {
if (strpos($var, '$this->') === 0) {
$use_context->vars_possibly_in_scope[$var] = true;
}
}
foreach ($stmt->uses as $use) {
// 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 (!isset($context->vars_in_scope['$' . $use->var]) && $use->byRef) {
$context->vars_in_scope['$' . $use->var] = Type::getMixed();
}
$use_context->vars_in_scope['$' . $use->var] = isset($context->vars_in_scope['$' . $use->var])
2016-11-02 07:29:00 +01:00
? clone $context->vars_in_scope['$' . $use->var]
: Type::getMixed();
2016-10-22 19:23:18 +02:00
$use_context->vars_possibly_in_scope['$' . $use->var] = true;
}
2016-10-23 07:57:11 +02:00
$closure_checker->check($use_context);
2016-10-22 19:23:18 +02:00
$stmt->inferredType = Type::getClosure();
2016-11-02 07:29:00 +01:00
} elseif ($stmt instanceof PhpParser\Node\Expr\ArrayDimFetch) {
if (FetchChecker::checkArrayAccess(
$statements_checker,
$stmt,
$context,
$array_assignment,
$assignment_key_type,
$assignment_value_type,
$assignment_key_value
) === 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_) {
2016-10-22 19:23:18 +02:00
if (self::check($statements_checker, $stmt->expr, $context) === false) {
return false;
}
2016-11-02 07:29:00 +01:00
$stmt->inferredType = Type::getInt();
} elseif ($stmt instanceof PhpParser\Node\Expr\Cast\Double) {
2016-10-22 19:23:18 +02:00
if (self::check($statements_checker, $stmt->expr, $context) === false) {
return false;
}
2016-11-02 07:29:00 +01:00
$stmt->inferredType = Type::getFloat();
} elseif ($stmt instanceof PhpParser\Node\Expr\Cast\Bool_) {
2016-10-22 19:23:18 +02:00
if (self::check($statements_checker, $stmt->expr, $context) === false) {
return false;
}
2016-11-02 07:29:00 +01:00
$stmt->inferredType = Type::getBool();
} elseif ($stmt instanceof PhpParser\Node\Expr\Cast\String_) {
2016-10-22 19:23:18 +02:00
if (self::check($statements_checker, $stmt->expr, $context) === false) {
return false;
}
2016-11-02 07:29:00 +01:00
$stmt->inferredType = Type::getString();
} elseif ($stmt instanceof PhpParser\Node\Expr\Cast\Object_) {
2016-10-22 19:23:18 +02:00
if (self::check($statements_checker, $stmt->expr, $context) === false) {
return false;
}
2016-11-02 07:29:00 +01:00
$stmt->inferredType = Type::getObject();
} elseif ($stmt instanceof PhpParser\Node\Expr\Cast\Array_) {
2016-10-22 19:23:18 +02:00
if (self::check($statements_checker, $stmt->expr, $context) === false) {
return false;
}
2016-11-02 07:29:00 +01:00
$stmt->inferredType = Type::getArray();
} elseif ($stmt instanceof PhpParser\Node\Expr\Cast\Unset_) {
2016-10-22 19:23:18 +02:00
if (self::check($statements_checker, $stmt->expr, $context) === false) {
return false;
}
2016-11-02 07:29:00 +01:00
$stmt->inferredType = Type::getNull();
} elseif ($stmt instanceof PhpParser\Node\Expr\Clone_) {
2016-10-22 19:23:18 +02:00
if (self::check($statements_checker, $stmt->expr, $context) === false) {
return false;
}
if (property_exists($stmt->expr, 'inferredType')) {
$stmt->inferredType = $stmt->expr->inferredType;
}
2016-11-02 07:29:00 +01:00
} elseif ($stmt instanceof PhpParser\Node\Expr\Instanceof_) {
2016-10-22 19:23:18 +02:00
if (self::check($statements_checker, $stmt->expr, $context) === false) {
return false;
}
2016-11-02 07:29:00 +01:00
if ($stmt->class instanceof PhpParser\Node\Name &&
!in_array($stmt->class->parts[0], ['self', 'static', 'parent'])
) {
2016-10-22 19:23:18 +02:00
if ($context->check_classes) {
2016-11-08 01:16:51 +01:00
$fq_class_name = ClassLikeChecker::getFQCLNFromNameObject(
2016-10-22 19:23:18 +02:00
$stmt->class,
$statements_checker->getNamespace(),
$statements_checker->getAliasedClasses()
);
2016-11-08 01:16:51 +01:00
if (ClassLikeChecker::checkFullyQualifiedClassLikeName(
$fq_class_name,
2016-11-02 07:29:00 +01:00
$statements_checker->getCheckedFileName(),
$stmt->getLine(),
$statements_checker->getSuppressedIssues()
) === false) {
2016-10-22 19:23:18 +02:00
return false;
}
}
}
2016-11-05 22:57:14 +01:00
$stmt->inferredType = Type::getBool();
2016-11-02 07:29:00 +01:00
} elseif ($stmt instanceof PhpParser\Node\Expr\Exit_) {
2016-10-22 19:23:18 +02:00
// do nothing
2016-11-02 07:29:00 +01:00
} elseif ($stmt instanceof PhpParser\Node\Expr\Include_) {
2016-10-22 19:23:18 +02:00
$statements_checker->checkInclude($stmt, $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;
if (self::check($statements_checker, $stmt->expr, $context) === false) {
return false;
}
2016-11-02 07:29:00 +01:00
} elseif ($stmt instanceof PhpParser\Node\Expr\AssignRef) {
2016-10-22 19:23:18 +02:00
if ($stmt->var instanceof PhpParser\Node\Expr\Variable) {
if (is_string($stmt->var->name)) {
$context->vars_in_scope['$' . $stmt->var->name] = Type::getMixed();
$context->vars_possibly_in_scope['$' . $stmt->var->name] = true;
$statements_checker->registerVariable('$' . $stmt->var->name, $stmt->var->getLine());
2016-11-02 07:29:00 +01:00
} else {
2016-10-22 19:23:18 +02:00
if (self::check($statements_checker, $stmt->var->name, $context) === false) {
return false;
}
}
} else {
if (self::check($statements_checker, $stmt->var, $context) === false) {
return false;
}
}
if (self::check($statements_checker, $stmt->expr, $context) === false) {
return false;
}
2016-11-02 07:29:00 +01:00
} elseif ($stmt instanceof PhpParser\Node\Expr\ErrorSuppress) {
2016-10-22 19:23:18 +02:00
// do nothing
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(
new ForbiddenCode('Use of shell_exec', $statements_checker->getCheckedFileName(), $stmt->getLine()),
$statements_checker->getSuppressedIssues()
)) {
return false;
}
2016-11-02 07:29:00 +01:00
} elseif ($stmt instanceof PhpParser\Node\Expr\Print_) {
2016-10-22 19:23:18 +02:00
if (self::check($statements_checker, $stmt->expr, $context) === false) {
return false;
}
2016-11-02 07:29:00 +01:00
} elseif ($stmt instanceof PhpParser\Node\Expr\Yield_) {
2016-10-22 19:23:18 +02:00
self::checkYield($statements_checker, $stmt, $context);
2016-11-02 07:29:00 +01:00
} elseif ($stmt instanceof PhpParser\Node\Expr\YieldFrom) {
2016-10-22 19:23:18 +02:00
self::checkYieldFrom($statements_checker, $stmt, $context);
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),
$statements_checker->getCheckedFileName(),
$stmt->getLine()
),
$statements_checker->getSuppressedIssues()
)) {
return false;
}
2016-10-22 19:23:18 +02:00
}
foreach (Config::getInstance()->getPlugins() as $plugin) {
2016-11-02 07:29:00 +01:00
if ($plugin->checkExpression(
$stmt,
$context,
$statements_checker->getCheckedFileName(),
$statements_checker->getSuppressedIssues()
) === false) {
2016-10-22 19:23:18 +02:00
return false;
}
}
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
* @param StatementsChecker $statements_checker
* @param PhpParser\Node\Expr\Variable $stmt
* @param Context $context
* @param bool $passed_by_reference
* @param Type\Union|null $by_ref_type
* @param bool $array_assignment
* @return false|null
2016-10-22 19:23:18 +02:00
*/
2016-11-01 19:14:35 +01:00
public static function checkVariable(
2016-10-22 19:23:18 +02:00
StatementsChecker $statements_checker,
PhpParser\Node\Expr\Variable $stmt,
Context $context,
$passed_by_reference = false,
Type\Union $by_ref_type = null,
$array_assignment = false
) {
if ($stmt->name === 'this') {
if ($statements_checker->isStatic()) {
if (IssueBuffer::accepts(
new InvalidStaticVariable(
'Invalid reference to $this in a static context',
$statements_checker->getCheckedFileName(),
$stmt->getLine()
),
$statements_checker->getSuppressedIssues()
)) {
return false;
}
return null;
2016-10-22 19:23:18 +02:00
}
$stmt->inferredType = clone $context->vars_in_scope['$this'];
return null;
2016-10-22 19:23:18 +02:00
}
if (!$context->check_variables) {
$stmt->inferredType = Type::getMixed();
if (is_string($stmt->name) && !isset($context->vars_in_scope['$' . $stmt->name])) {
$context->vars_in_scope['$' . $stmt->name] = Type::getMixed();
$context->vars_possibly_in_scope['$' . $stmt->name] = true;
}
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
if (in_array($stmt->name, [
'_SERVER', '_GET', '_POST', '_COOKIE', '_REQUEST', '_FILES', '_ENV', 'GLOBALS', 'argv', 'argc'
2016-11-02 07:29:00 +01:00
])) {
return null;
2016-10-22 19:23:18 +02:00
}
if (!is_string($stmt->name)) {
return self::check($statements_checker, $stmt->name, $context);
}
if ($passed_by_reference && $by_ref_type) {
self::assignByRefParam($statements_checker, $stmt, $by_ref_type, $context);
2016-11-02 07:29:00 +01:00
return null;
2016-10-22 19:23:18 +02:00
}
$var_name = '$' . $stmt->name;
if (!isset($context->vars_in_scope[$var_name])) {
2016-11-02 07:29:00 +01:00
if (!isset($context->vars_possibly_in_scope[$var_name]) ||
!$statements_checker->getFirstAppearance($var_name)
) {
2016-10-22 19:23:18 +02:00
if ($array_assignment) {
// if we're in an array assignment, let's assign the variable
// because PHP allows it
$context->vars_in_scope[$var_name] = Type::getArray();
$context->vars_possibly_in_scope[$var_name] = true;
$statements_checker->registerVariable($var_name, $stmt->getLine());
2016-11-02 07:29:00 +01:00
} else {
2016-10-22 19:23:18 +02:00
IssueBuffer::add(
2016-11-02 07:29:00 +01:00
new UndefinedVariable(
'Cannot find referenced variable ' . $var_name,
$statements_checker->getCheckedFileName(),
$stmt->getLine()
)
2016-10-22 19:23:18 +02:00
);
return false;
}
}
if ($statements_checker->getFirstAppearance($var_name)) {
if (IssueBuffer::accepts(
new PossiblyUndefinedVariable(
2016-11-02 07:29:00 +01:00
'Possibly undefined variable ' . $var_name .', first seen on line ' .
$statements_checker->getFirstAppearance($var_name),
2016-10-22 19:23:18 +02:00
$statements_checker->getCheckedFileName(),
$stmt->getLine()
),
$statements_checker->getSuppressedIssues()
)) {
return false;
}
}
2016-11-02 07:29:00 +01:00
} else {
2016-10-22 19:23:18 +02:00
$stmt->inferredType = $context->vars_in_scope[$var_name];
}
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
* @param StatementsChecker $statements_checker
* @param PhpParser\Node\Expr $stmt
* @param Type\Union $by_ref_type
* @param Context $context
2016-10-22 19:23:18 +02:00
* @return void
*/
2016-11-02 07:29:00 +01:00
public static function assignByRefParam(
StatementsChecker $statements_checker,
PhpParser\Node\Expr $stmt,
Type\Union $by_ref_type,
Context $context
) {
$var_id = self::getVarId(
$stmt,
2016-11-08 01:16:51 +01:00
$statements_checker->getFQCLN(),
2016-11-02 07:29:00 +01:00
$statements_checker->getNamespace(),
$statements_checker->getAliasedClasses()
);
2016-10-22 19:23:18 +02:00
if ($var_id && !isset($context->vars_in_scope[$var_id])) {
$context->vars_possibly_in_scope[$var_id] = true;
$statements_checker->registerVariable($var_id, $stmt->getLine());
}
$stmt->inferredType = $by_ref_type;
$context->vars_in_scope[$var_id] = $by_ref_type;
}
/**
2016-11-02 07:29:00 +01:00
* @param StatementsChecker $statements_checker
* @param PhpParser\Node\Expr\Array_ $stmt
* @param Context $context
* @return false|null
2016-10-22 19:23:18 +02:00
*/
2016-10-23 07:57:11 +02:00
protected static function checkArray(
2016-10-22 19:23:18 +02:00
StatementsChecker $statements_checker,
PhpParser\Node\Expr\Array_ $stmt,
Context $context
) {
// if the array is empty, this special type allows us to match any other array type against it
if (empty($stmt->items)) {
$stmt->inferredType = Type::getEmptyArray();
2016-11-02 07:29:00 +01:00
return null;
2016-10-22 19:23:18 +02:00
}
/** @var Type\Union|null */
$item_key_type = null;
/** @var Type\Union|null */
$item_value_type = null;
/** @var array<string,Type\Union> */
$property_types = [];
foreach ($stmt->items as $item) {
if ($item->key) {
if (self::check($statements_checker, $item->key, $context) === false) {
return false;
}
if (isset($item->key->inferredType)) {
if ($item_key_type) {
/** @var Type\Union */
$item_key_type = Type::combineUnionTypes($item->key->inferredType, $item_key_type);
2016-11-02 07:29:00 +01:00
} else {
2016-10-22 19:23:18 +02:00
/** @var Type\Union */
$item_key_type = $item->key->inferredType;
}
}
2016-11-02 07:29:00 +01:00
} else {
2016-10-22 19:23:18 +02:00
$item_key_type = Type::getInt();
}
if (self::check($statements_checker, $item->value, $context) === false) {
return false;
}
if (isset($item->value->inferredType)) {
if ($item->key instanceof PhpParser\Node\Scalar\String_) {
$property_types[$item->key->value] = $item->value->inferredType;
}
if ($item_value_type) {
$item_value_type = Type::combineUnionTypes($item->value->inferredType, $item_value_type);
2016-11-02 07:29:00 +01:00
} else {
2016-10-22 19:23:18 +02:00
$item_value_type = $item->value->inferredType;
}
}
}
// if this array looks like an object-like array, let's return that instead
if ($item_value_type && $item_key_type && $item_key_type->hasString() && !$item_key_type->hasInt()) {
$stmt->inferredType = new Type\Union([new Type\ObjectLike('array', $property_types)]);
2016-11-02 07:29:00 +01:00
return null;
2016-10-22 19:23:18 +02:00
}
$stmt->inferredType = new Type\Union([
new Type\Generic(
'array',
[
$item_key_type ?: new Type\Union([new Type\Atomic('int'), new Type\Atomic('string')]),
$item_value_type ?: Type::getMixed()
]
)
]);
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
* @param StatementsChecker $statements_checker
* @param PhpParser\Node\Expr\BinaryOp $stmt
* @param Context $context
* @param int $nesting
* @return false|null
2016-10-22 19:23:18 +02:00
*/
2016-10-23 07:57:11 +02:00
protected static function checkBinaryOp(
2016-10-22 19:23:18 +02:00
StatementsChecker $statements_checker,
PhpParser\Node\Expr\BinaryOp $stmt,
Context $context,
$nesting = 0
) {
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\Concat && $nesting > 20) {
// ignore deeply-nested string concatenation
2016-11-02 07:29:00 +01:00
} elseif ($stmt instanceof PhpParser\Node\Expr\BinaryOp\BooleanAnd) {
2016-10-22 19:23:18 +02:00
$left_type_assertions = TypeChecker::getReconcilableTypeAssertions(
$stmt->left,
2016-11-08 01:16:51 +01:00
$statements_checker->getFQCLN(),
2016-10-22 19:23:18 +02:00
$statements_checker->getNamespace(),
$statements_checker->getAliasedClasses()
);
if (self::check($statements_checker, $stmt->left, $context) === false) {
return false;
}
// while in an and, we allow scope to boil over to support
// statements of the form if ($x && $x->foo())
$op_vars_in_scope = TypeChecker::reconcileKeyedTypes(
$left_type_assertions,
$context->vars_in_scope,
$statements_checker->getCheckedFileName(),
$stmt->getLine(),
$statements_checker->getSuppressedIssues()
);
if ($op_vars_in_scope === false) {
return false;
}
$op_context = clone $context;
$op_context->vars_in_scope = $op_vars_in_scope;
if (self::check($statements_checker, $stmt->right, $op_context) === false) {
return false;
}
foreach ($op_context->vars_in_scope as $var => $type) {
if (!isset($context->vars_in_scope[$var])) {
$context->vars_in_scope[$var] = $type;
continue;
}
}
$context->updateChecks($op_context);
2016-11-02 07:29:00 +01:00
$context->vars_possibly_in_scope = array_merge(
$op_context->vars_possibly_in_scope,
$context->vars_possibly_in_scope
);
} elseif ($stmt instanceof PhpParser\Node\Expr\BinaryOp\BooleanOr) {
2016-10-22 19:23:18 +02:00
$left_type_assertions = TypeChecker::getNegatableTypeAssertions(
$stmt->left,
2016-11-08 01:16:51 +01:00
$statements_checker->getFQCLN(),
2016-10-22 19:23:18 +02:00
$statements_checker->getNamespace(),
$statements_checker->getAliasedClasses()
);
$negated_type_assertions = TypeChecker::negateTypes($left_type_assertions);
if (self::check($statements_checker, $stmt->left, $context) === false) {
return false;
}
// while in an or, we allow scope to boil over to support
// statements of the form if ($x === null || $x->foo())
$op_vars_in_scope = TypeChecker::reconcileKeyedTypes(
$negated_type_assertions,
$context->vars_in_scope,
$statements_checker->getCheckedFileName(),
$stmt->getLine(),
$statements_checker->getSuppressedIssues()
);
if ($op_vars_in_scope === false) {
return false;
}
$op_context = clone $context;
$op_context->vars_in_scope = $op_vars_in_scope;
if (self::check($statements_checker, $stmt->right, $op_context) === false) {
return false;
}
$context->updateChecks($op_context);
2016-11-02 07:29:00 +01:00
$context->vars_possibly_in_scope = array_merge(
$op_context->vars_possibly_in_scope,
$context->vars_possibly_in_scope
);
} else {
2016-10-22 19:23:18 +02:00
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\Concat) {
$stmt->inferredType = Type::getString();
}
if ($stmt->left instanceof PhpParser\Node\Expr\BinaryOp) {
if (self::checkBinaryOp($statements_checker, $stmt->left, $context, ++$nesting) === false) {
return false;
}
2016-11-02 07:29:00 +01:00
} else {
2016-10-22 19:23:18 +02:00
if (self::check($statements_checker, $stmt->left, $context) === false) {
return false;
}
}
if ($stmt->right instanceof PhpParser\Node\Expr\BinaryOp) {
if (self::checkBinaryOp($statements_checker, $stmt->right, $context, ++$nesting) === false) {
return false;
}
2016-11-02 07:29:00 +01:00
} else {
2016-10-22 19:23:18 +02:00
if (self::check($statements_checker, $stmt->right, $context) === false) {
return false;
}
}
}
// let's do some fun type assignment
if (isset($stmt->left->inferredType) && isset($stmt->right->inferredType)) {
2016-11-13 21:39:16 +01:00
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\Plus) {
self::checkPlusOp(
$statements_checker,
$stmt->getLine(),
$stmt->left->inferredType,
$stmt->right->inferredType,
$result_type
);
if ($result_type) {
$stmt->inferredType = $result_type;
}
} elseif ($stmt instanceof PhpParser\Node\Expr\BinaryOp\Mul
2016-10-22 19:23:18 +02:00
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\Minus
) {
if ($stmt->left->inferredType->isInt() && $stmt->right->inferredType->isInt()) {
$stmt->inferredType = Type::getInt();
2016-11-02 07:29:00 +01:00
} elseif ($stmt->left->inferredType->hasNumericType() &&
$stmt->right->inferredType->hasNumericType()
) {
2016-10-22 19:23:18 +02:00
$stmt->inferredType = Type::getFloat();
}
2016-11-02 07:29:00 +01:00
} elseif ($stmt instanceof PhpParser\Node\Expr\BinaryOp\Div
2016-10-22 19:23:18 +02:00
&& $stmt->left->inferredType->hasNumericType()
&& $stmt->right->inferredType->hasNumericType()
) {
$stmt->inferredType = Type::getFloat();
}
}
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\BooleanAnd
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\BooleanOr
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\Equal
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\NotEqual
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\Identical
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\NotIdentical
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\Greater
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\GreaterOrEqual
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\Smaller
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\SmallerOrEqual
) {
$stmt->inferredType = Type::getBool();
}
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\Spaceship) {
$stmt->inferredType = Type::getInt();
}
2016-11-02 07:29:00 +01:00
return null;
2016-10-22 19:23:18 +02:00
}
2016-11-18 22:13:59 +01:00
/**
* @param StatementsChecker $statements_checker
* @param int $line_number
* @param Type\Union|null $left_type
* @param Type\Union|null $right_type
* @param Type\Union|null &$result_type
* @return void
*/
2016-11-13 21:39:16 +01:00
public static function checkPlusOp(
StatementsChecker $statements_checker,
$line_number,
2016-11-13 21:46:35 +01:00
Type\Union $left_type = null,
Type\Union $right_type = null,
2016-11-13 21:39:16 +01:00
Type\Union &$result_type = null
) {
if ($left_type && !$left_type->isMixed()) {
if ($left_type->hasNumericType()) {
2016-11-13 21:55:06 +01:00
if ($right_type) {
if ($left_type->isInt() && $right_type->isInt()) {
$result_type = Type::getInt();
return;
}
2016-11-13 21:39:16 +01:00
2016-11-13 21:55:06 +01:00
if ($left_type->hasNumericType() && $right_type->hasNumericType()) {
$result_type = Type::getFloat();
return;
}
2016-11-13 21:39:16 +01:00
}
} elseif ($left_type->hasArray()) {
if (!$right_type || $right_type->isMixed()) {
// @todo emit issue
$result_type = Type::getMixed();
return;
}
if (!$right_type->hasArray()) {
// @todo emit issue
return;
}
$var_array_type = $left_type->types['array'];
$expr_array_type = $right_type->types['array'];
$result_type = Type::combineTypes([$var_array_type, $expr_array_type]);
}
}
}
2016-10-22 19:23:18 +02:00
/**
* @param PhpParser\Node\Expr $stmt
* @param string $this_class_name
* @param string $namespace
* @param array $aliased_classes
* @param int|null &$nesting
* @return string|null
*/
public static function getVarId(
PhpParser\Node\Expr $stmt,
$this_class_name,
$namespace,
array $aliased_classes,
&$nesting = null
) {
if ($stmt instanceof PhpParser\Node\Expr\Variable && is_string($stmt->name)) {
return '$' . $stmt->name;
}
if ($stmt instanceof PhpParser\Node\Expr\StaticPropertyFetch
&& is_string($stmt->name)
&& $stmt->class instanceof PhpParser\Node\Name
) {
if (count($stmt->class->parts) === 1 && in_array($stmt->class->parts[0], ['self', 'static', 'parent'])) {
$fq_class_name = $this_class_name;
2016-11-02 07:29:00 +01:00
} else {
2016-11-08 01:16:51 +01:00
$fq_class_name = ClassLikeChecker::getFQCLNFromNameObject(
2016-11-02 07:29:00 +01:00
$stmt->class,
$namespace,
$aliased_classes
);
2016-10-22 19:23:18 +02:00
}
return $fq_class_name . '::$' . $stmt->name;
2016-10-22 19:23:18 +02:00
}
if ($stmt instanceof PhpParser\Node\Expr\PropertyFetch && is_string($stmt->name)) {
$object_id = self::getVarId($stmt->var, $this_class_name, $namespace, $aliased_classes);
if (!$object_id) {
return null;
}
return $object_id . '->' . $stmt->name;
}
if ($stmt instanceof PhpParser\Node\Expr\ArrayDimFetch && $nesting !== null) {
$nesting++;
return self::getVarId($stmt->var, $this_class_name, $namespace, $aliased_classes, $nesting);
}
return null;
}
/**
* @param PhpParser\Node\Expr $stmt
* @param string $this_class_name
* @param string $namespace
* @param array $aliased_classes
* @return string|null
*/
2016-11-02 07:29:00 +01:00
public static function getArrayVarId(
PhpParser\Node\Expr $stmt,
$this_class_name,
$namespace,
array $aliased_classes
) {
if ($stmt instanceof PhpParser\Node\Expr\ArrayDimFetch &&
$stmt->dim instanceof PhpParser\Node\Scalar\String_
) {
2016-10-22 19:23:18 +02:00
$root_var_id = self::getArrayVarId($stmt->var, $this_class_name, $namespace, $aliased_classes);
return $root_var_id ? $root_var_id . '[\'' . $stmt->dim->value . '\']' : null;
}
return self::getVarId($stmt, $this_class_name, $namespace, $aliased_classes);
}
2016-11-01 19:14:35 +01:00
/**
* @param Type\Union $return_type
* @param array<PhpParser\Node\Arg> $args
* @param string|null $calling_class
* @param string|null $method_id
* @return Type\Union
*/
public static function fleshOutTypes(Type\Union $return_type, array $args, $calling_class = null, $method_id = null)
2016-11-01 19:14:35 +01:00
{
$return_type = clone $return_type;
2016-10-22 19:23:18 +02:00
2016-11-01 19:14:35 +01:00
$new_return_type_parts = [];
2016-10-22 19:23:18 +02:00
2016-11-01 19:14:35 +01:00
foreach ($return_type->types as $key => $return_type_part) {
$new_return_type_parts[] = self::fleshOutAtomicType($return_type_part, $args, $calling_class, $method_id);
2016-10-22 19:23:18 +02:00
}
2016-11-01 19:14:35 +01:00
return new Type\Union($new_return_type_parts);
2016-10-22 19:23:18 +02:00
}
/**
2016-11-01 19:14:35 +01:00
* @param Type\Atomic &$return_type
* @param array<PhpParser\Node\Arg> $args
* @param string|null $calling_class
* @param string|null $method_id
* @return Type\Atomic
2016-10-22 19:23:18 +02:00
*/
2016-11-01 19:14:35 +01:00
protected static function fleshOutAtomicType(Type\Atomic $return_type, array $args, $calling_class, $method_id)
2016-10-22 19:23:18 +02:00
{
if ($return_type->value === '$this' || $return_type->value === 'static' || $return_type->value === 'self') {
if (!$calling_class) {
2016-11-02 07:29:00 +01:00
throw new \InvalidArgumentException(
'Cannot handle ' . $return_type->value . ' when $calling_class is empty',
null
);
2016-10-22 19:23:18 +02:00
}
$return_type->value = $calling_class;
2016-11-02 07:29:00 +01:00
} elseif ($return_type->value[0] === '$' && $method_id) {
2016-10-22 19:23:18 +02:00
$method_params = MethodChecker::getMethodParams($method_id);
foreach ($args as $i => $arg) {
$method_param = $method_params[$i];
if ($return_type->value === '$' . $method_param->name) {
$arg_value = $arg->value;
if ($arg_value instanceof PhpParser\Node\Scalar\String_) {
$return_type->value = preg_replace('/^\\\/', '', $arg_value->value);
}
}
}
if ($return_type->value[0] === '$') {
$return_type = new Type\Atomic('mixed');
}
}
if ($return_type instanceof Type\Generic) {
foreach ($return_type->type_params as &$type_param) {
2016-11-01 19:14:35 +01:00
$type_param = self::fleshOutTypes($type_param, $args, $calling_class, $method_id);
2016-10-22 19:23:18 +02:00
}
}
2016-11-01 19:14:35 +01:00
return $return_type;
2016-10-22 19:23:18 +02:00
}
/**
2016-11-02 07:29:00 +01:00
* @param StatementsChecker $statements_checker
* @param PhpParser\Node\Expr\Closure $stmt
* @param Context $context
* @return false|null
2016-10-22 19:23:18 +02:00
*/
2016-11-02 07:29:00 +01:00
protected static function checkClosureUses(
StatementsChecker $statements_checker,
PhpParser\Node\Expr\Closure $stmt,
Context $context
) {
2016-11-01 19:14:35 +01:00
foreach ($stmt->uses as $use) {
if (!isset($context->vars_in_scope['$' . $use->var])) {
if ($use->byRef) {
$context->vars_in_scope['$' . $use->var] = Type::getMixed();
$context->vars_possibly_in_scope['$' . $use->var] = true;
$statements_checker->registerVariable('$' . $use->var, $use->getLine());
2016-10-22 19:23:18 +02:00
return;
}
2016-11-01 19:14:35 +01:00
if (!isset($context->vars_possibly_in_scope['$' . $use->var])) {
if ($context->check_variables) {
IssueBuffer::add(
2016-11-02 07:29:00 +01:00
new UndefinedVariable(
'Cannot find referenced variable $' . $use->var,
$statements_checker->getCheckedFileName(),
$use->getLine()
)
2016-11-01 19:14:35 +01:00
);
2016-10-22 19:23:18 +02:00
2016-11-01 19:14:35 +01:00
return false;
}
}
2016-10-22 19:23:18 +02:00
2016-11-01 19:14:35 +01:00
if ($statements_checker->getFirstAppearance('$' . $use->var)) {
if (IssueBuffer::accepts(
new PossiblyUndefinedVariable(
2016-11-02 07:29:00 +01:00
'Possibly undefined variable $' . $use->var . ', first seen on line ' .
$statements_checker->getFirstAppearance('$' . $use->var),
2016-11-01 19:14:35 +01:00
$statements_checker->getCheckedFileName(),
$use->getLine()
),
$statements_checker->getSuppressedIssues()
)) {
return false;
}
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
}
2016-11-01 19:14:35 +01:00
if ($context->check_variables) {
2016-10-22 19:23:18 +02:00
IssueBuffer::add(
2016-11-02 07:29:00 +01:00
new UndefinedVariable(
'Cannot find referenced variable $' . $use->var,
$statements_checker->getCheckedFileName(),
$use->getLine()
)
2016-10-22 19:23:18 +02:00
);
2016-11-01 19:14:35 +01:00
return false;
}
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
2016-10-22 19:23:18 +02:00
/**
2016-11-02 07:29:00 +01:00
* @param StatementsChecker $statements_checker
* @param PhpParser\Node\Expr\Yield_ $stmt
* @param Context $context
* @return false|null
2016-10-22 19:23:18 +02:00
*/
2016-11-02 07:29:00 +01:00
protected static function checkYield(
StatementsChecker $statements_checker,
PhpParser\Node\Expr\Yield_ $stmt,
Context $context
) {
$type_in_comments = CommentChecker::getTypeFromComment(
(string) $stmt->getDocComment(),
$context,
$statements_checker->getSource()
);
2016-10-22 19:23:18 +02:00
if ($stmt->key) {
if (self::check($statements_checker, $stmt->key, $context) === false) {
return false;
}
}
if ($stmt->value) {
if (self::check($statements_checker, $stmt->value, $context) === false) {
return false;
}
if ($type_in_comments) {
$stmt->inferredType = $type_in_comments;
2016-11-02 07:29:00 +01:00
} elseif (isset($stmt->value->inferredType)) {
2016-10-22 19:23:18 +02:00
$stmt->inferredType = $stmt->value->inferredType;
2016-11-02 07:29:00 +01:00
} else {
2016-10-22 19:23:18 +02:00
$stmt->inferredType = Type::getMixed();
}
2016-11-02 07:29:00 +01:00
} else {
2016-10-22 19:23:18 +02:00
$stmt->inferredType = Type::getNull();
}
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
* @param StatementsChecker $statements_checker
* @param PhpParser\Node\Expr\YieldFrom $stmt
* @param Context $context
* @return false|null
2016-10-22 19:23:18 +02:00
*/
2016-11-02 07:29:00 +01:00
protected static function checkYieldFrom(
StatementsChecker $statements_checker,
PhpParser\Node\Expr\YieldFrom $stmt,
Context $context
) {
2016-10-22 19:23:18 +02:00
if (self::check($statements_checker, $stmt->expr, $context) === false) {
return false;
}
if (isset($stmt->expr->inferredType)) {
$stmt->inferredType = $stmt->expr->inferredType;
}
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
/**
* @param StatementsChecker $statements_checker
* @param PhpParser\Node\Expr\Ternary $stmt
* @param Context $context
* @return false|null
*/
protected static function checkTernary(
StatementsChecker $statements_checker,
PhpParser\Node\Expr\Ternary $stmt,
Context $context
) {
2016-10-22 19:23:18 +02:00
if (self::check($statements_checker, $stmt->cond, $context) === false) {
return false;
}
$t_if_context = clone $context;
if ($stmt->cond instanceof PhpParser\Node\Expr\BinaryOp) {
$reconcilable_if_types = TypeChecker::getReconcilableTypeAssertions(
$stmt->cond,
2016-11-08 01:16:51 +01:00
$statements_checker->getFQCLN(),
2016-10-22 19:23:18 +02:00
$statements_checker->getNamespace(),
$statements_checker->getAliasedClasses()
);
2016-11-02 07:29:00 +01:00
2016-10-22 19:23:18 +02:00
$negatable_if_types = TypeChecker::getNegatableTypeAssertions(
$stmt->cond,
2016-11-08 01:16:51 +01:00
$statements_checker->getFQCLN(),
2016-10-22 19:23:18 +02:00
$statements_checker->getNamespace(),
$statements_checker->getAliasedClasses()
);
2016-11-02 07:29:00 +01:00
} else {
2016-10-22 19:23:18 +02:00
$reconcilable_if_types = $negatable_if_types = TypeChecker::getTypeAssertions(
$stmt->cond,
2016-11-08 01:16:51 +01:00
$statements_checker->getFQCLN(),
2016-10-22 19:23:18 +02:00
$statements_checker->getNamespace(),
2016-11-02 07:29:00 +01:00
$statements_checker->getAliasedClasses()
);
2016-10-22 19:23:18 +02:00
}
$if_return_type = null;
2016-11-02 07:29:00 +01:00
$t_if_vars_in_scope_reconciled = TypeChecker::reconcileKeyedTypes(
$reconcilable_if_types,
$t_if_context->vars_in_scope,
$statements_checker->getCheckedFileName(),
$stmt->getLine(),
$statements_checker->getSuppressedIssues()
);
2016-10-22 19:23:18 +02:00
if ($t_if_vars_in_scope_reconciled === false) {
return false;
}
$t_if_context->vars_in_scope = $t_if_vars_in_scope_reconciled;
if ($stmt->if) {
if (self::check($statements_checker, $stmt->if, $t_if_context) === false) {
return false;
}
}
$t_else_context = clone $context;
if ($negatable_if_types) {
$negated_if_types = TypeChecker::negateTypes($negatable_if_types);
$t_else_vars_in_scope_reconciled = TypeChecker::reconcileKeyedTypes(
$negated_if_types,
$t_else_context->vars_in_scope,
$statements_checker->getCheckedFileName(),
$stmt->getLine(),
$statements_checker->getSuppressedIssues()
);
if ($t_else_vars_in_scope_reconciled === false) {
return false;
}
2016-11-02 07:29:00 +01:00
2016-10-22 19:23:18 +02:00
$t_else_context->vars_in_scope = $t_else_vars_in_scope_reconciled;
}
if (self::check($statements_checker, $stmt->else, $t_else_context) === false) {
return false;
}
$lhs_type = null;
if ($stmt->if) {
if (isset($stmt->if->inferredType)) {
$lhs_type = $stmt->if->inferredType;
}
2016-11-02 07:29:00 +01:00
} elseif ($stmt->cond) {
2016-10-22 19:23:18 +02:00
if (isset($stmt->cond->inferredType)) {
2016-11-02 07:29:00 +01:00
$if_return_type_reconciled = TypeChecker::reconcileTypes(
'!empty',
$stmt->cond->inferredType,
'',
$statements_checker->getCheckedFileName(),
$stmt->getLine(),
$statements_checker->getSuppressedIssues()
);
2016-10-22 19:23:18 +02:00
if ($if_return_type_reconciled === false) {
return false;
}
$lhs_type = $if_return_type_reconciled;
}
}
if (!$lhs_type || !isset($stmt->else->inferredType)) {
$stmt->inferredType = Type::getMixed();
2016-11-02 07:29:00 +01:00
} else {
2016-10-22 19:23:18 +02:00
$stmt->inferredType = Type::combineUnionTypes($lhs_type, $stmt->else->inferredType);
}
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
/**
* @param StatementsChecker $statements_checker
* @param PhpParser\Node\Expr\BooleanNot $stmt
* @param Context $context
* @return false|null
*/
protected static function checkBooleanNot(
StatementsChecker $statements_checker,
PhpParser\Node\Expr\BooleanNot $stmt,
Context $context
) {
$stmt->inferredType = Type::getBool();
2016-10-22 19:23:18 +02:00
return self::check($statements_checker, $stmt->expr, $context);
}
2016-11-02 07:29:00 +01:00
/**
* @param StatementsChecker $statements_checker
* @param PhpParser\Node\Expr\Empty_ $stmt
* @param Context $context
* @return false|null
*/
protected static function checkEmpty(
StatementsChecker $statements_checker,
PhpParser\Node\Expr\Empty_ $stmt,
Context $context
) {
2016-10-22 19:23:18 +02:00
return self::check($statements_checker, $stmt->expr, $context);
}
/**
2016-11-02 07:29:00 +01:00
* @param StatementsChecker $statements_checker
* @param PhpParser\Node\Scalar\Encapsed $stmt
* @param Context $context
* @return false|null
2016-10-22 19:23:18 +02:00
*/
2016-11-02 07:29:00 +01:00
protected static function checkEncapsulatedString(
StatementsChecker $statements_checker,
PhpParser\Node\Scalar\Encapsed $stmt,
Context $context
) {
2016-10-22 19:23:18 +02:00
/** @var PhpParser\Node\Expr $part */
foreach ($stmt->parts as $part) {
if (self::check($statements_checker, $part, $context) === false) {
return false;
}
}
$stmt->inferredType = Type::getString();
2016-11-02 07:29:00 +01:00
return null;
2016-10-22 19:23:18 +02:00
}
/**
* @param string $fq_class_name
2016-10-22 19:23:18 +02:00
* @return boolean
*/
public static function isMock($fq_class_name)
2016-10-22 19:23:18 +02:00
{
return in_array($fq_class_name, Config::getInstance()->getMockClasses());
2016-10-22 19:23:18 +02:00
}
2016-10-22 23:35:59 +02:00
2016-11-02 07:29:00 +01:00
/**
* @return void
*/
2016-10-22 23:35:59 +02:00
public static function clearCache()
{
self::$reflection_functions = [];
}
2016-10-22 19:23:18 +02:00
}