2016-10-22 19:23:18 +02:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Checker\Statements;
|
|
|
|
|
|
|
|
use PhpParser;
|
2017-03-18 18:37:00 +01:00
|
|
|
use Psalm\Checker\AlgebraChecker;
|
2016-10-22 19:23:18 +02:00
|
|
|
use Psalm\Checker\ClassChecker;
|
|
|
|
use Psalm\Checker\ClassLikeChecker;
|
|
|
|
use Psalm\Checker\ClosureChecker;
|
|
|
|
use Psalm\Checker\CommentChecker;
|
2017-09-03 01:23:00 +02:00
|
|
|
use Psalm\Checker\FunctionLikeChecker;
|
2016-10-22 19:23:18 +02:00
|
|
|
use Psalm\Checker\MethodChecker;
|
2017-07-29 21:05:06 +02:00
|
|
|
use Psalm\Checker\ProjectChecker;
|
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;
|
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;
|
2017-11-07 20:46:53 +01:00
|
|
|
use Psalm\FileManipulation\FileManipulationBuffer;
|
2016-10-22 19:23:18 +02:00
|
|
|
use Psalm\Issue\ForbiddenCode;
|
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;
|
2016-12-24 03:30:32 +01:00
|
|
|
use Psalm\Issue\InvalidOperand;
|
2016-12-12 05:40:46 +01:00
|
|
|
use Psalm\Issue\InvalidScope;
|
2016-12-24 03:30:32 +01:00
|
|
|
use Psalm\Issue\MixedOperand;
|
2016-12-29 06:32:12 +01:00
|
|
|
use Psalm\Issue\NullOperand;
|
2017-02-11 23:55:08 +01:00
|
|
|
use Psalm\Issue\PossiblyNullOperand;
|
2017-12-06 06:56:00 +01:00
|
|
|
use Psalm\Issue\PossiblyUndefinedGlobalVariable;
|
2016-10-22 19:23:18 +02:00
|
|
|
use Psalm\Issue\PossiblyUndefinedVariable;
|
2017-12-06 06:56:00 +01:00
|
|
|
use Psalm\Issue\UndefinedGlobalVariable;
|
2016-10-22 19:23:18 +02:00
|
|
|
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;
|
2017-01-07 20:35:07 +01:00
|
|
|
use Psalm\StatementsSource;
|
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;
|
|
|
|
use Psalm\Type\Atomic\TInt;
|
2017-01-15 01:06:58 +01:00
|
|
|
use Psalm\Type\Atomic\TMixed;
|
|
|
|
use Psalm\Type\Atomic\TNamedObject;
|
2017-09-20 17:22:06 +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;
|
2017-12-29 16:55:41 +01:00
|
|
|
use Psalm\Type\Reconciler;
|
2016-10-22 19:23:18 +02:00
|
|
|
|
|
|
|
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
|
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(
|
2016-10-22 19:23:18 +02:00
|
|
|
StatementsChecker $statements_checker,
|
|
|
|
PhpParser\Node\Expr $stmt,
|
|
|
|
Context $context,
|
2017-11-19 18:33:43 +01:00
|
|
|
$array_assignment = false
|
2016-10-22 19:23:18 +02:00
|
|
|
) {
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\Variable) {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (self::analyzeVariable($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) {
|
2017-01-07 21:09:47 +01:00
|
|
|
$assignment_type = AssignmentChecker::analyze(
|
2016-11-01 16:37:58 +01:00
|
|
|
$statements_checker,
|
|
|
|
$stmt->var,
|
|
|
|
$stmt->expr,
|
2016-12-04 20:14:00 +01:00
|
|
|
null,
|
2016-11-01 16:37:58 +01:00
|
|
|
$context,
|
2017-04-11 21:34:05 +02:00
|
|
|
(string)$stmt->getDocComment(),
|
|
|
|
$stmt->getLine()
|
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
|
|
|
|
|
|
|
$stmt->inferredType = $assignment_type;
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\AssignOp) {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (AssignmentChecker::analyzeAssignmentOperation($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) {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (CallChecker::analyzeMethodCall($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) {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (CallChecker::analyzeStaticCall($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) {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (FetchChecker::analyzeConstFetch($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) {
|
2017-12-06 06:05:01 +01:00
|
|
|
switch (strtolower($stmt->getName())) {
|
|
|
|
case '__line__':
|
|
|
|
$stmt->inferredType = Type::getInt();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '__file__':
|
|
|
|
case '__dir__':
|
|
|
|
case '__function__':
|
|
|
|
case '__class__':
|
|
|
|
case '__trait__':
|
|
|
|
case '__method__':
|
|
|
|
case '__namespace__':
|
|
|
|
$stmt->inferredType = Type::getString();
|
|
|
|
break;
|
|
|
|
}
|
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();
|
2017-01-26 04:02:19 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\UnaryMinus ||
|
|
|
|
$stmt instanceof PhpParser\Node\Expr\UnaryPlus
|
|
|
|
) {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (self::analyze($statements_checker, $stmt->expr, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-11-07 21:29:47 +01:00
|
|
|
|
2017-01-26 04:02:19 +01:00
|
|
|
if (!isset($stmt->expr->inferredType)) {
|
|
|
|
$stmt->inferredType = new Type\Union([new TInt, new TFloat]);
|
2017-01-26 05:50:34 +01:00
|
|
|
} elseif ($stmt->expr->inferredType->isMixed()) {
|
|
|
|
$stmt->inferredType = Type::getMixed();
|
2017-01-26 04:02:19 +01:00
|
|
|
} else {
|
2017-01-26 04:23:06 +01:00
|
|
|
$acceptable_types = [];
|
|
|
|
|
2018-01-09 21:05:48 +01:00
|
|
|
foreach ($stmt->expr->inferredType->getTypes() as $type_part) {
|
2017-01-26 04:23:06 +01:00
|
|
|
if ($type_part instanceof TInt || $type_part instanceof TFloat) {
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
2017-01-26 05:50:34 +01:00
|
|
|
$stmt->inferredType = 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_) {
|
2017-01-15 19:58:20 +01:00
|
|
|
self::analyzeIsset($statements_checker, $stmt, $context);
|
2016-11-05 22:53:30 +01:00
|
|
|
$stmt->inferredType = Type::getBool();
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\ClassConstFetch) {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (FetchChecker::analyzeClassConstFetch($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) {
|
2017-02-12 01:30:06 +01:00
|
|
|
if (FetchChecker::analyzePropertyFetch($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\StaticPropertyFetch) {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (FetchChecker::analyzeStaticPropertyFetch($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) {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (self::analyze($statements_checker, $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\BinaryOp) {
|
2017-02-13 00:06:18 +01:00
|
|
|
if (self::analyzeBinaryOp(
|
|
|
|
$statements_checker,
|
|
|
|
$stmt,
|
2017-02-13 01:07:25 +01:00
|
|
|
$context
|
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
|
|
|
|
) {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (self::analyze($statements_checker, $stmt->var, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-12-25 00:44:11 +01:00
|
|
|
|
|
|
|
if (isset($stmt->var->inferredType)) {
|
|
|
|
$stmt->inferredType = clone $stmt->var->inferredType;
|
|
|
|
} else {
|
|
|
|
$stmt->inferredType = Type::getMixed();
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\New_) {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (CallChecker::analyzeNew($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_) {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (self::analyzeArray($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\Encapsed) {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (self::analyzeEncapsulatedString($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\FuncCall) {
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker = $statements_checker->getFileChecker()->project_checker;
|
|
|
|
if (CallChecker::analyzeFunctionCall(
|
|
|
|
$project_checker,
|
|
|
|
$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) {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (self::analyzeTernary($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\BooleanNot) {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (self::analyzeBooleanNot($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\Empty_) {
|
2017-12-05 18:14:10 +01:00
|
|
|
self::analyzeEmpty($statements_checker, $stmt, $context);
|
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());
|
|
|
|
|
2017-01-07 21:09:47 +01:00
|
|
|
if (self::analyzeClosureUses($statements_checker, $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);
|
2017-04-28 06:31:55 +02:00
|
|
|
$use_context->collect_references =
|
|
|
|
$statements_checker->getFileChecker()->project_checker->collect_references;
|
2016-10-22 19:23:18 +02:00
|
|
|
|
|
|
|
if (!$statements_checker->isStatic()) {
|
2017-01-12 06:54:41 +01:00
|
|
|
if ($context->collect_mutations &&
|
|
|
|
$context->self &&
|
2017-01-02 21:31:18 +01:00
|
|
|
ClassChecker::classExtends(
|
2017-07-29 21:05:06 +02:00
|
|
|
$statements_checker->getFileChecker()->project_checker,
|
2017-01-12 06:54:41 +01:00
|
|
|
$context->self,
|
2017-01-07 20:35:07 +01:00
|
|
|
(string)$statements_checker->getFQCLN()
|
2017-01-02 21:31:18 +01:00
|
|
|
)
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2016-10-30 03:17:46 +01: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.
|
2017-02-01 05:24:33 +01:00
|
|
|
if (!$context->hasVariable('$' . $use->var) && $use->byRef) {
|
2016-10-30 03:17:46 +01:00
|
|
|
$context->vars_in_scope['$' . $use->var] = Type::getMixed();
|
|
|
|
}
|
|
|
|
|
2018-01-12 18:33:26 +01:00
|
|
|
$use_context->vars_in_scope['$' . $use->var] =
|
|
|
|
$context->hasVariable('$' . $use->var) && !$use->byRef
|
2016-11-02 07:29:00 +01:00
|
|
|
? clone $context->vars_in_scope['$' . $use->var]
|
|
|
|
: Type::getMixed();
|
2016-10-30 03:17:46 +01:00
|
|
|
|
2016-10-22 19:23:18 +02:00
|
|
|
$use_context->vars_possibly_in_scope['$' . $use->var] = true;
|
|
|
|
}
|
|
|
|
|
2017-04-14 12:03:09 +02:00
|
|
|
$closure_checker->analyze($use_context, $context);
|
2016-10-22 19:23:18 +02:00
|
|
|
|
2016-12-07 20:13:39 +01:00
|
|
|
if (!isset($stmt->inferredType)) {
|
|
|
|
$stmt->inferredType = Type::getClosure();
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\ArrayDimFetch) {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (FetchChecker::analyzeArrayAccess(
|
2016-11-02 07:29:00 +01:00
|
|
|
$statements_checker,
|
|
|
|
$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_) {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (self::analyze($statements_checker, $stmt->expr, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmt->inferredType = Type::getInt();
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Cast\Double) {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (self::analyze($statements_checker, $stmt->expr, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmt->inferredType = Type::getFloat();
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Cast\Bool_) {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (self::analyze($statements_checker, $stmt->expr, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmt->inferredType = Type::getBool();
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Cast\String_) {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (self::analyze($statements_checker, $stmt->expr, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-07 17:27:54 +02:00
|
|
|
$container_type = Type::getString();
|
|
|
|
|
2017-10-07 21:05:05 +02:00
|
|
|
if (isset($stmt->expr->inferredType)
|
2017-10-07 17:27:54 +02:00
|
|
|
&& !$stmt->expr->inferredType->isMixed()
|
|
|
|
&& !TypeChecker::isContainedBy(
|
|
|
|
$statements_checker->getFileChecker()->project_checker,
|
|
|
|
$stmt->expr->inferredType,
|
|
|
|
$container_type,
|
|
|
|
true,
|
2017-10-23 17:47:00 +02:00
|
|
|
false,
|
2017-10-07 17:27:54 +02:00
|
|
|
$has_scalar_match
|
|
|
|
)
|
|
|
|
&& !$has_scalar_match
|
|
|
|
) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidCast(
|
|
|
|
$stmt->expr->inferredType . ' cannot be cast to ' . $container_type,
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$stmt->inferredType = $container_type;
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Cast\Object_) {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (self::analyze($statements_checker, $stmt->expr, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-09 21:58:09 +01:00
|
|
|
$stmt->inferredType = new Type\Union([new TNamedObject('stdClass')]);
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Cast\Array_) {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (self::analyze($statements_checker, $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
|
|
|
|
|
|
|
if (isset($stmt->expr->inferredType)) {
|
2017-04-21 22:33:18 +02:00
|
|
|
$all_permissible = true;
|
|
|
|
|
2018-01-09 21:05:48 +01:00
|
|
|
foreach ($stmt->expr->inferredType->getTypes() as $type) {
|
2017-04-21 20:06:47 +02:00
|
|
|
if ($type instanceof Scalar) {
|
|
|
|
$permissible_atomic_types[] = new TArray([Type::getInt(), new Type\Union([$type])]);
|
|
|
|
} elseif ($type instanceof TArray) {
|
|
|
|
$permissible_atomic_types[] = $type;
|
|
|
|
} elseif ($type instanceof ObjectLike) {
|
2017-12-19 00:47:17 +01:00
|
|
|
$permissible_atomic_types[] = $type->getGenericArrayType();
|
2017-04-21 20:06:47 +02:00
|
|
|
} else {
|
|
|
|
$all_permissible = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($all_permissible) {
|
|
|
|
$stmt->inferredType = Type::combineTypes($permissible_atomic_types);
|
|
|
|
} else {
|
|
|
|
$stmt->inferredType = Type::getArray();
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Cast\Unset_) {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (self::analyze($statements_checker, $stmt->expr, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmt->inferredType = Type::getNull();
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Clone_) {
|
2017-01-16 18:59:09 +01:00
|
|
|
self::analyzeClone($statements_checker, $stmt, $context);
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Instanceof_) {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (self::analyze($statements_checker, $stmt->expr, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
if ($stmt->class instanceof PhpParser\Node\Name &&
|
2017-11-15 03:56:29 +01:00
|
|
|
!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) {
|
2016-11-08 01:16:51 +01:00
|
|
|
$fq_class_name = ClassLikeChecker::getFQCLNFromNameObject(
|
2016-10-22 19:23:18 +02:00
|
|
|
$stmt->class,
|
2017-07-25 22:11:02 +02:00
|
|
|
$statements_checker->getAliases()
|
2016-10-22 19:23:18 +02:00
|
|
|
);
|
|
|
|
|
2016-11-08 01:16:51 +01:00
|
|
|
if (ClassLikeChecker::checkFullyQualifiedClassLikeName(
|
2018-01-02 02:04:03 +01:00
|
|
|
$statements_checker,
|
2016-11-07 23:29:51 +01:00
|
|
|
$fq_class_name,
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt->class),
|
2018-01-02 02:04:03 +01:00
|
|
|
$statements_checker->getSuppressedIssues(),
|
|
|
|
false
|
2016-11-02 07:29:00 +01:00
|
|
|
) === 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_) {
|
2017-02-12 01:42:12 +01:00
|
|
|
if ($stmt->expr) {
|
|
|
|
if (self::analyze($statements_checker, $stmt->expr, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Include_) {
|
2017-01-07 21:09:47 +01:00
|
|
|
$statements_checker->analyzeInclude($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;
|
|
|
|
|
2017-01-07 21:09:47 +01:00
|
|
|
if (self::analyze($statements_checker, $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) {
|
2017-02-04 04:07:14 +01:00
|
|
|
if (AssignmentChecker::analyzeAssignmentRef($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\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(
|
2016-12-04 01:11:30 +01:00
|
|
|
new ForbiddenCode(
|
|
|
|
'Use of shell_exec',
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
),
|
2016-10-22 19:23:18 +02:00
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Print_) {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (self::analyze($statements_checker, $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\Yield_) {
|
2017-01-07 21:09:47 +01:00
|
|
|
self::analyzeYield($statements_checker, $stmt, $context);
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\YieldFrom) {
|
2017-01-07 21:09:47 +01:00
|
|
|
self::analyzeYieldFrom($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),
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
2016-11-06 01:53:39 +01:00
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
|
2016-12-04 01:11:30 +01:00
|
|
|
$plugins = Config::getInstance()->getPlugins();
|
|
|
|
|
|
|
|
if ($plugins) {
|
2017-11-07 20:46:53 +01:00
|
|
|
$file_manipulations = [];
|
2016-12-04 01:11:30 +01:00
|
|
|
$code_location = new CodeLocation($statements_checker->getSource(), $stmt);
|
2016-12-08 21:57:18 +01:00
|
|
|
|
2016-12-04 01:11:30 +01:00
|
|
|
foreach ($plugins as $plugin) {
|
2017-11-07 20:46:53 +01:00
|
|
|
if ($plugin->afterExpressionCheck(
|
2017-01-02 21:31:18 +01:00
|
|
|
$statements_checker,
|
2016-12-04 01:11:30 +01:00
|
|
|
$stmt,
|
|
|
|
$context,
|
|
|
|
$code_location,
|
2017-11-07 20:46:53 +01:00
|
|
|
$statements_checker->getSuppressedIssues(),
|
|
|
|
$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) {
|
|
|
|
FileManipulationBuffer::add($statements_checker->getFilePath(), $file_manipulations);
|
|
|
|
}
|
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-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
|
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 analyzeVariable(
|
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
|
|
|
|
) {
|
2016-11-05 23:31:09 +01:00
|
|
|
if ($stmt->name === 'this') {
|
|
|
|
if ($statements_checker->isStatic()) {
|
|
|
|
if (IssueBuffer::accepts(
|
2017-12-22 16:09:56 +01:00
|
|
|
new InvalidScope(
|
2016-11-05 23:31:09 +01:00
|
|
|
'Invalid reference to $this in a static context',
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
2016-11-05 23:31:09 +01:00
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-12 05:40:46 +01:00
|
|
|
return null;
|
|
|
|
} elseif (!isset($context->vars_in_scope['$this'])) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidScope(
|
|
|
|
'Invalid reference to $this in a non-class context',
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-05 23:31:09 +01:00
|
|
|
return null;
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
2016-11-05 23:31:09 +01:00
|
|
|
|
|
|
|
$stmt->inferredType = clone $context->vars_in_scope['$this'];
|
|
|
|
|
|
|
|
return null;
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$context->check_variables) {
|
2017-02-25 03:27:52 +01:00
|
|
|
if (is_string($stmt->name)) {
|
|
|
|
$var_name = '$' . $stmt->name;
|
|
|
|
|
|
|
|
if (!$context->hasVariable($var_name)) {
|
|
|
|
$context->vars_in_scope[$var_name] = Type::getMixed();
|
|
|
|
$context->vars_possibly_in_scope[$var_name] = true;
|
|
|
|
$stmt->inferredType = Type::getMixed();
|
|
|
|
} else {
|
2017-12-18 05:22:26 +01:00
|
|
|
$stmt->inferredType = clone $context->vars_in_scope[$var_name];
|
2017-02-25 03:27:52 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$stmt->inferredType = Type::getMixed();
|
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
|
|
|
}
|
|
|
|
|
2017-01-16 06:49:12 +01:00
|
|
|
if (in_array(
|
|
|
|
$stmt->name,
|
|
|
|
[
|
2017-02-13 04:12:31 +01:00
|
|
|
'GLOBALS',
|
2017-01-16 06:49:12 +01:00
|
|
|
'_SERVER',
|
|
|
|
'_GET',
|
|
|
|
'_POST',
|
2017-02-13 04:12:31 +01:00
|
|
|
'_FILES',
|
2017-01-16 06:49:12 +01:00
|
|
|
'_COOKIE',
|
2017-02-13 04:12:31 +01:00
|
|
|
'_SESSION',
|
2017-01-16 06:49:12 +01:00
|
|
|
'_REQUEST',
|
|
|
|
'_ENV',
|
2017-12-01 01:00:09 +01:00
|
|
|
],
|
|
|
|
true
|
2017-01-16 06:49:12 +01:00
|
|
|
)
|
|
|
|
) {
|
2017-02-12 22:58:04 +01:00
|
|
|
$stmt->inferredType = Type::getArray();
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_string($stmt->name)) {
|
2017-01-07 21:09:47 +01:00
|
|
|
return self::analyze($statements_checker, $stmt->name, $context);
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($passed_by_reference && $by_ref_type) {
|
|
|
|
self::assignByRefParam($statements_checker, $stmt, $by_ref_type, $context);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
$var_name = '$' . $stmt->name;
|
|
|
|
|
2017-02-01 05:24:33 +01:00
|
|
|
if (!$context->hasVariable($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;
|
2017-02-08 00:09:12 +01:00
|
|
|
|
|
|
|
// it might have been defined first in another if/else branch
|
|
|
|
if (!$statements_checker->hasVariable($var_name)) {
|
|
|
|
$statements_checker->registerVariable(
|
|
|
|
$var_name,
|
|
|
|
new CodeLocation($statements_checker, $stmt)
|
|
|
|
);
|
|
|
|
}
|
2018-01-10 01:33:39 +01:00
|
|
|
} elseif (!$context->inside_isset) {
|
2017-12-06 06:56:00 +01:00
|
|
|
if ($context->is_global) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UndefinedGlobalVariable(
|
|
|
|
'Cannot find referenced variable ' . $var_name . ' in global scope',
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$stmt->inferredType = Type::getMixed();
|
|
|
|
|
|
|
|
return null;
|
2017-12-06 07:05:51 +01:00
|
|
|
}
|
2018-01-10 01:33:39 +01:00
|
|
|
|
2017-12-06 07:05:51 +01:00
|
|
|
IssueBuffer::add(
|
2018-01-03 03:23:48 +01:00
|
|
|
new UndefinedVariable(
|
|
|
|
'Cannot find referenced variable ' . $var_name,
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
)
|
|
|
|
);
|
2016-10-22 19:23:18 +02:00
|
|
|
|
2017-12-06 07:05:51 +01:00
|
|
|
$stmt->inferredType = Type::getMixed();
|
2017-12-06 06:35:41 +01:00
|
|
|
|
2017-12-06 07:05:51 +01:00
|
|
|
return false;
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-08 00:09:12 +01:00
|
|
|
$first_appearance = $statements_checker->getFirstAppearance($var_name);
|
|
|
|
|
2018-01-10 01:33:39 +01:00
|
|
|
if ($first_appearance && !$context->inside_isset) {
|
2017-12-06 06:56:00 +01:00
|
|
|
if ($context->is_global) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new PossiblyUndefinedGlobalVariable(
|
|
|
|
'Possibly undefined global variable ' . $var_name . ', first seen on line ' .
|
|
|
|
$first_appearance->getLineNumber(),
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new PossiblyUndefinedVariable(
|
|
|
|
'Possibly undefined variable ' . $var_name . ', first seen on line ' .
|
|
|
|
$first_appearance->getLineNumber(),
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2017-12-18 05:22:26 +01:00
|
|
|
$stmt->inferredType = clone $context->vars_in_scope[$var_name];
|
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-02 07:29:00 +01:00
|
|
|
* @param StatementsChecker $statements_checker
|
|
|
|
* @param PhpParser\Node\Expr $stmt
|
|
|
|
* @param Type\Union $by_ref_type
|
2017-10-28 21:33:29 +02:00
|
|
|
* @param bool $constrain_type
|
2016-11-02 07:29:00 +01:00
|
|
|
* @param Context $context
|
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(
|
|
|
|
StatementsChecker $statements_checker,
|
|
|
|
PhpParser\Node\Expr $stmt,
|
|
|
|
Type\Union $by_ref_type,
|
2017-10-28 21:33:29 +02:00
|
|
|
Context $context,
|
|
|
|
$constrain_type = true
|
2016-11-02 07:29:00 +01:00
|
|
|
) {
|
|
|
|
$var_id = self::getVarId(
|
|
|
|
$stmt,
|
2016-11-08 01:16:51 +01:00
|
|
|
$statements_checker->getFQCLN(),
|
2017-01-07 20:35:07 +01:00
|
|
|
$statements_checker
|
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) {
|
2017-10-28 21:33:29 +02:00
|
|
|
if (!$by_ref_type->isMixed() && $constrain_type) {
|
2017-02-23 06:25:28 +01:00
|
|
|
$context->byref_constraints[$var_id] = new \Psalm\ReferenceConstraint($by_ref_type);
|
|
|
|
}
|
|
|
|
|
2017-02-01 05:24:33 +01:00
|
|
|
if (!$context->hasVariable($var_id)) {
|
2016-12-24 19:23:22 +01:00
|
|
|
$context->vars_possibly_in_scope[$var_id] = true;
|
2017-02-08 00:09:12 +01:00
|
|
|
|
|
|
|
if (!$statements_checker->hasVariable($var_id)) {
|
|
|
|
$statements_checker->registerVariable($var_id, new CodeLocation($statements_checker, $stmt));
|
2017-12-30 15:30:02 +01:00
|
|
|
$context->hasVariable($var_id);
|
2017-02-08 00:09:12 +01:00
|
|
|
}
|
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
|
|
|
|
|
|
|
// removes dependennt vars from $context
|
|
|
|
$context->removeDescendents(
|
|
|
|
$var_id,
|
|
|
|
$existing_type,
|
|
|
|
$by_ref_type,
|
2017-06-23 06:39:37 +02:00
|
|
|
$statements_checker
|
2017-04-02 23:37:56 +02:00
|
|
|
);
|
|
|
|
|
2018-01-09 21:44:31 +01:00
|
|
|
if ($existing_type->getId() !== 'array<empty, empty>') {
|
2017-03-13 23:06:56 +01:00
|
|
|
$context->vars_in_scope[$var_id] = $by_ref_type;
|
2016-12-24 19:23:22 +01:00
|
|
|
$stmt->inferredType = $context->vars_in_scope[$var_id];
|
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
|
|
|
|
|
|
|
$context->vars_in_scope[$var_id] = $by_ref_type;
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$stmt->inferredType = $by_ref_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-02 07:29:00 +01:00
|
|
|
* @param StatementsChecker $statements_checker
|
|
|
|
* @param PhpParser\Node\Expr\Array_ $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 analyzeArray(
|
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();
|
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
|
|
|
}
|
|
|
|
|
|
|
|
$item_key_type = null;
|
|
|
|
|
|
|
|
$item_value_type = null;
|
|
|
|
|
|
|
|
$property_types = [];
|
|
|
|
|
2017-02-11 05:35:34 +01:00
|
|
|
$can_create_objectlike = true;
|
|
|
|
|
2017-12-19 00:47:17 +01:00
|
|
|
foreach ($stmt->items as $int_offset => $item) {
|
2016-10-22 19:23:18 +02:00
|
|
|
if ($item->key) {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (self::analyze($statements_checker, $item->key, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($item->key->inferredType)) {
|
|
|
|
if ($item_key_type) {
|
|
|
|
$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();
|
|
|
|
}
|
|
|
|
|
2017-01-07 21:09:47 +01:00
|
|
|
if (self::analyze($statements_checker, $item->value, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-02-11 05:35:34 +01:00
|
|
|
if ($item_value_type && $item_value_type->isMixed() && !$can_create_objectlike) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-10-22 19:23:18 +02:00
|
|
|
if (isset($item->value->inferredType)) {
|
2017-12-19 00:47:17 +01:00
|
|
|
if ($item->key instanceof PhpParser\Node\Scalar\String_
|
|
|
|
|| $item->key instanceof PhpParser\Node\Scalar\LNumber
|
|
|
|
|| !$item->key
|
|
|
|
) {
|
|
|
|
$property_types[$item->key ? $item->key->value : $int_offset] = $item->value->inferredType;
|
2017-02-11 05:35:34 +01:00
|
|
|
} else {
|
|
|
|
$can_create_objectlike = false;
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2017-02-11 05:35:34 +01:00
|
|
|
} else {
|
|
|
|
$item_value_type = Type::getMixed();
|
2017-03-20 07:05:58 +01:00
|
|
|
|
2017-12-19 00:47:17 +01:00
|
|
|
if ($item->key instanceof PhpParser\Node\Scalar\String_
|
|
|
|
|| $item->key instanceof PhpParser\Node\Scalar\LNumber
|
|
|
|
|| !$item->key
|
|
|
|
) {
|
|
|
|
$property_types[$item->key ? $item->key->value : $int_offset] = $item_value_type;
|
2017-03-20 07:05:58 +01:00
|
|
|
} else {
|
|
|
|
$can_create_objectlike = false;
|
|
|
|
}
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if this array looks like an object-like array, let's return that instead
|
2017-12-19 00:47:17 +01:00
|
|
|
if ($item_value_type
|
|
|
|
&& $item_key_type
|
|
|
|
&& ($item_key_type->hasString() || $item_key_type->hasInt())
|
|
|
|
&& $can_create_objectlike
|
2017-02-11 05:35:34 +01:00
|
|
|
) {
|
2017-01-15 01:06:58 +01:00
|
|
|
$stmt->inferredType = new Type\Union([new Type\Atomic\ObjectLike($property_types)]);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
$stmt->inferredType = new Type\Union([
|
2017-01-15 01:06:58 +01:00
|
|
|
new Type\Atomic\TArray([
|
|
|
|
$item_key_type ?: new Type\Union([new TInt, new TString]),
|
2017-05-27 02:05:57 +02:00
|
|
|
$item_value_type ?: Type::getMixed(),
|
|
|
|
]),
|
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-02 07:29:00 +01:00
|
|
|
* @param StatementsChecker $statements_checker
|
|
|
|
* @param PhpParser\Node\Expr\BinaryOp $stmt
|
|
|
|
* @param Context $context
|
|
|
|
* @param int $nesting
|
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 analyzeBinaryOp(
|
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
|
2017-02-17 02:43:23 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\BinaryOp\BooleanAnd ||
|
|
|
|
$stmt instanceof PhpParser\Node\Expr\BinaryOp\LogicalAnd
|
|
|
|
) {
|
2017-03-18 18:37:00 +01:00
|
|
|
$if_clauses = AlgebraChecker::getFormula(
|
2016-10-22 19:23:18 +02:00
|
|
|
$stmt->left,
|
2016-11-08 01:16:51 +01:00
|
|
|
$statements_checker->getFQCLN(),
|
2017-01-07 20:35:07 +01:00
|
|
|
$statements_checker
|
2016-10-22 19:23:18 +02:00
|
|
|
);
|
|
|
|
|
2017-11-28 06:46:41 +01:00
|
|
|
$pre_referenced_var_ids = $context->referenced_var_ids;
|
|
|
|
$context->referenced_var_ids = [];
|
|
|
|
|
|
|
|
$pre_assigned_var_ids = $context->assigned_var_ids;
|
|
|
|
|
2017-01-07 21:09:47 +01:00
|
|
|
if (self::analyze($statements_checker, $stmt->left, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-28 06:46:41 +01:00
|
|
|
$new_referenced_var_ids = $context->referenced_var_ids;
|
|
|
|
$context->referenced_var_ids = array_merge($pre_referenced_var_ids, $new_referenced_var_ids);
|
|
|
|
|
|
|
|
$new_assigned_var_ids = array_diff_key($context->assigned_var_ids, $pre_assigned_var_ids);
|
|
|
|
|
|
|
|
$new_referenced_var_ids = array_diff_key($new_referenced_var_ids, $new_assigned_var_ids);
|
|
|
|
|
2017-12-04 00:33:54 +01:00
|
|
|
$simplified_clauses = AlgebraChecker::simplifyCNF(array_merge($context->clauses, $if_clauses));
|
|
|
|
|
|
|
|
$left_type_assertions = AlgebraChecker::getTruthsFromFormula($simplified_clauses);
|
|
|
|
|
2017-11-28 06:46:41 +01:00
|
|
|
$changed_var_ids = [];
|
2016-12-28 20:20:16 +01:00
|
|
|
|
2016-10-22 19:23:18 +02:00
|
|
|
// while in an and, we allow scope to boil over to support
|
|
|
|
// statements of the form if ($x && $x->foo())
|
2017-12-29 16:55:41 +01:00
|
|
|
$op_vars_in_scope = Reconciler::reconcileKeyedTypes(
|
2016-10-22 19:23:18 +02:00
|
|
|
$left_type_assertions,
|
|
|
|
$context->vars_in_scope,
|
2017-11-28 06:46:41 +01:00
|
|
|
$changed_var_ids,
|
|
|
|
$new_referenced_var_ids,
|
2017-06-23 06:39:37 +02:00
|
|
|
$statements_checker,
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt),
|
2016-10-22 19:23:18 +02:00
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($op_vars_in_scope === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$op_context = clone $context;
|
|
|
|
$op_context->vars_in_scope = $op_vars_in_scope;
|
|
|
|
|
2017-12-16 16:51:04 +01:00
|
|
|
$op_context->removeReconciledClauses($changed_var_ids);
|
2017-11-28 06:46:41 +01:00
|
|
|
|
2017-01-07 21:09:47 +01:00
|
|
|
if (self::analyze($statements_checker, $stmt->right, $op_context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-24 18:17:28 +01:00
|
|
|
$context->referenced_var_ids = array_merge(
|
|
|
|
$op_context->referenced_var_ids,
|
|
|
|
$context->referenced_var_ids
|
2017-02-27 05:09:18 +01:00
|
|
|
);
|
|
|
|
|
2017-04-07 04:56:37 +02:00
|
|
|
foreach ($op_context->vars_in_scope as $var_id => $type) {
|
|
|
|
if (isset($context->vars_in_scope[$var_id])) {
|
|
|
|
$context->vars_in_scope[$var_id] = Type::combineUnionTypes($context->vars_in_scope[$var_id], $type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-13 01:07:25 +01:00
|
|
|
if ($context->inside_conditional) {
|
|
|
|
foreach ($op_context->vars_in_scope as $var => $type) {
|
|
|
|
if (!isset($context->vars_in_scope[$var])) {
|
|
|
|
$context->vars_in_scope[$var] = $type;
|
|
|
|
continue;
|
|
|
|
}
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
|
2017-02-13 01:07:25 +01:00
|
|
|
$context->updateChecks($op_context);
|
2016-10-22 19:23:18 +02:00
|
|
|
|
2017-02-13 01:07:25 +01:00
|
|
|
$context->vars_possibly_in_scope = array_merge(
|
|
|
|
$op_context->vars_possibly_in_scope,
|
|
|
|
$context->vars_possibly_in_scope
|
|
|
|
);
|
2017-11-28 06:46:41 +01:00
|
|
|
|
|
|
|
$context->assigned_var_ids = array_merge(
|
|
|
|
$context->assigned_var_ids,
|
|
|
|
$op_context->assigned_var_ids
|
|
|
|
);
|
2017-02-13 01:07:25 +01:00
|
|
|
}
|
2017-02-17 02:43:23 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\BinaryOp\BooleanOr ||
|
|
|
|
$stmt instanceof PhpParser\Node\Expr\BinaryOp\LogicalOr
|
|
|
|
) {
|
2017-11-28 06:46:41 +01:00
|
|
|
$pre_referenced_var_ids = $context->referenced_var_ids;
|
|
|
|
$context->referenced_var_ids = [];
|
|
|
|
|
|
|
|
$pre_assigned_var_ids = $context->assigned_var_ids;
|
|
|
|
|
2017-01-07 21:09:47 +01:00
|
|
|
if (self::analyze($statements_checker, $stmt->left, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-28 06:46:41 +01:00
|
|
|
$new_referenced_var_ids = $context->referenced_var_ids;
|
|
|
|
$context->referenced_var_ids = array_merge($pre_referenced_var_ids, $new_referenced_var_ids);
|
|
|
|
|
|
|
|
$new_assigned_var_ids = array_diff_key($context->assigned_var_ids, $pre_assigned_var_ids);
|
|
|
|
|
|
|
|
$new_referenced_var_ids = array_diff_key($new_referenced_var_ids, $new_assigned_var_ids);
|
|
|
|
|
2017-12-04 00:22:25 +01:00
|
|
|
$left_clauses = AlgebraChecker::getFormula(
|
|
|
|
$stmt->left,
|
|
|
|
$statements_checker->getFQCLN(),
|
|
|
|
$statements_checker
|
|
|
|
);
|
|
|
|
|
|
|
|
$rhs_clauses = AlgebraChecker::simplifyCNF(
|
|
|
|
array_merge(
|
|
|
|
$context->clauses,
|
|
|
|
AlgebraChecker::negateFormula($left_clauses)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$negated_type_assertions = AlgebraChecker::getTruthsFromFormula($rhs_clauses);
|
|
|
|
|
2017-11-28 06:46:41 +01:00
|
|
|
$changed_var_ids = [];
|
2016-12-28 20:20:16 +01:00
|
|
|
|
2016-10-22 19:23:18 +02:00
|
|
|
// while in an or, we allow scope to boil over to support
|
|
|
|
// statements of the form if ($x === null || $x->foo())
|
2017-12-29 16:55:41 +01:00
|
|
|
$op_vars_in_scope = Reconciler::reconcileKeyedTypes(
|
2016-10-22 19:23:18 +02:00
|
|
|
$negated_type_assertions,
|
|
|
|
$context->vars_in_scope,
|
2017-11-28 06:46:41 +01:00
|
|
|
$changed_var_ids,
|
|
|
|
$new_referenced_var_ids,
|
2017-06-23 06:39:37 +02:00
|
|
|
$statements_checker,
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt),
|
2016-10-22 19:23:18 +02:00
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($op_vars_in_scope === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$op_context = clone $context;
|
2016-12-27 19:58:58 +01:00
|
|
|
$op_context->clauses = $rhs_clauses;
|
2016-10-22 19:23:18 +02:00
|
|
|
$op_context->vars_in_scope = $op_vars_in_scope;
|
|
|
|
|
2017-12-16 16:51:04 +01:00
|
|
|
$op_context->removeReconciledClauses($changed_var_ids);
|
2017-11-28 06:46:41 +01:00
|
|
|
|
2017-01-07 21:09:47 +01:00
|
|
|
if (self::analyze($statements_checker, $stmt->right, $op_context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-06-20 19:25:37 +02:00
|
|
|
if (!($stmt->right instanceof PhpParser\Node\Expr\Exit_)) {
|
|
|
|
foreach ($op_context->vars_in_scope as $var_id => $type) {
|
|
|
|
if (isset($context->vars_in_scope[$var_id])) {
|
2017-06-20 20:07:54 +02:00
|
|
|
$context->vars_in_scope[$var_id] = Type::combineUnionTypes(
|
|
|
|
$context->vars_in_scope[$var_id],
|
|
|
|
$type
|
|
|
|
);
|
2017-06-20 19:25:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} elseif ($stmt->left instanceof PhpParser\Node\Expr\Assign) {
|
|
|
|
$var_id = self::getVarId($stmt->left->var, $context->self);
|
|
|
|
|
|
|
|
if ($var_id && isset($context->vars_in_scope[$var_id])) {
|
2017-12-29 16:55:41 +01:00
|
|
|
$left_inferred_reconciled = Reconciler::reconcileTypes(
|
2017-10-22 17:57:41 +02:00
|
|
|
'!falsy',
|
2017-06-20 19:25:37 +02:00
|
|
|
$context->vars_in_scope[$var_id],
|
|
|
|
'',
|
2017-06-23 06:39:37 +02:00
|
|
|
$statements_checker,
|
2017-06-20 19:25:37 +02:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt->left),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($left_inferred_reconciled) {
|
|
|
|
$context->vars_in_scope[$var_id] = $left_inferred_reconciled;
|
|
|
|
}
|
2017-04-07 04:56:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-13 01:07:25 +01:00
|
|
|
if ($context->inside_conditional) {
|
|
|
|
$context->updateChecks($op_context);
|
2017-11-09 22:42:39 +01:00
|
|
|
}
|
2016-10-22 19:23:18 +02:00
|
|
|
|
2017-11-24 18:17:28 +01:00
|
|
|
$context->referenced_var_ids = array_merge(
|
|
|
|
$op_context->referenced_var_ids,
|
|
|
|
$context->referenced_var_ids
|
2017-11-09 22:42:39 +01:00
|
|
|
);
|
2017-02-02 06:45:23 +01:00
|
|
|
|
2017-11-09 22:42:39 +01:00
|
|
|
$context->vars_possibly_in_scope = array_merge(
|
|
|
|
$op_context->vars_possibly_in_scope,
|
|
|
|
$context->vars_possibly_in_scope
|
|
|
|
);
|
2016-12-09 18:07:47 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\BinaryOp\Concat) {
|
|
|
|
$stmt->inferredType = Type::getString();
|
|
|
|
|
2017-01-07 21:09:47 +01:00
|
|
|
if (self::analyze($statements_checker, $stmt->left, $context) === false) {
|
2016-12-09 18:07:47 +01:00
|
|
|
return false;
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
|
2017-01-07 21:09:47 +01:00
|
|
|
if (self::analyze($statements_checker, $stmt->right, $context) === false) {
|
2016-12-09 18:07:47 +01:00
|
|
|
return false;
|
|
|
|
}
|
2017-02-18 02:50:47 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\BinaryOp\Coalesce) {
|
|
|
|
$t_if_context = clone $context;
|
|
|
|
|
2017-03-18 18:37:00 +01:00
|
|
|
$if_clauses = AlgebraChecker::getFormula(
|
2017-02-18 02:50:47 +01:00
|
|
|
$stmt,
|
|
|
|
$statements_checker->getFQCLN(),
|
|
|
|
$statements_checker
|
|
|
|
);
|
|
|
|
|
2017-03-18 18:37:00 +01:00
|
|
|
$ternary_clauses = AlgebraChecker::simplifyCNF(array_merge($context->clauses, $if_clauses));
|
2017-02-18 02:50:47 +01:00
|
|
|
|
2017-03-18 18:37:00 +01:00
|
|
|
$negated_clauses = AlgebraChecker::negateFormula($if_clauses);
|
2017-02-18 02:50:47 +01:00
|
|
|
|
2017-03-18 18:37:00 +01:00
|
|
|
$negated_if_types = AlgebraChecker::getTruthsFromFormula($negated_clauses);
|
2017-02-18 02:50:47 +01:00
|
|
|
|
2017-03-18 18:37:00 +01:00
|
|
|
$reconcilable_if_types = AlgebraChecker::getTruthsFromFormula($ternary_clauses);
|
2017-02-18 02:50:47 +01:00
|
|
|
|
2017-11-28 06:46:41 +01:00
|
|
|
$changed_var_ids = [];
|
2017-02-18 02:50:47 +01:00
|
|
|
|
2017-12-29 16:55:41 +01:00
|
|
|
$t_if_vars_in_scope_reconciled = Reconciler::reconcileKeyedTypes(
|
2017-02-18 02:50:47 +01:00
|
|
|
$reconcilable_if_types,
|
|
|
|
$t_if_context->vars_in_scope,
|
2017-11-28 06:46:41 +01:00
|
|
|
$changed_var_ids,
|
|
|
|
[],
|
2017-06-23 06:39:37 +02:00
|
|
|
$statements_checker,
|
2017-02-18 02:50:47 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt->left),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($t_if_vars_in_scope_reconciled === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$t_if_context->vars_in_scope = $t_if_vars_in_scope_reconciled;
|
|
|
|
|
|
|
|
if (self::analyze($statements_checker, $stmt->left, $t_if_context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-06-20 21:38:32 +02:00
|
|
|
foreach ($t_if_context->vars_in_scope as $var_id => $type) {
|
|
|
|
if (isset($context->vars_in_scope[$var_id])) {
|
|
|
|
$context->vars_in_scope[$var_id] = Type::combineUnionTypes($context->vars_in_scope[$var_id], $type);
|
|
|
|
} else {
|
|
|
|
$context->vars_in_scope[$var_id] = $type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-24 18:17:28 +01:00
|
|
|
$context->referenced_var_ids = array_merge(
|
|
|
|
$context->referenced_var_ids,
|
|
|
|
$t_if_context->referenced_var_ids
|
|
|
|
);
|
2017-02-18 02:50:47 +01:00
|
|
|
|
|
|
|
$t_else_context = clone $context;
|
|
|
|
|
|
|
|
if ($negated_if_types) {
|
2017-12-29 16:55:41 +01:00
|
|
|
$t_else_vars_in_scope_reconciled = Reconciler::reconcileKeyedTypes(
|
2017-02-18 02:50:47 +01:00
|
|
|
$negated_if_types,
|
|
|
|
$t_else_context->vars_in_scope,
|
2017-11-28 06:46:41 +01:00
|
|
|
$changed_var_ids,
|
|
|
|
[],
|
2017-06-23 06:39:37 +02:00
|
|
|
$statements_checker,
|
2017-02-18 02:50:47 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt->right),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($t_else_vars_in_scope_reconciled === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$t_else_context->vars_in_scope = $t_else_vars_in_scope_reconciled;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self::analyze($statements_checker, $stmt->right, $t_else_context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-24 18:17:28 +01:00
|
|
|
$context->referenced_var_ids = array_merge(
|
|
|
|
$context->referenced_var_ids,
|
|
|
|
$t_else_context->referenced_var_ids
|
|
|
|
);
|
2017-02-18 02:50:47 +01:00
|
|
|
|
|
|
|
$lhs_type = null;
|
|
|
|
|
|
|
|
if (isset($stmt->left->inferredType)) {
|
2017-12-29 16:55:41 +01:00
|
|
|
$if_return_type_reconciled = Reconciler::reconcileTypes(
|
2017-09-13 20:35:42 +02:00
|
|
|
'!null',
|
|
|
|
$stmt->left->inferredType,
|
|
|
|
'',
|
|
|
|
$statements_checker,
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($if_return_type_reconciled === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$lhs_type = $if_return_type_reconciled;
|
2017-02-18 02:50:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$lhs_type || !isset($stmt->right->inferredType)) {
|
|
|
|
$stmt->inferredType = Type::getMixed();
|
|
|
|
} else {
|
|
|
|
$stmt->inferredType = Type::combineUnionTypes($lhs_type, $stmt->right->inferredType);
|
|
|
|
}
|
2016-12-09 18:07:47 +01:00
|
|
|
} else {
|
2016-10-22 19:23:18 +02:00
|
|
|
if ($stmt->left instanceof PhpParser\Node\Expr\BinaryOp) {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (self::analyzeBinaryOp($statements_checker, $stmt->left, $context, ++$nesting) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (self::analyze($statements_checker, $stmt->left, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt->right instanceof PhpParser\Node\Expr\BinaryOp) {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (self::analyzeBinaryOp($statements_checker, $stmt->right, $context, ++$nesting) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (self::analyze($statements_checker, $stmt->right, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// let's do some fun type assignment
|
|
|
|
if (isset($stmt->left->inferredType) && isset($stmt->right->inferredType)) {
|
2016-12-08 22:37:14 +01:00
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\Plus ||
|
|
|
|
$stmt instanceof PhpParser\Node\Expr\BinaryOp\Minus ||
|
|
|
|
$stmt instanceof PhpParser\Node\Expr\BinaryOp\Mod ||
|
|
|
|
$stmt instanceof PhpParser\Node\Expr\BinaryOp\Mul ||
|
|
|
|
$stmt instanceof PhpParser\Node\Expr\BinaryOp\Pow
|
|
|
|
) {
|
2017-01-07 21:09:47 +01:00
|
|
|
self::analyzeNonDivArithmenticOp(
|
2016-12-24 03:30:32 +01:00
|
|
|
$statements_checker,
|
|
|
|
$stmt->left,
|
|
|
|
$stmt->right,
|
|
|
|
$stmt,
|
2017-09-07 03:44:26 +02:00
|
|
|
$result_type,
|
|
|
|
$context
|
2016-11-13 21:39:16 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
if ($result_type) {
|
|
|
|
$stmt->inferredType = $result_type;
|
|
|
|
}
|
2017-09-07 03:44:26 +02:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\BinaryOp\Div) {
|
|
|
|
$project_checker = $statements_checker->getFileChecker()->project_checker;
|
|
|
|
|
|
|
|
if ($project_checker->infer_types_from_usage
|
|
|
|
&& isset($stmt->left->inferredType)
|
|
|
|
&& isset($stmt->right->inferredType)
|
|
|
|
&& ($stmt->left->inferredType->isMixed() || $stmt->right->inferredType->isMixed())
|
|
|
|
) {
|
|
|
|
$source_checker = $statements_checker->getSource();
|
|
|
|
|
|
|
|
if ($source_checker instanceof FunctionLikeChecker) {
|
2017-12-07 21:50:25 +01:00
|
|
|
$function_storage = $source_checker->getFunctionLikeStorage($statements_checker);
|
2017-09-07 03:44:26 +02:00
|
|
|
|
|
|
|
$context->inferType($stmt->left, $function_storage, new Type\Union([new TInt, new TFloat]));
|
|
|
|
$context->inferType($stmt->right, $function_storage, new Type\Union([new TInt, new TFloat]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$stmt->inferredType = new Type\Union([new TInt, new TFloat]);
|
2016-12-24 03:30:32 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\BinaryOp\Concat) {
|
2017-01-07 21:09:47 +01:00
|
|
|
self::analyzeConcatOp(
|
2016-12-24 03:30:32 +01:00
|
|
|
$statements_checker,
|
|
|
|
$stmt->left,
|
|
|
|
$stmt->right,
|
2017-09-03 01:23:00 +02:00
|
|
|
$context,
|
2016-12-24 03:30:32 +01:00
|
|
|
$result_type
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($result_type) {
|
|
|
|
$stmt->inferredType = $result_type;
|
|
|
|
}
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\BooleanAnd
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\BooleanOr
|
2017-02-17 03:00:45 +01:00
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\LogicalAnd
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\LogicalOr
|
2016-10-22 19:23:18 +02:00
|
|
|
|| $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
|
|
|
/**
|
2017-07-25 22:11:02 +02:00
|
|
|
* @param StatementsSource $statements_source
|
2016-12-24 03:30:32 +01:00
|
|
|
* @param PhpParser\Node\Expr $left
|
|
|
|
* @param PhpParser\Node\Expr $right
|
|
|
|
* @param PhpParser\Node $parent
|
2016-11-18 22:13:59 +01:00
|
|
|
* @param Type\Union|null &$result_type
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-11-18 22:13:59 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
2017-01-07 21:09:47 +01:00
|
|
|
public static function analyzeNonDivArithmenticOp(
|
2017-07-25 22:11:02 +02:00
|
|
|
StatementsSource $statements_source,
|
2016-12-24 03:30:32 +01:00
|
|
|
PhpParser\Node\Expr $left,
|
|
|
|
PhpParser\Node\Expr $right,
|
|
|
|
PhpParser\Node $parent,
|
2017-09-07 03:44:26 +02:00
|
|
|
Type\Union &$result_type = null,
|
|
|
|
Context $context = null
|
2016-11-13 21:39:16 +01:00
|
|
|
) {
|
2017-09-07 03:44:26 +02:00
|
|
|
$project_checker = $statements_source->getFileChecker()->project_checker;
|
|
|
|
|
2016-12-30 05:37:09 +01:00
|
|
|
$left_type = isset($left->inferredType) ? $left->inferredType : null;
|
|
|
|
$right_type = isset($right->inferredType) ? $right->inferredType : null;
|
2016-12-24 03:30:32 +01:00
|
|
|
$config = Config::getInstance();
|
|
|
|
|
2017-09-07 03:44:26 +02:00
|
|
|
if ($project_checker->infer_types_from_usage
|
|
|
|
&& $context
|
|
|
|
&& $left_type
|
|
|
|
&& $right_type
|
|
|
|
&& ($left_type->isMixed() || $right_type->isMixed())
|
|
|
|
&& ($left_type->hasNumericType() || $right_type->hasNumericType())
|
|
|
|
) {
|
|
|
|
$source_checker = $statements_source->getSource();
|
2017-12-07 21:50:25 +01:00
|
|
|
if ($source_checker instanceof FunctionLikeChecker
|
|
|
|
&& $statements_source instanceof StatementsChecker
|
|
|
|
) {
|
|
|
|
$function_storage = $source_checker->getFunctionLikeStorage($statements_source);
|
2017-09-07 03:44:26 +02:00
|
|
|
|
|
|
|
$context->inferType($left, $function_storage, new Type\Union([new TInt, new TFloat]));
|
|
|
|
$context->inferType($right, $function_storage, new Type\Union([new TInt, new TFloat]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-24 03:30:32 +01:00
|
|
|
if ($left_type && $right_type) {
|
2017-09-20 17:22:06 +02:00
|
|
|
if ($left_type->isNullable()) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new PossiblyNullOperand(
|
|
|
|
'Left operand cannot be nullable, got ' . $left_type,
|
|
|
|
new CodeLocation($statements_source, $left)
|
|
|
|
),
|
|
|
|
$statements_source->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
} elseif ($left_type->isNull()) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new NullOperand(
|
|
|
|
'Left operand cannot be null',
|
|
|
|
new CodeLocation($statements_source, $left)
|
|
|
|
),
|
|
|
|
$statements_source->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($right_type->isNullable()) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new PossiblyNullOperand(
|
2017-10-12 14:51:57 +02:00
|
|
|
'Right operand cannot be nullable, got ' . $right_type,
|
2017-09-20 17:22:06 +02:00
|
|
|
new CodeLocation($statements_source, $right)
|
|
|
|
),
|
|
|
|
$statements_source->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
} elseif ($right_type->isNull()) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new NullOperand(
|
2017-10-12 14:51:57 +02:00
|
|
|
'Right operand cannot be null',
|
2017-09-20 17:22:06 +02:00
|
|
|
new CodeLocation($statements_source, $right)
|
|
|
|
),
|
|
|
|
$statements_source->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-09 21:05:48 +01:00
|
|
|
foreach ($left_type->getTypes() as $left_type_part) {
|
|
|
|
foreach ($right_type->getTypes() as $right_type_part) {
|
2017-09-20 17:22:06 +02:00
|
|
|
if ($left_type_part instanceof TNull) {
|
|
|
|
// null case is handled above
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-01-15 01:06:58 +01:00
|
|
|
if ($left_type_part instanceof TMixed || $right_type_part instanceof TMixed) {
|
|
|
|
if ($left_type_part instanceof TMixed) {
|
2016-12-24 03:30:32 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MixedOperand(
|
|
|
|
'Left operand cannot be mixed',
|
2017-07-25 22:11:02 +02:00
|
|
|
new CodeLocation($statements_source, $left)
|
2016-12-24 03:30:32 +01:00
|
|
|
),
|
2017-07-25 22:11:02 +02:00
|
|
|
$statements_source->getSuppressedIssues()
|
2016-12-24 03:30:32 +01:00
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MixedOperand(
|
|
|
|
'Right operand cannot be mixed',
|
2017-07-25 22:11:02 +02:00
|
|
|
new CodeLocation($statements_source, $right)
|
2016-12-24 03:30:32 +01:00
|
|
|
),
|
2017-07-25 22:11:02 +02:00
|
|
|
$statements_source->getSuppressedIssues()
|
2016-12-24 03:30:32 +01:00
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$result_type = Type::getMixed();
|
2017-05-25 04:07:49 +02:00
|
|
|
|
2016-11-13 21:55:06 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-11-13 21:39:16 +01:00
|
|
|
|
2017-11-11 20:19:45 +01:00
|
|
|
if ($left_type_part instanceof TArray
|
|
|
|
|| $right_type_part instanceof TArray
|
|
|
|
|| $left_type_part instanceof ObjectLike
|
|
|
|
|| $right_type_part instanceof ObjectLike
|
|
|
|
) {
|
2017-12-19 00:47:17 +01:00
|
|
|
if ((!$right_type_part instanceof TArray && !$right_type_part instanceof ObjectLike)
|
|
|
|
|| (!$left_type_part instanceof TArray && !$left_type_part instanceof ObjectLike)
|
|
|
|
) {
|
2017-01-23 23:40:32 +01:00
|
|
|
if (!$left_type_part instanceof TArray && !$left_type_part instanceof ObjectLike) {
|
2016-12-24 03:30:32 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidOperand(
|
2017-01-23 23:40:32 +01:00
|
|
|
'Cannot add an array to a non-array ' . $left_type_part,
|
2017-07-25 22:11:02 +02:00
|
|
|
new CodeLocation($statements_source, $left)
|
2016-12-24 03:30:32 +01:00
|
|
|
),
|
2017-07-25 22:11:02 +02:00
|
|
|
$statements_source->getSuppressedIssues()
|
2016-12-24 03:30:32 +01:00
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
2017-01-23 23:40:32 +01:00
|
|
|
} else {
|
2017-01-13 19:09:52 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidOperand(
|
2017-01-23 23:40:32 +01:00
|
|
|
'Cannot add an array to a non-array ' . $right_type_part,
|
2017-07-25 22:11:02 +02:00
|
|
|
new CodeLocation($statements_source, $right)
|
2017-01-13 19:09:52 +01:00
|
|
|
),
|
2017-07-25 22:11:02 +02:00
|
|
|
$statements_source->getSuppressedIssues()
|
2017-01-13 19:09:52 +01:00
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
2016-12-24 03:30:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$result_type = Type::getArray();
|
2017-05-25 04:07:49 +02:00
|
|
|
|
2016-12-24 03:30:32 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-19 00:47:17 +01:00
|
|
|
if ($left_type_part instanceof ObjectLike && $right_type_part instanceof ObjectLike) {
|
|
|
|
$properties = $left_type_part->properties + $right_type_part->properties;
|
|
|
|
|
|
|
|
$result_type_member = new Type\Union([new ObjectLike($properties)]);
|
|
|
|
} else {
|
|
|
|
$result_type_member = Type::combineTypes([$left_type_part, $right_type_part]);
|
|
|
|
}
|
2016-12-24 03:30:32 +01:00
|
|
|
|
|
|
|
if (!$result_type) {
|
|
|
|
$result_type = $result_type_member;
|
|
|
|
} else {
|
|
|
|
$result_type = Type::combineUnionTypes($result_type_member, $result_type);
|
|
|
|
}
|
|
|
|
|
2017-11-19 18:33:43 +01:00
|
|
|
if ($left instanceof PhpParser\Node\Expr\ArrayDimFetch
|
|
|
|
&& $context
|
|
|
|
&& $statements_source instanceof StatementsChecker
|
|
|
|
) {
|
|
|
|
AssignmentChecker::updateArrayType(
|
|
|
|
$statements_source,
|
|
|
|
$left,
|
|
|
|
$result_type,
|
|
|
|
$context
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-12-24 03:30:32 +01:00
|
|
|
continue;
|
2016-11-13 21:55:06 +01:00
|
|
|
}
|
2016-11-13 21:39:16 +01:00
|
|
|
|
2016-12-24 03:30:32 +01:00
|
|
|
if ($left_type_part->isNumericType() || $right_type_part->isNumericType()) {
|
2017-01-15 01:06:58 +01:00
|
|
|
if ($left_type_part instanceof TInt && $right_type_part instanceof TInt) {
|
2016-12-24 03:30:32 +01:00
|
|
|
if (!$result_type) {
|
|
|
|
$result_type = Type::getInt();
|
|
|
|
} else {
|
|
|
|
$result_type = Type::combineUnionTypes(Type::getInt(), $result_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-01-15 01:06:58 +01:00
|
|
|
if ($left_type_part instanceof TFloat && $right_type_part instanceof TFloat) {
|
2016-12-24 03:30:32 +01:00
|
|
|
if (!$result_type) {
|
|
|
|
$result_type = Type::getFloat();
|
|
|
|
} else {
|
|
|
|
$result_type = Type::combineUnionTypes(Type::getFloat(), $result_type);
|
|
|
|
}
|
2016-12-27 19:58:58 +01:00
|
|
|
|
2016-12-24 03:30:32 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-01-15 01:06:58 +01:00
|
|
|
if (($left_type_part instanceof TFloat && $right_type_part instanceof TInt) ||
|
|
|
|
($left_type_part instanceof TInt && $right_type_part instanceof TFloat)
|
2016-12-24 03:30:32 +01:00
|
|
|
) {
|
|
|
|
if ($config->strict_binary_operands) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidOperand(
|
|
|
|
'Cannot add ints to floats',
|
2017-07-25 22:11:02 +02:00
|
|
|
new CodeLocation($statements_source, $parent)
|
2016-12-24 03:30:32 +01:00
|
|
|
),
|
2017-07-25 22:11:02 +02:00
|
|
|
$statements_source->getSuppressedIssues()
|
2016-12-24 03:30:32 +01:00
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$result_type) {
|
|
|
|
$result_type = Type::getFloat();
|
|
|
|
} else {
|
|
|
|
$result_type = Type::combineUnionTypes(Type::getFloat(), $result_type);
|
|
|
|
}
|
2016-12-27 19:58:58 +01:00
|
|
|
|
2016-12-24 03:30:32 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($left_type_part->isNumericType() && $right_type_part->isNumericType()) {
|
|
|
|
if ($config->strict_binary_operands) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidOperand(
|
|
|
|
'Cannot add numeric types together, please cast explicitly',
|
2017-07-25 22:11:02 +02:00
|
|
|
new CodeLocation($statements_source, $parent)
|
2016-12-24 03:30:32 +01:00
|
|
|
),
|
2017-07-25 22:11:02 +02:00
|
|
|
$statements_source->getSuppressedIssues()
|
2016-12-24 03:30:32 +01:00
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$result_type) {
|
|
|
|
$result_type = Type::getFloat();
|
|
|
|
} else {
|
|
|
|
$result_type = Type::combineUnionTypes(Type::getFloat(), $result_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$non_numeric_type = $left_type_part->isNumericType() ? $right_type_part : $left_type_part;
|
|
|
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidOperand(
|
|
|
|
'Cannot add a numeric type to a non-numeric type ' . $non_numeric_type,
|
2017-07-25 22:11:02 +02:00
|
|
|
new CodeLocation($statements_source, $parent)
|
2016-12-24 03:30:32 +01:00
|
|
|
),
|
2017-07-25 22:11:02 +02:00
|
|
|
$statements_source->getSuppressedIssues()
|
2016-12-24 03:30:32 +01:00
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
2016-11-13 21:39:16 +01:00
|
|
|
}
|
2016-12-24 03:30:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param StatementsChecker $statements_checker
|
|
|
|
* @param PhpParser\Node\Expr $left
|
|
|
|
* @param PhpParser\Node\Expr $right
|
2017-02-12 01:30:06 +01:00
|
|
|
* @param Type\Union|null &$result_type
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-12-24 03:30:32 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
2017-01-07 21:09:47 +01:00
|
|
|
public static function analyzeConcatOp(
|
2016-12-24 03:30:32 +01:00
|
|
|
StatementsChecker $statements_checker,
|
|
|
|
PhpParser\Node\Expr $left,
|
|
|
|
PhpParser\Node\Expr $right,
|
2017-09-03 01:23:00 +02:00
|
|
|
Context $context,
|
2016-12-24 03:30:32 +01:00
|
|
|
Type\Union &$result_type = null
|
|
|
|
) {
|
2017-09-03 01:23:00 +02:00
|
|
|
$project_checker = $statements_checker->getFileChecker()->project_checker;
|
|
|
|
|
2016-12-31 02:05:32 +01:00
|
|
|
$left_type = isset($left->inferredType) ? $left->inferredType : null;
|
|
|
|
$right_type = isset($right->inferredType) ? $right->inferredType : null;
|
2016-12-24 03:30:32 +01:00
|
|
|
$config = Config::getInstance();
|
|
|
|
|
2017-09-03 01:23:00 +02:00
|
|
|
if ($project_checker->infer_types_from_usage
|
|
|
|
&& $left_type
|
|
|
|
&& $right_type
|
|
|
|
&& ($left_type->isMixed() || $right_type->isMixed())
|
|
|
|
) {
|
|
|
|
$source_checker = $statements_checker->getSource();
|
|
|
|
|
|
|
|
if ($source_checker instanceof FunctionLikeChecker) {
|
2017-12-07 21:50:25 +01:00
|
|
|
$function_storage = $source_checker->getFunctionLikeStorage($statements_checker);
|
2017-09-03 01:23:00 +02:00
|
|
|
|
2017-09-04 02:52:54 +02:00
|
|
|
$context->inferType($left, $function_storage, Type::getString());
|
|
|
|
$context->inferType($right, $function_storage, Type::getString());
|
2017-09-03 01:23:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-24 03:30:32 +01:00
|
|
|
if ($left_type && $right_type) {
|
2016-12-29 06:14:06 +01:00
|
|
|
$result_type = Type::getString();
|
|
|
|
|
|
|
|
if ($left_type->isMixed() || $right_type->isMixed()) {
|
|
|
|
if ($left_type->isMixed()) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MixedOperand(
|
|
|
|
'Left operand cannot be mixed',
|
|
|
|
new CodeLocation($statements_checker->getSource(), $left)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
2016-12-24 03:30:32 +01:00
|
|
|
}
|
2016-12-29 06:14:06 +01:00
|
|
|
} else {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MixedOperand(
|
|
|
|
'Right operand cannot be mixed',
|
|
|
|
new CodeLocation($statements_checker->getSource(), $right)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
2016-12-24 03:30:32 +01:00
|
|
|
}
|
2016-12-29 06:14:06 +01:00
|
|
|
}
|
2016-12-27 19:58:58 +01:00
|
|
|
|
2016-12-29 06:14:06 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-11 23:55:08 +01:00
|
|
|
if ($left_type->isNull()) {
|
2016-12-29 06:32:12 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new NullOperand(
|
|
|
|
'Cannot concatenate with a ' . $left_type,
|
|
|
|
new CodeLocation($statements_checker->getSource(), $left)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
2017-02-11 23:55:08 +01:00
|
|
|
|
|
|
|
return;
|
2016-12-29 06:32:12 +01:00
|
|
|
}
|
|
|
|
|
2017-02-11 23:55:08 +01:00
|
|
|
if ($right_type->isNull()) {
|
2016-12-29 06:32:12 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new NullOperand(
|
|
|
|
'Cannot concatenate with a ' . $right_type,
|
|
|
|
new CodeLocation($statements_checker->getSource(), $right)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
2017-02-11 23:55:08 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-09 22:38:58 +02:00
|
|
|
if ($left_type->isNullable() && !$left_type->ignore_nullable_issues) {
|
2017-02-11 23:55:08 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new PossiblyNullOperand(
|
|
|
|
'Cannot concatenate with a possibly null ' . $left_type,
|
|
|
|
new CodeLocation($statements_checker->getSource(), $left)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-09 22:38:58 +02:00
|
|
|
if ($right_type->isNullable() && !$right_type->ignore_nullable_issues) {
|
2017-02-11 23:55:08 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new PossiblyNullOperand(
|
|
|
|
'Cannot concatenate with a possibly null ' . $right_type,
|
|
|
|
new CodeLocation($statements_checker->getSource(), $right)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
2016-12-29 06:32:12 +01:00
|
|
|
}
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker = $statements_checker->getFileChecker()->project_checker;
|
|
|
|
|
2016-12-29 06:14:06 +01:00
|
|
|
$left_type_match = TypeChecker::isContainedBy(
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker,
|
2016-12-29 06:14:06 +01:00
|
|
|
$left_type,
|
|
|
|
Type::getString(),
|
2016-12-29 06:32:12 +01:00
|
|
|
true,
|
2017-10-23 17:47:00 +02:00
|
|
|
false,
|
2016-12-29 06:32:12 +01:00
|
|
|
$left_has_scalar_match
|
2016-12-29 06:14:06 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$right_type_match = TypeChecker::isContainedBy(
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker,
|
2016-12-29 06:14:06 +01:00
|
|
|
$right_type,
|
|
|
|
Type::getString(),
|
2016-12-29 06:32:12 +01:00
|
|
|
true,
|
2017-10-23 17:47:00 +02:00
|
|
|
false,
|
2016-12-29 06:32:12 +01:00
|
|
|
$right_has_scalar_match
|
2016-12-29 06:14:06 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
if (!$left_type_match && (!$left_has_scalar_match || $config->strict_binary_operands)) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidOperand(
|
2016-12-29 06:32:12 +01:00
|
|
|
'Cannot concatenate with a ' . $left_type,
|
2016-12-29 06:14:06 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $left)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
2016-11-13 21:39:16 +01:00
|
|
|
|
2016-12-29 06:14:06 +01:00
|
|
|
if (!$right_type_match && (!$right_has_scalar_match || $config->strict_binary_operands)) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidOperand(
|
2016-12-29 06:32:12 +01:00
|
|
|
'Cannot concatenate with a ' . $right_type,
|
2016-12-29 06:14:06 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $right)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
2016-12-24 03:30:32 +01:00
|
|
|
}
|
2016-11-13 21:39:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-22 19:23:18 +02:00
|
|
|
/**
|
2016-12-24 19:23:22 +01:00
|
|
|
* @param PhpParser\Node\Expr $stmt
|
2017-01-07 20:35:07 +01:00
|
|
|
* @param string|null $this_class_name
|
2017-03-15 01:14:25 +01:00
|
|
|
* @param StatementsSource|null $source
|
2016-12-24 19:23:22 +01:00
|
|
|
* @param int|null &$nesting
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-10-22 19:23:18 +02:00
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
public static function getVarId(
|
|
|
|
PhpParser\Node\Expr $stmt,
|
|
|
|
$this_class_name,
|
2017-03-15 01:14:25 +01:00
|
|
|
StatementsSource $source = null,
|
2016-10-22 19:23:18 +02:00
|
|
|
&$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
|
|
|
|
) {
|
2017-05-27 02:17:56 +02:00
|
|
|
if (count($stmt->class->parts) === 1
|
2017-11-15 03:56:29 +01:00
|
|
|
&& in_array(strtolower($stmt->class->parts[0]), ['self', 'static', 'parent'], true)
|
2017-05-27 02:17:56 +02:00
|
|
|
) {
|
2017-01-07 20:35:07 +01:00
|
|
|
if (!$this_class_name) {
|
2017-03-15 01:14:25 +01:00
|
|
|
$fq_class_name = $stmt->class->parts[0];
|
|
|
|
} else {
|
|
|
|
$fq_class_name = $this_class_name;
|
2017-01-07 20:35:07 +01:00
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2017-03-15 01:14:25 +01:00
|
|
|
$fq_class_name = $source
|
|
|
|
? ClassLikeChecker::getFQCLNFromNameObject(
|
|
|
|
$stmt->class,
|
2017-07-25 22:11:02 +02:00
|
|
|
$source->getAliases()
|
2017-03-15 01:14:25 +01:00
|
|
|
)
|
|
|
|
: implode('\\', $stmt->class->parts);
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
|
2016-11-07 23:29:51 +01: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)) {
|
2017-01-07 20:35:07 +01:00
|
|
|
$object_id = self::getVarId($stmt->var, $this_class_name, $source);
|
2016-10-22 19:23:18 +02:00
|
|
|
|
|
|
|
if (!$object_id) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $object_id . '->' . $stmt->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\ArrayDimFetch && $nesting !== null) {
|
2017-05-27 02:05:57 +02:00
|
|
|
++$nesting;
|
2017-05-25 04:07:49 +02:00
|
|
|
|
2017-01-07 20:35:07 +01:00
|
|
|
return self::getVarId($stmt->var, $this_class_name, $source, $nesting);
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-10-11 05:01:52 +02:00
|
|
|
/**
|
|
|
|
* @param PhpParser\Node\Expr $stmt
|
|
|
|
* @param string|null $this_class_name
|
|
|
|
* @param StatementsSource|null $source
|
|
|
|
*
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
public static function getRootVarId(
|
|
|
|
PhpParser\Node\Expr $stmt,
|
|
|
|
$this_class_name,
|
|
|
|
StatementsSource $source = null
|
|
|
|
) {
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\Variable
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\StaticPropertyFetch
|
|
|
|
) {
|
|
|
|
return self::getVarId($stmt, $this_class_name, $source);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\PropertyFetch && is_string($stmt->name)) {
|
|
|
|
$property_root = self::getRootVarId($stmt->var, $this_class_name, $source);
|
|
|
|
|
|
|
|
if ($property_root) {
|
|
|
|
return $property_root . '->' . $stmt->name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\ArrayDimFetch) {
|
|
|
|
return self::getRootVarId($stmt->var, $this_class_name, $source);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-10-22 19:23:18 +02:00
|
|
|
/**
|
2016-12-24 19:23:22 +01:00
|
|
|
* @param PhpParser\Node\Expr $stmt
|
2017-01-07 20:35:07 +01:00
|
|
|
* @param string|null $this_class_name
|
2017-03-15 01:14:25 +01:00
|
|
|
* @param StatementsSource|null $source
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-10-22 19:23:18 +02:00
|
|
|
* @return string|null
|
|
|
|
*/
|
2016-11-02 07:29:00 +01:00
|
|
|
public static function getArrayVarId(
|
|
|
|
PhpParser\Node\Expr $stmt,
|
|
|
|
$this_class_name,
|
2017-03-15 01:14:25 +01:00
|
|
|
StatementsSource $source = null
|
2016-11-02 07:29:00 +01:00
|
|
|
) {
|
2016-12-28 22:39:05 +01:00
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\Assign) {
|
2017-01-07 20:35:07 +01:00
|
|
|
return self::getArrayVarId($stmt->var, $this_class_name, $source);
|
2016-12-28 22:39:05 +01:00
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\ArrayDimFetch &&
|
2016-12-10 21:01:59 +01:00
|
|
|
($stmt->dim instanceof PhpParser\Node\Scalar\String_ ||
|
|
|
|
$stmt->dim instanceof PhpParser\Node\Scalar\LNumber)
|
2016-11-02 07:29:00 +01:00
|
|
|
) {
|
2017-01-07 20:35:07 +01:00
|
|
|
$root_var_id = self::getArrayVarId($stmt->var, $this_class_name, $source);
|
2016-12-11 00:24:28 +01:00
|
|
|
$offset = $stmt->dim instanceof PhpParser\Node\Scalar\String_
|
|
|
|
? '\'' . $stmt->dim->value . '\''
|
|
|
|
: $stmt->dim->value;
|
|
|
|
|
|
|
|
return $root_var_id ? $root_var_id . '[' . $offset . ']' : null;
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
|
2017-01-07 20:35:07 +01:00
|
|
|
return self::getVarId($stmt, $this_class_name, $source);
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
|
2016-11-01 19:14:35 +01:00
|
|
|
/**
|
2017-03-02 04:27:52 +01:00
|
|
|
* @param Type\Union $return_type
|
|
|
|
* @param string|null $calling_class
|
|
|
|
* @param string|null $method_id
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-11-01 19:14:35 +01:00
|
|
|
* @return Type\Union
|
|
|
|
*/
|
2017-07-29 21:05:06 +02:00
|
|
|
public static function fleshOutType(
|
|
|
|
ProjectChecker $project_checker,
|
|
|
|
Type\Union $return_type,
|
|
|
|
$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
|
|
|
|
2018-01-09 21:05:48 +01:00
|
|
|
foreach ($return_type->getTypes() as $return_type_part) {
|
2017-07-29 21:05:06 +02:00
|
|
|
$new_return_type_parts[] = self::fleshOutAtomicType(
|
|
|
|
$project_checker,
|
|
|
|
$return_type_part,
|
|
|
|
$calling_class,
|
|
|
|
$method_id
|
|
|
|
);
|
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-12 05:18:13 +01:00
|
|
|
$fleshed_out_type->by_ref = $return_type->by_ref;
|
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
|
|
|
|
* @param string|null $calling_class
|
|
|
|
* @param string|null $method_id
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-11-01 19:14:35 +01:00
|
|
|
* @return Type\Atomic
|
2016-10-22 19:23:18 +02:00
|
|
|
*/
|
2017-07-29 21:05:06 +02:00
|
|
|
protected static function fleshOutAtomicType(
|
|
|
|
ProjectChecker $project_checker,
|
|
|
|
Type\Atomic $return_type,
|
|
|
|
$calling_class,
|
|
|
|
$method_id
|
|
|
|
) {
|
2017-01-15 01:06:58 +01:00
|
|
|
if ($return_type instanceof TNamedObject) {
|
2017-11-15 03:56:29 +01:00
|
|
|
if (in_array(strtolower($return_type->value), ['$this', 'static', 'self'], true)) {
|
2017-01-15 01:06:58 +01:00
|
|
|
if (!$calling_class) {
|
|
|
|
throw new \InvalidArgumentException(
|
|
|
|
'Cannot handle ' . $return_type->value . ' when $calling_class is empty'
|
|
|
|
);
|
|
|
|
}
|
2016-10-22 19:23:18 +02:00
|
|
|
|
2017-11-15 03:56:29 +01:00
|
|
|
if (strtolower($return_type->value) === 'static' || !$method_id) {
|
2017-01-15 01:06:58 +01:00
|
|
|
$return_type->value = $calling_class;
|
2017-12-05 07:47:49 +01:00
|
|
|
} elseif (strpos($method_id, ':-:closure') !== false) {
|
|
|
|
$return_type->value = $calling_class;
|
2017-01-15 01:06:58 +01:00
|
|
|
} else {
|
|
|
|
list(, $method_name) = explode('::', $method_id);
|
2017-01-02 21:31:18 +01:00
|
|
|
|
2017-12-05 07:47:49 +01:00
|
|
|
$appearing_method_id = MethodChecker::getAppearingMethodId(
|
|
|
|
$project_checker,
|
|
|
|
$calling_class . '::' . $method_name
|
|
|
|
);
|
2016-12-10 21:08:38 +01:00
|
|
|
|
2017-12-05 07:47:49 +01:00
|
|
|
$return_type->value = explode('::', (string)$appearing_method_id)[0];
|
2017-01-15 01:06:58 +01:00
|
|
|
}
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-15 01:06:58 +01:00
|
|
|
if ($return_type instanceof Type\Atomic\TArray || $return_type instanceof Type\Atomic\TGenericObject) {
|
2016-10-22 19:23:18 +02:00
|
|
|
foreach ($return_type->type_params as &$type_param) {
|
2017-07-29 21:05:06 +02:00
|
|
|
$type_param = self::fleshOutType($project_checker, $type_param, $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
|
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(
|
2016-11-02 07:29:00 +01:00
|
|
|
StatementsChecker $statements_checker,
|
|
|
|
PhpParser\Node\Expr\Closure $stmt,
|
|
|
|
Context $context
|
|
|
|
) {
|
2016-11-01 19:14:35 +01:00
|
|
|
foreach ($stmt->uses as $use) {
|
2017-02-08 00:09:12 +01:00
|
|
|
$use_var_id = '$' . $use->var;
|
|
|
|
if (!$context->hasVariable($use_var_id)) {
|
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;
|
|
|
|
|
|
|
|
if (!$statements_checker->hasVariable($use_var_id)) {
|
|
|
|
$statements_checker->registerVariable($use_var_id, new CodeLocation($statements_checker, $use));
|
|
|
|
}
|
2017-05-25 04:07:49 +02:00
|
|
|
|
2016-10-22 19:23:18 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-08 00:09:12 +01: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,
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $use)
|
2017-12-06 06:35:41 +01:00
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-10-22 19:23:18 +02:00
|
|
|
|
2017-12-06 06:35:41 +01:00
|
|
|
return null;
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
|
|
|
}
|
2016-10-22 19:23:18 +02:00
|
|
|
|
2017-02-08 00:09:12 +01:00
|
|
|
$first_appearance = $statements_checker->getFirstAppearance($use_var_id);
|
|
|
|
|
|
|
|
if ($first_appearance) {
|
2016-11-01 19:14:35 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new PossiblyUndefinedVariable(
|
2017-02-08 00:09:12 +01:00
|
|
|
'Possibly undefined variable ' . $use_var_id . ', first seen on line ' .
|
|
|
|
$first_appearance->getLineNumber(),
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $use)
|
2016-11-01 19:14:35 +01:00
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-10-22 19:23:18 +02:00
|
|
|
|
2017-12-06 06:35:41 +01:00
|
|
|
continue;
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
|
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,
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $use)
|
2017-12-06 06:35:41 +01:00
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-10-22 19:23:18 +02:00
|
|
|
|
2017-12-06 06:35:41 +01:00
|
|
|
continue;
|
2016-11-01 19:14:35 +01:00
|
|
|
}
|
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
|
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(
|
2016-11-02 07:29:00 +01:00
|
|
|
StatementsChecker $statements_checker,
|
|
|
|
PhpParser\Node\Expr\Yield_ $stmt,
|
|
|
|
Context $context
|
|
|
|
) {
|
2017-03-02 04:27:52 +01:00
|
|
|
$doc_comment_text = (string)$stmt->getDocComment();
|
|
|
|
|
2017-05-25 07:32:34 +02:00
|
|
|
$var_comment = null;
|
|
|
|
|
2017-03-02 04:27:52 +01:00
|
|
|
if ($doc_comment_text) {
|
2017-11-15 03:43:31 +01:00
|
|
|
try {
|
|
|
|
$var_comment = CommentChecker::getTypeFromComment(
|
|
|
|
$doc_comment_text,
|
|
|
|
$statements_checker,
|
|
|
|
$statements_checker->getAliases()
|
|
|
|
);
|
|
|
|
} catch (DocblockParseException $e) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidDocblock(
|
|
|
|
(string)$e->getMessage(),
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
)
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
2017-05-25 07:32:34 +02:00
|
|
|
|
|
|
|
if ($var_comment && $var_comment->var_id) {
|
2017-10-07 16:22:52 +02:00
|
|
|
$comment_type = ExpressionChecker::fleshOutType(
|
|
|
|
$statements_checker->getFileChecker()->project_checker,
|
2018-01-02 02:04:03 +01:00
|
|
|
$var_comment->type,
|
2017-10-07 16:22:52 +02:00
|
|
|
$context->self
|
|
|
|
);
|
|
|
|
|
|
|
|
$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) {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (self::analyze($statements_checker, $stmt->key, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt->value) {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (self::analyze($statements_checker, $stmt->value, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-25 07:32:34 +02:00
|
|
|
if ($var_comment && !$var_comment->var_id) {
|
2018-01-02 02:04:03 +01:00
|
|
|
$stmt->inferredType = $var_comment->type;
|
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
|
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(
|
2016-11-02 07:29:00 +01:00
|
|
|
StatementsChecker $statements_checker,
|
|
|
|
PhpParser\Node\Expr\YieldFrom $stmt,
|
|
|
|
Context $context
|
|
|
|
) {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (self::analyze($statements_checker, $stmt->expr, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($stmt->expr->inferredType)) {
|
2017-11-15 04:29:15 +01:00
|
|
|
// this should be whatever the generator above returns, but *not* the return type
|
|
|
|
$stmt->inferredType = Type::getMixed();
|
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-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @param StatementsChecker $statements_checker
|
|
|
|
* @param PhpParser\Node\Expr\Ternary $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 analyzeTernary(
|
2016-11-02 07:29:00 +01:00
|
|
|
StatementsChecker $statements_checker,
|
|
|
|
PhpParser\Node\Expr\Ternary $stmt,
|
|
|
|
Context $context
|
|
|
|
) {
|
2017-11-28 06:46:41 +01:00
|
|
|
$pre_referenced_var_ids = $context->referenced_var_ids;
|
|
|
|
$context->referenced_var_ids = [];
|
|
|
|
|
2017-02-13 00:47:02 +01:00
|
|
|
$context->inside_conditional = true;
|
2017-01-07 21:09:47 +01:00
|
|
|
if (self::analyze($statements_checker, $stmt->cond, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
2017-11-28 06:46:41 +01:00
|
|
|
|
|
|
|
$new_referenced_var_ids = $context->referenced_var_ids;
|
|
|
|
$context->referenced_var_ids = array_merge($pre_referenced_var_ids, $new_referenced_var_ids);
|
|
|
|
|
2017-02-13 00:47:02 +01:00
|
|
|
$context->inside_conditional = false;
|
2016-10-22 19:23:18 +02:00
|
|
|
|
|
|
|
$t_if_context = clone $context;
|
|
|
|
|
2017-03-18 18:37:00 +01:00
|
|
|
$if_clauses = AlgebraChecker::getFormula(
|
2016-12-27 19:58:58 +01:00
|
|
|
$stmt->cond,
|
|
|
|
$statements_checker->getFQCLN(),
|
2017-01-07 20:35:07 +01:00
|
|
|
$statements_checker
|
2016-12-27 19:58:58 +01:00
|
|
|
);
|
2016-11-02 07:29:00 +01:00
|
|
|
|
2017-03-18 18:37:00 +01:00
|
|
|
$ternary_clauses = AlgebraChecker::simplifyCNF(array_merge($context->clauses, $if_clauses));
|
2016-12-27 19:58:58 +01:00
|
|
|
|
2017-03-18 18:37:00 +01:00
|
|
|
$negated_clauses = AlgebraChecker::negateFormula($if_clauses);
|
2016-12-27 19:58:58 +01:00
|
|
|
|
2017-03-18 18:37:00 +01:00
|
|
|
$negated_if_types = AlgebraChecker::getTruthsFromFormula($negated_clauses);
|
2016-12-27 19:58:58 +01:00
|
|
|
|
2017-03-18 18:37:00 +01:00
|
|
|
$reconcilable_if_types = AlgebraChecker::getTruthsFromFormula($ternary_clauses);
|
2016-10-22 19:23:18 +02:00
|
|
|
|
2017-11-28 06:46:41 +01:00
|
|
|
$changed_var_ids = [];
|
2016-12-28 20:20:16 +01:00
|
|
|
|
2017-12-29 16:55:41 +01:00
|
|
|
$t_if_vars_in_scope_reconciled = Reconciler::reconcileKeyedTypes(
|
2016-11-02 07:29:00 +01:00
|
|
|
$reconcilable_if_types,
|
|
|
|
$t_if_context->vars_in_scope,
|
2017-11-28 06:46:41 +01:00
|
|
|
$changed_var_ids,
|
|
|
|
$new_referenced_var_ids,
|
2017-06-23 06:39:37 +02:00
|
|
|
$statements_checker,
|
2016-12-11 19:59:36 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt->cond),
|
2016-11-02 07:29:00 +01:00
|
|
|
$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;
|
2017-04-15 19:46:18 +02:00
|
|
|
$t_else_context = clone $context;
|
2016-10-22 19:23:18 +02:00
|
|
|
|
|
|
|
if ($stmt->if) {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (self::analyze($statements_checker, $stmt->if, $t_if_context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
2017-02-01 05:24:33 +01:00
|
|
|
|
2017-04-07 03:53:29 +02:00
|
|
|
foreach ($t_if_context->vars_in_scope as $var_id => $type) {
|
|
|
|
if (isset($context->vars_in_scope[$var_id])) {
|
|
|
|
$context->vars_in_scope[$var_id] = Type::combineUnionTypes($context->vars_in_scope[$var_id], $type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-24 18:17:28 +01:00
|
|
|
$context->referenced_var_ids = array_merge(
|
|
|
|
$context->referenced_var_ids,
|
|
|
|
$t_if_context->referenced_var_ids
|
|
|
|
);
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
|
2016-12-27 19:58:58 +01:00
|
|
|
if ($negated_if_types) {
|
2017-12-29 16:55:41 +01:00
|
|
|
$t_else_vars_in_scope_reconciled = Reconciler::reconcileKeyedTypes(
|
2016-10-22 19:23:18 +02:00
|
|
|
$negated_if_types,
|
|
|
|
$t_else_context->vars_in_scope,
|
2017-11-28 06:46:41 +01:00
|
|
|
$changed_var_ids,
|
|
|
|
$new_referenced_var_ids,
|
2017-06-23 06:39:37 +02:00
|
|
|
$statements_checker,
|
2016-12-11 19:59:36 +01:00
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt->else),
|
2016-10-22 19:23:18 +02:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2017-01-07 21:09:47 +01:00
|
|
|
if (self::analyze($statements_checker, $stmt->else, $t_else_context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-04-07 03:53:29 +02:00
|
|
|
foreach ($t_else_context->vars_in_scope as $var_id => $type) {
|
|
|
|
if (isset($context->vars_in_scope[$var_id])) {
|
|
|
|
$context->vars_in_scope[$var_id] = Type::combineUnionTypes($context->vars_in_scope[$var_id], $type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-24 18:17:28 +01:00
|
|
|
$context->referenced_var_ids = array_merge(
|
|
|
|
$context->referenced_var_ids,
|
|
|
|
$t_else_context->referenced_var_ids
|
|
|
|
);
|
2017-02-01 05:24:33 +01:00
|
|
|
|
2016-10-22 19:23:18 +02:00
|
|
|
$lhs_type = null;
|
|
|
|
|
|
|
|
if ($stmt->if) {
|
|
|
|
if (isset($stmt->if->inferredType)) {
|
|
|
|
$lhs_type = $stmt->if->inferredType;
|
|
|
|
}
|
2017-11-24 18:10:30 +01:00
|
|
|
} elseif (isset($stmt->cond->inferredType)) {
|
2017-12-29 16:55:41 +01:00
|
|
|
$if_return_type_reconciled = Reconciler::reconcileTypes(
|
2017-11-24 18:10:30 +01:00
|
|
|
'!falsy',
|
|
|
|
$stmt->cond->inferredType,
|
|
|
|
'',
|
|
|
|
$statements_checker,
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
);
|
2016-10-22 19:23:18 +02:00
|
|
|
|
2017-11-24 18:10:30 +01:00
|
|
|
if ($if_return_type_reconciled === false) {
|
|
|
|
return false;
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
2017-11-24 18:10:30 +01:00
|
|
|
|
|
|
|
$lhs_type = $if_return_type_reconciled;
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
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(
|
2016-11-02 07:29:00 +01:00
|
|
|
StatementsChecker $statements_checker,
|
|
|
|
PhpParser\Node\Expr\BooleanNot $stmt,
|
|
|
|
Context $context
|
|
|
|
) {
|
2016-11-05 22:53:30 +01:00
|
|
|
$stmt->inferredType = Type::getBool();
|
2016-12-28 22:29:50 +01:00
|
|
|
|
2017-01-07 21:09:47 +01:00
|
|
|
if (self::analyze($statements_checker, $stmt->expr, $context) === false) {
|
2016-12-28 22:29:50 +01:00
|
|
|
return false;
|
|
|
|
}
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @param StatementsChecker $statements_checker
|
|
|
|
* @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(
|
2016-11-02 07:29:00 +01:00
|
|
|
StatementsChecker $statements_checker,
|
|
|
|
PhpParser\Node\Expr\Empty_ $stmt,
|
|
|
|
Context $context
|
|
|
|
) {
|
2017-12-05 18:14:10 +01:00
|
|
|
self::analyzeIssetVar($statements_checker, $stmt->expr, $context);
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-02 07:29:00 +01:00
|
|
|
* @param StatementsChecker $statements_checker
|
|
|
|
* @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(
|
2016-11-02 07:29:00 +01:00
|
|
|
StatementsChecker $statements_checker,
|
|
|
|
PhpParser\Node\Scalar\Encapsed $stmt,
|
|
|
|
Context $context
|
|
|
|
) {
|
2017-09-03 01:48:59 +02:00
|
|
|
$project_checker = $statements_checker->getFileChecker()->project_checker;
|
|
|
|
|
|
|
|
$function_storage = null;
|
|
|
|
|
|
|
|
if ($project_checker->infer_types_from_usage) {
|
|
|
|
$source_checker = $statements_checker->getSource();
|
|
|
|
|
|
|
|
if ($source_checker instanceof FunctionLikeChecker) {
|
2017-12-07 21:50:25 +01:00
|
|
|
$function_storage = $source_checker->getFunctionLikeStorage($statements_checker);
|
2017-09-03 01:48:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-22 19:23:18 +02:00
|
|
|
/** @var PhpParser\Node\Expr $part */
|
|
|
|
foreach ($stmt->parts as $part) {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (self::analyze($statements_checker, $part, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
2017-09-03 01:48:59 +02:00
|
|
|
|
2017-09-04 02:52:54 +02:00
|
|
|
if ($function_storage) {
|
|
|
|
$context->inferType($part, $function_storage, Type::getString());
|
2017-09-03 01:48:59 +02:00
|
|
|
}
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$stmt->inferredType = 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
|
|
|
}
|
|
|
|
|
2017-01-15 19:58:20 +01:00
|
|
|
/**
|
|
|
|
* @param StatementsChecker $statements_checker
|
|
|
|
* @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(
|
|
|
|
StatementsChecker $statements_checker,
|
|
|
|
PhpParser\Node\Expr\Isset_ $stmt,
|
|
|
|
Context $context
|
|
|
|
) {
|
|
|
|
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;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
self::analyzeIssetVar($statements_checker, $isset_var, $context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param StatementsChecker $statements_checker
|
|
|
|
* @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(
|
|
|
|
StatementsChecker $statements_checker,
|
|
|
|
PhpParser\Node\Expr $stmt,
|
|
|
|
Context $context
|
|
|
|
) {
|
2018-01-10 01:33:39 +01:00
|
|
|
$context->inside_isset = true;
|
2017-01-15 19:58:20 +01:00
|
|
|
|
2018-01-10 01:33:39 +01:00
|
|
|
if (self::analyze($statements_checker, $stmt, $context) === false) {
|
|
|
|
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
|
|
|
/**
|
|
|
|
* @param StatementsChecker $statements_checker
|
|
|
|
* @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(
|
|
|
|
StatementsChecker $statements_checker,
|
|
|
|
PhpParser\Node\Expr\Clone_ $stmt,
|
|
|
|
Context $context
|
|
|
|
) {
|
|
|
|
if (self::analyze($statements_checker, $stmt->expr, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($stmt->expr->inferredType)) {
|
2018-01-09 21:05:48 +01:00
|
|
|
foreach ($stmt->expr->inferredType->getTypes() as $clone_type_part) {
|
2017-01-19 21:00:05 +01:00
|
|
|
if (!$clone_type_part instanceof TNamedObject &&
|
|
|
|
!$clone_type_part instanceof TObject &&
|
|
|
|
!$clone_type_part instanceof TMixed
|
|
|
|
) {
|
2017-01-16 18:59:09 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidClone(
|
|
|
|
'Cannot clone ' . $clone_type_part,
|
|
|
|
new CodeLocation($statements_checker->getSource(), $stmt)
|
|
|
|
),
|
|
|
|
$statements_checker->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$stmt->inferredType = $stmt->expr->inferredType;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2017-05-27 02:05:57 +02:00
|
|
|
return in_array($fq_class_name, Config::getInstance()->getMockClasses(), true);
|
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
|
|
|
}
|