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

1269 lines
47 KiB
PHP
Raw Normal View History

2016-10-22 19:23:18 +02:00
<?php
namespace Psalm\Checker\Statements;
use PhpParser;
use Psalm\Checker\ClassLikeChecker;
use Psalm\Checker\ClosureChecker;
use Psalm\Checker\CommentChecker;
use Psalm\Checker\FunctionLikeChecker;
use Psalm\Checker\ProjectChecker;
2018-01-14 18:09:40 +01:00
use Psalm\Checker\Statements\Expression\ArrayChecker;
2016-11-01 16:37:58 +01:00
use Psalm\Checker\Statements\Expression\AssignmentChecker;
2018-01-14 18:09:40 +01:00
use Psalm\Checker\Statements\Expression\BinaryOpChecker;
2018-01-29 00:29:38 +01:00
use Psalm\Checker\Statements\Expression\Call\FunctionCallChecker;
use Psalm\Checker\Statements\Expression\Call\MethodCallChecker;
use Psalm\Checker\Statements\Expression\Call\NewChecker;
use Psalm\Checker\Statements\Expression\Call\StaticCallChecker;
2018-01-14 18:09:40 +01:00
use Psalm\Checker\Statements\Expression\Fetch\ArrayFetchChecker;
use Psalm\Checker\Statements\Expression\Fetch\ConstFetchChecker;
use Psalm\Checker\Statements\Expression\Fetch\PropertyFetchChecker;
use Psalm\Checker\Statements\Expression\Fetch\VariableFetchChecker;
use Psalm\Checker\Statements\Expression\IncludeChecker;
use Psalm\Checker\Statements\Expression\TernaryChecker;
2016-11-02 07:29:00 +01:00
use Psalm\Checker\StatementsChecker;
2016-10-22 19:23:18 +02:00
use Psalm\Checker\TypeChecker;
use Psalm\CodeLocation;
2016-10-22 19:23:18 +02:00
use Psalm\Config;
use Psalm\Context;
use Psalm\Exception\DocblockParseException;
use Psalm\FileManipulation\FileManipulationBuffer;
use Psalm\FileSource;
2016-10-22 19:23:18 +02:00
use Psalm\Issue\ForbiddenCode;
use Psalm\Issue\InvalidCast;
use Psalm\Issue\InvalidClone;
use Psalm\Issue\InvalidDocblock;
2016-10-22 19:23:18 +02:00
use Psalm\Issue\PossiblyUndefinedVariable;
use Psalm\Issue\UndefinedVariable;
2016-11-06 01:53:39 +01:00
use Psalm\Issue\UnrecognizedExpression;
2016-11-02 07:29:00 +01:00
use Psalm\IssueBuffer;
2017-01-07 20:35:07 +01:00
use Psalm\StatementsSource;
2016-10-22 19:23:18 +02:00
use Psalm\Type;
use Psalm\Type\Atomic\ObjectLike;
use Psalm\Type\Atomic\Scalar;
use Psalm\Type\Atomic\TArray;
use Psalm\Type\Atomic\TFloat;
use Psalm\Type\Atomic\TGenericParam;
use Psalm\Type\Atomic\TInt;
use Psalm\Type\Atomic\TMixed;
use Psalm\Type\Atomic\TNamedObject;
use Psalm\Type\Atomic\TObject;
use Psalm\Type\Atomic\TString;
2016-10-22 19:23:18 +02:00
class ExpressionChecker
{
/**
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
*/
public static function analyze(
2016-10-22 19:23:18 +02:00
StatementsChecker $statements_checker,
PhpParser\Node\Expr $stmt,
Context $context,
$array_assignment = false
2016-10-22 19:23:18 +02:00
) {
if ($stmt instanceof PhpParser\Node\Expr\Variable) {
2018-01-14 18:09:40 +01:00
if (VariableFetchChecker::analyze(
$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) {
$assignment_type = AssignmentChecker::analyze(
2016-11-01 16:37:58 +01:00
$statements_checker,
$stmt->var,
$stmt->expr,
null,
2016-11-01 16:37:58 +01:00
$context,
(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) {
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) {
2018-01-29 00:29:38 +01:00
if (MethodCallChecker::analyze($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) {
2018-01-29 00:29:38 +01:00
if (StaticCallChecker::analyze($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) {
ConstFetchChecker::analyze($statements_checker, $stmt, $context);
2016-11-02 07:29:00 +01:00
} elseif ($stmt instanceof PhpParser\Node\Scalar\String_) {
$stmt->inferredType = Type::getString(strlen($stmt->value) < 30 ? [$stmt->value => true] : null);
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) {
switch (strtolower($stmt->getName())) {
case '__line__':
$stmt->inferredType = Type::getInt();
break;
case '__class__':
$stmt->inferredType = Type::getClassString();
break;
case '__file__':
case '__dir__':
case '__function__':
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) {
$stmt->inferredType = Type::getInt(
false,
[($stmt->value >= 0 ? $stmt->value : (string) $stmt->value) => true]
);
2016-11-02 07:29:00 +01:00
} elseif ($stmt instanceof PhpParser\Node\Scalar\DNumber) {
$stmt->inferredType = Type::getFloat([(string)$stmt->value => true]);
2017-01-26 04:02:19 +01:00
} elseif ($stmt instanceof PhpParser\Node\Expr\UnaryMinus ||
$stmt instanceof PhpParser\Node\Expr\UnaryPlus
) {
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]);
} 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 = [];
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) {
if ($type_part instanceof Type\Atomic\TLiteralInt
&& $stmt instanceof PhpParser\Node\Expr\UnaryMinus
) {
$inverted_values = [];
foreach ($type_part->values as $value => $_) {
$inverted_values[$value > 0 ? (string) (-$value) : (int) (-$value)] = true;
}
$type_part->values = $inverted_values;
} elseif ($type_part instanceof Type\Atomic\TLiteralFloat
&& $stmt instanceof PhpParser\Node\Expr\UnaryMinus
) {
$inverted_values = [];
foreach ($type_part->values as $value => $_) {
$inverted_values[(string)(-$value)] = true;
}
$type_part->values = $inverted_values;
}
2017-01-26 04:23:06 +01:00
$acceptable_types[] = $type_part;
} elseif ($type_part instanceof TString) {
$acceptable_types[] = new TInt;
$acceptable_types[] = new TFloat;
} else {
$acceptable_types[] = new TInt;
2017-01-26 04:02:19 +01:00
}
2017-01-26 04:23:06 +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_) {
self::analyzeIsset($statements_checker, $stmt, $context);
$stmt->inferredType = Type::getBool();
2016-11-02 07:29:00 +01:00
} elseif ($stmt instanceof PhpParser\Node\Expr\ClassConstFetch) {
2018-01-14 18:09:40 +01:00
if (ConstFetchChecker::analyzeClassConst($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) {
2018-01-14 18:09:40 +01:00
if (PropertyFetchChecker::analyzeInstance($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) {
2018-01-14 18:09:40 +01:00
if (PropertyFetchChecker::analyzeStatic($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) {
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) {
2018-01-14 18:09:40 +01:00
if (BinaryOpChecker::analyze(
2017-02-13 00:06:18 +01:00
$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
) {
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;
$stmt->inferredType->from_calculation = true;
2018-05-08 23:42:02 +02:00
foreach ($stmt->inferredType->getTypes() as $atomic_type) {
if ($atomic_type instanceof Type\Atomic\TLiteralInt) {
$stmt->inferredType->addType(new Type\Atomic\TInt);
} elseif ($atomic_type instanceof Type\Atomic\TLiteralFloat) {
$stmt->inferredType->addType(new Type\Atomic\TFloat);
}
}
$stmt->inferredType->bustCache();
$var_id = self::getArrayVarId($stmt->var, null);
if ($var_id && isset($context->vars_in_scope[$var_id])) {
$context->vars_in_scope[$var_id] = $stmt->inferredType;
}
2016-12-25 00:44:11 +01:00
} else {
$stmt->inferredType = Type::getMixed();
}
2016-11-02 07:29:00 +01:00
} elseif ($stmt instanceof PhpParser\Node\Expr\New_) {
2018-01-29 00:29:38 +01:00
if (NewChecker::analyze($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_) {
2018-01-14 18:09:40 +01:00
if (ArrayChecker::analyze($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) {
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) {
$project_checker = $statements_checker->getFileChecker()->project_checker;
2018-01-29 00:29:38 +01:00
if (FunctionCallChecker::analyze(
$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) {
2018-01-14 18:09:40 +01:00
if (TernaryChecker::analyze($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) {
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_) {
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());
if (self::analyzeClosureUses($statements_checker, $stmt, $context) === false) {
2016-10-22 19:23:18 +02:00
return false;
}
$codebase = $statements_checker->getFileChecker()->project_checker->codebase;
$use_context = new Context($context->self);
$use_context->collect_references = $codebase->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 &&
2018-02-01 06:50:01 +01:00
$codebase->classExtends(
2017-01-12 06:54:41 +01:00
$context->self,
2017-01-07 20:35:07 +01:00
(string)$statements_checker->getFQCLN()
)
2017-01-12 06:54:41 +01:00
) {
$use_context->vars_in_scope['$this'] = clone $context->vars_in_scope['$this'];
} elseif ($context->self) {
$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;
}
}
2018-01-28 18:43:19 +01:00
foreach ($context->vars_possibly_in_scope as $var => $_) {
2016-10-22 19:23:18 +02:00
if (strpos($var, '$this->') === 0) {
$use_context->vars_possibly_in_scope[$var] = true;
}
}
foreach ($stmt->uses as $use) {
if (!is_string($use->var->name)) {
continue;
}
$use_var_id = '$' . $use->var->name;
// insert the ref into the current context if passed by ref, as whatever we're passing
// the closure to could execute it straight away.
if (!$context->hasVariable($use_var_id, $statements_checker) && $use->byRef) {
$context->vars_in_scope[$use_var_id] = Type::getMixed();
}
$use_context->vars_in_scope[$use_var_id] =
$context->hasVariable($use_var_id, $statements_checker) && !$use->byRef
? clone $context->vars_in_scope[$use_var_id]
2016-11-02 07:29:00 +01:00
: Type::getMixed();
$use_context->vars_possibly_in_scope[$use_var_id] = true;
2016-10-22 19:23:18 +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) {
2018-01-14 18:09:40 +01:00
if (ArrayFetchChecker::analyze(
2016-11-02 07:29:00 +01:00
$statements_checker,
$stmt,
$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_) {
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) {
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_) {
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_) {
if (self::analyze($statements_checker, $stmt->expr, $context) === false) {
2016-10-22 19:23:18 +02:00
return false;
}
$container_type = Type::getString();
2017-10-07 21:05:05 +02:00
if (isset($stmt->expr->inferredType)
&& !$stmt->expr->inferredType->isMixed()
2018-04-30 06:19:35 +02:00
&& !isset($stmt->expr->inferredType->getTypes()['resource'])
&& !TypeChecker::isContainedBy(
2018-02-01 06:50:01 +01:00
$statements_checker->getFileChecker()->project_checker->codebase,
$stmt->expr->inferredType,
$container_type,
true,
false,
$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_) {
if (self::analyze($statements_checker, $stmt->expr, $context) === false) {
2016-10-22 19:23:18 +02:00
return false;
}
$stmt->inferredType = new Type\Union([new TNamedObject('stdClass')]);
2016-11-02 07:29:00 +01:00
} elseif ($stmt instanceof PhpParser\Node\Expr\Cast\Array_) {
if (self::analyze($statements_checker, $stmt->expr, $context) === false) {
2016-10-22 19:23:18 +02:00
return false;
}
$permissible_atomic_types = [];
$all_permissible = false;
if (isset($stmt->expr->inferredType)) {
$all_permissible = true;
foreach ($stmt->expr->inferredType->getTypes() as $type) {
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) {
$permissible_atomic_types[] = $type->getGenericArrayType();
} 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_) {
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_) {
self::analyzeClone($statements_checker, $stmt, $context);
2016-11-02 07:29:00 +01:00
} elseif ($stmt instanceof PhpParser\Node\Expr\Instanceof_) {
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 &&
!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,
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
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(
$statements_checker,
$fq_class_name,
new CodeLocation($statements_checker->getSource(), $stmt->class),
$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_) {
2018-01-14 18:09:40 +01:00
IncludeChecker::analyze($statements_checker, $stmt, $context);
2016-11-02 07:29:00 +01:00
} elseif ($stmt instanceof PhpParser\Node\Expr\Eval_) {
2016-10-22 19:23:18 +02:00
$context->check_classes = false;
$context->check_variables = false;
if (self::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) {
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) {
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\ShellExec) {
2016-10-22 19:23:18 +02:00
if (IssueBuffer::accepts(
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_) {
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_) {
self::analyzeYield($statements_checker, $stmt, $context);
2016-11-02 07:29:00 +01:00
} elseif ($stmt instanceof PhpParser\Node\Expr\YieldFrom) {
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),
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
}
$project_checker = $statements_checker->getFileChecker()->project_checker;
$plugin_classes = $project_checker->config->after_expression_checks;
if ($plugin_classes) {
$file_manipulations = [];
$code_location = new CodeLocation($statements_checker->getSource(), $stmt);
foreach ($plugin_classes as $plugin_fq_class_name) {
if ($plugin_fq_class_name::afterExpressionCheck(
$statements_checker,
$stmt,
$context,
$code_location,
$statements_checker->getSuppressedIssues(),
$file_manipulations
) === false) {
return false;
}
2016-10-22 19:23:18 +02:00
}
if ($file_manipulations) {
/** @psalm-suppress MixedTypeCoercion */
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 $stmt
* @param Type\Union $by_ref_type
* @param Context $context
* @param bool $constrain_type
2017-05-27 02:16:18 +02:00
*
2016-10-22 19:23:18 +02:00
* @return void
*/
2016-11-02 07:29:00 +01:00
public static function assignByRefParam(
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
if ($var_id) {
2017-10-28 21:33:29 +02:00
if (!$by_ref_type->isMixed() && $constrain_type) {
$context->byref_constraints[$var_id] = new \Psalm\ReferenceConstraint($by_ref_type);
}
if (!$context->hasVariable($var_id, $statements_checker)) {
$context->vars_possibly_in_scope[$var_id] = true;
if (!$statements_checker->hasVariable($var_id)) {
$location = new CodeLocation($statements_checker, $stmt);
$statements_checker->registerVariable($var_id, $location, null);
if ($context->collect_references) {
$context->unreferenced_vars[$var_id] = $location;
}
$context->hasVariable($var_id, $statements_checker);
}
} 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,
$statements_checker
2017-04-02 23:37:56 +02: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;
$stmt->inferredType = $context->vars_in_scope[$var_id];
2017-05-25 04:07:49 +02:00
return;
}
}
$context->vars_in_scope[$var_id] = $by_ref_type;
2016-10-22 19:23:18 +02:00
}
$stmt->inferredType = $by_ref_type;
}
/**
2018-01-14 18:09:40 +01:00
* @param PhpParser\Node\Expr $stmt
* @param string|null $this_class_name
* @param FileSource|null $source
2018-01-14 18:09:40 +01:00
* @param int|null &$nesting
2017-05-27 02:16:18 +02:00
*
2018-01-14 18:09:40 +01:00
* @return string|null
2016-10-22 19:23:18 +02:00
*/
2018-01-14 18:09:40 +01:00
public static function getVarId(
PhpParser\Node\Expr $stmt,
$this_class_name,
FileSource $source = null,
2018-01-14 18:09:40 +01:00
&$nesting = null
2016-10-22 19:23:18 +02:00
) {
2018-01-14 18:09:40 +01:00
if ($stmt instanceof PhpParser\Node\Expr\Variable && is_string($stmt->name)) {
return '$' . $stmt->name;
2016-10-22 19:23:18 +02:00
}
2018-01-14 18:09:40 +01:00
if ($stmt instanceof PhpParser\Node\Expr\StaticPropertyFetch
&& $stmt->name instanceof PhpParser\Node\Identifier
2018-01-14 18:09:40 +01:00
&& $stmt->class instanceof PhpParser\Node\Name
) {
if (count($stmt->class->parts) === 1
&& in_array(strtolower($stmt->class->parts[0]), ['self', 'static', 'parent'], true)
) {
if (!$this_class_name) {
$fq_class_name = $stmt->class->parts[0];
} else {
$fq_class_name = $this_class_name;
2016-10-22 19:23:18 +02:00
}
2016-11-02 07:29:00 +01:00
} else {
2018-01-14 18:09:40 +01:00
$fq_class_name = $source
? ClassLikeChecker::getFQCLNFromNameObject(
$stmt->class,
$source->getAliases()
)
: implode('\\', $stmt->class->parts);
}
return $fq_class_name . '::$' . $stmt->name->name;
2018-01-14 18:09:40 +01:00
}
2016-10-22 19:23:18 +02:00
if ($stmt instanceof PhpParser\Node\Expr\PropertyFetch && $stmt->name instanceof PhpParser\Node\Identifier) {
2018-01-14 18:09:40 +01:00
$object_id = self::getVarId($stmt->var, $this_class_name, $source);
2017-03-20 07:05:58 +01:00
2018-01-14 18:09:40 +01:00
if (!$object_id) {
return null;
2016-10-22 19:23:18 +02:00
}
2018-01-14 18:09:40 +01:00
return $object_id . '->' . $stmt->name->name;
2016-10-22 19:23:18 +02:00
}
2018-01-14 18:09:40 +01:00
if ($stmt instanceof PhpParser\Node\Expr\ArrayDimFetch && $nesting !== null) {
++$nesting;
2017-05-25 04:07:49 +02:00
2018-01-14 18:09:40 +01:00
return self::getVarId($stmt->var, $this_class_name, $source, $nesting);
2016-10-22 19:23:18 +02:00
}
2016-11-02 07:29:00 +01:00
return null;
2016-10-22 19:23:18 +02:00
}
/**
2018-01-14 18:09:40 +01:00
* @param PhpParser\Node\Expr $stmt
* @param string|null $this_class_name
* @param FileSource|null $source
2017-05-27 02:16:18 +02:00
*
2018-01-14 18:09:40 +01:00
* @return string|null
2016-10-22 19:23:18 +02:00
*/
2018-01-14 18:09:40 +01:00
public static function getRootVarId(
PhpParser\Node\Expr $stmt,
$this_class_name,
FileSource $source = null
2016-10-22 19:23:18 +02:00
) {
2018-01-14 18:09:40 +01:00
if ($stmt instanceof PhpParser\Node\Expr\Variable
|| $stmt instanceof PhpParser\Node\Expr\StaticPropertyFetch
) {
2018-01-14 18:09:40 +01:00
return self::getVarId($stmt, $this_class_name, $source);
}
if ($stmt instanceof PhpParser\Node\Expr\PropertyFetch && $stmt->name instanceof PhpParser\Node\Identifier) {
2018-01-14 18:09:40 +01:00
$property_root = self::getRootVarId($stmt->var, $this_class_name, $source);
2018-01-14 18:09:40 +01:00
if ($property_root) {
return $property_root . '->' . $stmt->name->name;
2016-10-22 19:23:18 +02:00
}
2018-01-14 18:09:40 +01:00
}
2016-10-22 19:23:18 +02:00
2018-01-14 18:09:40 +01:00
if ($stmt instanceof PhpParser\Node\Expr\ArrayDimFetch) {
return self::getRootVarId($stmt->var, $this_class_name, $source);
}
2018-01-14 18:09:40 +01:00
return null;
}
2018-01-14 18:09:40 +01:00
/**
* @param PhpParser\Node\Expr $stmt
* @param string|null $this_class_name
* @param FileSource|null $source
2018-01-14 18:09:40 +01:00
*
* @return string|null
*/
public static function getArrayVarId(
PhpParser\Node\Expr $stmt,
$this_class_name,
FileSource $source = null
2018-01-14 18:09:40 +01:00
) {
if ($stmt instanceof PhpParser\Node\Expr\Assign) {
return self::getArrayVarId($stmt->var, $this_class_name, $source);
}
if ($stmt instanceof PhpParser\Node\Expr\ArrayDimFetch) {
2018-01-14 18:09:40 +01:00
$root_var_id = self::getArrayVarId($stmt->var, $this_class_name, $source);
$offset = null;
if ($root_var_id) {
if ($stmt->dim instanceof PhpParser\Node\Scalar\String_
|| $stmt->dim instanceof PhpParser\Node\Scalar\LNumber
) {
$offset = $stmt->dim instanceof PhpParser\Node\Scalar\String_
? '\'' . $stmt->dim->value . '\''
: $stmt->dim->value;
} elseif ($stmt->dim instanceof PhpParser\Node\Expr\Variable
&& is_string($stmt->dim->name)
) {
$offset = '$' . $stmt->dim->name;
}
return $root_var_id && $offset !== null ? $root_var_id . '[' . $offset . ']' : null;
}
2018-01-14 18:09:40 +01:00
}
if ($stmt instanceof PhpParser\Node\Expr\PropertyFetch) {
$object_id = self::getArrayVarId($stmt->var, $this_class_name, $source);
if (!$object_id) {
return null;
}
if ($stmt->name instanceof PhpParser\Node\Identifier) {
return $object_id . '->' . $stmt->name;
} elseif (isset($stmt->name->inferredType) && $stmt->name->inferredType->isSingleStringLiteral()) {
return $object_id . '->' . $stmt->name->inferredType->getSingleStringLiteral();
} else {
return null;
}
}
2018-04-28 19:25:44 +02:00
if ($stmt instanceof PhpParser\Node\Expr\MethodCall
&& $stmt->name instanceof PhpParser\Node\Identifier
&& !$stmt->args
) {
$config = \Psalm\Config::getInstance();
if ($config->memoize_method_calls) {
$lhs_var_name = self::getArrayVarId(
$stmt->var,
$this_class_name,
$source
);
if (!$lhs_var_name) {
return null;
}
return $lhs_var_name . '->' . strtolower($stmt->name->name) . '()';
}
}
2018-01-14 18:09:40 +01:00
return self::getVarId($stmt, $this_class_name, $source);
}
2016-10-22 19:23:18 +02:00
2018-01-14 18:09:40 +01:00
/**
* @param Type\Union $return_type
2018-01-26 19:51:00 +01:00
* @param string|null $self_class
* @param string|null $static_class
2018-01-14 18:09:40 +01:00
*
* @return Type\Union
*/
public static function fleshOutType(
ProjectChecker $project_checker,
Type\Union $return_type,
2018-01-26 19:51:00 +01:00
$self_class = null,
$static_class = null
2018-01-14 18:09:40 +01:00
) {
$return_type = clone $return_type;
2016-10-22 19:23:18 +02:00
2018-01-14 18:09:40 +01:00
$new_return_type_parts = [];
2016-10-22 19:23:18 +02:00
foreach ($return_type->getTypes() as $return_type_part) {
$new_return_type_parts[] = self::fleshOutAtomicType(
$project_checker,
$return_type_part,
2018-01-26 19:51:00 +01:00
$self_class,
$static_class
);
2016-10-22 19:23:18 +02:00
}
$fleshed_out_type = new Type\Union($new_return_type_parts);
$fleshed_out_type->from_docblock = $return_type->from_docblock;
$fleshed_out_type->ignore_nullable_issues = $return_type->ignore_nullable_issues;
$fleshed_out_type->ignore_falsable_issues = $return_type->ignore_falsable_issues;
$fleshed_out_type->possibly_undefined = $return_type->possibly_undefined;
$fleshed_out_type->by_ref = $return_type->by_ref;
return $fleshed_out_type;
2016-10-22 19:23:18 +02:00
}
/**
2017-03-02 04:27:52 +01:00
* @param Type\Atomic &$return_type
2018-01-26 19:51:00 +01:00
* @param string|null $self_class
* @param string|null $static_class
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
*/
2018-01-26 19:51:00 +01:00
private static function fleshOutAtomicType(
ProjectChecker $project_checker,
Type\Atomic $return_type,
2018-01-26 19:51:00 +01:00
$self_class,
$static_class
) {
if ($return_type instanceof TNamedObject) {
$return_type_lc = strtolower($return_type->value);
2018-01-26 19:51:00 +01:00
if ($return_type_lc === 'static' || $return_type_lc === '$this') {
if (!$static_class) {
throw new \UnexpectedValueException(
2018-01-26 19:51:00 +01:00
'Cannot handle ' . $return_type->value . ' when $static_class is empty'
);
}
2016-10-22 19:23:18 +02:00
2018-01-26 19:51:00 +01:00
$return_type->value = $static_class;
} elseif ($return_type_lc === 'self') {
if (!$self_class) {
throw new \UnexpectedValueException(
2018-01-26 19:51:00 +01:00
'Cannot handle ' . $return_type->value . ' when $self_class is empty'
);
}
2018-01-26 19:51:00 +01:00
$return_type->value = $self_class;
2016-10-22 19:23:18 +02: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) {
2018-01-26 19:51:00 +01:00
$type_param = self::fleshOutType(
$project_checker,
$type_param,
$self_class,
$static_class
);
2016-10-22 19:23:18 +02:00
}
2018-03-07 19:54:46 +01:00
} elseif ($return_type instanceof Type\Atomic\ObjectLike) {
foreach ($return_type->properties as &$property_type) {
$property_type = self::fleshOutType(
$project_checker,
$property_type,
$self_class,
$static_class
);
}
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
*/
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) {
2018-05-08 23:42:02 +02:00
if (!is_string($use->var->name)) {
continue;
}
$use_var_id = '$' . $use->var->name;
if (!$context->hasVariable($use_var_id, $statements_checker)) {
2018-05-08 23:43:26 +02:00
if ($use_var_id === '$argv' || $use_var_id === '$argc') {
continue;
}
2016-11-01 19:14:35 +01:00
if ($use->byRef) {
$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->var),
null
);
}
2017-05-25 04:07:49 +02:00
2016-10-22 19:23:18 +02:00
return;
}
2018-05-08 23:43:26 +02:00
if (!isset($context->vars_possibly_in_scope[$use_var_id])) {
2016-11-01 19:14:35 +01:00
if ($context->check_variables) {
if (IssueBuffer::accepts(
2016-11-02 07:29:00 +01:00
new UndefinedVariable(
'Cannot find referenced variable ' . $use_var_id,
new CodeLocation($statements_checker->getSource(), $use->var)
),
$statements_checker->getSuppressedIssues()
)) {
return false;
}
2016-10-22 19:23:18 +02:00
2018-05-08 23:43:26 +02:00
return null;
}
}
$first_appearance = $statements_checker->getFirstAppearance($use_var_id);
if ($first_appearance) {
if (IssueBuffer::accepts(
new PossiblyUndefinedVariable(
'Possibly undefined variable ' . $use_var_id . ', first seen on line ' .
$first_appearance->getLineNumber(),
new CodeLocation($statements_checker->getSource(), $use->var)
),
$statements_checker->getSuppressedIssues()
)) {
return false;
}
continue;
}
if ($context->check_variables) {
if (IssueBuffer::accepts(
new UndefinedVariable(
'Cannot find referenced variable ' . $use_var_id,
new CodeLocation($statements_checker->getSource(), $use->var)
),
$statements_checker->getSuppressedIssues()
)) {
return false;
2016-11-01 19:14:35 +01:00
}
2018-05-08 23:43:26 +02:00
continue;
2016-11-01 19:14:35 +01:00
}
} elseif ($use->byRef) {
2018-05-08 23:42:02 +02:00
foreach ($context->vars_in_scope[$use_var_id]->getTypes() as $atomic_type) {
if ($atomic_type instanceof Type\Atomic\TLiteralInt) {
$context->vars_in_scope[$use_var_id]->addType(new Type\Atomic\TInt);
} elseif ($atomic_type instanceof Type\Atomic\TLiteralFloat) {
$context->vars_in_scope[$use_var_id]->addType(new Type\Atomic\TFloat);
} elseif ($atomic_type instanceof Type\Atomic\TLiteralString) {
$context->vars_in_scope[$use_var_id]->addType(new Type\Atomic\TString);
}
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
*/
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();
$var_comments = [];
$var_comment_type = null;
2017-03-02 04:27:52 +01:00
if ($doc_comment_text) {
try {
$var_comments = 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
}
}
foreach ($var_comments as $var_comment) {
$comment_type = ExpressionChecker::fleshOutType(
$statements_checker->getFileChecker()->project_checker,
$var_comment->type,
2018-01-26 19:51:00 +01:00
$context->self,
$context->self
);
if (!$var_comment->var_id) {
$var_comment_type = $comment_type;
continue;
}
$context->vars_in_scope[$var_comment->var_id] = $comment_type;
}
2017-03-02 04:27:52 +01:00
}
2016-10-22 19:23:18 +02:00
if ($stmt->key) {
if (self::analyze($statements_checker, $stmt->key, $context) === false) {
2016-10-22 19:23:18 +02:00
return false;
}
}
if ($stmt->value) {
if (self::analyze($statements_checker, $stmt->value, $context) === false) {
2016-10-22 19:23:18 +02:00
return false;
}
if ($var_comment_type) {
$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
*/
protected static function analyzeYieldFrom(
2016-11-02 07:29:00 +01:00
StatementsChecker $statements_checker,
PhpParser\Node\Expr\YieldFrom $stmt,
Context $context
) {
if (self::analyze($statements_checker, $stmt->expr, $context) === false) {
2016-10-22 19:23:18 +02:00
return false;
}
if (isset($stmt->expr->inferredType)) {
// 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\BooleanNot $stmt
* @param Context $context
2017-05-27 02:16:18 +02:00
*
2016-11-02 07:29:00 +01:00
* @return false|null
*/
protected static function analyzeBooleanNot(
2016-11-02 07:29:00 +01:00
StatementsChecker $statements_checker,
PhpParser\Node\Expr\BooleanNot $stmt,
Context $context
) {
$stmt->inferredType = Type::getBool();
2016-12-28 22:29:50 +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
*
* @return void
2016-11-02 07:29:00 +01:00
*/
protected static function analyzeEmpty(
2016-11-02 07:29:00 +01:00
StatementsChecker $statements_checker,
PhpParser\Node\Expr\Empty_ $stmt,
Context $context
) {
self::analyzeIssetVar($statements_checker, $stmt->expr, $context);
2018-04-09 16:19:23 +02:00
$stmt->inferredType = Type::getBool();
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
*/
protected static function analyzeEncapsulatedString(
2016-11-02 07:29:00 +01:00
StatementsChecker $statements_checker,
PhpParser\Node\Scalar\Encapsed $stmt,
Context $context
) {
$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) {
$function_storage = $source_checker->getFunctionLikeStorage($statements_checker);
}
}
2016-10-22 19:23:18 +02:00
/** @var PhpParser\Node\Expr $part */
foreach ($stmt->parts as $part) {
if (self::analyze($statements_checker, $part, $context) === false) {
2016-10-22 19:23:18 +02:00
return false;
}
2017-09-04 02:52:54 +02:00
if ($function_storage) {
$context->inferType($part, $function_storage, Type::getString());
}
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
}
/**
* @param StatementsChecker $statements_checker
* @param PhpParser\Node\Expr\Isset_ $stmt
* @param Context $context
2017-05-27 02:16:18 +02: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'
&& $isset_var->name instanceof PhpParser\Node\Identifier
) {
$var_id = '$this->' . $isset_var->name->name;
if (!isset($context->vars_in_scope[$var_id])) {
$context->vars_in_scope[$var_id] = Type::getMixed();
$context->vars_possibly_in_scope[$var_id] = true;
}
}
self::analyzeIssetVar($statements_checker, $isset_var, $context);
}
2018-04-09 16:20:13 +02:00
$stmt->inferredType = Type::getBool();
}
/**
* @param StatementsChecker $statements_checker
* @param PhpParser\Node\Expr $stmt
* @param Context $context
2017-05-27 02:16:18 +02:00
*
* @return false|null
*/
protected static function analyzeIssetVar(
StatementsChecker $statements_checker,
PhpParser\Node\Expr $stmt,
Context $context
) {
2018-05-01 04:13:13 +02:00
$context->inside_isset = true;
if (self::analyze($statements_checker, $stmt, $context) === false) {
return false;
}
$context->inside_isset = false;
}
/**
* @param StatementsChecker $statements_checker
* @param PhpParser\Node\Expr\Clone_ $stmt
* @param Context $context
2017-05-27 02:16:18 +02: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)) {
foreach ($stmt->expr->inferredType->getTypes() as $clone_type_part) {
if (!$clone_type_part instanceof TNamedObject
&& !$clone_type_part instanceof TObject
&& !$clone_type_part instanceof TMixed
&& !$clone_type_part instanceof TGenericParam
2017-01-19 21:00:05 +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
/**
* @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
*/
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
}
}