2016-01-20 00:27:06 +01:00
|
|
|
<?php
|
|
|
|
|
2016-08-13 20:20:46 +02:00
|
|
|
namespace Psalm\Checker;
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-02-04 15:22:46 +01:00
|
|
|
use PhpParser;
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-07-26 00:37:44 +02:00
|
|
|
use Psalm\IssueBuffer;
|
2016-10-09 23:54:58 +02:00
|
|
|
use Psalm\Issue\FailedTypeResolution;
|
2016-07-26 00:37:44 +02:00
|
|
|
use Psalm\Issue\ForbiddenCode;
|
2016-10-09 23:54:58 +02:00
|
|
|
use Psalm\Issue\InvalidArrayAccess;
|
|
|
|
use Psalm\Issue\InvalidArrayAssignment;
|
2016-07-26 00:37:44 +02:00
|
|
|
use Psalm\Issue\InvalidArgument;
|
2016-10-09 23:54:58 +02:00
|
|
|
use Psalm\Issue\InvalidContinue;
|
2016-07-26 00:37:44 +02:00
|
|
|
use Psalm\Issue\InvalidIterator;
|
2016-10-09 23:54:58 +02:00
|
|
|
use Psalm\Issue\InvalidNamespace;
|
|
|
|
use Psalm\Issue\InvalidPropertyAssignment;
|
|
|
|
use Psalm\Issue\InvalidScalarArgument;
|
|
|
|
use Psalm\Issue\InvalidScope;
|
|
|
|
use Psalm\Issue\InvalidStaticVariable;
|
|
|
|
use Psalm\Issue\InvalidPropertyFetch;
|
|
|
|
use Psalm\Issue\InvisibleProperty;
|
|
|
|
use Psalm\Issue\MixedArgument;
|
|
|
|
use Psalm\Issue\MixedArrayOffset;
|
2016-07-26 00:37:44 +02:00
|
|
|
use Psalm\Issue\MixedMethodCall;
|
2016-10-09 23:54:58 +02:00
|
|
|
use Psalm\Issue\MixedPropertyAssignment;
|
|
|
|
use Psalm\Issue\MixedPropertyFetch;
|
|
|
|
use Psalm\Issue\MixedStringOffsetAssignment;
|
2016-08-07 17:35:27 +02:00
|
|
|
use Psalm\Issue\MissingPropertyDeclaration;
|
|
|
|
use Psalm\Issue\NoInterfaceProperties;
|
2016-10-09 23:54:58 +02:00
|
|
|
use Psalm\Issue\NullPropertyAssignment;
|
2016-08-07 02:27:13 +02:00
|
|
|
use Psalm\Issue\NullPropertyFetch;
|
2016-07-26 00:37:44 +02:00
|
|
|
use Psalm\Issue\NullReference;
|
|
|
|
use Psalm\Issue\ParentNotFound;
|
|
|
|
use Psalm\Issue\PossiblyUndefinedVariable;
|
2016-10-09 23:54:58 +02:00
|
|
|
use Psalm\Issue\TooFewArguments;
|
|
|
|
use Psalm\Issue\TooManyArguments;
|
|
|
|
use Psalm\Issue\TypeCoercion;
|
2016-08-07 17:35:27 +02:00
|
|
|
use Psalm\Issue\UndefinedClass;
|
2016-07-26 00:37:44 +02:00
|
|
|
use Psalm\Issue\UndefinedConstant;
|
|
|
|
use Psalm\Issue\UndefinedFunction;
|
2016-10-11 01:29:38 +02:00
|
|
|
use Psalm\Issue\UndefinedPropertyAssignment;
|
|
|
|
use Psalm\Issue\UndefinedPropertyFetch;
|
|
|
|
use Psalm\Issue\UndefinedThisPropertyAssignment;
|
|
|
|
use Psalm\Issue\UndefinedThisPropertyFetch;
|
2016-07-26 00:37:44 +02:00
|
|
|
use Psalm\Issue\UndefinedVariable;
|
2016-04-17 22:20:36 +02:00
|
|
|
|
2016-08-13 20:20:46 +02:00
|
|
|
use Psalm\Type;
|
|
|
|
use Psalm\StatementsSource;
|
|
|
|
use Psalm\Config;
|
|
|
|
use Psalm\Context;
|
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
class StatementsChecker
|
|
|
|
{
|
2016-08-11 00:10:12 +02:00
|
|
|
protected $stmts;
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* @var StatementsSource
|
|
|
|
*/
|
2016-08-11 00:10:12 +02:00
|
|
|
protected $source;
|
2016-10-09 23:54:58 +02:00
|
|
|
|
2016-08-11 00:10:12 +02:00
|
|
|
protected $all_vars = [];
|
|
|
|
protected $warn_vars = [];
|
|
|
|
protected $check_classes = true;
|
|
|
|
protected $check_variables = true;
|
|
|
|
protected $check_methods = true;
|
|
|
|
protected $check_consts = true;
|
|
|
|
protected $check_functions = true;
|
|
|
|
protected $class_name;
|
2016-10-09 23:54:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string|null
|
|
|
|
*/
|
2016-08-11 00:10:12 +02:00
|
|
|
protected $parent_class;
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/** @var string */
|
2016-08-11 00:10:12 +02:00
|
|
|
protected $namespace;
|
2016-10-09 23:54:58 +02:00
|
|
|
|
|
|
|
/** @var array<string,string> */
|
2016-08-11 00:10:12 +02:00
|
|
|
protected $aliased_classes;
|
2016-10-09 23:54:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-08-11 00:10:12 +02:00
|
|
|
protected $file_name;
|
2016-10-09 23:54:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-09-01 06:07:29 +02:00
|
|
|
protected $checked_file_name;
|
2016-10-09 23:54:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string|null
|
|
|
|
*/
|
2016-08-25 01:00:44 +02:00
|
|
|
protected $include_file_name;
|
2016-10-09 23:54:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
2016-08-11 00:10:12 +02:00
|
|
|
protected $is_static;
|
2016-10-09 23:54:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-08-11 00:10:12 +02:00
|
|
|
protected $absolute_class;
|
2016-10-09 23:54:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var TypeChecker
|
|
|
|
*/
|
2016-08-11 00:10:12 +02:00
|
|
|
protected $type_checker;
|
|
|
|
|
|
|
|
protected $available_functions = [];
|
2016-03-23 18:05:25 +01:00
|
|
|
|
2016-07-22 19:29:46 +02:00
|
|
|
/**
|
|
|
|
* A list of suppressed issues
|
|
|
|
* @var array
|
|
|
|
*/
|
2016-08-11 00:10:12 +02:00
|
|
|
protected $suppressed_issues;
|
2016-07-22 19:29:46 +02:00
|
|
|
|
2016-08-11 00:10:12 +02:00
|
|
|
protected $require_file_name = null;
|
2016-04-17 16:25:43 +02:00
|
|
|
|
2016-08-14 05:26:45 +02:00
|
|
|
protected $existing_functions = [];
|
|
|
|
|
2016-10-02 19:05:49 +02:00
|
|
|
/**
|
|
|
|
* A list of classes checked with class_exists
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $phantom_classes = [];
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/** @var array<string,array<int,string>> */
|
2016-08-11 00:10:12 +02:00
|
|
|
protected static $method_call_index = [];
|
|
|
|
protected static $reflection_functions = [];
|
2016-10-09 23:54:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array<string,array<string,Type\Union>>
|
|
|
|
*/
|
2016-08-11 00:10:12 +02:00
|
|
|
protected static $this_assignments = [];
|
|
|
|
protected static $this_calls = [];
|
2016-04-16 22:28:25 +02:00
|
|
|
|
2016-08-11 00:10:12 +02:00
|
|
|
protected static $mock_interfaces = [];
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-08-14 06:38:29 +02:00
|
|
|
protected static $user_constants = [];
|
|
|
|
|
2016-06-02 23:44:50 +02:00
|
|
|
public function __construct(StatementsSource $source, $enforce_variable_checks = false, $check_methods = true)
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->source = $source;
|
|
|
|
$this->check_classes = true;
|
|
|
|
$this->check_methods = $check_methods;
|
2016-05-10 20:00:44 +02:00
|
|
|
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->check_consts = true;
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->file_name = $this->source->getFileName();
|
2016-08-25 01:00:44 +02:00
|
|
|
$this->checked_file_name = $this->source->getCheckedFileName();
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->aliased_classes = $this->source->getAliasedClasses();
|
|
|
|
$this->namespace = $this->source->getNamespace();
|
|
|
|
$this->is_static = $this->source->isStatic();
|
|
|
|
$this->absolute_class = $this->source->getAbsoluteClass();
|
|
|
|
$this->class_name = $this->source->getClassName();
|
|
|
|
$this->parent_class = $this->source->getParentClass();
|
|
|
|
$this->suppressed_issues = $this->source->getSuppressedIssues();
|
2016-04-16 22:28:25 +02:00
|
|
|
|
2016-06-27 19:22:16 +02:00
|
|
|
$config = Config::getInstance();
|
|
|
|
|
2016-08-25 01:00:44 +02:00
|
|
|
$this->check_variables = !$config->excludeIssueInFile('UndefinedVariable', $this->checked_file_name) || $enforce_variable_checks;
|
2016-05-10 20:00:44 +02:00
|
|
|
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->type_checker = new TypeChecker($source, $this);
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-06-18 20:45:55 +02:00
|
|
|
/**
|
|
|
|
* Checks an array of statements for validity
|
|
|
|
*
|
|
|
|
* @param array<PhpParser\Node> $stmts
|
2016-08-24 00:09:34 +02:00
|
|
|
* @param Context $context
|
|
|
|
* @param Context|null $loop_context
|
2016-06-18 20:45:55 +02:00
|
|
|
* @return null|false
|
|
|
|
*/
|
2016-08-24 00:09:34 +02:00
|
|
|
public function check(array $stmts, Context $context, Context $loop_context = null)
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
|
|
|
$has_returned = false;
|
|
|
|
|
2016-08-14 19:13:53 +02:00
|
|
|
$function_checkers = [];
|
|
|
|
|
2016-08-15 06:58:30 +02:00
|
|
|
// hoist functions to the top
|
|
|
|
foreach ($stmts as $stmt) {
|
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\Function_) {
|
|
|
|
$function_checker = new FunctionChecker($stmt, $this->source, $context->file_name);
|
|
|
|
$function_checkers[$stmt->name] = $function_checker;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
foreach ($stmts as $stmt) {
|
2016-06-18 20:45:55 +02:00
|
|
|
foreach (Config::getInstance()->getPlugins() as $plugin) {
|
2016-08-25 01:00:44 +02:00
|
|
|
if ($plugin->checkStatement($stmt, $context, $this->checked_file_name) === false) {
|
2016-06-18 20:45:55 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-03 02:27:36 +02:00
|
|
|
if ($has_returned && !($stmt instanceof PhpParser\Node\Stmt\Nop) && !($stmt instanceof PhpParser\Node\Stmt\InlineHTML)) {
|
2016-08-25 01:00:44 +02:00
|
|
|
echo('Warning: Expressions after return/throw/continue in ' . $this->checked_file_name . ' on line ' . $stmt->getLine() . PHP_EOL);
|
2016-01-20 16:57:56 +01:00
|
|
|
break;
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/*
|
2016-10-14 00:27:23 +02:00
|
|
|
if (isset($context->vars_in_scope['Vimeo\Test\Framework\API\VimeoAPIUnitTest::$_factory'])) {
|
|
|
|
var_dump($stmt->getLine() . ' ' . $context->vars_in_scope['Vimeo\Test\Framework\API\VimeoAPIUnitTest::$_factory']);
|
2016-10-09 23:54:58 +02:00
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\If_) {
|
2016-08-24 00:09:34 +02:00
|
|
|
$this->checkIf($stmt, $context, $loop_context);
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\TryCatch) {
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->checkTryCatch($stmt, $context);
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\For_) {
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->checkFor($stmt, $context);
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Foreach_) {
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->checkForeach($stmt, $context);
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\While_) {
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->checkWhile($stmt, $context);
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Do_) {
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->checkDo($stmt, $context);
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Const_) {
|
|
|
|
foreach ($stmt->consts as $const) {
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->checkExpression($const->value, $context);
|
2016-08-13 20:20:46 +02:00
|
|
|
|
2016-08-14 06:38:29 +02:00
|
|
|
self::$user_constants[$this->file_name][$const->name] = isset($const->value->inferredType) ? $const->value->inferredType : Type::getMixed();
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Unset_) {
|
2016-09-17 17:57:44 +02:00
|
|
|
foreach ($stmt->vars as $var) {
|
2016-10-14 00:27:23 +02:00
|
|
|
$var_id = self::getArrayVarId($var, $this->absolute_class, $this->namespace, $this->aliased_classes);
|
2016-09-17 17:57:44 +02:00
|
|
|
|
|
|
|
if ($var_id) {
|
|
|
|
$context->remove($var_id);
|
|
|
|
}
|
|
|
|
}
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Return_) {
|
|
|
|
$has_returned = true;
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->checkReturn($stmt, $context);
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Throw_) {
|
2016-04-03 02:27:36 +02:00
|
|
|
$has_returned = true;
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->checkThrow($stmt, $context);
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Switch_) {
|
2016-08-24 00:09:34 +02:00
|
|
|
$this->checkSwitch($stmt, $context, $loop_context);
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Break_) {
|
|
|
|
// do nothing
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Continue_) {
|
2016-04-03 02:27:36 +02:00
|
|
|
$has_returned = true;
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Static_) {
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->checkStatic($stmt, $context);
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Echo_) {
|
|
|
|
foreach ($stmt->exprs as $expr) {
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->checkExpression($expr, $context);
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Function_) {
|
2016-08-15 06:58:30 +02:00
|
|
|
$function_context = new Context($this->file_name, $context->self);
|
|
|
|
$function_checkers[$stmt->name]->check($function_context);
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr) {
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->checkExpression($stmt, $context);
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\InlineHTML) {
|
|
|
|
// do nothing
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Use_) {
|
|
|
|
foreach ($stmt->uses as $use) {
|
2016-09-10 05:17:56 +02:00
|
|
|
$this->aliased_classes[strtolower($use->alias)] = implode('\\', $use->name->parts);
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Global_) {
|
|
|
|
foreach ($stmt->vars as $var) {
|
|
|
|
if ($var instanceof PhpParser\Node\Expr\Variable) {
|
|
|
|
if (is_string($var->name)) {
|
2016-10-14 00:27:23 +02:00
|
|
|
$context->vars_in_scope['$' . $var->name] = Type::getMixed();
|
|
|
|
$context->vars_possibly_in_scope['$' . $var->name] = true;
|
2016-01-20 00:27:06 +01:00
|
|
|
} else {
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->checkExpression($var, $context);
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-26 20:11:56 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Property) {
|
|
|
|
foreach ($stmt->props as $prop) {
|
|
|
|
if ($prop->default) {
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->checkExpression($prop->default, $context);
|
2016-06-24 00:45:46 +02:00
|
|
|
|
|
|
|
if (isset($prop->default->inferredType)) {
|
2016-08-07 21:15:21 +02:00
|
|
|
if (!$stmt->isStatic()) {
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->checkPropertyAssignment($prop, $prop->name, $prop->default->inferredType, $context) === false) {
|
2016-08-07 21:15:21 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-06-24 00:45:46 +02:00
|
|
|
}
|
2016-08-07 21:15:21 +02:00
|
|
|
|
2016-06-24 00:45:46 +02:00
|
|
|
}
|
2016-01-26 20:11:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\ClassConst) {
|
2016-08-14 03:14:32 +02:00
|
|
|
foreach ($stmt->consts as $const) {
|
|
|
|
$this->checkExpression($const->value, $context);
|
2016-08-13 20:20:46 +02:00
|
|
|
|
2016-08-14 03:14:32 +02:00
|
|
|
if (isset($const->value->inferredType) && !$const->value->inferredType->isMixed()) {
|
|
|
|
ClassLikeChecker::setConstantType($this->absolute_class, $const->name, $const->value->inferredType);
|
|
|
|
}
|
|
|
|
}
|
2016-01-26 20:11:56 +01:00
|
|
|
|
2016-01-28 17:00:48 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Class_) {
|
2016-08-25 01:00:44 +02:00
|
|
|
(new ClassChecker($stmt, $this->source, $stmt->name))->check();
|
2016-01-28 17:00:48 +01:00
|
|
|
|
2016-03-14 17:38:08 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Nop) {
|
|
|
|
// do nothing
|
|
|
|
|
2016-04-17 16:25:43 +02:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Namespace_) {
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->namespace) {
|
2016-06-26 21:18:40 +02:00
|
|
|
if (IssueBuffer::accepts(
|
2016-08-11 00:10:12 +02:00
|
|
|
new InvalidNamespace('Cannot redeclare namespace', $this->require_file_name, $stmt->getLine()),
|
|
|
|
$this->suppressed_issues
|
2016-06-06 02:25:16 +02:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-17 16:25:43 +02:00
|
|
|
}
|
|
|
|
|
2016-08-11 00:10:12 +02:00
|
|
|
$namespace_checker = new NamespaceChecker($stmt, $this->source);
|
2016-04-17 16:25:43 +02:00
|
|
|
$namespace_checker->check(true);
|
2016-01-20 00:27:06 +01:00
|
|
|
} else {
|
2016-08-25 01:00:44 +02:00
|
|
|
var_dump('Unrecognised statement in ' . $this->checked_file_name);
|
2016-01-20 00:27:06 +01:00
|
|
|
var_dump($stmt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-17 00:52:12 +02:00
|
|
|
/**
|
2016-06-20 06:38:13 +02:00
|
|
|
* System of type substitution and deletion
|
2016-06-17 00:52:12 +02:00
|
|
|
*
|
2016-06-20 06:38:13 +02:00
|
|
|
* for example
|
2016-06-17 00:52:12 +02:00
|
|
|
*
|
2016-06-20 06:38:13 +02:00
|
|
|
* x: A|null
|
|
|
|
*
|
|
|
|
* if (x)
|
|
|
|
* (x: A)
|
|
|
|
* x = B -- effects: remove A from the type of x, add B
|
|
|
|
* else
|
|
|
|
* (x: null)
|
|
|
|
* x = C -- effects: remove null from the type of x, add C
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* x: A|null
|
|
|
|
*
|
|
|
|
* if (!x)
|
|
|
|
* (x: null)
|
|
|
|
* throw new Exception -- effects: remove null from the type of x
|
2016-06-17 00:52:12 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param PhpParser\Node\Stmt\If_ $stmt
|
2016-08-24 00:09:34 +02:00
|
|
|
* @param Context $context
|
|
|
|
* @param Context|null $loop_context
|
2016-06-17 00:52:12 +02:00
|
|
|
* @return null|false
|
|
|
|
*/
|
2016-08-24 00:09:34 +02:00
|
|
|
protected function checkIf(PhpParser\Node\Stmt\If_ $stmt, Context $context, Context $loop_context = null)
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
2016-10-02 04:47:32 +02:00
|
|
|
// get the first expression in the if, which should be evaluated on its own
|
|
|
|
// this allows us to update the context of $matches in
|
|
|
|
// if (!preg_match('/a/', 'aa', $matches)) {
|
|
|
|
// exit
|
|
|
|
// }
|
|
|
|
// echo $matches[0];
|
|
|
|
$first_if_cond_expr = $this->getFirstFunctionCall($stmt->cond);
|
|
|
|
|
|
|
|
if ($first_if_cond_expr && $this->checkExpression($first_if_cond_expr, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
$if_context = clone $context;
|
|
|
|
|
|
|
|
// we need to clone the current context so our ongoing updates to $context don't mess with elseif/else blocks
|
2016-07-25 21:05:58 +02:00
|
|
|
$original_context = clone $context;
|
2016-06-20 06:38:13 +02:00
|
|
|
|
2016-10-02 04:47:32 +02:00
|
|
|
if ($first_if_cond_expr !== $stmt->cond && $this->checkExpression($stmt->cond, $if_context) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
$reconcilable_if_types = null;
|
|
|
|
$negatable_if_types = null;
|
|
|
|
|
2016-06-28 21:28:05 +02:00
|
|
|
if ($stmt->cond instanceof PhpParser\Node\Expr\BinaryOp) {
|
2016-10-14 00:27:23 +02:00
|
|
|
$reconcilable_if_types = $this->type_checker->getReconcilableTypeAssertions(
|
|
|
|
$stmt->cond,
|
|
|
|
$this->absolute_class,
|
|
|
|
$this->namespace,
|
|
|
|
$this->aliased_classes
|
|
|
|
);
|
|
|
|
|
|
|
|
$negatable_if_types = $this->type_checker->getNegatableTypeAssertions(
|
|
|
|
$stmt->cond,
|
|
|
|
$this->absolute_class,
|
|
|
|
$this->namespace,
|
|
|
|
$this->aliased_classes
|
|
|
|
);
|
2016-06-28 21:28:05 +02:00
|
|
|
}
|
|
|
|
else {
|
2016-10-14 00:27:23 +02:00
|
|
|
$reconcilable_if_types = $negatable_if_types = $this->type_checker->getTypeAssertions(
|
|
|
|
$stmt->cond,
|
|
|
|
$this->absolute_class,
|
|
|
|
$this->namespace,
|
|
|
|
$this->aliased_classes);
|
2016-06-28 21:28:05 +02:00
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-08-24 23:06:20 +02:00
|
|
|
$has_ending_statements = ScopeChecker::doesAlwaysReturnOrThrow($stmt->stmts);
|
2016-06-30 00:30:27 +02:00
|
|
|
|
2016-08-24 23:06:20 +02:00
|
|
|
$has_leaving_statements = $has_ending_statements || ScopeChecker::doesAlwaysBreakOrContinue($stmt->stmts);
|
2016-06-13 07:48:29 +02:00
|
|
|
|
2016-08-23 05:02:03 +02:00
|
|
|
$negated_types = $negatable_if_types ? TypeChecker::negateTypes($negatable_if_types) : [];
|
2016-06-15 01:22:29 +02:00
|
|
|
|
2016-04-03 20:25:41 +02:00
|
|
|
$negated_if_types = $negated_types;
|
|
|
|
|
2016-06-13 07:48:29 +02:00
|
|
|
// if the if has an || in the conditional, we cannot easily reason about it
|
2016-06-28 21:28:05 +02:00
|
|
|
if ($reconcilable_if_types) {
|
|
|
|
$if_vars_in_scope_reconciled =
|
|
|
|
TypeChecker::reconcileKeyedTypes(
|
|
|
|
$reconcilable_if_types,
|
|
|
|
$if_context->vars_in_scope,
|
2016-08-25 01:00:44 +02:00
|
|
|
$this->checked_file_name,
|
2016-08-09 00:55:22 +02:00
|
|
|
$stmt->getLine(),
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->suppressed_issues
|
2016-06-28 21:28:05 +02:00
|
|
|
);
|
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
if ($if_vars_in_scope_reconciled === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-06-28 21:28:05 +02:00
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
$if_context->vars_in_scope = $if_vars_in_scope_reconciled;
|
2016-06-28 21:28:05 +02:00
|
|
|
$if_context->vars_possibly_in_scope = array_merge($reconcilable_if_types, $if_context->vars_possibly_in_scope);
|
2016-04-03 20:25:41 +02:00
|
|
|
}
|
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
$old_if_context = clone $if_context;
|
2016-06-20 22:18:31 +02:00
|
|
|
$context->vars_possibly_in_scope = array_merge($if_context->vars_possibly_in_scope, $context->vars_possibly_in_scope);
|
2016-04-04 06:17:19 +02:00
|
|
|
|
2016-08-23 05:02:03 +02:00
|
|
|
$else_context = clone $original_context;
|
|
|
|
|
|
|
|
if ($negated_types) {
|
|
|
|
$else_vars_reconciled = TypeChecker::reconcileKeyedTypes(
|
|
|
|
$negated_types,
|
|
|
|
$else_context->vars_in_scope,
|
2016-08-25 01:00:44 +02:00
|
|
|
$this->checked_file_name,
|
2016-08-23 05:02:03 +02:00
|
|
|
$stmt->getLine(),
|
|
|
|
$this->suppressed_issues
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($else_vars_reconciled === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$else_context->vars_in_scope = $else_vars_reconciled;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we calculate the vars redefined in a hypothetical else statement to determine
|
|
|
|
// which vars of the if we can safely change
|
|
|
|
$pre_assignment_else_redefined_vars = Context::getRedefinedVars($context, $else_context);
|
2016-08-23 02:06:28 +02:00
|
|
|
|
2016-08-24 00:09:34 +02:00
|
|
|
if ($this->check($stmt->stmts, $if_context, $loop_context) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-09-22 18:26:24 +02:00
|
|
|
$forced_new_vars = null;
|
2016-01-20 00:27:06 +01:00
|
|
|
$new_vars = null;
|
|
|
|
$new_vars_possibly_in_scope = [];
|
2016-04-04 06:17:19 +02:00
|
|
|
$redefined_vars = null;
|
2016-06-14 01:57:32 +02:00
|
|
|
$possibly_redefined_vars = [];
|
2016-04-27 00:17:47 +02:00
|
|
|
|
2016-08-24 04:09:06 +02:00
|
|
|
$redefined_loop_vars = null;
|
|
|
|
$possibly_redefined_loop_vars = [];
|
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
$updated_vars = [];
|
2016-08-24 04:09:06 +02:00
|
|
|
$updated_loop_vars = [];
|
2016-06-20 06:38:13 +02:00
|
|
|
|
|
|
|
$mic_drop = false;
|
2016-06-17 00:52:12 +02:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
if (count($stmt->stmts)) {
|
2016-08-24 04:09:06 +02:00
|
|
|
if (!$has_leaving_statements) {
|
2016-06-20 06:38:13 +02:00
|
|
|
$new_vars = array_diff_key($if_context->vars_in_scope, $context->vars_in_scope);
|
2016-04-30 20:14:22 +02:00
|
|
|
|
2016-09-22 18:26:24 +02:00
|
|
|
// if we have a check like if (!isset($a)) { $a = true; } we want to make sure $a is always set
|
|
|
|
foreach ($new_vars as $var_id => $type) {
|
|
|
|
if (isset($negated_if_types[$var_id]) && $negated_if_types[$var_id] === '!null') {
|
|
|
|
$forced_new_vars[$var_id] = Type::getMixed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
$redefined_vars = Context::getRedefinedVars($context, $if_context);
|
2016-04-30 20:14:22 +02:00
|
|
|
$possibly_redefined_vars = $redefined_vars;
|
2016-06-20 06:38:13 +02:00
|
|
|
}
|
|
|
|
elseif (!$stmt->else && !$stmt->elseifs && $negated_types) {
|
2016-08-09 00:55:22 +02:00
|
|
|
$context_vars_reconciled = TypeChecker::reconcileKeyedTypes(
|
|
|
|
$negated_types,
|
|
|
|
$context->vars_in_scope,
|
2016-08-25 01:00:44 +02:00
|
|
|
$this->checked_file_name,
|
2016-08-09 00:55:22 +02:00
|
|
|
$stmt->getLine(),
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->suppressed_issues
|
2016-08-09 00:55:22 +02:00
|
|
|
);
|
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
if ($context_vars_reconciled === false) {
|
|
|
|
return false;
|
2016-05-09 14:56:07 +02:00
|
|
|
}
|
2016-08-23 02:06:28 +02:00
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
$context->vars_in_scope = $context_vars_reconciled;
|
|
|
|
$mic_drop = true;
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
2016-06-20 06:38:13 +02:00
|
|
|
|
2016-08-24 04:09:06 +02:00
|
|
|
|
|
|
|
// update the parent context as necessary, but only if we can safely reason about type negation.
|
|
|
|
// We only update vars that changed both at the start of the if block and then again by an assignment
|
|
|
|
// in the if statement.
|
|
|
|
if ($negatable_if_types && !$mic_drop) {
|
|
|
|
$context->update(
|
|
|
|
$old_if_context,
|
|
|
|
$if_context,
|
|
|
|
$has_leaving_statements,
|
|
|
|
array_intersect(array_keys($pre_assignment_else_redefined_vars), array_keys($negatable_if_types)),
|
|
|
|
$updated_vars
|
|
|
|
);
|
2016-04-27 00:17:47 +02:00
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-08-24 04:09:06 +02:00
|
|
|
if (!$has_ending_statements) {
|
2016-06-20 06:38:13 +02:00
|
|
|
$vars = array_diff_key($if_context->vars_possibly_in_scope, $context->vars_possibly_in_scope);
|
2016-04-03 20:25:41 +02:00
|
|
|
|
2016-08-24 04:09:06 +02:00
|
|
|
if ($has_leaving_statements && $loop_context) {
|
|
|
|
$redefined_loop_vars = Context::getRedefinedVars($loop_context, $if_context);
|
|
|
|
$possibly_redefined_loop_vars = $redefined_loop_vars;
|
|
|
|
}
|
|
|
|
|
2016-05-16 22:12:02 +02:00
|
|
|
// if we're leaving this block, add vars to outer for loop scope
|
2016-08-24 04:09:06 +02:00
|
|
|
if ($has_leaving_statements) {
|
|
|
|
if ($loop_context) {
|
|
|
|
$loop_context->vars_possibly_in_scope = array_merge($loop_context->vars_possibly_in_scope, $vars);
|
|
|
|
}
|
2016-04-03 20:25:41 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$new_vars_possibly_in_scope = $vars;
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($stmt->elseifs as $elseif) {
|
2016-06-20 06:38:13 +02:00
|
|
|
$elseif_context = clone $original_context;
|
|
|
|
|
2016-04-03 20:25:41 +02:00
|
|
|
if ($negated_types) {
|
2016-08-09 00:55:22 +02:00
|
|
|
$elseif_vars_reconciled = TypeChecker::reconcileKeyedTypes(
|
|
|
|
$negated_types,
|
|
|
|
$elseif_context->vars_in_scope,
|
2016-08-25 01:00:44 +02:00
|
|
|
$this->checked_file_name,
|
2016-08-09 00:55:22 +02:00
|
|
|
$stmt->getLine(),
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->suppressed_issues
|
2016-08-09 00:55:22 +02:00
|
|
|
);
|
|
|
|
|
2016-06-06 02:25:16 +02:00
|
|
|
if ($elseif_vars_reconciled === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-20 06:38:13 +02:00
|
|
|
$elseif_context->vars_in_scope = $elseif_vars_reconciled;
|
2016-04-03 20:25:41 +02:00
|
|
|
}
|
|
|
|
|
2016-06-28 21:28:05 +02:00
|
|
|
if ($elseif->cond instanceof PhpParser\Node\Expr\BinaryOp) {
|
2016-10-14 00:27:23 +02:00
|
|
|
$reconcilable_elseif_types = $this->type_checker->getReconcilableTypeAssertions(
|
|
|
|
$elseif->cond,
|
|
|
|
$this->absolute_class,
|
|
|
|
$this->namespace,
|
|
|
|
$this->aliased_classes
|
|
|
|
);
|
|
|
|
$negatable_elseif_types = $this->type_checker->getNegatableTypeAssertions(
|
|
|
|
$elseif->cond,
|
|
|
|
$this->absolute_class,
|
|
|
|
$this->namespace,
|
|
|
|
$this->aliased_classes
|
|
|
|
);
|
2016-06-28 21:28:05 +02:00
|
|
|
}
|
|
|
|
else {
|
2016-10-14 00:27:23 +02:00
|
|
|
$reconcilable_elseif_types = $negatable_elseif_types = $this->type_checker->getTypeAssertions(
|
|
|
|
$elseif->cond,
|
|
|
|
$this->absolute_class,
|
|
|
|
$this->namespace,
|
|
|
|
$this->aliased_classes);
|
2016-06-28 21:28:05 +02:00
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-06-28 21:28:05 +02:00
|
|
|
$negated_elseif_types = $negatable_elseif_types
|
|
|
|
? TypeChecker::negateTypes($negatable_elseif_types)
|
2016-06-20 06:38:13 +02:00
|
|
|
: [];
|
|
|
|
|
|
|
|
$negated_types = array_merge($negated_types, $negated_elseif_types);
|
|
|
|
|
|
|
|
// if the elseif has an || in the conditional, we cannot easily reason about it
|
2016-10-03 17:39:32 +02:00
|
|
|
if ($reconcilable_elseif_types) {
|
2016-08-09 00:55:22 +02:00
|
|
|
$elseif_vars_reconciled = TypeChecker::reconcileKeyedTypes(
|
|
|
|
$reconcilable_elseif_types,
|
|
|
|
$elseif_context->vars_in_scope,
|
2016-08-25 01:00:44 +02:00
|
|
|
$this->checked_file_name,
|
2016-08-09 00:55:22 +02:00
|
|
|
$stmt->getLine(),
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->suppressed_issues
|
2016-08-09 00:55:22 +02:00
|
|
|
);
|
2016-04-03 20:25:41 +02:00
|
|
|
|
2016-06-06 02:25:16 +02:00
|
|
|
if ($elseif_vars_reconciled === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-20 06:38:13 +02:00
|
|
|
|
|
|
|
$elseif_context->vars_in_scope = $elseif_vars_reconciled;
|
2016-04-21 13:02:12 +02:00
|
|
|
}
|
2016-04-03 20:25:41 +02:00
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
// check the elseif
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->checkExpression($elseif->cond, $elseif_context) === false) {
|
2016-06-20 06:38:13 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$old_elseif_context = clone $elseif_context;
|
|
|
|
|
2016-08-24 00:09:34 +02:00
|
|
|
if ($this->check($elseif->stmts, $elseif_context, $loop_context) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
|
|
|
|
if (count($elseif->stmts)) {
|
2016-08-24 23:06:20 +02:00
|
|
|
// has a return/throw at end
|
|
|
|
$has_ending_statements = ScopeChecker::doesAlwaysReturnOrThrow($elseif->stmts);
|
|
|
|
|
|
|
|
$has_leaving_statements = $has_ending_statements || ScopeChecker::doesAlwaysBreakOrContinue($elseif->stmts);
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-08-24 05:41:06 +02:00
|
|
|
// update the parent context as necessary
|
|
|
|
$elseif_redefined_vars = Context::getRedefinedVars($original_context, $elseif_context);
|
|
|
|
|
2016-04-03 20:25:41 +02:00
|
|
|
if (!$has_leaving_statements) {
|
2016-08-24 04:09:06 +02:00
|
|
|
if ($new_vars === null) {
|
|
|
|
$new_vars = array_diff_key($elseif_context->vars_in_scope, $context->vars_in_scope);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
foreach ($new_vars as $new_var => $type) {
|
|
|
|
if (!isset($elseif_context->vars_in_scope[$new_var])) {
|
|
|
|
unset($new_vars[$new_var]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$new_vars[$new_var] = Type::combineUnionTypes($type, $elseif_context->vars_in_scope[$new_var]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-04 06:17:19 +02:00
|
|
|
if ($redefined_vars === null) {
|
|
|
|
$redefined_vars = $elseif_redefined_vars;
|
2016-04-30 20:14:22 +02:00
|
|
|
$possibly_redefined_vars = $redefined_vars;
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
foreach ($redefined_vars as $redefined_var => $type) {
|
|
|
|
if (!isset($elseif_redefined_vars[$redefined_var])) {
|
|
|
|
unset($redefined_vars[$redefined_var]);
|
|
|
|
}
|
2016-06-17 00:52:12 +02:00
|
|
|
else {
|
2016-06-17 22:05:28 +02:00
|
|
|
$redefined_vars[$redefined_var] = Type::combineUnionTypes($elseif_redefined_vars[$redefined_var], $type);
|
2016-06-17 00:52:12 +02:00
|
|
|
}
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
2016-04-30 20:14:22 +02:00
|
|
|
|
|
|
|
foreach ($elseif_redefined_vars as $var => $type) {
|
2016-06-16 02:16:40 +02:00
|
|
|
if ($type->isMixed()) {
|
|
|
|
$possibly_redefined_vars[$var] = $type;
|
2016-04-30 20:14:22 +02:00
|
|
|
}
|
|
|
|
else if (isset($possibly_redefined_vars[$var])) {
|
2016-06-16 02:16:40 +02:00
|
|
|
$possibly_redefined_vars[$var] = Type::combineUnionTypes($type, $possibly_redefined_vars[$var]);
|
2016-04-30 20:14:22 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$possibly_redefined_vars[$var] = $type;
|
|
|
|
}
|
|
|
|
}
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
2016-06-20 06:38:13 +02:00
|
|
|
|
2016-08-10 08:13:55 +02:00
|
|
|
if ($negatable_elseif_types) {
|
2016-08-24 04:09:06 +02:00
|
|
|
$context->update($old_elseif_context, $elseif_context, $has_leaving_statements, array_keys($negated_elseif_types), $updated_vars);
|
2016-04-27 00:17:47 +02:00
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-08-24 04:09:06 +02:00
|
|
|
if (!$has_ending_statements) {
|
2016-06-20 06:38:13 +02:00
|
|
|
$vars = array_diff_key($elseif_context->vars_possibly_in_scope, $context->vars_possibly_in_scope);
|
2016-04-03 20:25:41 +02:00
|
|
|
|
2016-05-16 22:12:02 +02:00
|
|
|
// if we're leaving this block, add vars to outer for loop scope
|
2016-08-24 04:09:06 +02:00
|
|
|
if ($has_leaving_statements && $loop_context) {
|
|
|
|
if ($redefined_loop_vars === null) {
|
|
|
|
$redefined_loop_vars = $elseif_redefined_vars;
|
|
|
|
$possibly_redefined_loop_vars = $redefined_loop_vars;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
foreach ($redefined_loop_vars as $redefined_var => $type) {
|
|
|
|
if (!isset($elseif_redefined_vars[$redefined_var])) {
|
|
|
|
unset($redefined_loop_vars[$redefined_var]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$redefined_loop_vars[$redefined_var] = Type::combineUnionTypes($elseif_redefined_vars[$redefined_var], $type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($elseif_redefined_vars as $var => $type) {
|
|
|
|
if ($type->isMixed()) {
|
|
|
|
$possibly_redefined_loop_vars[$var] = $type;
|
|
|
|
}
|
|
|
|
else if (isset($possibly_redefined_loop_vars[$var])) {
|
|
|
|
$possibly_redefined_loop_vars[$var] = Type::combineUnionTypes($type, $possibly_redefined_loop_vars[$var]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$possibly_redefined_loop_vars[$var] = $type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-24 00:09:34 +02:00
|
|
|
$loop_context->vars_possibly_in_scope = array_merge($vars, $loop_context->vars_possibly_in_scope);
|
2016-04-03 20:25:41 +02:00
|
|
|
}
|
2016-08-24 04:09:06 +02:00
|
|
|
elseif (!$has_leaving_statements) {
|
2016-04-03 20:25:41 +02:00
|
|
|
$new_vars_possibly_in_scope = array_merge($vars, $new_vars_possibly_in_scope);
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt->else) {
|
2016-08-23 06:34:37 +02:00
|
|
|
$else_context = clone $original_context;
|
|
|
|
|
|
|
|
if ($negated_types) {
|
|
|
|
$else_vars_reconciled = TypeChecker::reconcileKeyedTypes(
|
|
|
|
$negated_types,
|
|
|
|
$else_context->vars_in_scope,
|
2016-08-25 01:00:44 +02:00
|
|
|
$this->checked_file_name,
|
2016-08-23 06:34:37 +02:00
|
|
|
$stmt->getLine(),
|
|
|
|
$this->suppressed_issues
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($else_vars_reconciled === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$else_context->vars_in_scope = $else_vars_reconciled;
|
|
|
|
}
|
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
$old_else_context = clone $else_context;
|
2016-04-04 06:17:19 +02:00
|
|
|
|
2016-08-24 00:09:34 +02:00
|
|
|
if ($this->check($stmt->else->stmts, $else_context, $loop_context) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
|
|
|
|
if (count($stmt->else->stmts)) {
|
2016-08-24 23:06:20 +02:00
|
|
|
// has a return/throw at end
|
|
|
|
$has_ending_statements = ScopeChecker::doesAlwaysReturnOrThrow($stmt->else->stmts);
|
|
|
|
|
|
|
|
$has_leaving_statements = $has_ending_statements || ScopeChecker::doesAlwaysBreakOrContinue($stmt->else->stmts);
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-08-24 05:41:06 +02:00
|
|
|
/** @var Context $original_context */
|
|
|
|
$else_redefined_vars = Context::getRedefinedVars($original_context, $else_context);
|
|
|
|
|
2016-04-04 06:17:19 +02:00
|
|
|
// if it doesn't end in a return
|
2016-04-03 20:25:41 +02:00
|
|
|
if (!$has_leaving_statements) {
|
2016-08-24 04:09:06 +02:00
|
|
|
if ($new_vars === null) {
|
|
|
|
$new_vars = array_diff_key($else_context->vars_in_scope, $context->vars_in_scope);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
foreach ($new_vars as $new_var => $type) {
|
|
|
|
if (!isset($else_context->vars_in_scope[$new_var])) {
|
|
|
|
unset($new_vars[$new_var]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$new_vars[$new_var] = Type::combineUnionTypes($type, $else_context->vars_in_scope[$new_var]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-04 06:17:19 +02:00
|
|
|
if ($redefined_vars === null) {
|
|
|
|
$redefined_vars = $else_redefined_vars;
|
2016-04-30 20:14:22 +02:00
|
|
|
$possibly_redefined_vars = $redefined_vars;
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
foreach ($redefined_vars as $redefined_var => $type) {
|
|
|
|
if (!isset($else_redefined_vars[$redefined_var])) {
|
|
|
|
unset($redefined_vars[$redefined_var]);
|
|
|
|
}
|
2016-06-17 00:52:12 +02:00
|
|
|
else {
|
2016-06-17 22:05:28 +02:00
|
|
|
$redefined_vars[$redefined_var] = Type::combineUnionTypes($else_redefined_vars[$redefined_var], $type);
|
2016-06-17 00:52:12 +02:00
|
|
|
}
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
2016-04-30 20:14:22 +02:00
|
|
|
|
|
|
|
foreach ($else_redefined_vars as $var => $type) {
|
2016-06-16 02:16:40 +02:00
|
|
|
if ($type->isMixed()) {
|
|
|
|
$possibly_redefined_vars[$var] = $type;
|
2016-04-30 20:14:22 +02:00
|
|
|
}
|
|
|
|
else if (isset($possibly_redefined_vars[$var])) {
|
2016-06-16 02:16:40 +02:00
|
|
|
$possibly_redefined_vars[$var] = Type::combineUnionTypes($type, $possibly_redefined_vars[$var]);
|
2016-04-30 20:14:22 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$possibly_redefined_vars[$var] = $type;
|
|
|
|
}
|
|
|
|
}
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
2016-06-20 06:38:13 +02:00
|
|
|
|
|
|
|
// update the parent context as necessary
|
2016-06-28 21:28:05 +02:00
|
|
|
if ($negatable_if_types) {
|
2016-08-24 04:09:06 +02:00
|
|
|
$context->update($old_else_context, $else_context, $has_leaving_statements, array_keys($negatable_if_types), $updated_vars);
|
2016-06-17 00:52:12 +02:00
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-08-24 04:09:06 +02:00
|
|
|
if (!$has_ending_statements) {
|
2016-06-20 06:38:13 +02:00
|
|
|
$vars = array_diff_key($else_context->vars_possibly_in_scope, $context->vars_possibly_in_scope);
|
2016-04-03 20:25:41 +02:00
|
|
|
|
2016-08-24 04:09:06 +02:00
|
|
|
if ($has_leaving_statements && $loop_context) {
|
|
|
|
if ($redefined_loop_vars === null) {
|
|
|
|
$redefined_loop_vars = $else_redefined_vars;
|
|
|
|
$possibly_redefined_loop_vars = $redefined_loop_vars;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
foreach ($redefined_loop_vars as $redefined_var => $type) {
|
|
|
|
if (!isset($else_redefined_vars[$redefined_var])) {
|
|
|
|
unset($redefined_loop_vars[$redefined_var]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$redefined_loop_vars[$redefined_var] = Type::combineUnionTypes($else_redefined_vars[$redefined_var], $type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($else_redefined_vars as $var => $type) {
|
|
|
|
if ($type->isMixed()) {
|
|
|
|
$possibly_redefined_loop_vars[$var] = $type;
|
|
|
|
}
|
|
|
|
else if (isset($possibly_redefined_loop_vars[$var])) {
|
|
|
|
$possibly_redefined_loop_vars[$var] = Type::combineUnionTypes($type, $possibly_redefined_loop_vars[$var]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$possibly_redefined_loop_vars[$var] = $type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-24 00:09:34 +02:00
|
|
|
$loop_context->vars_possibly_in_scope = array_merge($vars, $loop_context->vars_possibly_in_scope);
|
2016-04-03 20:25:41 +02:00
|
|
|
}
|
2016-08-24 04:09:06 +02:00
|
|
|
elseif (!$has_leaving_statements) {
|
2016-04-03 20:25:41 +02:00
|
|
|
$new_vars_possibly_in_scope = array_merge($vars, $new_vars_possibly_in_scope);
|
|
|
|
}
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
2016-06-17 00:52:12 +02:00
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
$context->vars_possibly_in_scope = array_merge($context->vars_possibly_in_scope, $new_vars_possibly_in_scope);
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-09-22 18:26:24 +02:00
|
|
|
// vars can only be defined/redefined if there was an else (defined in every block)
|
2016-08-24 04:09:06 +02:00
|
|
|
if ($stmt->else) {
|
2016-09-22 18:26:24 +02:00
|
|
|
if ($new_vars) {
|
|
|
|
$context->vars_in_scope = array_merge($context->vars_in_scope, $new_vars);
|
|
|
|
}
|
|
|
|
|
2016-08-24 04:09:06 +02:00
|
|
|
if ($redefined_vars) {
|
|
|
|
foreach ($redefined_vars as $var => $type) {
|
|
|
|
$context->vars_in_scope[$var] = $type;
|
|
|
|
$updated_vars[$var] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($redefined_loop_vars && $loop_context) {
|
|
|
|
foreach ($redefined_loop_vars as $var => $type) {
|
|
|
|
$loop_context->vars_in_scope[$var] = $type;
|
|
|
|
$updated_loop_vars[$var] = true;
|
|
|
|
}
|
2016-04-03 20:25:41 +02:00
|
|
|
}
|
2016-04-03 02:27:36 +02:00
|
|
|
}
|
2016-09-22 18:26:24 +02:00
|
|
|
else {
|
|
|
|
if ($forced_new_vars) {
|
|
|
|
$context->vars_in_scope = array_merge($context->vars_in_scope, $forced_new_vars);
|
|
|
|
}
|
|
|
|
}
|
2016-04-30 20:14:22 +02:00
|
|
|
|
|
|
|
if ($possibly_redefined_vars) {
|
|
|
|
foreach ($possibly_redefined_vars as $var => $type) {
|
2016-06-20 06:38:13 +02:00
|
|
|
if (isset($context->vars_in_scope[$var]) && !isset($updated_vars[$var])) {
|
|
|
|
$context->vars_in_scope[$var] = Type::combineUnionTypes($context->vars_in_scope[$var], $type);
|
2016-04-27 00:17:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-24 04:09:06 +02:00
|
|
|
|
|
|
|
if ($possibly_redefined_loop_vars && $loop_context) {
|
|
|
|
foreach ($possibly_redefined_loop_vars as $var => $type) {
|
|
|
|
if (isset($loop_context->vars_in_scope[$var]) && !isset($updated_loop_vars[$var])) {
|
|
|
|
$loop_context->vars_in_scope[$var] = Type::combineUnionTypes($loop_context->vars_in_scope[$var], $type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* @return false|null
|
|
|
|
*/
|
2016-08-11 00:10:12 +02:00
|
|
|
protected function checkStatic(PhpParser\Node\Stmt\Static_ $stmt, Context $context)
|
2016-04-03 02:27:36 +02:00
|
|
|
{
|
|
|
|
foreach ($stmt->vars as $var) {
|
2016-09-13 01:30:57 +02:00
|
|
|
if ($var->default) {
|
|
|
|
if ($this->checkExpression($var->default, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
if ($this->check_variables) {
|
2016-10-14 00:27:23 +02:00
|
|
|
$context->vars_in_scope['$' . $var->name] = $var->default && isset($var->default->inferredType)
|
2016-10-09 23:54:58 +02:00
|
|
|
? $var->default->inferredType
|
|
|
|
: Type::getMixed();
|
2016-09-13 01:30:57 +02:00
|
|
|
|
2016-10-14 00:27:23 +02:00
|
|
|
$context->vars_possibly_in_scope['$' . $var->name] = true;
|
2016-10-09 23:54:58 +02:00
|
|
|
$this->registerVariable($var->name, $var->getLine());
|
2016-04-03 02:27:36 +02:00
|
|
|
}
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-04-27 00:42:48 +02:00
|
|
|
/**
|
2016-06-10 20:47:44 +02:00
|
|
|
* @return false|null
|
2016-04-27 00:42:48 +02:00
|
|
|
*/
|
2016-10-03 04:00:42 +02:00
|
|
|
protected function checkExpression(
|
|
|
|
PhpParser\Node\Expr $stmt,
|
|
|
|
Context $context,
|
|
|
|
$array_assignment = false,
|
|
|
|
Type\Union $assignment_key_type = null,
|
|
|
|
Type\Union $assignment_value_type = null,
|
|
|
|
$assignment_key_value = null
|
|
|
|
) {
|
2016-10-13 07:32:52 +02:00
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\Variable) {
|
|
|
|
if ($this->checkVariable($stmt, $context, null, null, $array_assignment) === false) {
|
2016-06-18 20:45:55 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-10-13 07:32:52 +02:00
|
|
|
elseif ($stmt instanceof PhpParser\Node\Expr\Assign) {
|
|
|
|
if ($this->checkAssignment($stmt, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elseif ($stmt instanceof PhpParser\Node\Expr\AssignOp) {
|
|
|
|
if ($this->checkAssignmentOperation($stmt, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elseif ($stmt instanceof PhpParser\Node\Expr\MethodCall) {
|
|
|
|
if ($this->checkMethodCall($stmt, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elseif ($stmt instanceof PhpParser\Node\Expr\StaticCall) {
|
|
|
|
if ($this->checkStaticCall($stmt, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\ConstFetch) {
|
2016-10-13 07:32:52 +02:00
|
|
|
if ($this->checkConstFetch($stmt) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Scalar\String_) {
|
2016-06-16 18:04:55 +02:00
|
|
|
$stmt->inferredType = Type::getString();
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Scalar\EncapsedStringPart) {
|
|
|
|
// do nothing
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Scalar\MagicConst) {
|
|
|
|
// do nothing
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Scalar\LNumber) {
|
2016-06-16 18:04:55 +02:00
|
|
|
$stmt->inferredType = Type::getInt();
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Scalar\DNumber) {
|
2016-06-16 18:04:55 +02:00
|
|
|
$stmt->inferredType = Type::getFloat();
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\UnaryMinus) {
|
2016-10-13 07:32:52 +02:00
|
|
|
if ($this->checkExpression($stmt->expr, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\UnaryPlus) {
|
2016-10-13 07:32:52 +02:00
|
|
|
if ($this->checkExpression($stmt->expr, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Isset_) {
|
2016-05-16 05:06:03 +02:00
|
|
|
foreach ($stmt->vars as $isset_var) {
|
|
|
|
if ($isset_var instanceof PhpParser\Node\Expr\PropertyFetch &&
|
|
|
|
$isset_var->var instanceof PhpParser\Node\Expr\Variable &&
|
|
|
|
$isset_var->var->name === 'this' &&
|
|
|
|
is_string($isset_var->name)
|
|
|
|
) {
|
2016-10-14 00:27:23 +02:00
|
|
|
$var_id = '$this->' . $isset_var->name;
|
2016-06-20 06:38:13 +02:00
|
|
|
$context->vars_in_scope[$var_id] = Type::getMixed();
|
|
|
|
$context->vars_possibly_in_scope[$var_id] = true;
|
2016-05-16 05:06:03 +02:00
|
|
|
}
|
|
|
|
}
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\ClassConstFetch) {
|
2016-10-13 07:32:52 +02:00
|
|
|
if ($this->checkClassConstFetch($stmt, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\PropertyFetch) {
|
2016-10-13 07:32:52 +02:00
|
|
|
if ($this->checkPropertyFetch($stmt, $context, $array_assignment) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\StaticPropertyFetch) {
|
2016-10-13 07:32:52 +02:00
|
|
|
if ($this->checkStaticPropertyFetch($stmt, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\BitwiseNot) {
|
2016-10-13 07:32:52 +02:00
|
|
|
if ($this->checkExpression($stmt->expr, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\BinaryOp) {
|
2016-10-13 07:32:52 +02:00
|
|
|
if ($this->checkBinaryOp($stmt, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\PostInc) {
|
2016-10-13 07:32:52 +02:00
|
|
|
if ($this->checkExpression($stmt->var, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\PostDec) {
|
2016-10-13 07:32:52 +02:00
|
|
|
if ($this->checkExpression($stmt->var, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\PreInc) {
|
2016-10-13 07:32:52 +02:00
|
|
|
if ($this->checkExpression($stmt->var, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\PreDec) {
|
2016-10-13 07:32:52 +02:00
|
|
|
if ($this->checkExpression($stmt->var, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\New_) {
|
2016-10-13 07:32:52 +02:00
|
|
|
if ($this->checkNew($stmt, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Array_) {
|
2016-10-13 07:32:52 +02:00
|
|
|
if ($this->checkArray($stmt, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Scalar\Encapsed) {
|
2016-10-13 07:32:52 +02:00
|
|
|
if ($this->checkEncapsulatedString($stmt, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\FuncCall) {
|
2016-10-13 07:32:52 +02:00
|
|
|
if ($this->checkFunctionCall($stmt, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Ternary) {
|
2016-10-13 07:32:52 +02:00
|
|
|
if ($this->checkTernary($stmt, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\BooleanNot) {
|
2016-10-13 07:32:52 +02:00
|
|
|
if ($this->checkBooleanNot($stmt, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Empty_) {
|
2016-10-13 07:32:52 +02:00
|
|
|
if ($this->checkEmpty($stmt, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Closure) {
|
2016-08-11 00:10:12 +02:00
|
|
|
$closure_checker = new ClosureChecker($stmt, $this->source);
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->checkClosureUses($stmt, $context) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-01-26 20:11:56 +01:00
|
|
|
|
2016-08-14 03:14:32 +02:00
|
|
|
$use_context = new Context($this->file_name, $context->self);
|
2016-05-16 05:06:03 +02:00
|
|
|
|
2016-08-11 00:10:12 +02:00
|
|
|
if (!$this->is_static) {
|
2016-10-09 23:54:58 +02:00
|
|
|
$this_class = ClassLikeChecker::getThisClass();
|
|
|
|
$this_class = $this_class && ClassChecker::classExtends($this_class, $this->absolute_class)
|
|
|
|
? $this_class
|
|
|
|
: $context->self;
|
2016-06-16 02:16:40 +02:00
|
|
|
|
2016-06-20 18:38:31 +02:00
|
|
|
if ($this_class) {
|
2016-10-14 00:27:23 +02:00
|
|
|
$use_context->vars_in_scope['$this'] = new Type\Union([new Type\Atomic($this_class)]);
|
2016-06-20 18:38:31 +02:00
|
|
|
}
|
2016-05-16 05:06:03 +02:00
|
|
|
}
|
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
foreach ($context->vars_in_scope as $var => $type) {
|
2016-10-14 00:27:23 +02:00
|
|
|
if (strpos($var, '$this->') === 0) {
|
2016-08-10 08:13:55 +02:00
|
|
|
$use_context->vars_in_scope[$var] = clone $type;
|
2016-05-16 05:06:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
foreach ($context->vars_possibly_in_scope as $var => $type) {
|
2016-10-14 00:27:23 +02:00
|
|
|
if (strpos($var, '$this->') === 0) {
|
2016-08-11 01:21:03 +02:00
|
|
|
$use_context->vars_possibly_in_scope[$var] = true;
|
2016-06-02 23:44:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-16 05:06:03 +02:00
|
|
|
foreach ($stmt->uses as $use) {
|
2016-10-14 00:27:23 +02:00
|
|
|
$use_context->vars_in_scope['$' . $use->var] = isset($context->vars_in_scope['$' . $use->var]) ? clone $context->vars_in_scope['$' . $use->var] : Type::getMixed();
|
|
|
|
$use_context->vars_possibly_in_scope['$' . $use->var] = true;
|
2016-05-16 05:06:03 +02:00
|
|
|
}
|
|
|
|
|
2016-08-11 00:10:12 +02:00
|
|
|
$closure_checker->check($use_context, $this->check_methods);
|
2016-05-16 05:06:03 +02:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\ArrayDimFetch) {
|
2016-10-13 07:32:52 +02:00
|
|
|
if ($this->checkArrayAccess($stmt, $context, $array_assignment, $assignment_key_type, $assignment_value_type, $assignment_key_value) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Cast\Int_) {
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->checkExpression($stmt->expr, $context) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-06-16 18:04:55 +02:00
|
|
|
$stmt->inferredType = Type::getInt();
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Cast\Double) {
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->checkExpression($stmt->expr, $context) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-08-14 18:07:19 +02:00
|
|
|
$stmt->inferredType = Type::getFloat();
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Cast\Bool_) {
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->checkExpression($stmt->expr, $context) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-06-16 18:04:55 +02:00
|
|
|
$stmt->inferredType = Type::getBool();
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Cast\String_) {
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->checkExpression($stmt->expr, $context) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-06-16 18:04:55 +02:00
|
|
|
$stmt->inferredType = Type::getString();
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Cast\Object_) {
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->checkExpression($stmt->expr, $context) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-06-16 18:04:55 +02:00
|
|
|
$stmt->inferredType = Type::getObject();
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Cast\Array_) {
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->checkExpression($stmt->expr, $context) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-06-16 18:04:55 +02:00
|
|
|
$stmt->inferredType = Type::getArray();
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Clone_) {
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->checkExpression($stmt->expr, $context) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-06-16 18:04:55 +02:00
|
|
|
if (property_exists($stmt->expr, 'inferredType')) {
|
|
|
|
$stmt->inferredType = $stmt->expr->inferredType;
|
2016-05-06 03:20:15 +02:00
|
|
|
}
|
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Instanceof_) {
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->checkExpression($stmt->expr, $context) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
|
|
|
|
if ($stmt->class instanceof PhpParser\Node\Name && !in_array($stmt->class->parts[0], ['self', 'static', 'parent'])) {
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->check_classes) {
|
2016-10-02 19:05:49 +02:00
|
|
|
$absolute_class = ClassLikeChecker::getAbsoluteClassFromName($stmt->class, $this->namespace, $this->aliased_classes);
|
|
|
|
|
|
|
|
if (ClassLikeChecker::checkAbsoluteClassOrInterface($absolute_class, $this->checked_file_name, $stmt->getLine(), $this->suppressed_issues) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
}
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Exit_) {
|
|
|
|
// do nothing
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Include_) {
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->checkInclude($stmt, $context);
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Eval_) {
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->check_classes = false;
|
|
|
|
$this->check_variables = false;
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->checkExpression($stmt->expr, $context) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\AssignRef) {
|
|
|
|
if ($stmt->var instanceof PhpParser\Node\Expr\Variable) {
|
2016-10-09 23:54:58 +02:00
|
|
|
if (is_string($stmt->var->name)) {
|
2016-10-14 00:27:23 +02:00
|
|
|
$context->vars_in_scope['$' . $stmt->var->name] = Type::getMixed();
|
|
|
|
$context->vars_possibly_in_scope['$' . $stmt->var->name] = true;
|
2016-10-09 23:54:58 +02:00
|
|
|
$this->registerVariable($stmt->var->name, $stmt->var->getLine());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if ($this->checkExpression($stmt->var->name, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
} else {
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->checkExpression($stmt->var, $context) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->checkExpression($stmt->expr, $context) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\ErrorSuppress) {
|
|
|
|
// do nothing
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\ShellExec) {
|
2016-06-26 21:18:40 +02:00
|
|
|
if (IssueBuffer::accepts(
|
2016-08-25 01:00:44 +02:00
|
|
|
new ForbiddenCode('Use of shell_exec', $this->checked_file_name, $stmt->getLine()),
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->suppressed_issues
|
2016-06-06 02:25:16 +02:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Print_) {
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->checkExpression($stmt->expr, $context) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} else {
|
2016-08-25 01:00:44 +02:00
|
|
|
var_dump('Unrecognised expression in ' . $this->checked_file_name);
|
2016-01-20 00:27:06 +01:00
|
|
|
var_dump($stmt);
|
|
|
|
}
|
2016-10-13 07:32:52 +02:00
|
|
|
|
|
|
|
foreach (Config::getInstance()->getPlugins() as $plugin) {
|
|
|
|
if ($plugin->checkExpression($stmt, $context, $this->checked_file_name) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-04-27 00:42:48 +02:00
|
|
|
/**
|
2016-06-10 20:47:44 +02:00
|
|
|
* @return false|null
|
2016-04-27 00:42:48 +02:00
|
|
|
*/
|
2016-10-09 23:54:58 +02:00
|
|
|
protected function checkVariable(PhpParser\Node\Expr\Variable $stmt, Context $context, $passed_by_reference = false, Type\Union $by_ref_type = null, $array_assignment = false)
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->is_static && $stmt->name === 'this') {
|
2016-06-26 21:18:40 +02:00
|
|
|
if (IssueBuffer::accepts(
|
2016-08-25 01:00:44 +02:00
|
|
|
new InvalidStaticVariable('Invalid reference to $this in a static context', $this->checked_file_name, $stmt->getLine()),
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->suppressed_issues
|
2016-06-06 02:25:16 +02:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-08-11 00:10:12 +02:00
|
|
|
if (!$this->check_variables) {
|
2016-06-16 18:04:55 +02:00
|
|
|
$stmt->inferredType = Type::getMixed();
|
2016-04-30 23:31:49 +02:00
|
|
|
|
2016-10-14 00:27:23 +02:00
|
|
|
if (is_string($stmt->name) && !isset($context->vars_in_scope['$' . $stmt->name])) {
|
|
|
|
$context->vars_in_scope['$' . $stmt->name] = Type::getMixed();
|
|
|
|
$context->vars_possibly_in_scope['$' . $stmt->name] = true;
|
2016-04-30 23:31:49 +02:00
|
|
|
}
|
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-24 18:11:17 +02:00
|
|
|
if (in_array($stmt->name, ['_SERVER', '_GET', '_POST', '_COOKIE', '_REQUEST', '_FILES', '_ENV', 'GLOBALS', 'argv'])) {
|
2016-01-20 00:27:06 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-16 05:06:03 +02:00
|
|
|
if (!is_string($stmt->name)) {
|
2016-08-11 00:10:12 +02:00
|
|
|
return $this->checkExpression($stmt->name, $context);
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-05-24 18:11:17 +02:00
|
|
|
if ($stmt->name === 'this') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
if ($passed_by_reference && $by_ref_type) {
|
|
|
|
$this->assignByRefParam($stmt, $by_ref_type, $context);
|
2016-05-16 05:06:03 +02:00
|
|
|
return;
|
2016-05-10 17:46:36 +02:00
|
|
|
}
|
|
|
|
|
2016-10-14 00:27:23 +02:00
|
|
|
$var_name = '$' . $stmt->name;
|
2016-05-10 17:46:36 +02:00
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
if (!isset($context->vars_in_scope[$var_name])) {
|
2016-08-11 00:10:12 +02:00
|
|
|
if (!isset($context->vars_possibly_in_scope[$var_name]) || !isset($this->all_vars[$var_name])) {
|
2016-06-15 01:22:29 +02:00
|
|
|
if ($array_assignment) {
|
|
|
|
// if we're in an array assignment, let's assign the variable
|
|
|
|
// because PHP allows it
|
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
$context->vars_in_scope[$var_name] = Type::getArray();
|
|
|
|
$context->vars_possibly_in_scope[$var_name] = true;
|
2016-06-15 01:22:29 +02:00
|
|
|
$this->registerVariable($var_name, $stmt->getLine());
|
|
|
|
}
|
|
|
|
else {
|
2016-06-27 19:22:16 +02:00
|
|
|
IssueBuffer::add(
|
2016-10-14 00:27:23 +02:00
|
|
|
new UndefinedVariable('Cannot find referenced variable ' . $var_name, $this->checked_file_name, $stmt->getLine())
|
2016-06-27 19:22:16 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
return false;
|
2016-05-10 17:46:36 +02:00
|
|
|
}
|
2016-06-06 02:25:16 +02:00
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-08-11 00:10:12 +02:00
|
|
|
if (isset($this->all_vars[$var_name]) && !isset($this->warn_vars[$var_name])) {
|
|
|
|
$this->warn_vars[$var_name] = true;
|
2016-06-06 02:25:16 +02:00
|
|
|
|
2016-06-26 21:18:40 +02:00
|
|
|
if (IssueBuffer::accepts(
|
2016-06-14 01:57:32 +02:00
|
|
|
new PossiblyUndefinedVariable(
|
2016-10-14 00:27:23 +02:00
|
|
|
'Possibly undefined variable ' . $var_name .', first seen on line ' . $this->all_vars[$var_name],
|
2016-08-25 01:00:44 +02:00
|
|
|
$this->checked_file_name,
|
2016-06-06 02:25:16 +02:00
|
|
|
$stmt->getLine()
|
2016-07-22 19:29:46 +02:00
|
|
|
),
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->suppressed_issues
|
2016-06-06 02:25:16 +02:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-16 05:06:03 +02:00
|
|
|
}
|
2016-01-26 20:11:56 +01:00
|
|
|
|
2016-05-16 05:06:03 +02:00
|
|
|
} else {
|
2016-06-20 06:38:13 +02:00
|
|
|
$stmt->inferredType = $context->vars_in_scope[$var_name];
|
2016-04-30 20:14:22 +02:00
|
|
|
}
|
2016-05-16 05:06:03 +02:00
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* @return Type\Union|null
|
|
|
|
*/
|
2016-09-12 17:32:44 +02:00
|
|
|
public static function getSimpleType(PhpParser\Node\Expr $stmt)
|
|
|
|
{
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\ConstFetch) {
|
|
|
|
// @todo support this
|
|
|
|
}
|
|
|
|
elseif ($stmt instanceof PhpParser\Node\Expr\ClassConstFetch) {
|
|
|
|
// @todo support this as well
|
|
|
|
}
|
|
|
|
elseif ($stmt instanceof PhpParser\Node\Scalar\String_) {
|
|
|
|
return Type::getString();
|
|
|
|
}
|
|
|
|
elseif ($stmt instanceof PhpParser\Node\Scalar\LNumber) {
|
|
|
|
return Type::getInt();
|
|
|
|
}
|
|
|
|
elseif ($stmt instanceof PhpParser\Node\Scalar\DNumber) {
|
|
|
|
return Type::getFloat();
|
|
|
|
}
|
|
|
|
elseif ($stmt instanceof PhpParser\Node\Expr\Array_) {
|
|
|
|
return Type::getArray();
|
|
|
|
}
|
|
|
|
elseif ($stmt instanceof PhpParser\Node\Expr\Cast\Int_) {
|
|
|
|
return Type::getInt();
|
|
|
|
}
|
|
|
|
elseif ($stmt instanceof PhpParser\Node\Expr\Cast\Double) {
|
|
|
|
return Type::getFloat();
|
|
|
|
}
|
|
|
|
elseif ($stmt instanceof PhpParser\Node\Expr\Cast\Bool_) {
|
|
|
|
return Type::getBool();
|
|
|
|
}
|
|
|
|
elseif ($stmt instanceof PhpParser\Node\Expr\Cast\String_) {
|
|
|
|
return Type::getString();
|
|
|
|
}
|
|
|
|
elseif ($stmt instanceof PhpParser\Node\Expr\Cast\Object_) {
|
|
|
|
return Type::getObject();
|
|
|
|
}
|
|
|
|
elseif ($stmt instanceof PhpParser\Node\Expr\Cast\Array_) {
|
|
|
|
return Type::getArray();
|
|
|
|
}
|
2016-10-02 16:30:44 +02:00
|
|
|
elseif ($stmt instanceof PhpParser\Node\Expr\UnaryMinus || $stmt instanceof PhpParser\Node\Expr\UnaryPlus) {
|
|
|
|
return self::getSimpleType($stmt->expr);
|
|
|
|
}
|
2016-09-12 17:32:44 +02:00
|
|
|
else {
|
2016-09-13 01:44:33 +02:00
|
|
|
var_dump('Unrecognised default property type');
|
2016-09-12 17:32:44 +02:00
|
|
|
var_dump($stmt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-15 18:22:43 +02:00
|
|
|
/**
|
|
|
|
* @param PhpParser\Node\Expr\Variable|PhpParser\Node\Expr\PropertyFetch $stmt
|
2016-10-09 23:54:58 +02:00
|
|
|
* @param Type\Union $by_ref_type
|
2016-08-15 18:22:43 +02:00
|
|
|
* @param Context $context
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-10-09 23:54:58 +02:00
|
|
|
protected function assignByRefParam(PhpParser\Node\Expr $stmt, Type\Union $by_ref_type, Context $context)
|
2016-05-16 05:06:03 +02:00
|
|
|
{
|
2016-10-14 00:27:23 +02:00
|
|
|
$var_id = self::getVarId($stmt, $this->absolute_class, $this->namespace, $this->aliased_classes);
|
2016-04-30 20:14:22 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
if ($var_id && !isset($context->vars_in_scope[$var_id])) {
|
2016-08-15 18:22:43 +02:00
|
|
|
$context->vars_possibly_in_scope[$var_id] = true;
|
|
|
|
$this->registerVariable($var_id, $stmt->getLine());
|
2016-05-10 17:46:36 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\PropertyFetch
|
|
|
|
&& $this->source instanceof FunctionLikeChecker
|
|
|
|
&& ($this_method_id = $this->source->getMethodId())
|
|
|
|
) {
|
2016-08-11 00:10:12 +02:00
|
|
|
if (!isset(self::$this_assignments[$this_method_id])) {
|
|
|
|
self::$this_assignments[$this_method_id] = [];
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
2016-01-26 20:11:56 +01:00
|
|
|
|
2016-08-11 00:10:12 +02:00
|
|
|
self::$this_assignments[$this_method_id][$stmt->name] = Type::getMixed();
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
}
|
2016-05-16 05:06:03 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
$stmt->inferredType = $by_ref_type;
|
|
|
|
|
|
|
|
$context->vars_in_scope[$var_id] = $by_ref_type;
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* @return false|null
|
|
|
|
*/
|
2016-08-11 00:10:12 +02:00
|
|
|
protected function checkPropertyFetch(PhpParser\Node\Expr\PropertyFetch $stmt, Context $context, $array_assignment = false)
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
2016-06-06 02:25:16 +02:00
|
|
|
if (!is_string($stmt->name)) {
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->checkExpression($stmt->name, $context) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
$var_id = null;
|
|
|
|
|
|
|
|
if (!($stmt->var instanceof PhpParser\Node\Expr\Variable)) {
|
|
|
|
if ($this->checkExpression($stmt->var, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->checkVariable($stmt->var, $context) === false) {
|
2016-08-05 23:45:47 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-10-09 23:54:58 +02:00
|
|
|
}
|
2016-08-05 23:45:47 +02:00
|
|
|
|
2016-10-14 00:27:23 +02:00
|
|
|
$stmt_var_id = self::getVarId($stmt->var, $this->absolute_class, $this->namespace, $this->aliased_classes);
|
|
|
|
$var_id = self::getVarId($stmt, $this->absolute_class, $this->namespace, $this->aliased_classes);
|
2016-08-07 02:27:13 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
$var_name = is_string($stmt->name) ? $stmt->name : null;
|
2016-08-07 02:27:13 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
$stmt_var_type = null;
|
2016-08-07 02:27:13 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
if ($var_id && isset($context->vars_in_scope[$var_id])) {
|
|
|
|
// we don't need to check anything
|
|
|
|
$stmt->inferredType = $context->vars_in_scope[$var_id];
|
|
|
|
return;
|
|
|
|
}
|
2016-08-07 02:27:13 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
if ($stmt_var_id && isset($context->vars_in_scope[$stmt_var_id])) {
|
|
|
|
$stmt_var_type = $context->vars_in_scope[$stmt_var_id];
|
|
|
|
}
|
|
|
|
elseif (isset($stmt->var->inferredType)) {
|
|
|
|
/** @var Type\Union */
|
|
|
|
$stmt_var_type = $stmt->var->inferredType;
|
|
|
|
}
|
2016-08-07 02:27:13 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
if (!$stmt_var_type) {
|
|
|
|
return;
|
|
|
|
}
|
2016-08-07 02:27:13 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
if ($stmt_var_type->isNull()) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new NullReference(
|
2016-10-14 00:27:23 +02:00
|
|
|
'Cannot get property on null variable ' . $stmt_var_id,
|
2016-10-09 23:54:58 +02:00
|
|
|
$this->checked_file_name,
|
|
|
|
$stmt->getLine()
|
|
|
|
),
|
|
|
|
$this->suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-07 02:27:13 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-08-05 23:45:47 +02:00
|
|
|
|
2016-10-11 16:47:31 +02:00
|
|
|
if ($stmt_var_type->isEmpty()) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new NullReference(
|
2016-10-14 00:27:23 +02:00
|
|
|
'Cannot fetch property on empty var ' . $stmt_var_id,
|
2016-10-11 16:47:31 +02:00
|
|
|
$this->checked_file_name,
|
|
|
|
$stmt->getLine()
|
|
|
|
),
|
|
|
|
$this->suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
if ($stmt_var_type->isMixed()) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MixedPropertyFetch(
|
2016-10-14 00:27:23 +02:00
|
|
|
'Cannot fetch property on mixed var ' . $stmt_var_id,
|
2016-10-09 23:54:58 +02:00
|
|
|
$this->checked_file_name,
|
|
|
|
$stmt->getLine()
|
|
|
|
),
|
|
|
|
$this->suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-07 02:27:13 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-09-17 17:57:44 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
if ($stmt_var_type->isNullable()) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new NullPropertyFetch(
|
2016-10-14 00:27:23 +02:00
|
|
|
'Cannot get property on possibly null variable ' . $stmt_var_id,
|
2016-10-09 23:54:58 +02:00
|
|
|
$this->checked_file_name,
|
|
|
|
$stmt->getLine()
|
|
|
|
),
|
|
|
|
$this->suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-05 23:45:47 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
$stmt->inferredType = Type::getNull();
|
|
|
|
}
|
2016-09-17 17:57:44 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
if (!is_string($stmt->name)) {
|
|
|
|
return;
|
|
|
|
}
|
2016-08-05 23:45:47 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
foreach ($stmt_var_type->types as $lhs_type_part) {
|
|
|
|
if ($lhs_type_part->isNull()) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-05-09 14:56:07 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
if (!$lhs_type_part->isObjectType()) {
|
2016-10-14 00:27:23 +02:00
|
|
|
$stmt_var_id = self::getVarId($stmt->var, $this->absolute_class, $this->namespace, $this->aliased_classes);
|
2016-08-07 17:35:27 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidPropertyFetch(
|
|
|
|
'Cannot fetch property on non-object ' . $stmt_var_id . ' of type ' . $lhs_type_part,
|
|
|
|
$this->checked_file_name,
|
|
|
|
$stmt->getLine()
|
|
|
|
),
|
|
|
|
$this->suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-07 17:35:27 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
continue;
|
|
|
|
}
|
2016-08-07 17:35:27 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
// stdClass and SimpleXMLElement are special cases where we cannot infer the return types
|
|
|
|
// but we don't want to throw an error
|
|
|
|
// Hack has a similar issue: https://github.com/facebook/hhvm/issues/5164
|
2016-10-11 05:52:05 +02:00
|
|
|
if ($lhs_type_part->isObject() || in_array(strtolower($lhs_type_part->value), ['stdclass', 'simplexmlelement', 'dateinterval', 'domdocument', 'domnode'])) {
|
2016-10-09 23:54:58 +02:00
|
|
|
$stmt->inferredType = Type::getMixed();
|
|
|
|
continue;
|
|
|
|
}
|
2016-08-07 17:35:27 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
if (method_exists((string) $lhs_type_part, '__get')) {
|
2016-10-11 19:45:19 +02:00
|
|
|
$stmt->inferredType = Type::getMixed();
|
2016-10-09 23:54:58 +02:00
|
|
|
continue;
|
|
|
|
}
|
2016-08-07 02:27:13 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
if (!ClassChecker::classExists($lhs_type_part->value)) {
|
|
|
|
if (InterfaceChecker::interfaceExists($lhs_type_part->value)) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new NoInterfaceProperties(
|
|
|
|
'Interfaces cannot have properties',
|
|
|
|
$this->checked_file_name,
|
|
|
|
$stmt->getLine()
|
|
|
|
),
|
|
|
|
$this->suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-07 02:27:13 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
continue;
|
|
|
|
}
|
2016-08-07 02:27:13 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UndefinedClass(
|
|
|
|
'Cannot get properties of undefined class ' . $lhs_type_part->value,
|
|
|
|
$this->checked_file_name,
|
|
|
|
$stmt->getLine()
|
|
|
|
),
|
|
|
|
$this->suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-24 05:51:01 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
continue;
|
|
|
|
}
|
2016-08-07 02:27:13 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
if ($var_name === 'this'
|
|
|
|
|| $lhs_type_part->value === $context->self
|
|
|
|
|| ($this->source->getSource() instanceof TraitChecker && $lhs_type_part->value === $this->source->getAbsoluteClass())
|
|
|
|
) {
|
|
|
|
$class_visibility = \ReflectionProperty::IS_PRIVATE;
|
|
|
|
}
|
|
|
|
elseif (ClassChecker::classExtends($lhs_type_part->value, $context->self)) {
|
|
|
|
$class_visibility = \ReflectionProperty::IS_PROTECTED;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$class_visibility = \ReflectionProperty::IS_PUBLIC;
|
|
|
|
}
|
2016-08-07 02:27:13 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
$class_properties = ClassLikeChecker::getInstancePropertiesForClass(
|
|
|
|
$lhs_type_part->value,
|
|
|
|
$class_visibility
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!$class_properties || !isset($class_properties[$stmt->name])) {
|
2016-10-14 00:27:23 +02:00
|
|
|
if ($stmt_var_id === '$this') {
|
2016-10-09 23:54:58 +02:00
|
|
|
if (IssueBuffer::accepts(
|
2016-10-11 01:29:38 +02:00
|
|
|
new UndefinedThisPropertyFetch(
|
2016-10-14 00:27:23 +02:00
|
|
|
'Instance property ' . $lhs_type_part->value .'::$' . $stmt->name . ' is not defined',
|
2016-10-09 23:54:58 +02:00
|
|
|
$this->checked_file_name,
|
|
|
|
$stmt->getLine()
|
|
|
|
),
|
|
|
|
$this->suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
2016-08-05 23:45:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2016-10-09 23:54:58 +02:00
|
|
|
if (IssueBuffer::accepts(
|
2016-10-11 01:29:38 +02:00
|
|
|
new UndefinedPropertyFetch(
|
2016-10-14 00:27:23 +02:00
|
|
|
'Instance property ' . $lhs_type_part->value .'::$' . $stmt->name . ' is not defined',
|
2016-10-09 23:54:58 +02:00
|
|
|
$this->checked_file_name,
|
|
|
|
$stmt->getLine()
|
|
|
|
),
|
|
|
|
$this->suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-05 23:45:47 +02:00
|
|
|
}
|
2016-10-09 23:54:58 +02:00
|
|
|
|
|
|
|
if ($var_id) {
|
|
|
|
$context->vars_in_scope[$var_id] = Type::getMixed();
|
|
|
|
}
|
|
|
|
|
|
|
|
$stmt->inferredType = Type::getMixed();
|
|
|
|
|
|
|
|
return;
|
2016-08-05 23:45:47 +02:00
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
if (isset($stmt->inferredType)) {
|
|
|
|
$stmt->inferredType = Type::combineUnionTypes(clone $class_properties[$stmt->name], $stmt->inferredType);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$stmt->inferredType = $class_properties[$stmt->name];
|
|
|
|
}
|
2016-06-06 02:25:16 +02:00
|
|
|
}
|
2016-05-16 05:06:03 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
if ($var_id) {
|
2016-10-10 07:35:12 +02:00
|
|
|
$context->vars_in_scope[$var_id] = isset($stmt->inferredType) ? $stmt->inferredType : Type::getMixed();
|
2016-10-09 23:54:58 +02:00
|
|
|
}
|
2016-06-06 02:25:16 +02:00
|
|
|
}
|
2016-05-16 05:06:03 +02:00
|
|
|
|
2016-08-07 02:27:13 +02:00
|
|
|
/**
|
|
|
|
* @param PhpParser\Node\Expr\PropertyFetch|PhpParser\Node\Stmt\PropertyProperty $stmt
|
|
|
|
* @param string $prop_name
|
|
|
|
* @param Type\Union $assignment_type
|
|
|
|
* @param Context $context
|
|
|
|
* @return false|null
|
|
|
|
*/
|
2016-08-11 00:10:12 +02:00
|
|
|
protected function checkPropertyAssignment($stmt, $prop_name, Type\Union $assignment_type, Context $context)
|
2016-08-07 02:27:13 +02:00
|
|
|
{
|
|
|
|
$class_property_types = [];
|
2016-06-06 02:25:16 +02:00
|
|
|
|
2016-08-07 02:27:13 +02:00
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\PropertyProperty) {
|
2016-08-13 20:20:46 +02:00
|
|
|
$class_properties = ClassLikeChecker::getInstancePropertiesForClass($context->self, \ReflectionProperty::IS_PRIVATE);
|
2016-06-06 02:25:16 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
$class_property_types[] = clone $class_properties[$prop_name];
|
2016-06-15 01:22:29 +02:00
|
|
|
|
2016-08-07 02:27:13 +02:00
|
|
|
$var_id = 'this->' . $prop_name;
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
2016-10-09 23:54:58 +02:00
|
|
|
elseif ($stmt->var instanceof PhpParser\Node\Expr\Variable) {
|
2016-10-14 00:27:23 +02:00
|
|
|
if (!isset($context->vars_in_scope['$' . $stmt->var->name])) {
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->checkVariable($stmt->var, $context) === false) {
|
2016-08-07 18:23:29 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-14 00:27:23 +02:00
|
|
|
$stmt->var->inferredType = $context->vars_in_scope['$' . $stmt->var->name];
|
2016-10-13 07:32:52 +02:00
|
|
|
|
2016-10-14 00:27:23 +02:00
|
|
|
$lhs_type = $context->vars_in_scope['$' . $stmt->var->name];
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-08-13 20:20:46 +02:00
|
|
|
if ($stmt->var->name === 'this' && !$this->source->getClassLikeChecker()) {
|
2016-08-07 22:13:36 +02:00
|
|
|
if (IssueBuffer::accepts(
|
2016-08-25 01:00:44 +02:00
|
|
|
new InvalidScope('Cannot use $this when not inside class', $this->checked_file_name, $stmt->getLine()),
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->suppressed_issues
|
2016-08-07 22:13:36 +02:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-14 00:27:23 +02:00
|
|
|
$var_id = self::getVarId($stmt, $this->absolute_class, $this->namespace, $this->aliased_classes);
|
2016-06-24 00:45:46 +02:00
|
|
|
|
2016-08-07 02:27:13 +02:00
|
|
|
if ($lhs_type->isMixed()) {
|
2016-10-09 23:54:58 +02:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MixedPropertyAssignment(
|
2016-10-14 00:27:23 +02:00
|
|
|
$var_id . ' with mixed type cannot be assigned to',
|
2016-10-09 23:54:58 +02:00
|
|
|
$this->checked_file_name,
|
|
|
|
$stmt->getLine()
|
|
|
|
),
|
|
|
|
$this->suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-07 02:27:13 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-06-24 00:45:46 +02:00
|
|
|
|
2016-08-07 02:27:13 +02:00
|
|
|
if ($lhs_type->isNull()) {
|
2016-10-09 23:54:58 +02:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new NullPropertyAssignment(
|
2016-10-14 00:27:23 +02:00
|
|
|
$var_id . ' with null type cannot be assigned to',
|
2016-10-09 23:54:58 +02:00
|
|
|
$this->checked_file_name,
|
|
|
|
$stmt->getLine()
|
|
|
|
),
|
|
|
|
$this->suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-07 02:27:13 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-06-24 00:45:46 +02:00
|
|
|
|
2016-08-07 02:27:13 +02:00
|
|
|
if ($lhs_type->isNullable()) {
|
2016-10-09 23:54:58 +02:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new NullPropertyAssignment(
|
2016-10-14 00:27:23 +02:00
|
|
|
$var_id . ' with possibly null type \'' . $lhs_type . '\' cannot be assigned to',
|
2016-10-09 23:54:58 +02:00
|
|
|
$this->checked_file_name,
|
|
|
|
$stmt->getLine()
|
|
|
|
),
|
|
|
|
$this->suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-07 02:27:13 +02:00
|
|
|
}
|
|
|
|
|
2016-08-07 17:35:27 +02:00
|
|
|
$has_regular_setter = false;
|
|
|
|
|
2016-08-07 02:27:13 +02:00
|
|
|
foreach ($lhs_type->types as $lhs_type_part) {
|
|
|
|
if ($lhs_type_part->isNull()) {
|
|
|
|
continue;
|
2016-06-24 00:45:46 +02:00
|
|
|
}
|
|
|
|
|
2016-08-07 17:35:27 +02:00
|
|
|
if (method_exists((string) $lhs_type_part, '__set')) {
|
2016-08-07 21:15:21 +02:00
|
|
|
$context->vars_in_scope[$var_id] = Type::getMixed();
|
2016-08-07 17:35:27 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$has_regular_setter = true;
|
|
|
|
|
2016-08-07 02:27:13 +02:00
|
|
|
if (!$lhs_type_part->isObjectType()) {
|
2016-06-26 21:18:40 +02:00
|
|
|
if (IssueBuffer::accepts(
|
2016-06-24 00:45:46 +02:00
|
|
|
new InvalidPropertyAssignment(
|
2016-10-14 00:27:23 +02:00
|
|
|
$var_id . ' with possible non-object type \'' . $lhs_type_part . '\' cannot be assigned to',
|
2016-08-25 01:00:44 +02:00
|
|
|
$this->checked_file_name,
|
2016-08-07 02:27:13 +02:00
|
|
|
$stmt->getLine()
|
2016-07-22 19:29:46 +02:00
|
|
|
),
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->suppressed_issues
|
2016-06-24 00:45:46 +02:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-07 02:27:13 +02:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-08-07 17:35:27 +02:00
|
|
|
if ($lhs_type_part->isObject()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-10-11 05:52:05 +02:00
|
|
|
// stdClass and SimpleXMLElement are special cases where we cannot infer the return types
|
|
|
|
// but we don't want to throw an error
|
|
|
|
// Hack has a similar issue: https://github.com/facebook/hhvm/issues/5164
|
|
|
|
if ($lhs_type_part->isObject() || in_array(strtolower($lhs_type_part->value), ['stdclass', 'simplexmlelement', 'dateinterval', 'domdocument', 'domnode'])) {
|
|
|
|
$context->vars_in_scope[$var_id] = Type::getMixed();
|
2016-08-07 02:27:13 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-08-07 18:23:29 +02:00
|
|
|
if (self::isMock($lhs_type_part->value)) {
|
2016-08-07 21:15:21 +02:00
|
|
|
$context->vars_in_scope[$var_id] = Type::getMixed();
|
2016-08-07 18:23:29 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-08 17:28:14 +02:00
|
|
|
if ($stmt->var->name === 'this' || $lhs_type_part->value === $context->self) {
|
2016-08-07 18:23:29 +02:00
|
|
|
$class_visibility = \ReflectionProperty::IS_PRIVATE;
|
|
|
|
}
|
2016-08-08 17:28:14 +02:00
|
|
|
elseif (ClassChecker::classExtends($lhs_type_part->value, $context->self)) {
|
2016-08-07 18:23:29 +02:00
|
|
|
$class_visibility = \ReflectionProperty::IS_PROTECTED;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$class_visibility = \ReflectionProperty::IS_PUBLIC;
|
|
|
|
}
|
2016-08-07 02:27:13 +02:00
|
|
|
|
2016-08-09 18:11:58 +02:00
|
|
|
if (!ClassChecker::classExists($lhs_type_part->value)) {
|
2016-08-14 00:54:49 +02:00
|
|
|
if (InterfaceChecker::interfaceExists($lhs_type_part->value)) {
|
2016-08-09 18:11:58 +02:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new NoInterfaceProperties(
|
|
|
|
'Interfaces cannot have properties',
|
2016-08-25 01:00:44 +02:00
|
|
|
$this->checked_file_name,
|
2016-08-09 18:11:58 +02:00
|
|
|
$stmt->getLine()
|
|
|
|
),
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->suppressed_issues
|
2016-08-09 18:11:58 +02:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UndefinedClass(
|
|
|
|
'Cannot set properties of undefined class ' . $lhs_type_part->value,
|
2016-08-25 01:00:44 +02:00
|
|
|
$this->checked_file_name,
|
2016-08-09 18:11:58 +02:00
|
|
|
$stmt->getLine()
|
|
|
|
),
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->suppressed_issues
|
2016-08-09 18:11:58 +02:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-13 20:20:46 +02:00
|
|
|
$class_properties = ClassLikeChecker::getInstancePropertiesForClass(
|
2016-08-09 18:11:58 +02:00
|
|
|
$lhs_type_part->value,
|
2016-08-07 02:27:13 +02:00
|
|
|
$class_visibility
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!isset($class_properties[$prop_name])) {
|
2016-10-10 07:35:12 +02:00
|
|
|
if ($stmt->var->name === 'this') {
|
|
|
|
if (IssueBuffer::accepts(
|
2016-10-11 01:29:38 +02:00
|
|
|
new UndefinedThisPropertyAssignment(
|
2016-10-10 07:35:12 +02:00
|
|
|
'Instance property ' . $lhs_type_part->value . '::' . $prop_name . ' is not defined',
|
|
|
|
$this->checked_file_name,
|
|
|
|
$stmt->getLine()
|
|
|
|
),
|
|
|
|
$this->suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (IssueBuffer::accepts(
|
2016-10-11 01:29:38 +02:00
|
|
|
new UndefinedPropertyAssignment(
|
2016-10-10 07:35:12 +02:00
|
|
|
'Instance property ' . $lhs_type_part->value . '::' . $prop_name . ' is not defined',
|
|
|
|
$this->checked_file_name,
|
|
|
|
$stmt->getLine()
|
|
|
|
),
|
|
|
|
$this->suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-10-09 23:54:58 +02:00
|
|
|
}
|
|
|
|
|
2016-08-07 02:27:13 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
$class_property_types[] = clone $class_properties[$prop_name];
|
2016-08-07 02:27:13 +02:00
|
|
|
}
|
2016-08-07 17:35:27 +02:00
|
|
|
|
|
|
|
if (!$has_regular_setter) {
|
|
|
|
return;
|
|
|
|
}
|
2016-08-08 20:36:18 +02:00
|
|
|
|
|
|
|
// because we don't want to be assigning for property declarations
|
|
|
|
$context->vars_in_scope[$var_id] = $assignment_type;
|
2016-08-07 17:35:27 +02:00
|
|
|
}
|
2016-10-09 23:54:58 +02:00
|
|
|
else {
|
|
|
|
return;
|
|
|
|
}
|
2016-08-07 17:35:27 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
if ($var_id && count($class_property_types) === 1 && isset($class_property_types[0]->types['stdClass'])) {
|
2016-08-07 21:15:21 +02:00
|
|
|
$context->vars_in_scope[$var_id] = Type::getMixed();
|
2016-08-07 17:35:27 +02:00
|
|
|
return;
|
2016-08-07 02:27:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$class_property_types) {
|
2016-08-07 17:35:27 +02:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MissingPropertyDeclaration(
|
2016-10-14 00:27:23 +02:00
|
|
|
'Missing property declaration for ' . $var_id,
|
2016-08-25 01:00:44 +02:00
|
|
|
$this->checked_file_name,
|
2016-08-07 17:35:27 +02:00
|
|
|
$stmt->getLine()
|
|
|
|
),
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->suppressed_issues
|
2016-08-07 17:35:27 +02:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-07 22:13:36 +02:00
|
|
|
if ($assignment_type->isMixed()) {
|
2016-08-07 02:27:13 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($class_property_types as $class_property_type) {
|
|
|
|
if ($class_property_type->isMixed()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$assignment_type->isIn($class_property_type)) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidPropertyAssignment(
|
2016-10-14 00:27:23 +02:00
|
|
|
$var_id . ' with declared type \'' . $class_property_type . '\' cannot be assigned type \'' . $assignment_type . '\'',
|
2016-08-25 01:00:44 +02:00
|
|
|
$this->checked_file_name,
|
2016-08-07 02:27:13 +02:00
|
|
|
$stmt->getLine()
|
|
|
|
),
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->suppressed_issues
|
2016-08-07 02:27:13 +02:00
|
|
|
)) {
|
|
|
|
return false;
|
2016-06-24 00:45:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-14 00:27:23 +02:00
|
|
|
/**
|
|
|
|
* @param PhpParser\Node\Expr\StaticPropertyFetch $stmt
|
|
|
|
* @param string $prop_name
|
|
|
|
* @param Type\Union $assignment_type
|
|
|
|
* @param Context $context
|
|
|
|
* @return false|null
|
|
|
|
*/
|
|
|
|
protected function checkStaticPropertyAssignment(PhpParser\Node\Expr\StaticPropertyFetch $stmt, Type\Union $assignment_type, Context $context)
|
|
|
|
{
|
|
|
|
$class_property_types = [];
|
|
|
|
|
|
|
|
if ($this->checkStaticPropertyFetch($stmt, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$var_id = self::getVarId($stmt, $this->absolute_class, $this->namespace, $this->aliased_classes);
|
|
|
|
|
|
|
|
$absolute_class = (string)$stmt->class->inferredType;
|
|
|
|
|
|
|
|
if ($stmt->class->parts[0] === 'this' || $absolute_class === $context->self) {
|
|
|
|
$class_visibility = \ReflectionProperty::IS_PRIVATE;
|
|
|
|
}
|
|
|
|
elseif (ClassChecker::classExtends($absolute_class, $context->self)) {
|
|
|
|
$class_visibility = \ReflectionProperty::IS_PROTECTED;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$class_visibility = \ReflectionProperty::IS_PUBLIC;
|
|
|
|
}
|
|
|
|
|
|
|
|
$class_properties = ClassLikeChecker::getStaticPropertiesForClass(
|
|
|
|
$absolute_class,
|
|
|
|
$class_visibility
|
|
|
|
);
|
|
|
|
|
|
|
|
$prop_name = $stmt->name;
|
|
|
|
|
|
|
|
if (!isset($class_properties[$prop_name])) {
|
|
|
|
if ($stmt->class->parts[0] === 'this') {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UndefinedThisPropertyAssignment(
|
|
|
|
'Static property ' . $var_id . ' is not defined',
|
|
|
|
$this->checked_file_name,
|
|
|
|
$stmt->getLine()
|
|
|
|
),
|
|
|
|
$this->suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UndefinedPropertyAssignment(
|
|
|
|
'Static property ' . $var_id . ' is not defined',
|
|
|
|
$this->checked_file_name,
|
|
|
|
$stmt->getLine()
|
|
|
|
),
|
|
|
|
$this->suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$context->vars_in_scope[$var_id] = $assignment_type;
|
|
|
|
|
|
|
|
$class_property_type = clone $class_properties[$prop_name];
|
|
|
|
|
|
|
|
if ($assignment_type->isMixed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($class_property_type->isMixed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$assignment_type->isIn($class_property_type)) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidPropertyAssignment(
|
|
|
|
$var_id . ' with declared type \'' . $class_property_type . '\' cannot be assigned type \'' . $assignment_type . '\'',
|
|
|
|
$this->checked_file_name,
|
|
|
|
$stmt->getLine()
|
|
|
|
),
|
|
|
|
$this->suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$context->vars_in_scope[$var_id] = $assignment_type;
|
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* @return false|null
|
|
|
|
*/
|
2016-08-11 00:10:12 +02:00
|
|
|
protected function checkNew(PhpParser\Node\Expr\New_ $stmt, Context $context)
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
|
|
|
$absolute_class = null;
|
|
|
|
|
2016-08-07 22:27:00 +02:00
|
|
|
if ($stmt->class instanceof PhpParser\Node\Name) {
|
|
|
|
if (!in_array($stmt->class->parts[0], ['self', 'static', 'parent'])) {
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->check_classes) {
|
2016-10-02 19:05:49 +02:00
|
|
|
$absolute_class = ClassLikeChecker::getAbsoluteClassFromName($stmt->class, $this->namespace, $this->aliased_classes);
|
|
|
|
|
|
|
|
if ($this->isPhantomClass($absolute_class)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ClassLikeChecker::checkAbsoluteClassOrInterface($absolute_class, $this->checked_file_name, $stmt->getLine(), $this->suppressed_issues) === false) {
|
2016-08-07 22:27:00 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-06-06 02:25:16 +02:00
|
|
|
}
|
2016-08-07 22:27:00 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
switch ($stmt->class->parts[0]) {
|
|
|
|
case 'self':
|
2016-08-08 17:28:14 +02:00
|
|
|
$absolute_class = $context->self;
|
2016-08-07 22:27:00 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'parent':
|
2016-08-08 17:28:14 +02:00
|
|
|
$absolute_class = $context->parent;
|
2016-08-07 22:27:00 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'static':
|
|
|
|
// @todo maybe we can do better here
|
2016-08-08 17:28:14 +02:00
|
|
|
$absolute_class = $context->self;
|
2016-08-07 22:27:00 +02:00
|
|
|
break;
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($absolute_class) {
|
2016-08-07 22:27:00 +02:00
|
|
|
$stmt->inferredType = new Type\Union([new Type\Atomic($absolute_class)]);
|
|
|
|
|
2016-07-29 20:48:04 +02:00
|
|
|
if (method_exists($absolute_class, '__construct')) {
|
|
|
|
$method_id = $absolute_class . '::__construct';
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-08-30 06:04:10 +02:00
|
|
|
if ($this->checkFunctionArguments($stmt->args, $method_id, $context, $stmt->getLine()) === false) {
|
2016-07-29 20:48:04 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-06-06 02:25:16 +02:00
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* @return false|null
|
|
|
|
*/
|
2016-08-11 00:10:12 +02:00
|
|
|
protected function checkArray(PhpParser\Node\Expr\Array_ $stmt, Context $context)
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
2016-06-14 01:57:32 +02:00
|
|
|
// if the array is empty, this special type allows us to match any other array type against it
|
|
|
|
if (empty($stmt->items)) {
|
2016-09-12 06:02:26 +02:00
|
|
|
$stmt->inferredType = Type::getEmptyArray();
|
2016-06-14 01:57:32 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/** @var Type\Union|null */
|
2016-09-09 22:21:49 +02:00
|
|
|
$item_key_type = null;
|
2016-10-09 23:54:58 +02:00
|
|
|
|
|
|
|
/** @var Type\Union|null */
|
2016-09-09 22:21:49 +02:00
|
|
|
$item_value_type = null;
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/** @var array<string,Type\Union> */
|
2016-09-22 06:49:48 +02:00
|
|
|
$property_types = [];
|
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
foreach ($stmt->items as $item) {
|
|
|
|
if ($item->key) {
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->checkExpression($item->key, $context) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-09-09 22:21:49 +02:00
|
|
|
|
|
|
|
if (isset($item->key->inferredType)) {
|
|
|
|
if ($item_key_type) {
|
2016-10-09 23:54:58 +02:00
|
|
|
/** @var Type\Union */
|
2016-09-09 22:21:49 +02:00
|
|
|
$item_key_type = Type::combineUnionTypes($item->key->inferredType, $item_key_type);
|
|
|
|
}
|
|
|
|
else {
|
2016-10-09 23:54:58 +02:00
|
|
|
/** @var Type\Union */
|
2016-09-09 22:21:49 +02:00
|
|
|
$item_key_type = $item->key->inferredType;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$item_key_type = Type::getInt();
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->checkExpression($item->value, $context) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-09-09 22:21:49 +02:00
|
|
|
|
|
|
|
if (isset($item->value->inferredType)) {
|
2016-09-22 06:49:48 +02:00
|
|
|
if ($item->key instanceof PhpParser\Node\Scalar\String_) {
|
|
|
|
$property_types[$item->key->value] = $item->value->inferredType;
|
|
|
|
}
|
|
|
|
|
2016-09-09 22:21:49 +02:00
|
|
|
if ($item_value_type) {
|
2016-09-21 03:45:49 +02:00
|
|
|
$item_value_type = Type::combineUnionTypes($item->value->inferredType, $item_value_type);
|
2016-09-09 22:21:49 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$item_value_type = $item->value->inferredType;
|
|
|
|
}
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-09-21 03:45:49 +02:00
|
|
|
// if this array looks like an object-like array, let's return that instead
|
2016-10-03 04:00:42 +02:00
|
|
|
if ($item_value_type && $item_key_type && $item_key_type->hasString() && !$item_key_type->hasInt()) {
|
2016-09-22 06:49:48 +02:00
|
|
|
$stmt->inferredType = new Type\Union([new Type\ObjectLike('object-like', $property_types)]);
|
2016-09-21 03:45:49 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-09 22:21:49 +02:00
|
|
|
$stmt->inferredType = new Type\Union([
|
|
|
|
new Type\Generic(
|
|
|
|
'array',
|
|
|
|
[
|
2016-09-10 05:17:56 +02:00
|
|
|
$item_key_type ?: new Type\Union([new Type\Atomic('int'), new Type\Atomic('string')]),
|
2016-09-21 03:45:49 +02:00
|
|
|
$item_value_type ?: Type::getMixed()
|
2016-09-09 22:21:49 +02:00
|
|
|
]
|
|
|
|
)
|
|
|
|
]);
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* @return false|null
|
|
|
|
*/
|
2016-08-11 00:10:12 +02:00
|
|
|
protected function checkTryCatch(PhpParser\Node\Stmt\TryCatch $stmt, Context $context)
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
2016-06-20 06:38:13 +02:00
|
|
|
$this->check($stmt->stmts, $context);
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-06-20 22:18:31 +02:00
|
|
|
// clone context for catches after running the try block, as
|
|
|
|
// we optimistically assume it only failed at the very end
|
|
|
|
$original_context = clone $context;
|
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
foreach ($stmt->catches as $catch) {
|
2016-06-20 06:38:13 +02:00
|
|
|
$catch_context = clone $original_context;
|
2016-06-17 19:21:57 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
$catch_class = ClassLikeChecker::getAbsoluteClassFromName($catch->type, $this->namespace, $this->aliased_classes);
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->check_classes) {
|
2016-10-09 23:54:58 +02:00
|
|
|
$absolute_class = $catch_class;
|
2016-10-02 19:05:49 +02:00
|
|
|
|
|
|
|
if (ClassLikeChecker::checkAbsoluteClassOrInterface($absolute_class, $this->checked_file_name, $stmt->getLine(), $this->suppressed_issues) === false) {
|
|
|
|
return false;
|
2016-06-06 02:25:16 +02:00
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-10-14 00:27:23 +02:00
|
|
|
$catch_context->vars_in_scope['$' . $catch->var] = new Type\Union([
|
2016-10-09 23:54:58 +02:00
|
|
|
new Type\Atomic($catch_class)
|
|
|
|
]);
|
|
|
|
|
2016-10-14 00:27:23 +02:00
|
|
|
$catch_context->vars_possibly_in_scope['$' . $catch->var] = true;
|
2016-10-09 23:54:58 +02:00
|
|
|
|
|
|
|
$this->registerVariable($catch->var, $catch->getLine());
|
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
$this->check($catch->stmts, $catch_context);
|
2016-06-17 00:52:12 +02:00
|
|
|
|
2016-08-24 23:06:20 +02:00
|
|
|
if (!ScopeChecker::doesAlwaysReturnOrThrow($catch->stmts)) {
|
2016-06-20 22:18:31 +02:00
|
|
|
foreach ($catch_context->vars_in_scope as $catch_var => $type) {
|
|
|
|
if ($catch->var !== $catch_var && isset($context->vars_in_scope[$catch_var]) && (string) $context->vars_in_scope[$catch_var] !== (string) $type) {
|
|
|
|
$context->vars_in_scope[$catch_var] = Type::combineUnionTypes($context->vars_in_scope[$catch_var], $type);
|
|
|
|
}
|
2016-06-17 00:52:12 +02:00
|
|
|
}
|
2016-06-20 22:18:31 +02:00
|
|
|
|
|
|
|
$context->vars_possibly_in_scope = array_merge($catch_context->vars_possibly_in_scope, $context->vars_possibly_in_scope);
|
2016-06-17 00:52:12 +02:00
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt->finallyStmts) {
|
2016-06-20 06:38:13 +02:00
|
|
|
$this->check($stmt->finallyStmts, $context);
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* @return false|null
|
|
|
|
*/
|
2016-08-11 00:10:12 +02:00
|
|
|
protected function checkFor(PhpParser\Node\Stmt\For_ $stmt, Context $context)
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
2016-06-20 07:05:44 +02:00
|
|
|
$for_context = clone $context;
|
2016-06-20 22:30:31 +02:00
|
|
|
$for_context->in_loop = true;
|
2016-01-20 00:27:06 +01:00
|
|
|
|
|
|
|
foreach ($stmt->init as $init) {
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->checkExpression($init, $for_context) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($stmt->cond as $condition) {
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->checkExpression($condition, $for_context) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($stmt->loop as $expr) {
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->checkExpression($expr, $for_context) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-08-24 06:22:38 +02:00
|
|
|
$this->check($stmt->stmts, $for_context, $context);
|
2016-04-03 20:25:41 +02:00
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
foreach ($context->vars_in_scope as $var => $type) {
|
2016-06-16 02:16:40 +02:00
|
|
|
if ($type->isMixed()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-06-20 07:05:44 +02:00
|
|
|
if ($for_context->vars_in_scope[$var]->isMixed()) {
|
|
|
|
$context->vars_in_scope[$var] = $for_context->vars_in_scope[$var];
|
2016-06-16 02:16:40 +02:00
|
|
|
}
|
|
|
|
|
2016-06-20 07:05:44 +02:00
|
|
|
if ((string) $for_context->vars_in_scope[$var] !== (string) $type) {
|
|
|
|
$context->vars_in_scope[$var] = Type::combineUnionTypes($context->vars_in_scope[$var], $for_context->vars_in_scope[$var]);
|
2016-05-06 03:20:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-20 18:38:31 +02:00
|
|
|
$context->vars_possibly_in_scope = array_merge($for_context->vars_possibly_in_scope, $context->vars_possibly_in_scope);
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* @return false|null
|
|
|
|
*/
|
2016-08-11 00:10:12 +02:00
|
|
|
protected function checkForeach(PhpParser\Node\Stmt\Foreach_ $stmt, Context $context)
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->checkExpression($stmt->expr, $context) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-06-20 18:38:31 +02:00
|
|
|
$foreach_context = clone $context;
|
2016-06-20 22:30:31 +02:00
|
|
|
$foreach_context->in_loop = true;
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/** @var Type\Union|null */
|
2016-09-09 22:21:49 +02:00
|
|
|
$key_type = null;
|
2016-10-09 23:54:58 +02:00
|
|
|
|
|
|
|
/** @var Type\Union|null */
|
2016-09-09 22:21:49 +02:00
|
|
|
$value_type = null;
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-10-14 00:27:23 +02:00
|
|
|
$var_id = self::getVarId($stmt->expr, $this->absolute_class, $this->namespace, $this->aliased_classes);
|
2016-04-12 01:13:50 +02:00
|
|
|
|
2016-09-09 22:21:49 +02:00
|
|
|
if (isset($stmt->expr->inferredType)) {
|
2016-10-09 23:54:58 +02:00
|
|
|
/** @var Type\Union */
|
2016-09-09 22:21:49 +02:00
|
|
|
$iterator_type = $stmt->expr->inferredType;
|
|
|
|
}
|
|
|
|
elseif (isset($foreach_context->vars_in_scope[$var_id])) {
|
|
|
|
$iterator_type = $foreach_context->vars_in_scope[$var_id];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$iterator_type = null;
|
|
|
|
}
|
2016-06-15 01:22:29 +02:00
|
|
|
|
2016-09-09 22:21:49 +02:00
|
|
|
if ($iterator_type) {
|
|
|
|
foreach ($iterator_type->types as $return_type) {
|
|
|
|
// if it's an empty array, we cannot iterate over it
|
2016-09-21 22:42:39 +02:00
|
|
|
if ((string) $return_type === 'array<empty,empty>') {
|
2016-09-09 22:21:49 +02:00
|
|
|
continue;
|
|
|
|
}
|
2016-04-12 22:12:20 +02:00
|
|
|
|
2016-09-09 22:21:49 +02:00
|
|
|
if ($return_type instanceof Type\Generic) {
|
|
|
|
$value_index = count($return_type->type_params) - 1;
|
|
|
|
$value_type_part = $return_type->type_params[$value_index];
|
|
|
|
|
|
|
|
if (!$value_type) {
|
|
|
|
$value_type = $value_type_part;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$value_type = Type::combineUnionTypes($value_type, $value_type_part);
|
2016-08-07 20:36:24 +02:00
|
|
|
}
|
|
|
|
|
2016-09-09 22:21:49 +02:00
|
|
|
if ($value_index) {
|
2016-09-10 00:36:35 +02:00
|
|
|
$key_type_part = $return_type->type_params[0];
|
2016-08-31 23:08:04 +02:00
|
|
|
|
2016-09-09 22:21:49 +02:00
|
|
|
if (!$key_type) {
|
|
|
|
$key_type = $key_type_part;
|
2016-08-31 23:08:04 +02:00
|
|
|
}
|
|
|
|
else {
|
2016-09-09 22:21:49 +02:00
|
|
|
$key_type = Type::combineUnionTypes($key_type, $key_type_part);
|
2016-08-31 23:08:04 +02:00
|
|
|
}
|
2016-07-10 22:09:09 +02:00
|
|
|
}
|
2016-09-09 22:21:49 +02:00
|
|
|
continue;
|
|
|
|
}
|
2016-07-10 22:09:09 +02:00
|
|
|
|
2016-09-09 22:21:49 +02:00
|
|
|
switch ($return_type->value) {
|
|
|
|
case 'mixed':
|
|
|
|
case 'empty':
|
|
|
|
$value_type = Type::getMixed();
|
|
|
|
break;
|
2016-07-10 22:09:09 +02:00
|
|
|
|
2016-09-09 22:21:49 +02:00
|
|
|
case 'array':
|
|
|
|
case 'object':
|
2016-10-02 15:50:24 +02:00
|
|
|
case 'object-like':
|
2016-09-09 22:21:49 +02:00
|
|
|
$value_type = Type::getMixed();
|
|
|
|
break;
|
2016-06-06 02:25:16 +02:00
|
|
|
|
2016-09-09 22:21:49 +02:00
|
|
|
case 'null':
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new NullReference('Cannot iterate over ' . $return_type->value, $this->checked_file_name, $stmt->getLine()),
|
|
|
|
$this->suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-31 23:08:04 +02:00
|
|
|
|
2016-09-09 22:21:49 +02:00
|
|
|
$value_type = Type::getMixed();
|
|
|
|
break;
|
2016-06-06 02:25:16 +02:00
|
|
|
|
2016-09-09 22:21:49 +02:00
|
|
|
case 'string':
|
|
|
|
case 'void':
|
|
|
|
case 'int':
|
|
|
|
case 'bool':
|
|
|
|
case 'false':
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidIterator('Cannot iterate over ' . $return_type->value, $this->checked_file_name, $stmt->getLine()),
|
|
|
|
$this->suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$value_type = Type::getMixed();
|
|
|
|
break;
|
2016-04-12 01:13:50 +02:00
|
|
|
|
2016-09-09 22:21:49 +02:00
|
|
|
default:
|
|
|
|
if (ClassChecker::classImplements($return_type->value, 'Iterator')) {
|
|
|
|
$iterator_method = $return_type->value . '::current';
|
|
|
|
$iterator_class_type = MethodChecker::getMethodReturnTypes($iterator_method);
|
2016-07-10 22:09:09 +02:00
|
|
|
|
2016-09-09 22:21:49 +02:00
|
|
|
if ($iterator_class_type) {
|
|
|
|
$value_type_part = self::fleshOutTypes($iterator_class_type, [], $return_type->value, $iterator_method);
|
2016-08-31 23:08:04 +02:00
|
|
|
|
2016-09-09 22:21:49 +02:00
|
|
|
if (!$value_type) {
|
|
|
|
$value_type = $value_type_part;
|
2016-08-31 23:08:04 +02:00
|
|
|
}
|
|
|
|
else {
|
2016-09-09 22:21:49 +02:00
|
|
|
$value_type = Type::combineUnionTypes($value_type, $value_type_part);
|
2016-07-10 22:09:09 +02:00
|
|
|
}
|
2016-06-06 02:25:16 +02:00
|
|
|
}
|
2016-09-09 22:21:49 +02:00
|
|
|
else {
|
|
|
|
$value_type = Type::getMixed();
|
|
|
|
}
|
|
|
|
}
|
2016-06-06 02:25:16 +02:00
|
|
|
|
2016-09-09 22:21:49 +02:00
|
|
|
if ($return_type->value !== 'Traversable' && $return_type->value !== $this->class_name) {
|
|
|
|
if (ClassLikeChecker::checkAbsoluteClassOrInterface($return_type->value, $this->checked_file_name, $stmt->getLine(), $this->suppressed_issues) === false) {
|
|
|
|
return false;
|
2016-06-06 02:25:16 +02:00
|
|
|
}
|
2016-09-09 22:21:49 +02:00
|
|
|
}
|
2016-04-12 01:13:50 +02:00
|
|
|
}
|
|
|
|
}
|
2016-09-09 22:21:49 +02:00
|
|
|
}
|
2016-04-12 01:13:50 +02:00
|
|
|
|
2016-09-09 22:21:49 +02:00
|
|
|
if ($stmt->keyVar) {
|
2016-10-14 00:27:23 +02:00
|
|
|
$foreach_context->vars_in_scope['$' . $stmt->keyVar->name] = $key_type ?: Type::getMixed();
|
|
|
|
$foreach_context->vars_possibly_in_scope['$' . $stmt->keyVar->name] = true;
|
2016-09-09 22:21:49 +02:00
|
|
|
$this->registerVariable($stmt->keyVar->name, $stmt->getLine());
|
|
|
|
}
|
2016-07-10 22:09:09 +02:00
|
|
|
|
2016-09-09 22:21:49 +02:00
|
|
|
if ($value_type && $value_type instanceof Type\Atomic) {
|
|
|
|
$value_type = new Type\Union([$value_type]);
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-10-14 00:27:23 +02:00
|
|
|
$foreach_context->vars_in_scope['$' . $stmt->valueVar->name] = $value_type ? $value_type : Type::getMixed();
|
|
|
|
$foreach_context->vars_possibly_in_scope['$' . $stmt->valueVar->name] = true;
|
2016-09-09 22:21:49 +02:00
|
|
|
$this->registerVariable($stmt->valueVar->name, $stmt->getLine());
|
|
|
|
|
2016-08-11 00:10:12 +02:00
|
|
|
CommentChecker::getTypeFromComment((string) $stmt->getDocComment(), $foreach_context, $this->source, null);
|
2016-07-12 06:50:16 +02:00
|
|
|
|
2016-08-24 06:22:38 +02:00
|
|
|
$this->check($stmt->stmts, $foreach_context, $context);
|
2016-06-20 06:38:13 +02:00
|
|
|
|
|
|
|
foreach ($context->vars_in_scope as $var => $type) {
|
2016-06-16 02:16:40 +02:00
|
|
|
if ($type->isMixed()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-09-21 22:42:39 +02:00
|
|
|
if (!isset($foreach_context->vars_in_scope[$var])) {
|
|
|
|
unset($context->vars_in_scope[$var]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
if ($foreach_context->vars_in_scope[$var]->isMixed()) {
|
2016-06-20 18:38:31 +02:00
|
|
|
$context->vars_in_scope[$var] = $foreach_context->vars_in_scope[$var];
|
2016-06-16 02:16:40 +02:00
|
|
|
}
|
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
if ((string) $foreach_context->vars_in_scope[$var] !== (string) $type) {
|
|
|
|
$context->vars_in_scope[$var] = Type::combineUnionTypes($context->vars_in_scope[$var], $foreach_context->vars_in_scope[$var]);
|
2016-05-06 03:20:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-20 18:38:31 +02:00
|
|
|
$context->vars_possibly_in_scope = array_merge($foreach_context->vars_possibly_in_scope, $context->vars_possibly_in_scope);
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* @return false|null
|
|
|
|
*/
|
2016-08-11 00:10:12 +02:00
|
|
|
protected function checkWhile(PhpParser\Node\Stmt\While_ $stmt, Context $context)
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
2016-06-20 06:38:13 +02:00
|
|
|
$while_context = clone $context;
|
2016-06-17 22:05:28 +02:00
|
|
|
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->checkExpression($stmt->cond, $while_context) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-10-14 00:27:23 +02:00
|
|
|
$while_types = $this->type_checker->getTypeAssertions(
|
|
|
|
$stmt->cond,
|
|
|
|
$this->absolute_class,
|
|
|
|
$this->namespace,
|
|
|
|
$this->aliased_classes);
|
2016-05-06 03:20:15 +02:00
|
|
|
|
|
|
|
// if the while has an or as the main component, we cannot safely reason about it
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($stmt->cond instanceof PhpParser\Node\Expr\BinaryOp && self::containsBooleanOr($stmt->cond)) {
|
2016-06-17 22:05:28 +02:00
|
|
|
// do nothing
|
2016-05-06 03:20:15 +02:00
|
|
|
}
|
|
|
|
else {
|
2016-08-09 00:55:22 +02:00
|
|
|
$while_vars_in_scope_reconciled = TypeChecker::reconcileKeyedTypes(
|
|
|
|
$while_types,
|
|
|
|
$while_context->vars_in_scope,
|
2016-08-25 01:00:44 +02:00
|
|
|
$this->checked_file_name,
|
2016-08-09 00:55:22 +02:00
|
|
|
$stmt->getLine(),
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->suppressed_issues
|
2016-08-09 00:55:22 +02:00
|
|
|
);
|
2016-06-06 02:25:16 +02:00
|
|
|
|
|
|
|
if ($while_vars_in_scope_reconciled === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
$while_context->vars_in_scope = $while_vars_in_scope_reconciled;
|
2016-05-06 03:20:15 +02:00
|
|
|
}
|
|
|
|
|
2016-08-24 06:22:38 +02:00
|
|
|
if ($this->check($stmt->stmts, $while_context, $context) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-05-06 03:20:15 +02:00
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
foreach ($context->vars_in_scope as $var => $type) {
|
2016-06-16 02:16:40 +02:00
|
|
|
if ($type->isMixed()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-10-02 16:26:13 +02:00
|
|
|
if (isset($while_context->vars_in_scope[$var])) {
|
|
|
|
if ($while_context->vars_in_scope[$var]->isMixed()) {
|
|
|
|
$context->vars_in_scope[$var] = $while_context->vars_in_scope[$var];
|
|
|
|
}
|
2016-06-16 02:16:40 +02:00
|
|
|
|
2016-10-02 16:26:13 +02:00
|
|
|
if ((string) $while_context->vars_in_scope[$var] !== (string) $type) {
|
|
|
|
$context->vars_in_scope[$var] = Type::combineUnionTypes($while_context->vars_in_scope[$var], $type);
|
|
|
|
}
|
2016-05-06 03:20:15 +02:00
|
|
|
}
|
|
|
|
}
|
2016-06-20 22:18:31 +02:00
|
|
|
|
|
|
|
$context->vars_possibly_in_scope = array_merge($context->vars_possibly_in_scope, $while_context->vars_possibly_in_scope);
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* @return false|null
|
|
|
|
*/
|
2016-08-11 00:10:12 +02:00
|
|
|
protected function checkDo(PhpParser\Node\Stmt\Do_ $stmt, Context $context)
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
2016-06-20 22:18:31 +02:00
|
|
|
// do not clone context for do, because it executes in current scope always
|
|
|
|
if ($this->check($stmt->stmts, $context) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-08-11 00:10:12 +02:00
|
|
|
return $this->checkExpression($stmt->cond, $context);
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* @return false|null
|
|
|
|
*/
|
2016-08-11 00:10:12 +02:00
|
|
|
protected function checkBinaryOp(PhpParser\Node\Expr\BinaryOp $stmt, Context $context, $nesting = 0)
|
2016-04-03 20:25:41 +02:00
|
|
|
{
|
2016-04-04 20:06:29 +02:00
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\Concat && $nesting > 20) {
|
|
|
|
// ignore deeply-nested string concatenation
|
|
|
|
}
|
|
|
|
else if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\BooleanAnd) {
|
2016-10-14 00:27:23 +02:00
|
|
|
$left_type_assertions = $this->type_checker->getReconcilableTypeAssertions(
|
|
|
|
$stmt->left,
|
|
|
|
$this->absolute_class,
|
|
|
|
$this->namespace,
|
|
|
|
$this->aliased_classes
|
|
|
|
);
|
2016-04-03 20:25:41 +02:00
|
|
|
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->checkExpression($stmt->left, $context) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-04-03 20:25:41 +02:00
|
|
|
|
|
|
|
// while in an and, we allow scope to boil over to support
|
|
|
|
// statements of the form if ($x && $x->foo())
|
2016-08-09 00:55:22 +02:00
|
|
|
$op_vars_in_scope = TypeChecker::reconcileKeyedTypes(
|
|
|
|
$left_type_assertions,
|
|
|
|
$context->vars_in_scope,
|
2016-08-25 01:00:44 +02:00
|
|
|
$this->checked_file_name,
|
2016-08-09 00:55:22 +02:00
|
|
|
$stmt->getLine(),
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->suppressed_issues
|
2016-08-09 00:55:22 +02:00
|
|
|
);
|
2016-04-03 20:25:41 +02:00
|
|
|
|
2016-06-14 01:57:32 +02:00
|
|
|
if ($op_vars_in_scope === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-06-20 22:18:31 +02:00
|
|
|
$op_context = clone $context;
|
|
|
|
$op_context->vars_in_scope = $op_vars_in_scope;
|
2016-06-20 06:38:13 +02:00
|
|
|
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->checkExpression($stmt->right, $op_context) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-06-20 22:18:31 +02:00
|
|
|
|
|
|
|
foreach ($op_context->vars_in_scope as $var => $type) {
|
|
|
|
if (!isset($context->vars_in_scope[$var])) {
|
|
|
|
$context->vars_in_scope[$var] = $type;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$context->vars_possibly_in_scope = array_merge($op_context->vars_possibly_in_scope, $context->vars_possibly_in_scope);
|
2016-04-03 20:25:41 +02:00
|
|
|
}
|
|
|
|
else if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\BooleanOr) {
|
2016-10-14 00:27:23 +02:00
|
|
|
$left_type_assertions = $this->type_checker->getNegatableTypeAssertions(
|
|
|
|
$stmt->left,
|
|
|
|
$this->absolute_class,
|
|
|
|
$this->namespace,
|
|
|
|
$this->aliased_classes
|
|
|
|
);
|
2016-04-03 20:25:41 +02:00
|
|
|
|
2016-04-16 22:28:25 +02:00
|
|
|
$negated_type_assertions = TypeChecker::negateTypes($left_type_assertions);
|
2016-04-03 20:25:41 +02:00
|
|
|
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->checkExpression($stmt->left, $context) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-04-03 20:25:41 +02:00
|
|
|
|
|
|
|
// while in an or, we allow scope to boil over to support
|
|
|
|
// statements of the form if ($x === null || $x->foo())
|
2016-08-09 00:55:22 +02:00
|
|
|
$op_vars_in_scope = TypeChecker::reconcileKeyedTypes(
|
|
|
|
$negated_type_assertions,
|
|
|
|
$context->vars_in_scope,
|
2016-08-25 01:00:44 +02:00
|
|
|
$this->checked_file_name,
|
2016-08-09 00:55:22 +02:00
|
|
|
$stmt->getLine(),
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->suppressed_issues
|
2016-08-09 00:55:22 +02:00
|
|
|
);
|
2016-04-03 20:25:41 +02:00
|
|
|
|
2016-06-14 01:57:32 +02:00
|
|
|
if ($op_vars_in_scope === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-06-20 22:18:31 +02:00
|
|
|
$op_context = clone $context;
|
|
|
|
$op_context->vars_in_scope = $op_vars_in_scope;
|
2016-06-20 06:38:13 +02:00
|
|
|
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->checkExpression($stmt->right, $op_context) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-06-20 22:18:31 +02:00
|
|
|
|
|
|
|
$context->vars_possibly_in_scope = array_merge($op_context->vars_possibly_in_scope, $context->vars_possibly_in_scope);
|
2016-04-03 20:25:41 +02:00
|
|
|
}
|
|
|
|
else {
|
2016-04-27 00:17:47 +02:00
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\Concat) {
|
2016-06-16 18:04:55 +02:00
|
|
|
$stmt->inferredType = Type::getString();
|
2016-04-27 00:17:47 +02:00
|
|
|
}
|
|
|
|
|
2016-04-04 20:06:29 +02:00
|
|
|
if ($stmt->left instanceof PhpParser\Node\Expr\BinaryOp) {
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->checkBinaryOp($stmt->left, $context, ++$nesting) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-04-04 20:06:29 +02:00
|
|
|
}
|
|
|
|
else {
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->checkExpression($stmt->left, $context) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-04-04 20:06:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt->right instanceof PhpParser\Node\Expr\BinaryOp) {
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->checkBinaryOp($stmt->right, $context, ++$nesting) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-04-04 20:06:29 +02:00
|
|
|
}
|
|
|
|
else {
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->checkExpression($stmt->right, $context) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-04-04 20:06:29 +02:00
|
|
|
}
|
2016-04-03 20:25:41 +02:00
|
|
|
}
|
2016-04-27 00:17:47 +02:00
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\BooleanAnd ||
|
|
|
|
$stmt instanceof PhpParser\Node\Expr\BinaryOp\BooleanOr ||
|
|
|
|
$stmt instanceof PhpParser\Node\Expr\BinaryOp\Equal ||
|
|
|
|
$stmt instanceof PhpParser\Node\Expr\BinaryOp\NotEqual ||
|
|
|
|
$stmt instanceof PhpParser\Node\Expr\BinaryOp\Identical ||
|
|
|
|
$stmt instanceof PhpParser\Node\Expr\BinaryOp\NotIdentical ||
|
|
|
|
$stmt instanceof PhpParser\Node\Expr\BinaryOp\Greater ||
|
|
|
|
$stmt instanceof PhpParser\Node\Expr\BinaryOp\GreaterOrEqual ||
|
|
|
|
$stmt instanceof PhpParser\Node\Expr\BinaryOp\Smaller ||
|
|
|
|
$stmt instanceof PhpParser\Node\Expr\BinaryOp\SmallerOrEqual
|
|
|
|
) {
|
2016-06-16 18:04:55 +02:00
|
|
|
$stmt->inferredType = Type::getBool();
|
2016-04-27 00:17:47 +02:00
|
|
|
}
|
2016-04-03 20:25:41 +02:00
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* @return false|null
|
|
|
|
*/
|
2016-08-11 00:10:12 +02:00
|
|
|
protected function checkAssignment(PhpParser\Node\Expr\Assign $stmt, Context $context)
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
2016-10-14 00:27:23 +02:00
|
|
|
$var_id = self::getVarId($stmt->var, $this->absolute_class, $this->namespace, $this->aliased_classes);
|
2016-06-21 00:10:55 +02:00
|
|
|
|
2016-10-14 00:27:23 +02:00
|
|
|
$array_var_id = self::getArrayVarId($stmt->var, $this->absolute_class, $this->namespace, $this->aliased_classes);
|
2016-09-17 17:57:44 +02:00
|
|
|
|
|
|
|
if ($array_var_id) {
|
|
|
|
// removes dependennt vars from $context
|
|
|
|
$context->removeDescendents($array_var_id);
|
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
$type_in_comments = CommentChecker::getTypeFromComment((string) $stmt->getDocComment(), $context, $this->source, $var_id);
|
|
|
|
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->checkExpression($stmt->expr, $context) === false) {
|
2016-06-21 00:10:55 +02:00
|
|
|
// if we're not exiting immediately, make everything mixed
|
2016-10-09 23:54:58 +02:00
|
|
|
$context->vars_in_scope[$var_id] = $type_in_comments ?: Type::getMixed();
|
|
|
|
$stmt->inferredType = $type_in_comments ?: Type::getMixed();
|
2016-06-21 00:10:55 +02:00
|
|
|
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-06-14 01:57:32 +02:00
|
|
|
if ($type_in_comments) {
|
2016-08-11 01:21:03 +02:00
|
|
|
$return_type = $type_in_comments;
|
2016-06-14 01:57:32 +02:00
|
|
|
}
|
2016-06-16 18:04:55 +02:00
|
|
|
elseif (isset($stmt->expr->inferredType)) {
|
2016-10-09 23:54:58 +02:00
|
|
|
/** @var Type\Union */
|
2016-06-16 18:04:55 +02:00
|
|
|
$return_type = $stmt->expr->inferredType;
|
2016-06-14 01:57:32 +02:00
|
|
|
}
|
|
|
|
else {
|
2016-06-15 01:22:29 +02:00
|
|
|
$return_type = Type::getMixed();
|
2016-06-14 01:57:32 +02:00
|
|
|
}
|
2016-04-30 20:14:22 +02:00
|
|
|
|
2016-06-17 00:52:12 +02:00
|
|
|
$stmt->inferredType = $return_type;
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
if ($stmt->var instanceof PhpParser\Node\Expr\Variable && is_string($stmt->var->name) && $var_id) {
|
2016-06-20 06:38:13 +02:00
|
|
|
$context->vars_in_scope[$var_id] = $return_type;
|
|
|
|
$context->vars_possibly_in_scope[$var_id] = true;
|
2016-06-14 01:57:32 +02:00
|
|
|
$this->registerVariable($var_id, $stmt->var->getLine());
|
2016-01-30 00:48:09 +01:00
|
|
|
|
2016-10-14 00:27:23 +02:00
|
|
|
}
|
|
|
|
elseif ($stmt->var instanceof PhpParser\Node\Expr\List_) {
|
2016-01-30 00:48:09 +01:00
|
|
|
foreach ($stmt->var->vars as $var) {
|
|
|
|
if ($var) {
|
2016-10-14 00:27:23 +02:00
|
|
|
$context->vars_in_scope['$' . $var->name] = Type::getMixed();
|
|
|
|
$context->vars_possibly_in_scope['$' . $var->name] = true;
|
2016-01-30 00:48:09 +01:00
|
|
|
$this->registerVariable($var->name, $var->getLine());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-14 00:27:23 +02:00
|
|
|
}
|
|
|
|
else if ($stmt->var instanceof PhpParser\Node\Expr\ArrayDimFetch) {
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->checkArrayAssignment($stmt->var, $context, $return_type) === false) {
|
2016-06-14 01:57:32 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-03-23 18:05:25 +01:00
|
|
|
|
2016-10-14 00:27:23 +02:00
|
|
|
}
|
|
|
|
else if ($stmt->var instanceof PhpParser\Node\Expr\PropertyFetch &&
|
2016-06-14 01:57:32 +02:00
|
|
|
$stmt->var->var instanceof PhpParser\Node\Expr\Variable &&
|
|
|
|
is_string($stmt->var->name)) {
|
2016-05-09 14:56:07 +02:00
|
|
|
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->checkPropertyAssignment($stmt->var, $stmt->var->name, $return_type, $context);
|
2016-05-09 14:56:07 +02:00
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
$context->vars_possibly_in_scope[$var_id] = true;
|
2016-03-03 23:24:44 +01:00
|
|
|
}
|
2016-10-14 00:27:23 +02:00
|
|
|
else if ($stmt->var instanceof PhpParser\Node\Expr\StaticPropertyFetch &&
|
|
|
|
$stmt->var->class instanceof PhpParser\Node\Name &&
|
|
|
|
is_string($stmt->var->name)) {
|
|
|
|
|
|
|
|
$this->checkStaticPropertyAssignment($stmt->var, $return_type, $context);
|
|
|
|
|
|
|
|
$context->vars_possibly_in_scope[$var_id] = true;
|
|
|
|
}
|
2016-03-03 23:24:44 +01:00
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
if ($var_id && isset($context->vars_in_scope[$var_id]) && $context->vars_in_scope[$var_id]->isVoid()) {
|
2016-06-26 21:18:40 +02:00
|
|
|
if (IssueBuffer::accepts(
|
2016-10-14 00:27:23 +02:00
|
|
|
new FailedTypeResolution('Cannot assign ' . $var_id . ' to type void', $this->checked_file_name, $stmt->getLine()),
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->suppressed_issues
|
2016-06-06 02:25:16 +02:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-14 01:57:32 +02:00
|
|
|
}
|
|
|
|
}
|
2016-06-06 02:25:16 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* @return string|null
|
|
|
|
*/
|
2016-10-14 00:27:23 +02:00
|
|
|
public static function getVarId(PhpParser\Node\Expr $stmt, $this_class_name, $namespace = null, array $aliased_classes = null, &$nesting = null)
|
2016-06-14 01:57:32 +02:00
|
|
|
{
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\Variable && is_string($stmt->name)) {
|
2016-10-14 00:27:23 +02:00
|
|
|
return '$' . $stmt->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\StaticPropertyFetch
|
|
|
|
&& is_string($stmt->name)
|
|
|
|
&& $stmt->class instanceof PhpParser\Node\Name
|
|
|
|
) {
|
|
|
|
if (count($stmt->class->parts) === 1 && in_array($stmt->class->parts[0], ['self', 'static', 'parent'])) {
|
|
|
|
$absolute_class = $this_class_name;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$absolute_class = ClassLikeChecker::getAbsoluteClassFromName($stmt->class, $namespace, $aliased_classes);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $absolute_class . '::$' . $stmt->name;
|
|
|
|
|
2016-06-14 01:57:32 +02:00
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\PropertyFetch && is_string($stmt->name)) {
|
2016-10-14 00:27:23 +02:00
|
|
|
$object_id = self::getVarId($stmt->var, $this_class_name, $namespace, $aliased_classes);
|
2016-06-14 01:57:32 +02:00
|
|
|
|
|
|
|
if (!$object_id) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-06-15 01:22:29 +02:00
|
|
|
return $object_id . '->' . $stmt->name;
|
2016-06-14 01:57:32 +02:00
|
|
|
}
|
2016-10-09 23:54:58 +02:00
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\ArrayDimFetch && $nesting !== null) {
|
2016-09-12 06:02:26 +02:00
|
|
|
$nesting++;
|
2016-10-14 00:27:23 +02:00
|
|
|
return self::getVarId($stmt->var, $this_class_name, $namespace, $aliased_classes, $nesting);
|
2016-09-12 06:02:26 +02:00
|
|
|
}
|
2016-06-14 01:57:32 +02:00
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* @return string|null
|
|
|
|
*/
|
2016-10-14 00:27:23 +02:00
|
|
|
public static function getArrayVarId(PhpParser\Node\Expr $stmt, $this_class_name, $namespace, array $aliased_classes)
|
2016-09-14 00:58:49 +02:00
|
|
|
{
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\ArrayDimFetch && $stmt->dim instanceof PhpParser\Node\Scalar\String_) {
|
2016-10-14 00:27:23 +02:00
|
|
|
$root_var_id = self::getArrayVarId($stmt->var, $this_class_name, $namespace, $aliased_classes);
|
2016-09-14 00:58:49 +02:00
|
|
|
return $root_var_id ? $root_var_id . '[\'' . $stmt->dim->value . '\']' : null;
|
|
|
|
}
|
|
|
|
|
2016-10-14 00:27:23 +02:00
|
|
|
return self::getVarId($stmt, $this_class_name, $namespace, $aliased_classes);
|
2016-09-14 00:58:49 +02:00
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* @return false|null
|
2016-10-11 20:17:55 +02:00
|
|
|
* @psalm-suppress MixedMethodCall - some funky logic here
|
2016-10-09 23:54:58 +02:00
|
|
|
*/
|
2016-09-09 22:21:49 +02:00
|
|
|
protected function checkArrayAssignment(PhpParser\Node\Expr\ArrayDimFetch $stmt, Context $context, Type\Union $assignment_value_type)
|
2016-06-14 01:57:32 +02:00
|
|
|
{
|
2016-09-12 06:02:26 +02:00
|
|
|
if ($stmt->dim && $this->checkExpression($stmt->dim, $context, false) === false) {
|
2016-09-09 22:21:49 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
$assignment_key_type = null;
|
2016-10-03 04:00:42 +02:00
|
|
|
$assignment_key_value = null;
|
|
|
|
|
2016-09-09 22:21:49 +02:00
|
|
|
if ($stmt->dim) {
|
|
|
|
if (isset($stmt->dim->inferredType)) {
|
2016-10-09 23:54:58 +02:00
|
|
|
/** @var Type\Union */
|
2016-09-09 22:21:49 +02:00
|
|
|
$assignment_key_type = $stmt->dim->inferredType;
|
2016-10-03 04:00:42 +02:00
|
|
|
|
|
|
|
if ($stmt->dim instanceof PhpParser\Node\Scalar\String_) {
|
|
|
|
$assignment_key_value = $stmt->dim->value;
|
|
|
|
}
|
2016-09-09 22:21:49 +02:00
|
|
|
}
|
|
|
|
else {
|
2016-09-21 03:45:49 +02:00
|
|
|
$assignment_key_type = Type::getMixed();
|
2016-09-09 22:21:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$assignment_key_type = Type::getInt();
|
|
|
|
}
|
|
|
|
|
2016-09-22 19:43:53 +02:00
|
|
|
$nesting = 0;
|
2016-10-14 00:27:23 +02:00
|
|
|
$var_id = self::getVarId($stmt->var, $this->absolute_class, $this->namespace, $this->aliased_classes, $nesting);
|
2016-09-22 19:43:53 +02:00
|
|
|
$is_object = $var_id && isset($context->vars_in_scope[$var_id]) && $context->vars_in_scope[$var_id]->hasObjectType();
|
2016-10-02 16:13:35 +02:00
|
|
|
$is_string = $var_id && isset($context->vars_in_scope[$var_id]) && $context->vars_in_scope[$var_id]->hasString();
|
2016-09-22 19:43:53 +02:00
|
|
|
|
2016-10-03 04:00:42 +02:00
|
|
|
if ($this->checkExpression($stmt->var, $context, !$is_object, $assignment_key_type, $assignment_value_type, $assignment_key_value) === false) {
|
2016-09-21 03:45:49 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-14 00:27:23 +02:00
|
|
|
$array_var_id = self::getArrayVarId($stmt->var, $this->absolute_class, $this->namespace, $this->aliased_classes);
|
2016-09-21 03:45:49 +02:00
|
|
|
$keyed_array_var_id = $array_var_id && $stmt->dim instanceof PhpParser\Node\Scalar\String_
|
|
|
|
? $array_var_id . '[\'' . $stmt->dim->value . '\']'
|
|
|
|
: null;
|
2016-06-14 01:57:32 +02:00
|
|
|
|
2016-09-21 03:45:49 +02:00
|
|
|
if (isset($stmt->var->inferredType)) {
|
|
|
|
$return_type = $stmt->var->inferredType;
|
2016-08-12 23:45:23 +02:00
|
|
|
|
2016-10-02 16:13:35 +02:00
|
|
|
if ($is_object) {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
elseif ($is_string) {
|
|
|
|
foreach ($assignment_value_type->types as $value_type) {
|
|
|
|
if (!$value_type->isString()) {
|
|
|
|
if ($value_type->isMixed()) {
|
|
|
|
if (IssueBuffer::accepts(
|
2016-10-09 23:54:58 +02:00
|
|
|
new MixedStringOffsetAssignment(
|
2016-10-14 00:27:23 +02:00
|
|
|
'Cannot assign a mixed variable to a string offset for ' . $var_id,
|
2016-10-02 16:13:35 +02:00
|
|
|
$this->checked_file_name,
|
|
|
|
$stmt->getLine()
|
|
|
|
),
|
|
|
|
$this->suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-09-12 06:02:26 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidArrayAssignment(
|
2016-10-14 00:27:23 +02:00
|
|
|
'Cannot assign string offset for ' . $var_id . ' of type ' . $value_type,
|
2016-10-09 23:54:58 +02:00
|
|
|
$this->checked_file_name,
|
|
|
|
$stmt->getLine()
|
|
|
|
),
|
|
|
|
$this->suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
2016-10-02 16:13:35 +02:00
|
|
|
}
|
2016-10-09 23:54:58 +02:00
|
|
|
|
|
|
|
break;
|
2016-10-02 16:13:35 +02:00
|
|
|
}
|
2016-09-21 03:45:49 +02:00
|
|
|
}
|
2016-10-02 16:13:35 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// we want to support multiple array types:
|
|
|
|
// - Dictionaries (which have the type array<string,T>)
|
|
|
|
// - pseudo-objects (which have the type array<string,mixed>)
|
|
|
|
// - typed arrays (which have the type array<int,T>)
|
|
|
|
// and completely freeform arrays
|
|
|
|
//
|
|
|
|
// When making assignments, we generally only know the shape of the array
|
|
|
|
// as it is being created.
|
|
|
|
if ($keyed_array_var_id) {
|
|
|
|
// when we have a pattern like
|
|
|
|
// $a = [];
|
|
|
|
// $a['b']['c']['d'] = 1;
|
|
|
|
// $a['c'] = 2;
|
|
|
|
// we need to create each type in turn
|
|
|
|
// so we get
|
|
|
|
// typeof $a['b']['c']['d'] => int
|
2016-10-03 04:00:42 +02:00
|
|
|
// typeof $a['b']['c'] => object-like{d:int}
|
|
|
|
// typeof $a['b'] => object-like{c:object-like{d:int}}
|
2016-10-02 16:13:35 +02:00
|
|
|
// typeof $a['c'] => int
|
2016-10-03 04:00:42 +02:00
|
|
|
// typeof $a => object-like{b:object-like{c:object-like{d:int}},c:int}
|
2016-10-02 16:13:35 +02:00
|
|
|
|
|
|
|
$context->vars_in_scope[$keyed_array_var_id] = $assignment_value_type;
|
|
|
|
|
|
|
|
$stmt->inferredType = $assignment_value_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$nesting) {
|
2016-10-03 04:00:42 +02:00
|
|
|
if ($assignment_key_type->hasString()
|
|
|
|
&& $assignment_key_value
|
|
|
|
&& (!isset($context->vars_in_scope[$var_id])
|
|
|
|
|| $context->vars_in_scope[$var_id]->hasObjectLike()
|
|
|
|
|| ($context->vars_in_scope[$var_id]->hasArray()
|
|
|
|
&& $context->vars_in_scope[$var_id]->types['array']->type_params[0]->isEmpty()))
|
|
|
|
) {
|
|
|
|
$assignment_type = new Type\Union([
|
|
|
|
new Type\ObjectLike(
|
|
|
|
'object-like',
|
|
|
|
[
|
|
|
|
$assignment_key_value => $assignment_value_type
|
|
|
|
]
|
|
|
|
)
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$assignment_type = new Type\Union([
|
|
|
|
new Type\Generic(
|
|
|
|
'array',
|
|
|
|
[
|
|
|
|
$assignment_key_type,
|
|
|
|
$assignment_value_type
|
|
|
|
]
|
|
|
|
)
|
|
|
|
]);
|
|
|
|
}
|
2016-10-02 16:13:35 +02:00
|
|
|
|
|
|
|
if (isset($context->vars_in_scope[$var_id])) {
|
|
|
|
$context->vars_in_scope[$var_id] = Type::combineUnionTypes(
|
|
|
|
$context->vars_in_scope[$var_id],
|
|
|
|
$assignment_type
|
|
|
|
);
|
|
|
|
}
|
2016-10-03 04:00:42 +02:00
|
|
|
else {
|
|
|
|
$context->vars_in_scope[$var_id] = $assignment_type;
|
|
|
|
}
|
2016-10-02 16:13:35 +02:00
|
|
|
}
|
2016-06-15 01:22:29 +02:00
|
|
|
}
|
2016-10-02 16:13:35 +02:00
|
|
|
|
2016-06-15 01:22:29 +02:00
|
|
|
}
|
2016-09-21 03:45:49 +02:00
|
|
|
else {
|
|
|
|
$context->vars_in_scope[$var_id] = Type::getMixed();
|
|
|
|
}
|
2016-06-15 01:22:29 +02:00
|
|
|
}
|
2016-06-14 01:57:32 +02:00
|
|
|
|
2016-06-15 01:22:29 +02:00
|
|
|
/**
|
|
|
|
* @param Type\Atomic $type
|
2016-10-09 23:54:58 +02:00
|
|
|
* @param string|null $var_id
|
2016-06-15 01:22:29 +02:00
|
|
|
* @param int $line_number
|
2016-08-12 23:45:23 +02:00
|
|
|
* @return Type\Atomic|null|false
|
2016-06-15 01:22:29 +02:00
|
|
|
*/
|
2016-09-09 22:21:49 +02:00
|
|
|
protected function refineArrayType(Type\Atomic $type, Type\Union $assignment_key_type, Type\Union $assignment_value_type, $var_id, $line_number)
|
2016-06-15 01:22:29 +02:00
|
|
|
{
|
|
|
|
if ($type->value === 'null') {
|
2016-06-26 21:18:40 +02:00
|
|
|
if (IssueBuffer::accepts(
|
2016-07-22 19:29:46 +02:00
|
|
|
new NullReference(
|
|
|
|
'Cannot assign value on possibly null array ' . $var_id,
|
2016-08-25 01:00:44 +02:00
|
|
|
$this->checked_file_name,
|
2016-07-22 19:29:46 +02:00
|
|
|
$line_number
|
|
|
|
),
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->suppressed_issues
|
2016-06-15 01:22:29 +02:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-14 01:57:32 +02:00
|
|
|
|
2016-06-15 01:22:29 +02:00
|
|
|
return $type;
|
|
|
|
}
|
|
|
|
|
2016-09-21 03:45:49 +02:00
|
|
|
if ($type->value === 'string' && $assignment_value_type->hasString() && !$assignment_key_type->hasString()) {
|
|
|
|
return;
|
2016-06-17 22:58:15 +02:00
|
|
|
}
|
|
|
|
|
2016-09-22 00:26:26 +02:00
|
|
|
if (!$type->isArray() && !$type->isObjectLike() && !ClassChecker::classImplements($type->value, 'ArrayAccess')) {
|
2016-06-26 21:18:40 +02:00
|
|
|
if (IssueBuffer::accepts(
|
2016-07-22 19:29:46 +02:00
|
|
|
new InvalidArrayAssignment(
|
2016-10-14 00:27:23 +02:00
|
|
|
'Cannot assign value on variable ' . $var_id . ' of type ' . $type->value . ' that does not implement ArrayAccess',
|
2016-08-25 01:00:44 +02:00
|
|
|
$this->checked_file_name,
|
2016-07-22 19:29:46 +02:00
|
|
|
$line_number
|
|
|
|
),
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->suppressed_issues
|
2016-06-15 01:22:29 +02:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $type;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($type instanceof Type\Generic) {
|
2016-09-09 22:21:49 +02:00
|
|
|
if ($type->isArray()) {
|
|
|
|
if ($type->type_params[1]->isEmpty()) {
|
|
|
|
$type->type_params[0] = $assignment_key_type;
|
|
|
|
$type->type_params[1] = $assignment_value_type;
|
|
|
|
return $type;
|
|
|
|
}
|
2016-06-15 01:22:29 +02:00
|
|
|
|
2016-09-09 22:21:49 +02:00
|
|
|
if ((string) $type->type_params[0] !== (string) $assignment_key_type) {
|
|
|
|
$type->type_params[0] = Type::combineUnionTypes($type->type_params[0], $assignment_key_type);
|
|
|
|
}
|
2016-06-15 01:22:29 +02:00
|
|
|
|
2016-09-09 22:21:49 +02:00
|
|
|
if ((string) $type->type_params[1] !== (string) $assignment_value_type) {
|
|
|
|
$type->type_params[1] = Type::combineUnionTypes($type->type_params[1], $assignment_value_type);
|
|
|
|
}
|
2016-06-14 01:57:32 +02:00
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
2016-06-15 01:22:29 +02:00
|
|
|
|
|
|
|
return $type;
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* @return false|null
|
|
|
|
*/
|
2016-08-11 00:10:12 +02:00
|
|
|
protected function checkAssignmentOperation(PhpParser\Node\Expr\AssignOp $stmt, Context $context)
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->checkExpression($stmt->var, $context) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-11 00:10:12 +02:00
|
|
|
return $this->checkExpression($stmt->expr, $context);
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* @return false|null
|
|
|
|
*/
|
2016-08-11 00:10:12 +02:00
|
|
|
protected function checkMethodCall(PhpParser\Node\Expr\MethodCall $stmt, Context $context)
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->checkExpression($stmt->var, $context) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-02-10 21:20:53 +01:00
|
|
|
$class_type = null;
|
2016-01-20 00:27:06 +01:00
|
|
|
$method_id = null;
|
|
|
|
|
|
|
|
if ($stmt->var instanceof PhpParser\Node\Expr\Variable) {
|
2016-08-11 00:10:12 +02:00
|
|
|
if (is_string($stmt->var->name) && $stmt->var->name === 'this' && !$this->class_name) {
|
2016-06-26 21:18:40 +02:00
|
|
|
if (IssueBuffer::accepts(
|
2016-08-25 01:00:44 +02:00
|
|
|
new InvalidScope('Use of $this in non-class context', $this->checked_file_name, $stmt->getLine()),
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->suppressed_issues
|
2016-06-06 02:25:16 +02:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-14 00:27:23 +02:00
|
|
|
$var_id = self::getVarId($stmt->var, $this->absolute_class, $this->namespace, $this->aliased_classes);
|
2016-06-15 01:22:29 +02:00
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
$class_type = isset($context->vars_in_scope[$var_id]) ? $context->vars_in_scope[$var_id] : null;
|
2016-04-12 22:12:20 +02:00
|
|
|
|
2016-06-30 06:27:47 +02:00
|
|
|
if (isset($stmt->var->inferredType)) {
|
2016-10-09 23:54:58 +02:00
|
|
|
/** @var Type\Union */
|
2016-06-30 06:27:47 +02:00
|
|
|
$class_type = $stmt->var->inferredType;
|
|
|
|
}
|
|
|
|
elseif (!$class_type) {
|
2016-06-16 18:04:55 +02:00
|
|
|
$stmt->inferredType = Type::getMixed();
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
if ($stmt->var instanceof PhpParser\Node\Expr\Variable
|
|
|
|
&& $stmt->var->name === 'this'
|
|
|
|
&& is_string($stmt->name)
|
|
|
|
&& $this->source instanceof FunctionLikeChecker
|
|
|
|
) {
|
2016-08-11 00:10:12 +02:00
|
|
|
$this_method_id = $this->source->getMethodId();
|
2016-05-09 14:56:07 +02:00
|
|
|
|
2016-08-11 00:10:12 +02:00
|
|
|
if (!isset(self::$this_calls[$this_method_id])) {
|
|
|
|
self::$this_calls[$this_method_id] = [];
|
2016-05-09 14:56:07 +02:00
|
|
|
}
|
|
|
|
|
2016-08-11 00:10:12 +02:00
|
|
|
self::$this_calls[$this_method_id][] = $stmt->name;
|
2016-05-16 05:06:03 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
if (($this_class = ClassLikeChecker::getThisClass()) &&
|
2016-05-20 06:24:26 +02:00
|
|
|
(
|
2016-10-09 23:54:58 +02:00
|
|
|
$this_class === $this->absolute_class ||
|
|
|
|
ClassChecker::classExtends($this_class, $this->absolute_class) ||
|
2016-08-11 00:10:12 +02:00
|
|
|
trait_exists($this->absolute_class)
|
2016-05-20 06:24:26 +02:00
|
|
|
)) {
|
2016-05-16 05:06:03 +02:00
|
|
|
|
2016-08-15 19:37:21 +02:00
|
|
|
$method_id = $this->absolute_class . '::' . strtolower($stmt->name);
|
2016-05-16 05:06:03 +02:00
|
|
|
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->checkInsideMethod($method_id, $context) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-05-16 05:06:03 +02:00
|
|
|
}
|
2016-05-09 14:56:07 +02:00
|
|
|
}
|
|
|
|
|
2016-10-02 19:05:49 +02:00
|
|
|
if (!$this->check_methods || !$this->check_classes) {
|
2016-06-02 23:44:50 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-16 02:16:40 +02:00
|
|
|
if ($class_type && is_string($stmt->name)) {
|
2016-08-31 23:08:04 +02:00
|
|
|
$return_type = null;
|
|
|
|
|
2016-06-16 02:16:40 +02:00
|
|
|
foreach ($class_type->types as $type) {
|
|
|
|
$absolute_class = $type->value;
|
2016-04-12 01:13:50 +02:00
|
|
|
|
2016-06-06 02:25:16 +02:00
|
|
|
switch ($absolute_class) {
|
|
|
|
case 'null':
|
2016-06-26 21:18:40 +02:00
|
|
|
if (IssueBuffer::accepts(
|
2016-07-22 19:29:46 +02:00
|
|
|
new NullReference(
|
2016-10-14 00:27:23 +02:00
|
|
|
'Cannot call method ' . $stmt->name . ' on possibly null variable ' . $var_id,
|
2016-08-25 01:00:44 +02:00
|
|
|
$this->checked_file_name,
|
2016-07-22 19:29:46 +02:00
|
|
|
$stmt->getLine()
|
|
|
|
),
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->suppressed_issues
|
2016-06-06 02:25:16 +02:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
2016-05-10 20:00:44 +02:00
|
|
|
|
2016-06-06 02:25:16 +02:00
|
|
|
case 'int':
|
|
|
|
case 'bool':
|
2016-08-15 22:22:32 +02:00
|
|
|
case 'false':
|
2016-06-06 02:25:16 +02:00
|
|
|
case 'array':
|
2016-08-15 19:51:43 +02:00
|
|
|
case 'string':
|
2016-06-26 21:18:40 +02:00
|
|
|
if (IssueBuffer::accepts(
|
2016-07-22 19:29:46 +02:00
|
|
|
new InvalidArgument(
|
2016-10-14 00:27:23 +02:00
|
|
|
'Cannot call method ' . $stmt->name . ' on ' . $class_type . ' variable ' . $var_id,
|
2016-08-25 01:00:44 +02:00
|
|
|
$this->checked_file_name,
|
2016-07-22 19:29:46 +02:00
|
|
|
$stmt->getLine()
|
|
|
|
),
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->suppressed_issues
|
2016-06-06 02:25:16 +02:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
2016-04-04 02:14:19 +02:00
|
|
|
|
2016-06-06 02:25:16 +02:00
|
|
|
case 'mixed':
|
2016-08-15 19:59:23 +02:00
|
|
|
case 'object':
|
2016-07-08 01:37:39 +02:00
|
|
|
if (IssueBuffer::accepts(
|
2016-07-22 19:29:46 +02:00
|
|
|
new MixedMethodCall(
|
2016-10-14 00:27:23 +02:00
|
|
|
'Cannot call method ' . $stmt->name . ' on a mixed variable ' . $var_id,
|
2016-08-25 01:00:44 +02:00
|
|
|
$this->checked_file_name,
|
2016-07-22 19:29:46 +02:00
|
|
|
$stmt->getLine()
|
|
|
|
),
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->suppressed_issues
|
2016-07-08 01:37:39 +02:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-06 02:25:16 +02:00
|
|
|
break;
|
2016-04-12 22:12:20 +02:00
|
|
|
|
2016-08-16 02:00:02 +02:00
|
|
|
case 'static':
|
2016-09-01 06:07:29 +02:00
|
|
|
$absolute_class = (string) $context->self;
|
2016-10-02 19:05:49 +02:00
|
|
|
// fall through to default
|
2016-08-16 02:00:02 +02:00
|
|
|
|
2016-06-06 02:25:16 +02:00
|
|
|
default:
|
2016-10-02 19:05:49 +02:00
|
|
|
if (!method_exists($absolute_class, '__call') && !self::isMock($absolute_class) && !$this->isPhantomClass($absolute_class)) {
|
2016-08-15 05:24:16 +02:00
|
|
|
$does_class_exist = ClassLikeChecker::checkAbsoluteClassOrInterface(
|
|
|
|
$absolute_class,
|
2016-08-25 01:00:44 +02:00
|
|
|
$this->checked_file_name,
|
2016-08-15 05:24:16 +02:00
|
|
|
$stmt->getLine(),
|
|
|
|
$this->suppressed_issues
|
|
|
|
);
|
2016-07-24 23:10:16 +02:00
|
|
|
|
|
|
|
if (!$does_class_exist) {
|
|
|
|
return $does_class_exist;
|
2016-06-06 02:25:16 +02:00
|
|
|
}
|
2016-04-16 17:28:19 +02:00
|
|
|
|
2016-08-15 19:37:21 +02:00
|
|
|
$method_id = $absolute_class . '::' . strtolower($stmt->name);
|
2016-08-15 19:51:43 +02:00
|
|
|
$cased_method_id = $absolute_class . '::' . $stmt->name;
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-08-11 00:10:12 +02:00
|
|
|
if (!isset(self::$method_call_index[$method_id])) {
|
|
|
|
self::$method_call_index[$method_id] = [];
|
2016-06-06 02:25:16 +02:00
|
|
|
}
|
2016-02-27 01:11:11 +01:00
|
|
|
|
2016-08-15 21:14:28 +02:00
|
|
|
if ($this->source instanceof MethodChecker) {
|
2016-08-11 00:10:12 +02:00
|
|
|
self::$method_call_index[$method_id][] = $this->source->getMethodId();
|
2016-06-06 02:25:16 +02:00
|
|
|
}
|
|
|
|
else {
|
2016-08-11 00:10:12 +02:00
|
|
|
self::$method_call_index[$method_id][] = $this->source->getFileName();
|
2016-06-06 02:25:16 +02:00
|
|
|
}
|
2016-02-27 01:11:11 +01:00
|
|
|
|
2016-08-25 01:00:44 +02:00
|
|
|
$does_method_exist = MethodChecker::checkMethodExists($cased_method_id, $this->checked_file_name, $stmt->getLine(), $this->suppressed_issues);
|
2016-07-24 23:10:16 +02:00
|
|
|
|
|
|
|
if (!$does_method_exist) {
|
|
|
|
return $does_method_exist;
|
2016-06-16 18:05:37 +02:00
|
|
|
}
|
2016-05-16 05:06:03 +02:00
|
|
|
|
2016-08-08 17:28:14 +02:00
|
|
|
/**
|
2016-08-13 20:20:46 +02:00
|
|
|
if (ClassLikeChecker::getThisClass() && ClassChecker::classExtends(ClassLikeChecker::getThisClass(), $this->absolute_class)) {
|
2016-08-08 17:28:14 +02:00
|
|
|
$calling_context = $context->self;
|
|
|
|
}
|
|
|
|
**/
|
2016-05-21 01:06:42 +02:00
|
|
|
|
2016-08-15 21:14:28 +02:00
|
|
|
if (MethodChecker::checkMethodVisibility($method_id, $context->self, $this->source, $stmt->getLine(), $this->suppressed_issues) === false) {
|
2016-08-08 17:28:14 +02:00
|
|
|
return false;
|
2016-07-22 19:29:46 +02:00
|
|
|
}
|
|
|
|
|
2016-08-25 01:00:44 +02:00
|
|
|
if (MethodChecker::checkMethodNotDeprecated($method_id, $this->checked_file_name, $stmt->getLine(), $this->suppressed_issues) === false) {
|
2016-07-22 19:29:46 +02:00
|
|
|
return false;
|
2016-06-06 02:25:16 +02:00
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-08-31 23:08:04 +02:00
|
|
|
$return_type_candidate = MethodChecker::getMethodReturnTypes($method_id);
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-08-31 23:08:04 +02:00
|
|
|
if ($return_type_candidate) {
|
|
|
|
$return_type_candidate = self::fleshOutTypes($return_type_candidate, $stmt->args, $absolute_class, $method_id);
|
2016-02-27 01:11:11 +01:00
|
|
|
|
2016-08-31 23:08:04 +02:00
|
|
|
if (!$return_type) {
|
|
|
|
$return_type = $return_type_candidate;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$return_type = Type::combineUnionTypes($return_type_candidate, $return_type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$return_type = Type::getMixed();
|
2016-06-06 02:25:16 +02:00
|
|
|
}
|
|
|
|
}
|
2016-10-02 03:02:13 +02:00
|
|
|
else {
|
|
|
|
$return_type = Type::getMixed();
|
|
|
|
}
|
2016-02-10 21:20:53 +01:00
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
2016-08-31 23:08:04 +02:00
|
|
|
|
|
|
|
$stmt->inferredType = $return_type;
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-08-30 06:04:10 +02:00
|
|
|
if ($this->checkFunctionArguments($stmt->args, $method_id, $context, $stmt->getLine()) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* @param string $method_id
|
|
|
|
* @param Context $context
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-08-11 00:10:12 +02:00
|
|
|
protected function checkInsideMethod($method_id, Context $context)
|
2016-06-03 01:21:48 +02:00
|
|
|
{
|
2016-08-13 20:20:46 +02:00
|
|
|
$method_checker = ClassLikeChecker::getMethodChecker($method_id);
|
2016-06-03 01:21:48 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
if ($method_checker && $this->source instanceof FunctionLikeChecker && $method_checker->getMethodId() !== $this->source->getMethodId()) {
|
2016-08-14 03:14:32 +02:00
|
|
|
$this_context = new Context($this->file_name, (string) $context->vars_in_scope['this']);
|
2016-06-03 01:21:48 +02:00
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
foreach ($context->vars_possibly_in_scope as $var => $type) {
|
2016-06-03 01:21:48 +02:00
|
|
|
if (strpos($var, 'this->') === 0) {
|
2016-06-20 06:38:13 +02:00
|
|
|
$this_context->vars_possibly_in_scope[$var] = true;
|
2016-06-03 01:21:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
foreach ($context->vars_in_scope as $var => $type) {
|
2016-06-03 01:21:48 +02:00
|
|
|
if (strpos($var, 'this->') === 0) {
|
2016-06-20 06:38:13 +02:00
|
|
|
$this_context->vars_in_scope[$var] = $type;
|
2016-06-03 01:21:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-20 07:29:30 +02:00
|
|
|
$this_context->vars_in_scope['this'] = $context->vars_in_scope['this'];
|
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
$method_checker->check($this_context);
|
2016-06-03 01:21:48 +02:00
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
foreach ($this_context->vars_in_scope as $var => $type) {
|
|
|
|
$context->vars_possibly_in_scope[$var] = true;
|
2016-06-03 01:21:48 +02:00
|
|
|
}
|
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
foreach ($this_context->vars_in_scope as $var => $type) {
|
|
|
|
$context->vars_in_scope[$var] = $type;
|
2016-06-03 01:21:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* @return false|null
|
|
|
|
*/
|
2016-08-11 00:10:12 +02:00
|
|
|
protected function checkClosureUses(PhpParser\Node\Expr\Closure $stmt, Context $context)
|
2016-04-03 02:27:36 +02:00
|
|
|
{
|
|
|
|
foreach ($stmt->uses as $use) {
|
2016-10-14 00:27:23 +02:00
|
|
|
if (!isset($context->vars_in_scope['$' . $use->var])) {
|
2016-04-03 02:27:36 +02:00
|
|
|
if ($use->byRef) {
|
2016-10-14 00:27:23 +02:00
|
|
|
$context->vars_in_scope['$' . $use->var] = Type::getMixed();
|
|
|
|
$context->vars_possibly_in_scope['$' . $use->var] = true;
|
2016-04-03 02:27:36 +02:00
|
|
|
$this->registerVariable($use->var, $use->getLine());
|
2016-06-06 02:25:16 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-04-03 02:27:36 +02:00
|
|
|
|
2016-10-14 00:27:23 +02:00
|
|
|
if (!isset($context->vars_possibly_in_scope['$' . $use->var])) {
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->check_variables) {
|
2016-06-27 19:22:16 +02:00
|
|
|
IssueBuffer::add(
|
2016-08-25 01:00:44 +02:00
|
|
|
new UndefinedVariable('Cannot find referenced variable $' . $use->var, $this->checked_file_name, $use->getLine())
|
2016-06-27 19:22:16 +02:00
|
|
|
);
|
|
|
|
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-04-03 02:27:36 +02:00
|
|
|
|
2016-08-11 00:10:12 +02:00
|
|
|
if (isset($this->all_vars[$use->var])) {
|
|
|
|
if (!isset($this->warn_vars[$use->var])) {
|
|
|
|
$this->warn_vars[$use->var] = true;
|
2016-06-26 21:18:40 +02:00
|
|
|
if (IssueBuffer::accepts(
|
2016-06-14 01:57:32 +02:00
|
|
|
new PossiblyUndefinedVariable(
|
2016-08-11 00:10:12 +02:00
|
|
|
'Possibly undefined variable $' . $use->var . ', first seen on line ' . $this->all_vars[$use->var],
|
2016-08-25 01:00:44 +02:00
|
|
|
$this->checked_file_name,
|
2016-06-06 02:25:16 +02:00
|
|
|
$use->getLine()
|
2016-07-22 19:29:46 +02:00
|
|
|
),
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->suppressed_issues
|
2016-06-06 02:25:16 +02:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-03 02:27:36 +02:00
|
|
|
}
|
|
|
|
|
2016-06-06 02:25:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->check_variables) {
|
2016-06-27 19:22:16 +02:00
|
|
|
IssueBuffer::add(
|
2016-08-25 01:00:44 +02:00
|
|
|
new UndefinedVariable('Cannot find referenced variable $' . $use->var, $this->checked_file_name, $use->getLine())
|
2016-06-27 19:22:16 +02:00
|
|
|
);
|
|
|
|
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
2016-04-03 02:27:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-27 00:42:48 +02:00
|
|
|
/**
|
2016-10-09 23:54:58 +02:00
|
|
|
* @return false|null
|
2016-04-27 00:42:48 +02:00
|
|
|
*/
|
2016-08-11 00:10:12 +02:00
|
|
|
protected function checkStaticCall(PhpParser\Node\Expr\StaticCall $stmt, Context $context)
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
|
|
|
if ($stmt->class instanceof PhpParser\Node\Expr\Variable || $stmt->class instanceof PhpParser\Node\Expr\ArrayDimFetch) {
|
|
|
|
// this is when calling $some_class::staticMethod() - which is a shitty way of doing things
|
|
|
|
// because it can't be statically type-checked
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$method_id = null;
|
|
|
|
$absolute_class = null;
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
$lhs_type = null;
|
|
|
|
|
|
|
|
if ($stmt->class instanceof PhpParser\Node\Name) {
|
|
|
|
$absolute_class = null;
|
|
|
|
|
|
|
|
if (count($stmt->class->parts) === 1 && in_array($stmt->class->parts[0], ['self', 'static', 'parent'])) {
|
|
|
|
if ($stmt->class->parts[0] === 'parent') {
|
|
|
|
if ($this->parent_class === null) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new ParentNotFound('Cannot call method on parent as this class does not extend another', $this->checked_file_name, $stmt->getLine()),
|
|
|
|
$this->suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-06 02:25:16 +02:00
|
|
|
}
|
2016-10-09 23:54:58 +02:00
|
|
|
|
|
|
|
$absolute_class = $this->parent_class;
|
|
|
|
} else {
|
|
|
|
$absolute_class = ($this->namespace ? $this->namespace . '\\' : '') . $this->class_name;
|
2016-04-17 16:25:43 +02:00
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
if ($this->isPhantomClass($absolute_class)) {
|
|
|
|
return;
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
2016-10-09 23:54:58 +02:00
|
|
|
elseif ($this->check_classes) {
|
2016-02-05 16:20:06 +01:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
$absolute_class = ClassLikeChecker::getAbsoluteClassFromName($stmt->class, $this->namespace, $this->aliased_classes);
|
2016-10-02 19:05:49 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
if ($this->isPhantomClass($absolute_class)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$does_class_exist = ClassLikeChecker::checkAbsoluteClassOrInterface(
|
|
|
|
$absolute_class,
|
|
|
|
$this->checked_file_name,
|
|
|
|
$stmt->getLine(),
|
|
|
|
$this->suppressed_issues
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!$does_class_exist) {
|
|
|
|
return $does_class_exist;
|
|
|
|
}
|
2016-10-02 19:05:49 +02:00
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
if ($stmt->class->parts === ['parent'] && is_string($stmt->name)) {
|
|
|
|
if (ClassLikeChecker::getThisClass()) {
|
|
|
|
$method_id = $absolute_class . '::' . strtolower($stmt->name);
|
2016-07-24 23:10:16 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
if ($this->checkInsideMethod($method_id, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-06-06 02:25:16 +02:00
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
if ($absolute_class) {
|
|
|
|
$lhs_type = new Type\Union([new Type\Atomic($absolute_class)]);
|
|
|
|
}
|
2016-06-02 23:44:50 +02:00
|
|
|
}
|
2016-10-09 23:54:58 +02:00
|
|
|
else {
|
|
|
|
$this->checkExpression($stmt->class, $context);
|
2016-06-02 23:44:50 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/** @var Type\Union */
|
|
|
|
$lhs_type = $stmt->class->inferredType;
|
|
|
|
}
|
2016-06-03 01:21:48 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
if (!$this->check_methods || !$lhs_type) {
|
|
|
|
return;
|
2016-06-03 01:21:48 +02:00
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
foreach ($lhs_type->types as $lhs_type_part) {
|
|
|
|
$absolute_class = $lhs_type_part->value;
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
if (is_string($stmt->name) && !method_exists($absolute_class, '__callStatic') && !self::isMock($absolute_class)) {
|
|
|
|
$method_id = $absolute_class . '::' . strtolower($stmt->name);
|
|
|
|
$cased_method_id = $absolute_class . '::' . $stmt->name;
|
2016-02-27 01:11:11 +01:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
if (!isset(self::$method_call_index[$method_id])) {
|
|
|
|
self::$method_call_index[$method_id] = [];
|
|
|
|
}
|
2016-02-27 01:11:11 +01:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
if ($this->source instanceof MethodChecker) {
|
|
|
|
self::$method_call_index[$method_id][] = $this->source->getMethodId();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
self::$method_call_index[$method_id][] = $this->source->getFileName();
|
|
|
|
}
|
2016-07-24 23:10:16 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
$does_method_exist = MethodChecker::checkMethodExists($cased_method_id, $this->checked_file_name, $stmt->getLine(), $this->suppressed_issues);
|
2016-07-22 19:29:46 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
if (!$does_method_exist) {
|
|
|
|
return $does_method_exist;
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
if (MethodChecker::checkMethodVisibility($method_id, $context->self, $this->source, $stmt->getLine(), $this->suppressed_issues) === false) {
|
2016-07-22 19:29:46 +02:00
|
|
|
return false;
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
2016-10-09 23:54:58 +02:00
|
|
|
|
|
|
|
if ($this->is_static) {
|
2016-08-25 01:00:44 +02:00
|
|
|
if (MethodChecker::checkMethodStatic($method_id, $this->checked_file_name, $stmt->getLine(), $this->suppressed_issues) === false) {
|
2016-07-22 19:29:46 +02:00
|
|
|
return false;
|
2016-02-05 16:20:06 +01:00
|
|
|
}
|
|
|
|
}
|
2016-10-09 23:54:58 +02:00
|
|
|
else {
|
|
|
|
if ($stmt->class->parts[0] === 'self' && $stmt->name !== '__construct') {
|
|
|
|
if (MethodChecker::checkMethodStatic($method_id, $this->checked_file_name, $stmt->getLine(), $this->suppressed_issues) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
if (MethodChecker::checkMethodNotDeprecated($method_id, $this->checked_file_name, $stmt->getLine(), $this->suppressed_issues) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$return_types = MethodChecker::getMethodReturnTypes($method_id);
|
|
|
|
|
|
|
|
if ($return_types) {
|
|
|
|
$return_types = self::fleshOutTypes($return_types, $stmt->args, $stmt->class->parts === ['parent'] ? $this->absolute_class : $absolute_class, $method_id);
|
2016-07-22 19:29:46 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
if (isset($stmt->inferredType)) {
|
|
|
|
$stmt->inferredType = Type::combineUnionTypes($stmt->inferredType, $return_types);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$stmt->inferredType = $return_types;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
if ($this->checkFunctionArguments($stmt->args, $method_id, $context, $stmt->getLine()) === false) {
|
|
|
|
return false;
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
return;
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-08-13 17:28:06 +02:00
|
|
|
/**
|
2016-09-01 06:07:29 +02:00
|
|
|
* @param Type\Union $return_type
|
2016-08-13 17:28:06 +02:00
|
|
|
* @param array<PhpParser\Node\Arg> $args
|
|
|
|
* @param string|null $calling_class
|
|
|
|
* @param string|null $method_id
|
|
|
|
* @return Type\Union
|
|
|
|
*/
|
2016-08-09 21:02:30 +02:00
|
|
|
public static function fleshOutTypes(Type\Union $return_type, array $args, $calling_class, $method_id)
|
2016-02-27 01:11:11 +01:00
|
|
|
{
|
2016-08-16 02:00:02 +02:00
|
|
|
$return_type = clone $return_type;
|
2016-07-19 01:04:51 +02:00
|
|
|
|
2016-08-16 02:00:02 +02:00
|
|
|
$new_return_type_parts = [];
|
|
|
|
|
|
|
|
foreach ($return_type->types as $key => $return_type_part) {
|
|
|
|
$new_return_type_parts[] = self::fleshOutAtomicType($return_type_part, $args, $calling_class, $method_id);
|
2016-06-15 01:22:29 +02:00
|
|
|
}
|
2016-03-10 21:43:23 +01:00
|
|
|
|
2016-08-16 02:00:02 +02:00
|
|
|
return new Type\Union($new_return_type_parts);
|
2016-06-15 01:22:29 +02:00
|
|
|
}
|
2016-04-20 19:36:46 +02:00
|
|
|
|
2016-08-13 17:28:06 +02:00
|
|
|
/**
|
|
|
|
* @param Type\Atomic &$return_type
|
|
|
|
* @param array<PhpParser\Node\Arg> $args
|
|
|
|
* @param string|null $calling_class
|
|
|
|
* @param string|null $method_id
|
2016-09-09 22:21:49 +02:00
|
|
|
* @return Type\Atomic
|
2016-08-13 17:28:06 +02:00
|
|
|
*/
|
2016-08-16 02:00:02 +02:00
|
|
|
protected static function fleshOutAtomicType(Type\Atomic $return_type, array $args, $calling_class, $method_id)
|
2016-06-15 01:22:29 +02:00
|
|
|
{
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($return_type->value === '$this' || $return_type->value === 'static' || $return_type->value === 'self') {
|
2016-08-13 17:28:06 +02:00
|
|
|
if (!$calling_class) {
|
2016-10-09 23:54:58 +02:00
|
|
|
throw new \InvalidArgumentException('Cannot handle ' . $return_type->value . ' when $calling_class is empty', null);
|
2016-08-13 17:28:06 +02:00
|
|
|
}
|
|
|
|
|
2016-08-09 00:55:22 +02:00
|
|
|
$return_type->value = $calling_class;
|
2016-06-15 01:22:29 +02:00
|
|
|
}
|
|
|
|
else if ($return_type->value[0] === '$') {
|
2016-08-15 21:14:28 +02:00
|
|
|
$method_params = MethodChecker::getMethodParams($method_id);
|
2016-02-27 01:11:11 +01:00
|
|
|
|
2016-06-15 01:22:29 +02:00
|
|
|
foreach ($args as $i => $arg) {
|
|
|
|
$method_param = $method_params[$i];
|
2016-02-27 01:11:11 +01:00
|
|
|
|
2016-10-10 07:35:12 +02:00
|
|
|
if ($return_type->value === '$' . $method_param->name) {
|
2016-08-13 17:28:06 +02:00
|
|
|
$arg_value = $arg->value;
|
|
|
|
if ($arg_value instanceof PhpParser\Node\Scalar\String_) {
|
|
|
|
$return_type->value = preg_replace('/^\\\/', '', $arg_value->value);
|
2016-04-20 19:36:46 +02:00
|
|
|
}
|
2016-02-27 01:11:11 +01:00
|
|
|
}
|
|
|
|
}
|
2016-04-20 19:36:46 +02:00
|
|
|
|
2016-06-15 01:22:29 +02:00
|
|
|
if ($return_type->value[0] === '$') {
|
2016-09-09 22:21:49 +02:00
|
|
|
$return_type = new Type\Atomic('mixed');
|
2016-06-15 01:22:29 +02:00
|
|
|
}
|
2016-02-27 01:11:11 +01:00
|
|
|
}
|
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
if ($return_type instanceof Type\Generic) {
|
2016-08-16 02:00:02 +02:00
|
|
|
foreach ($return_type->type_params as &$type_param) {
|
2016-08-13 17:28:06 +02:00
|
|
|
$type_param = self::fleshOutTypes($type_param, $args, $calling_class, $method_id);
|
2016-06-15 01:22:29 +02:00
|
|
|
}
|
|
|
|
}
|
2016-08-16 02:00:02 +02:00
|
|
|
|
|
|
|
return $return_type;
|
2016-02-27 01:11:11 +01:00
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* @param string $call
|
|
|
|
* @param PhpParser\Node\Arg[] $args
|
|
|
|
* @param string $method_id
|
|
|
|
* @return string|null
|
|
|
|
*/
|
2016-08-11 00:10:12 +02:00
|
|
|
protected static function getMethodFromCallBlock($call, array $args, $method_id)
|
2016-03-10 21:43:23 +01:00
|
|
|
{
|
|
|
|
$absolute_class = explode('::', $method_id)[0];
|
|
|
|
|
|
|
|
$original_call = $call;
|
|
|
|
|
|
|
|
$call = preg_replace('/^\$this(->|::)/', $absolute_class . '::', $call);
|
|
|
|
|
|
|
|
$call = preg_replace('/\(\)$/', '', $call);
|
|
|
|
|
|
|
|
if (strpos($call, '$') !== false) {
|
2016-08-15 21:14:28 +02:00
|
|
|
$method_params = MethodChecker::getMethodParams($method_id);
|
2016-03-10 21:43:23 +01:00
|
|
|
|
|
|
|
foreach ($args as $i => $arg) {
|
2016-04-16 22:28:25 +02:00
|
|
|
$method_param = $method_params[$i];
|
2016-03-10 21:43:23 +01:00
|
|
|
$preg_var_name = preg_quote('$' . $method_param['name']);
|
|
|
|
|
|
|
|
if (preg_match('/::' . $preg_var_name . '$/', $call)) {
|
|
|
|
if ($arg->value instanceof PhpParser\Node\Scalar\String_) {
|
|
|
|
$call = preg_replace('/' . $preg_var_name . '$/', $arg->value->value, $call);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $original_call === $call || strpos($call, '$') !== false ? null : $call;
|
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* @param PhpParser\Node\Arg[] $args
|
|
|
|
* @param string $method_id
|
|
|
|
* @param Context $context
|
|
|
|
* @param int $line_number
|
|
|
|
* @return false|null
|
|
|
|
*/
|
2016-08-30 06:04:10 +02:00
|
|
|
protected function checkFunctionArguments(array $args, $method_id, Context $context, $line_number)
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
2016-10-09 23:54:58 +02:00
|
|
|
$function_params = null;
|
|
|
|
|
|
|
|
if ($method_id) {
|
|
|
|
$function_params = FunctionLikeChecker::getParamsById($method_id, $args, $this->file_name);
|
|
|
|
}
|
|
|
|
|
2016-08-22 21:00:12 +02:00
|
|
|
foreach ($args as $argument_offset => $arg) {
|
2016-08-15 05:24:16 +02:00
|
|
|
if ($arg->value instanceof PhpParser\Node\Expr\PropertyFetch) {
|
2016-05-10 17:46:36 +02:00
|
|
|
if ($method_id) {
|
2016-10-09 23:54:58 +02:00
|
|
|
$by_ref = false;
|
|
|
|
$by_ref_type = null;
|
|
|
|
|
|
|
|
if ($function_params) {
|
|
|
|
$by_ref = $argument_offset < count($function_params) && $function_params[$argument_offset]->by_ref;
|
|
|
|
$by_ref_type = $by_ref && $argument_offset < count($function_params) ? clone $function_params[$argument_offset]->type : null;
|
|
|
|
}
|
2016-05-24 18:11:17 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
if ($by_ref && $by_ref_type) {
|
|
|
|
$this->assignByRefParam($arg->value, $by_ref_type, $context);
|
2016-05-16 05:06:03 +02:00
|
|
|
}
|
|
|
|
else {
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->checkPropertyFetch($arg->value, $context) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-05-16 05:06:03 +02:00
|
|
|
}
|
2016-10-09 23:54:58 +02:00
|
|
|
}
|
|
|
|
else {
|
2016-10-14 00:27:23 +02:00
|
|
|
$var_id = self::getVarId($arg->value, $this->absolute_class, $this->namespace, $this->aliased_classes);
|
2016-05-23 00:09:10 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
if ($var_id && (!isset($context->vars_in_scope[$var_id]) || $context->vars_in_scope[$var_id]->isNull())) {
|
2016-05-23 00:09:10 +02:00
|
|
|
// we don't know if it exists, assume it's passed by reference
|
2016-08-15 05:24:16 +02:00
|
|
|
$context->vars_in_scope[$var_id] = Type::getMixed();
|
|
|
|
$context->vars_possibly_in_scope[$var_id] = true;
|
|
|
|
$this->registerVariable($var_id, $arg->value->getLine());
|
2016-05-23 00:09:10 +02:00
|
|
|
}
|
2016-05-10 17:46:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
elseif ($arg->value instanceof PhpParser\Node\Expr\Variable) {
|
2016-01-20 00:27:06 +01:00
|
|
|
if ($method_id) {
|
2016-10-09 23:54:58 +02:00
|
|
|
$by_ref = false;
|
|
|
|
$by_ref_type = null;
|
|
|
|
|
|
|
|
if ($function_params) {
|
|
|
|
$by_ref = $argument_offset < count($function_params) && $function_params[$argument_offset]->by_ref;
|
|
|
|
$by_ref_type = $by_ref && $argument_offset < count($function_params) ? clone $function_params[$argument_offset]->type : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->checkVariable($arg->value, $context, $by_ref, $by_ref_type) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-05-24 18:11:17 +02:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif (is_string($arg->value->name)) {
|
2016-10-14 00:27:23 +02:00
|
|
|
if (false || !isset($context->vars_in_scope['$' . $arg->value->name]) || $context->vars_in_scope['$' . $arg->value->name]->isNull()) {
|
2016-05-23 00:09:10 +02:00
|
|
|
// we don't know if it exists, assume it's passed by reference
|
2016-10-14 00:27:23 +02:00
|
|
|
$context->vars_in_scope['$' . $arg->value->name] = Type::getMixed();
|
|
|
|
$context->vars_possibly_in_scope['$' . $arg->value->name] = true;
|
2016-05-23 00:09:10 +02:00
|
|
|
$this->registerVariable($arg->value->name, $arg->value->getLine());
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
2016-08-15 05:24:16 +02:00
|
|
|
}
|
|
|
|
else {
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->checkExpression($arg->value, $context) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
2016-08-22 21:00:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// we need to do this calculation after the above vars have already processed
|
|
|
|
$function_params = $method_id ? FunctionLikeChecker::getParamsById($method_id, $args, $this->file_name) : [];
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-08-22 21:00:12 +02:00
|
|
|
$cased_method_id = $method_id;
|
|
|
|
|
|
|
|
if (strpos($method_id, '::')) {
|
|
|
|
$cased_method_id = MethodChecker::getCasedMethodId($method_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($args as $argument_offset => $arg) {
|
2016-06-16 18:04:55 +02:00
|
|
|
if ($method_id && isset($arg->value->inferredType)) {
|
2016-08-22 21:00:12 +02:00
|
|
|
if (count($function_params) > $argument_offset) {
|
2016-10-09 23:54:58 +02:00
|
|
|
$param_type = $function_params[$argument_offset]->type;
|
2016-08-22 21:00:12 +02:00
|
|
|
|
|
|
|
if ($this->checkFunctionArgumentType(
|
|
|
|
$arg->value->inferredType,
|
|
|
|
$param_type,
|
|
|
|
$cased_method_id,
|
|
|
|
$argument_offset,
|
|
|
|
$arg->value->getLine()
|
|
|
|
) === false
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-29 20:48:04 +02:00
|
|
|
|
|
|
|
if ($method_id) {
|
2016-08-22 21:00:12 +02:00
|
|
|
if (count($args) > count($function_params)) {
|
2016-07-29 20:48:04 +02:00
|
|
|
if (IssueBuffer::accepts(
|
2016-08-25 01:00:44 +02:00
|
|
|
new TooManyArguments('Too many arguments for method ' . $cased_method_id, $this->checked_file_name, $line_number),
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->suppressed_issues
|
2016-07-29 20:48:04 +02:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-22 21:00:12 +02:00
|
|
|
if (count($args) < count($function_params)) {
|
|
|
|
for ($i = count($args); $i < count($function_params); $i++) {
|
|
|
|
$param = $function_params[$i];
|
2016-07-29 20:48:04 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
if (!$param->is_optional) {
|
2016-07-29 20:48:04 +02:00
|
|
|
if (IssueBuffer::accepts(
|
2016-08-25 01:00:44 +02:00
|
|
|
new TooFewArguments('Too few arguments for method ' . $cased_method_id, $this->checked_file_name, $line_number),
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->suppressed_issues
|
2016-07-29 20:48:04 +02:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* @return null|false
|
|
|
|
*/
|
2016-08-11 00:10:12 +02:00
|
|
|
protected function checkConstFetch(PhpParser\Node\Expr\ConstFetch $stmt)
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
2016-08-14 06:38:29 +02:00
|
|
|
$const_name = implode('', $stmt->name->parts);
|
|
|
|
switch (strtolower($const_name)) {
|
|
|
|
case 'null':
|
|
|
|
$stmt->inferredType = Type::getNull();
|
|
|
|
break;
|
2016-04-27 00:17:47 +02:00
|
|
|
|
2016-08-14 06:38:29 +02:00
|
|
|
case 'false':
|
|
|
|
// false is a subtype of bool
|
|
|
|
$stmt->inferredType = Type::getFalse();
|
|
|
|
break;
|
2016-04-30 20:14:22 +02:00
|
|
|
|
2016-08-14 06:38:29 +02:00
|
|
|
case 'true':
|
|
|
|
$stmt->inferredType = Type::getBool();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (!defined($const_name) && !isset(self::$user_constants[$this->file_name][$const_name])) {
|
|
|
|
if (IssueBuffer::accepts(
|
2016-08-25 01:00:44 +02:00
|
|
|
new UndefinedConstant('Const ' . $const_name . ' is not defined', $this->checked_file_name, $stmt->getLine()),
|
2016-08-14 06:38:29 +02:00
|
|
|
$this->suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* @return null|false
|
|
|
|
*/
|
2016-08-11 00:10:12 +02:00
|
|
|
protected function checkClassConstFetch(PhpParser\Node\Expr\ClassConstFetch $stmt, Context $context)
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->check_consts && $stmt->class instanceof PhpParser\Node\Name && $stmt->class->parts !== ['static']) {
|
2016-01-20 00:27:06 +01:00
|
|
|
if ($stmt->class->parts === ['self']) {
|
2016-08-08 17:28:14 +02:00
|
|
|
$absolute_class = $context->self;
|
2016-01-20 00:27:06 +01:00
|
|
|
} else {
|
2016-08-13 20:20:46 +02:00
|
|
|
$absolute_class = ClassLikeChecker::getAbsoluteClassFromName($stmt->class, $this->namespace, $this->aliased_classes);
|
2016-08-25 01:00:44 +02:00
|
|
|
if (ClassLikeChecker::checkAbsoluteClassOrInterface($absolute_class, $this->checked_file_name, $stmt->getLine(), $this->suppressed_issues) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$const_id = $absolute_class . '::' . $stmt->name;
|
|
|
|
|
2016-08-13 20:20:46 +02:00
|
|
|
$class_constants = ClassLikeChecker::getConstantsForClass($absolute_class, \ReflectionProperty::IS_PUBLIC);
|
2016-07-23 16:58:53 +02:00
|
|
|
|
2016-08-13 20:20:46 +02:00
|
|
|
if (!isset($class_constants[$stmt->name])) {
|
|
|
|
if (IssueBuffer::accepts(
|
2016-08-25 01:00:44 +02:00
|
|
|
new UndefinedConstant('Const ' . $const_id . ' is not defined', $this->checked_file_name, $stmt->getLine()),
|
2016-08-13 20:20:46 +02:00
|
|
|
$this->suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-06 02:25:16 +02:00
|
|
|
}
|
2016-08-14 03:14:32 +02:00
|
|
|
else {
|
|
|
|
$stmt->inferredType = $class_constants[$stmt->name];
|
|
|
|
}
|
2016-06-06 02:25:16 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt->class instanceof PhpParser\Node\Expr) {
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->checkExpression($stmt->class, $context) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-27 00:42:48 +02:00
|
|
|
/**
|
2016-06-10 20:47:44 +02:00
|
|
|
* @return null|false
|
2016-04-27 00:42:48 +02:00
|
|
|
*/
|
2016-08-11 00:10:12 +02:00
|
|
|
protected function checkStaticPropertyFetch(PhpParser\Node\Expr\StaticPropertyFetch $stmt, Context $context)
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
|
|
|
if ($stmt->class instanceof PhpParser\Node\Expr\Variable || $stmt->class instanceof PhpParser\Node\Expr\ArrayDimFetch) {
|
2016-10-09 23:54:58 +02:00
|
|
|
// @todo check this
|
2016-01-20 00:27:06 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$method_id = null;
|
|
|
|
$absolute_class = null;
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
if ($stmt->class instanceof PhpParser\Node\Name) {
|
|
|
|
if (count($stmt->class->parts) === 1 && in_array($stmt->class->parts[0], ['self', 'static', 'parent'])) {
|
|
|
|
if ($stmt->class->parts[0] === 'parent') {
|
|
|
|
$absolute_class = $this->parent_class;
|
|
|
|
} else {
|
|
|
|
$absolute_class = ($this->namespace ? $this->namespace . '\\' : '') . $this->class_name;
|
|
|
|
}
|
2016-10-02 19:05:49 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
if ($this->isPhantomClass($absolute_class)) {
|
|
|
|
return null;
|
|
|
|
}
|
2016-10-02 19:05:49 +02:00
|
|
|
}
|
2016-10-09 23:54:58 +02:00
|
|
|
elseif ($this->check_classes) {
|
|
|
|
$absolute_class = ClassLikeChecker::getAbsoluteClassFromName($stmt->class, $this->namespace, $this->aliased_classes);
|
2016-10-02 19:05:49 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
if ($this->isPhantomClass($absolute_class)) {
|
|
|
|
return;
|
|
|
|
}
|
2016-10-02 19:05:49 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
if (ClassLikeChecker::checkAbsoluteClassOrInterface($absolute_class, $this->checked_file_name, $stmt->getLine(), $this->suppressed_issues) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-06 02:25:16 +02:00
|
|
|
}
|
2016-10-14 00:27:23 +02:00
|
|
|
|
|
|
|
$stmt->class->inferredType = $absolute_class ? new Type\Union([new Type\Atomic($absolute_class)]) : null;
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($absolute_class && $this->check_variables && is_string($stmt->name) && !self::isMock($absolute_class)) {
|
2016-10-14 00:27:23 +02:00
|
|
|
$var_id = self::getVarId($stmt, $this->absolute_class, $this->namespace, $this->aliased_classes);
|
|
|
|
|
|
|
|
if ($var_id && isset($context->vars_in_scope[$var_id])) {
|
|
|
|
// we don't need to check anything
|
|
|
|
$stmt->inferredType = $context->vars_in_scope[$var_id];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-13 17:10:43 +02:00
|
|
|
if ($absolute_class === $context->self
|
2016-08-13 17:28:06 +02:00
|
|
|
|| ($this->source->getSource() instanceof TraitChecker && $absolute_class === $this->source->getAbsoluteClass())
|
2016-08-13 17:10:43 +02:00
|
|
|
) {
|
2016-08-07 18:23:29 +02:00
|
|
|
$class_visibility = \ReflectionProperty::IS_PRIVATE;
|
|
|
|
}
|
2016-08-11 01:21:03 +02:00
|
|
|
elseif ($context->self && ClassChecker::classExtends($context->self, $absolute_class)) {
|
2016-08-07 18:23:29 +02:00
|
|
|
$class_visibility = \ReflectionProperty::IS_PROTECTED;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$class_visibility = \ReflectionProperty::IS_PUBLIC;
|
|
|
|
}
|
|
|
|
|
2016-08-13 20:20:46 +02:00
|
|
|
$visible_class_properties = ClassLikeChecker::getStaticPropertiesForClass(
|
2016-08-07 02:27:13 +02:00
|
|
|
$absolute_class,
|
2016-08-07 18:23:29 +02:00
|
|
|
$class_visibility
|
2016-08-07 02:27:13 +02:00
|
|
|
);
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-10-12 07:38:16 +02:00
|
|
|
if (!isset($visible_class_properties[$stmt->name])) {
|
2016-08-07 02:27:13 +02:00
|
|
|
$all_class_properties = [];
|
|
|
|
|
2016-08-08 17:28:14 +02:00
|
|
|
if ($absolute_class !== $context->self) {
|
2016-08-13 20:20:46 +02:00
|
|
|
$all_class_properties = ClassLikeChecker::getStaticPropertiesForClass(
|
2016-08-07 02:27:13 +02:00
|
|
|
$absolute_class,
|
2016-08-07 18:23:29 +02:00
|
|
|
\ReflectionProperty::IS_PRIVATE
|
2016-06-27 19:22:16 +02:00
|
|
|
);
|
2016-08-07 02:27:13 +02:00
|
|
|
}
|
2016-06-27 19:22:16 +02:00
|
|
|
|
2016-08-07 02:27:13 +02:00
|
|
|
if ($all_class_properties && isset($all_class_properties[$stmt->name])) {
|
|
|
|
IssueBuffer::add(
|
2016-10-09 23:54:58 +02:00
|
|
|
new InvisibleProperty('Static property ' . $var_id . ' is not visible in this context', $this->checked_file_name, $stmt->getLine())
|
2016-08-07 02:27:13 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
IssueBuffer::add(
|
2016-10-11 01:29:38 +02:00
|
|
|
new UndefinedPropertyFetch('Static property ' . $var_id . ' does not exist', $this->checked_file_name, $stmt->getLine())
|
2016-08-07 02:27:13 +02:00
|
|
|
);
|
2016-06-06 02:25:16 +02:00
|
|
|
}
|
2016-08-07 02:27:13 +02:00
|
|
|
|
|
|
|
return false;
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
2016-10-12 07:38:16 +02:00
|
|
|
|
|
|
|
$context->vars_in_scope[$var_id] = clone $visible_class_properties[$stmt->name];
|
|
|
|
$stmt->inferredType = clone $visible_class_properties[$stmt->name];
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* @param PhpParser\Node\Stmt\Return_ $stmt
|
|
|
|
* @param Context $context
|
|
|
|
* @return false|null
|
|
|
|
*/
|
2016-08-11 00:10:12 +02:00
|
|
|
protected function checkReturn(PhpParser\Node\Stmt\Return_ $stmt, Context $context)
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
2016-08-11 00:10:12 +02:00
|
|
|
$type_in_comments = CommentChecker::getTypeFromComment((string) $stmt->getDocComment(), $context, $this->source);
|
2016-06-17 19:21:57 +02:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
if ($stmt->expr) {
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->checkExpression($stmt->expr, $context) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-04-27 00:17:47 +02:00
|
|
|
|
2016-05-06 03:20:15 +02:00
|
|
|
if ($type_in_comments) {
|
2016-08-11 01:21:03 +02:00
|
|
|
$stmt->inferredType = $type_in_comments;
|
2016-05-06 03:20:15 +02:00
|
|
|
}
|
2016-06-16 18:04:55 +02:00
|
|
|
elseif (isset($stmt->expr->inferredType)) {
|
|
|
|
$stmt->inferredType = $stmt->expr->inferredType;
|
2016-04-27 00:17:47 +02:00
|
|
|
}
|
|
|
|
else {
|
2016-06-16 18:04:55 +02:00
|
|
|
$stmt->inferredType = Type::getMixed();
|
2016-04-27 00:17:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2016-06-16 18:04:55 +02:00
|
|
|
$stmt->inferredType = Type::getVoid();
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
2016-05-16 22:12:02 +02:00
|
|
|
|
2016-08-14 05:26:45 +02:00
|
|
|
if ($this->source instanceof FunctionLikeChecker) {
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->source->addReturnTypes($stmt->expr ? (string) $stmt->inferredType : '', $context);
|
2016-05-16 22:12:02 +02:00
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-08-11 00:10:12 +02:00
|
|
|
protected function checkTernary(PhpParser\Node\Expr\Ternary $stmt, Context $context)
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->checkExpression($stmt->cond, $context) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
$t_if_context = clone $context;
|
|
|
|
|
2016-06-28 21:28:05 +02:00
|
|
|
if ($stmt->cond instanceof PhpParser\Node\Expr\BinaryOp) {
|
2016-10-14 00:27:23 +02:00
|
|
|
$reconcilable_if_types = $this->type_checker->getReconcilableTypeAssertions(
|
|
|
|
$stmt->cond,
|
|
|
|
$this->absolute_class,
|
|
|
|
$this->namespace,
|
|
|
|
$this->aliased_classes
|
|
|
|
);
|
|
|
|
$negatable_if_types = $this->type_checker->getNegatableTypeAssertions(
|
|
|
|
$stmt->cond,
|
|
|
|
$this->absolute_class,
|
|
|
|
$this->namespace,
|
|
|
|
$this->aliased_classes
|
|
|
|
);
|
2016-06-28 21:28:05 +02:00
|
|
|
}
|
|
|
|
else {
|
2016-10-14 00:27:23 +02:00
|
|
|
$reconcilable_if_types = $negatable_if_types = $this->type_checker->getTypeAssertions(
|
|
|
|
$stmt->cond,
|
|
|
|
$this->absolute_class,
|
|
|
|
$this->namespace,
|
|
|
|
$this->aliased_classes);
|
2016-06-28 21:28:05 +02:00
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-06-17 23:12:07 +02:00
|
|
|
$if_return_type = null;
|
2016-06-14 01:57:32 +02:00
|
|
|
|
2016-06-28 21:28:05 +02:00
|
|
|
$t_if_vars_in_scope_reconciled =
|
|
|
|
TypeChecker::reconcileKeyedTypes(
|
|
|
|
$reconcilable_if_types,
|
|
|
|
$t_if_context->vars_in_scope,
|
2016-08-25 01:00:44 +02:00
|
|
|
$this->checked_file_name,
|
2016-08-09 00:55:22 +02:00
|
|
|
$stmt->getLine(),
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->suppressed_issues
|
2016-06-28 21:28:05 +02:00
|
|
|
);
|
2016-06-14 01:57:32 +02:00
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
if ($t_if_vars_in_scope_reconciled === false) {
|
2016-06-17 23:12:07 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
$t_if_context->vars_in_scope = $t_if_vars_in_scope_reconciled;
|
|
|
|
|
2016-06-17 23:12:07 +02:00
|
|
|
if ($stmt->if) {
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->checkExpression($stmt->if, $t_if_context) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
$t_else_context = clone $context;
|
|
|
|
|
2016-06-28 21:28:05 +02:00
|
|
|
if ($negatable_if_types) {
|
|
|
|
$negated_if_types = TypeChecker::negateTypes($negatable_if_types);
|
|
|
|
|
2016-08-09 00:55:22 +02:00
|
|
|
$t_else_vars_in_scope_reconciled = TypeChecker::reconcileKeyedTypes(
|
|
|
|
$negated_if_types,
|
|
|
|
$t_else_context->vars_in_scope,
|
2016-08-25 01:00:44 +02:00
|
|
|
$this->checked_file_name,
|
2016-08-09 00:55:22 +02:00
|
|
|
$stmt->getLine(),
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->suppressed_issues
|
2016-08-09 00:55:22 +02:00
|
|
|
);
|
2016-06-14 01:57:32 +02:00
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
if ($t_else_vars_in_scope_reconciled === false) {
|
2016-06-14 01:57:32 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-06-20 06:38:13 +02:00
|
|
|
$t_else_context->vars_in_scope = $t_else_vars_in_scope_reconciled;
|
2016-04-04 21:00:57 +02:00
|
|
|
}
|
2016-04-04 02:14:19 +02:00
|
|
|
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->checkExpression($stmt->else, $t_else_context) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-06-16 17:57:12 +02:00
|
|
|
|
|
|
|
$lhs_type = null;
|
|
|
|
|
|
|
|
if ($stmt->if) {
|
2016-06-16 18:04:55 +02:00
|
|
|
if (isset($stmt->if->inferredType)) {
|
2016-06-16 18:05:37 +02:00
|
|
|
$lhs_type = $stmt->if->inferredType;
|
2016-06-16 17:57:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
elseif ($stmt->cond) {
|
2016-06-16 18:04:55 +02:00
|
|
|
if (isset($stmt->cond->inferredType)) {
|
2016-08-25 01:00:44 +02:00
|
|
|
$if_return_type_reconciled = TypeChecker::reconcileTypes('!empty', $stmt->cond->inferredType, '', $this->checked_file_name, $stmt->getLine(), $this->suppressed_issues);
|
2016-06-24 00:45:46 +02:00
|
|
|
|
|
|
|
if ($if_return_type_reconciled === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$lhs_type = $if_return_type_reconciled;
|
2016-06-16 17:57:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-16 18:04:55 +02:00
|
|
|
if (!$lhs_type || !isset($stmt->else->inferredType)) {
|
|
|
|
$stmt->inferredType = Type::getMixed();
|
2016-06-16 17:57:12 +02:00
|
|
|
}
|
|
|
|
else {
|
2016-06-16 18:04:55 +02:00
|
|
|
$stmt->inferredType = Type::combineUnionTypes($lhs_type, $stmt->else->inferredType);
|
2016-06-16 17:57:12 +02:00
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-08-11 00:10:12 +02:00
|
|
|
protected function checkBooleanNot(PhpParser\Node\Expr\BooleanNot $stmt, Context $context)
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
2016-08-11 00:10:12 +02:00
|
|
|
return $this->checkExpression($stmt->expr, $context);
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-08-11 00:10:12 +02:00
|
|
|
protected function checkEmpty(PhpParser\Node\Expr\Empty_ $stmt, Context $context)
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
2016-08-11 00:10:12 +02:00
|
|
|
return $this->checkExpression($stmt->expr, $context);
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-08-11 00:10:12 +02:00
|
|
|
protected function checkThrow(PhpParser\Node\Stmt\Throw_ $stmt, Context $context)
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
2016-08-11 00:10:12 +02:00
|
|
|
return $this->checkExpression($stmt->expr, $context);
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* @param PhpParser\Node\Stmt\Switch_ $stmt
|
|
|
|
* @param Context $context
|
|
|
|
* @param Context|null $loop_context
|
|
|
|
* @return false|null
|
|
|
|
*/
|
2016-08-24 00:09:34 +02:00
|
|
|
protected function checkSwitch(PhpParser\Node\Stmt\Switch_ $stmt, Context $context, Context $loop_context = null)
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
2016-02-10 21:20:53 +01:00
|
|
|
$type_candidate_var = null;
|
|
|
|
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->checkExpression($stmt->cond, $context) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-08-10 00:10:46 +02:00
|
|
|
if (isset($stmt->cond->inferredType) && array_values($stmt->cond->inferredType->types)[0] instanceof Type\T) {
|
|
|
|
$type_candidate_var = array_values($stmt->cond->inferredType->types)[0]->typeof;
|
|
|
|
}
|
|
|
|
|
2016-07-26 00:30:49 +02:00
|
|
|
$original_context = clone $context;
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/** @var array<string,Type\Union>|null */
|
2016-02-10 21:20:53 +01:00
|
|
|
$new_vars_in_scope = null;
|
2016-10-09 23:54:58 +02:00
|
|
|
|
|
|
|
/** @var array<string,bool> */
|
2016-02-10 21:20:53 +01:00
|
|
|
$new_vars_possibly_in_scope = [];
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/** @var array<string,Type\Union>|null */
|
2016-04-04 20:29:02 +02:00
|
|
|
$redefined_vars = null;
|
|
|
|
|
2016-06-28 19:56:44 +02:00
|
|
|
// the last statement always breaks, by default
|
|
|
|
$last_case_exit_type = 'break';
|
|
|
|
|
|
|
|
$case_exit_types = new \SplFixedArray(count($stmt->cases));
|
|
|
|
|
2016-06-30 00:10:41 +02:00
|
|
|
$has_default = false;
|
|
|
|
|
2016-06-28 19:56:44 +02:00
|
|
|
// create a map of case statement -> ultimate exit type
|
|
|
|
for ($i = count($stmt->cases) - 1; $i >= 0; $i--) {
|
|
|
|
$case = $stmt->cases[$i];
|
|
|
|
|
2016-08-24 23:06:20 +02:00
|
|
|
if (ScopeChecker::doesAlwaysReturnOrThrow($case->stmts)) {
|
2016-06-28 19:56:44 +02:00
|
|
|
$last_case_exit_type = 'return_throw';
|
|
|
|
}
|
2016-08-24 23:06:20 +02:00
|
|
|
elseif (ScopeChecker::doesAlwaysBreakOrContinue($case->stmts, true)) {
|
2016-06-28 19:56:44 +02:00
|
|
|
$last_case_exit_type = 'continue';
|
|
|
|
}
|
2016-08-24 23:06:20 +02:00
|
|
|
elseif (ScopeChecker::doesAlwaysBreakOrContinue($case->stmts)) {
|
2016-06-28 19:56:44 +02:00
|
|
|
$last_case_exit_type = 'break';
|
|
|
|
}
|
|
|
|
|
|
|
|
$case_exit_types[$i] = $last_case_exit_type;
|
|
|
|
}
|
|
|
|
|
2016-08-30 06:04:10 +02:00
|
|
|
$leftover_statements = [];
|
|
|
|
|
|
|
|
for ($i = count($stmt->cases) - 1; $i >= 0; $i--) {
|
2016-06-28 19:56:44 +02:00
|
|
|
$case = $stmt->cases[$i];
|
|
|
|
$case_exit_type = $case_exit_types[$i];
|
2016-08-30 06:04:10 +02:00
|
|
|
$case_type = null;
|
2016-06-28 19:56:44 +02:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
if ($case->cond) {
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->checkExpression($case->cond, $context) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-02-10 21:20:53 +01:00
|
|
|
|
|
|
|
if ($type_candidate_var && $case->cond instanceof PhpParser\Node\Scalar\String_) {
|
2016-08-30 06:04:10 +02:00
|
|
|
$case_type = $case->cond->value;
|
2016-02-10 21:20:53 +01:00
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-08-30 06:04:10 +02:00
|
|
|
$switch_vars = $type_candidate_var && $case_type
|
|
|
|
? [$type_candidate_var => Type::parseString($case_type)]
|
|
|
|
: [];
|
2016-02-10 21:20:53 +01:00
|
|
|
|
2016-08-30 06:04:10 +02:00
|
|
|
$case_context = clone $original_context;
|
2016-06-20 06:38:13 +02:00
|
|
|
|
2016-08-30 06:04:10 +02:00
|
|
|
$case_context->vars_in_scope = array_merge($case_context->vars_in_scope, $switch_vars);
|
|
|
|
$case_context->vars_possibly_in_scope = array_merge($case_context->vars_possibly_in_scope, $switch_vars);
|
2016-02-10 21:20:53 +01:00
|
|
|
|
2016-08-30 06:04:10 +02:00
|
|
|
$old_case_context = clone $case_context;
|
2016-02-10 21:20:53 +01:00
|
|
|
|
2016-08-30 06:04:10 +02:00
|
|
|
$case_stmts = $case->stmts;
|
2016-02-10 21:20:53 +01:00
|
|
|
|
2016-08-30 06:04:10 +02:00
|
|
|
// has a return/throw at end
|
|
|
|
$has_ending_statements = ScopeChecker::doesAlwaysReturnOrThrow($case_stmts);
|
|
|
|
$has_leaving_statements = ScopeChecker::doesAlwaysBreakOrContinue($case_stmts);
|
2016-02-10 21:20:53 +01:00
|
|
|
|
2016-08-30 06:04:10 +02:00
|
|
|
if (!$case_stmts || (!$has_ending_statements && !$has_leaving_statements)) {
|
|
|
|
$case_stmts = array_merge($case_stmts, $leftover_statements);
|
|
|
|
$has_ending_statements = ScopeChecker::doesAlwaysReturnOrThrow($case_stmts);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$leftover_statements = [];
|
|
|
|
}
|
2016-04-04 20:29:02 +02:00
|
|
|
|
2016-08-30 06:04:10 +02:00
|
|
|
$this->check($case_stmts, $case_context);
|
2016-04-04 20:29:02 +02:00
|
|
|
|
2016-08-30 06:04:10 +02:00
|
|
|
// has a return/throw at end
|
|
|
|
$has_ending_statements = ScopeChecker::doesAlwaysReturnOrThrow($case_stmts);
|
2016-07-26 00:30:49 +02:00
|
|
|
|
2016-08-30 06:04:10 +02:00
|
|
|
if ($case_exit_type !== 'return_throw') {
|
|
|
|
$vars = array_diff_key($case_context->vars_possibly_in_scope, $original_context->vars_possibly_in_scope);
|
2016-04-04 20:29:02 +02:00
|
|
|
|
2016-08-30 06:04:10 +02:00
|
|
|
// if we're leaving this block, add vars to outer for loop scope
|
|
|
|
if ($case_exit_type === 'continue') {
|
2016-09-01 06:07:29 +02:00
|
|
|
if ($loop_context) {
|
|
|
|
$loop_context->vars_possibly_in_scope = array_merge($vars, $loop_context->vars_possibly_in_scope);
|
|
|
|
}
|
|
|
|
else {
|
2016-10-09 23:54:58 +02:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidContinue(
|
|
|
|
'Continue called when not in loop',
|
|
|
|
$this->checked_file_name,
|
|
|
|
$case->getLine()
|
|
|
|
),
|
|
|
|
$this->suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-09-01 06:07:29 +02:00
|
|
|
}
|
2016-08-30 06:04:10 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$case_redefined_vars = Context::getRedefinedVars($original_context, $case_context);
|
|
|
|
|
|
|
|
Type::redefineGenericUnionTypes($case_redefined_vars, $context);
|
|
|
|
|
|
|
|
if ($redefined_vars === null) {
|
|
|
|
$redefined_vars = $case_redefined_vars;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
foreach ($redefined_vars as $redefined_var => $type) {
|
|
|
|
if (!isset($case_redefined_vars[$redefined_var])) {
|
|
|
|
unset($redefined_vars[$redefined_var]);
|
2016-02-10 21:20:53 +01:00
|
|
|
}
|
|
|
|
}
|
2016-08-30 06:04:10 +02:00
|
|
|
}
|
2016-02-10 21:20:53 +01:00
|
|
|
|
2016-08-30 06:04:10 +02:00
|
|
|
if ($new_vars_in_scope === null) {
|
|
|
|
$new_vars_in_scope = array_diff_key($case_context->vars_in_scope, $context->vars_in_scope);
|
|
|
|
$new_vars_possibly_in_scope = array_diff_key($case_context->vars_possibly_in_scope, $context->vars_possibly_in_scope);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
foreach ($new_vars_in_scope as $new_var => $type) {
|
|
|
|
if (!isset($case_context->vars_in_scope[$new_var])) {
|
|
|
|
unset($new_vars_in_scope[$new_var]);
|
2016-06-10 20:47:44 +02:00
|
|
|
}
|
|
|
|
}
|
2016-08-30 06:04:10 +02:00
|
|
|
|
|
|
|
$new_vars_possibly_in_scope = array_merge(
|
|
|
|
array_diff_key(
|
|
|
|
$case_context->vars_possibly_in_scope,
|
|
|
|
$context->vars_possibly_in_scope
|
|
|
|
),
|
|
|
|
$new_vars_possibly_in_scope
|
|
|
|
);
|
2016-02-10 21:20:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-30 06:04:10 +02:00
|
|
|
if ($case->stmts) {
|
|
|
|
$leftover_statements = array_merge($leftover_statements, $case->stmts);
|
2016-02-10 21:20:53 +01:00
|
|
|
}
|
|
|
|
|
2016-06-30 00:10:41 +02:00
|
|
|
if (!$case->cond) {
|
|
|
|
$has_default = true;
|
|
|
|
}
|
|
|
|
}
|
2016-04-04 20:29:02 +02:00
|
|
|
|
2016-06-30 00:10:41 +02:00
|
|
|
// only update vars if there is a default
|
|
|
|
// if that default has a throw/return/continue, that should be handled above
|
|
|
|
if ($has_default) {
|
|
|
|
if ($new_vars_in_scope) {
|
|
|
|
$context->vars_in_scope = array_merge($context->vars_in_scope, $new_vars_in_scope);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($redefined_vars) {
|
|
|
|
$context->vars_in_scope = array_merge($context->vars_in_scope, $redefined_vars);
|
2016-02-10 21:20:53 +01:00
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
2016-02-10 21:20:53 +01:00
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
$context->vars_possibly_in_scope = array_merge($context->vars_possibly_in_scope, $new_vars_possibly_in_scope);
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-08-22 21:00:12 +02:00
|
|
|
/**
|
|
|
|
* @param Type\Union $input_type
|
2016-09-01 06:07:29 +02:00
|
|
|
* @param Type\Union $param_type
|
2016-08-22 21:00:12 +02:00
|
|
|
* @param string $cased_method_id
|
|
|
|
* @param int $argument_offset
|
|
|
|
* @param int $line_number
|
|
|
|
* @return null|false
|
|
|
|
*/
|
|
|
|
protected function checkFunctionArgumentType(Type\Union $input_type, Type\Union $param_type, $cased_method_id, $argument_offset, $line_number)
|
2016-05-24 18:11:17 +02:00
|
|
|
{
|
2016-06-29 16:57:15 +02:00
|
|
|
if ($param_type->isMixed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($input_type->isMixed()) {
|
2016-10-09 23:54:58 +02:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MixedArgument(
|
|
|
|
'Argument ' . ($argument_offset + 1) . ' of ' . $cased_method_id . ' cannot be mixed',
|
|
|
|
$this->checked_file_name,
|
|
|
|
$line_number
|
|
|
|
),
|
|
|
|
$this->suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-06-29 16:57:15 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-30 00:10:41 +02:00
|
|
|
if ($input_type->isNullable() && !$param_type->isNullable()) {
|
2016-06-29 16:57:15 +02:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new NullReference(
|
2016-08-22 21:00:12 +02:00
|
|
|
'Argument ' . ($argument_offset + 1) . ' of ' . $cased_method_id . ' cannot be null, possibly null value provided',
|
2016-08-25 01:00:44 +02:00
|
|
|
$this->checked_file_name,
|
2016-06-29 16:57:15 +02:00
|
|
|
$line_number
|
2016-07-22 19:29:46 +02:00
|
|
|
),
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->suppressed_issues
|
2016-06-29 16:57:15 +02:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
$type_match_found = FunctionLikeChecker::doesParamMatch($input_type, $param_type, $scalar_type_match_found, $coerced_type);
|
|
|
|
|
|
|
|
if ($coerced_type) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new TypeCoercion(
|
|
|
|
'Argument ' . ($argument_offset + 1) . ' of ' . $cased_method_id . ' expects ' . $param_type . ', parent type ' . $input_type . ' provided',
|
|
|
|
$this->checked_file_name,
|
|
|
|
$line_number
|
|
|
|
),
|
|
|
|
$this->suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-07-10 22:09:09 +02:00
|
|
|
|
2016-08-22 21:00:12 +02:00
|
|
|
if (!$type_match_found) {
|
|
|
|
if ($scalar_type_match_found) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidScalarArgument(
|
|
|
|
'Argument ' . ($argument_offset + 1) . ' of ' . $cased_method_id . ' expects ' . $param_type . ', ' . $input_type . ' provided',
|
2016-08-25 01:00:44 +02:00
|
|
|
$this->checked_file_name,
|
2016-07-10 22:09:09 +02:00
|
|
|
$line_number
|
2016-07-22 19:29:46 +02:00
|
|
|
),
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->suppressed_issues
|
2016-07-10 22:09:09 +02:00
|
|
|
)) {
|
|
|
|
return false;
|
2016-06-16 02:16:40 +02:00
|
|
|
}
|
2016-05-24 18:11:17 +02:00
|
|
|
}
|
2016-08-22 21:00:12 +02:00
|
|
|
else if (IssueBuffer::accepts(
|
|
|
|
new InvalidArgument(
|
|
|
|
'Argument ' . ($argument_offset + 1) . ' of ' . $cased_method_id . ' expects ' . $param_type . ', ' . $input_type . ' provided',
|
2016-08-25 01:00:44 +02:00
|
|
|
$this->checked_file_name,
|
2016-08-22 21:00:12 +02:00
|
|
|
$line_number
|
|
|
|
),
|
|
|
|
$this->suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-24 18:11:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* @param PhpParser\Node\Expr\FuncCall $stmt
|
|
|
|
* @param Context $context
|
|
|
|
* @return false|null
|
|
|
|
*/
|
2016-08-11 00:10:12 +02:00
|
|
|
protected function checkFunctionCall(PhpParser\Node\Expr\FuncCall $stmt, Context $context)
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
|
|
|
$method = $stmt->name;
|
|
|
|
|
|
|
|
if ($method instanceof PhpParser\Node\Name) {
|
|
|
|
if ($method->parts === ['method_exists']) {
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->check_methods = false;
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-10-02 19:05:49 +02:00
|
|
|
}
|
|
|
|
elseif ($method->parts === ['class_exists']) {
|
|
|
|
if ($stmt->args[0]->value instanceof PhpParser\Node\Scalar\String_) {
|
|
|
|
$this->phantom_classes[$stmt->args[0]->value->value] = true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$this->check_classes = false;
|
|
|
|
}
|
|
|
|
|
2016-03-23 18:05:25 +01:00
|
|
|
} elseif ($method->parts === ['function_exists']) {
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->check_functions = false;
|
2016-03-23 18:05:25 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($method->parts === ['defined']) {
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->check_consts = false;
|
2016-01-20 20:19:47 +01:00
|
|
|
|
|
|
|
} elseif ($method->parts === ['extract']) {
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->check_variables = false;
|
2016-01-20 20:19:47 +01:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
} elseif ($method->parts === ['var_dump'] || $method->parts === ['die'] || $method->parts === ['exit']) {
|
2016-06-26 21:18:40 +02:00
|
|
|
if (IssueBuffer::accepts(
|
2016-08-25 01:00:44 +02:00
|
|
|
new ForbiddenCode('Unsafe ' . implode('', $method->parts), $this->checked_file_name, $stmt->getLine()),
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->suppressed_issues
|
2016-06-06 02:25:16 +02:00
|
|
|
)) {
|
|
|
|
return false;
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
}
|
2016-08-14 06:38:29 +02:00
|
|
|
elseif ($method->parts === ['define']) {
|
|
|
|
if ($stmt->args[0]->value instanceof PhpParser\Node\Scalar\String_) {
|
|
|
|
$this->checkExpression($stmt->args[1]->value, $context);
|
|
|
|
$const_name = $stmt->args[0]->value->value;
|
|
|
|
|
|
|
|
self::$user_constants[$this->file_name][$const_name] = isset($stmt->args[1]->value->inferredType)
|
|
|
|
? $stmt->args[1]->value->inferredType
|
|
|
|
: Type::getMixed();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$this->check_consts = false;
|
|
|
|
}
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-03-23 18:05:25 +01:00
|
|
|
$method_id = null;
|
|
|
|
|
2016-09-15 22:31:36 +02:00
|
|
|
if ($this->check_functions) {
|
2016-10-09 23:54:58 +02:00
|
|
|
if (!($stmt->name instanceof PhpParser\Node\Name)) {
|
|
|
|
return;
|
|
|
|
}
|
2016-03-23 18:05:25 +01:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
$method_id = implode('', $stmt->name->parts);
|
2016-09-15 22:31:36 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
if ($context->self) {
|
|
|
|
//$method_id = $this->absolute_class . '::' . $method_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->checkFunctionExists($method_id, $context, $stmt->getLine()) === false) {
|
|
|
|
return false;
|
2016-03-23 18:05:25 +01:00
|
|
|
}
|
|
|
|
|
2016-09-15 22:31:36 +02:00
|
|
|
if ($this->checkFunctionArguments($stmt->args, $method_id, $context, $stmt->getLine()) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-03-23 18:05:25 +01:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
$stmt->inferredType = FunctionChecker::getReturnTypeFromCallMap($method_id, $stmt->args, $this->checked_file_name, $stmt->getLine(), $this->suppressed_issues);
|
2016-08-11 23:36:22 +02:00
|
|
|
}
|
|
|
|
|
2016-08-10 01:14:50 +02:00
|
|
|
if ($stmt->name instanceof PhpParser\Node\Name && $stmt->name->parts === ['get_class'] && $stmt->args) {
|
2016-08-10 00:10:46 +02:00
|
|
|
$var = $stmt->args[0]->value;
|
|
|
|
|
|
|
|
if ($var instanceof PhpParser\Node\Expr\Variable && is_string($var->name)) {
|
|
|
|
$stmt->inferredType = new Type\Union([new Type\T($var->name)]);
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-17 22:58:15 +02:00
|
|
|
/**
|
|
|
|
* @param PhpParser\Node\Expr\ArrayDimFetch $stmt
|
2016-06-20 06:38:13 +02:00
|
|
|
* @param array &$context->vars_in_scope
|
|
|
|
* @param array &$context->vars_possibly_in_scope
|
2016-06-17 22:58:15 +02:00
|
|
|
* @return false|null
|
|
|
|
*/
|
2016-10-03 04:00:42 +02:00
|
|
|
protected function checkArrayAccess(
|
|
|
|
PhpParser\Node\Expr\ArrayDimFetch $stmt,
|
|
|
|
Context $context,
|
|
|
|
$array_assignment = false,
|
|
|
|
Type\Union $assignment_key_type = null,
|
|
|
|
Type\Union $assignment_value_type = null,
|
|
|
|
$assignment_key_value = null
|
|
|
|
) {
|
2016-09-21 03:45:49 +02:00
|
|
|
$var_type = null;
|
|
|
|
$key_type = null;
|
2016-10-03 04:00:42 +02:00
|
|
|
$key_value = null;
|
2016-09-21 03:45:49 +02:00
|
|
|
|
|
|
|
$nesting = 0;
|
2016-10-14 00:27:23 +02:00
|
|
|
$var_id = self::getVarId($stmt->var, $this->absolute_class, $this->namespace, $this->aliased_classes, $nesting);
|
2016-09-22 19:43:53 +02:00
|
|
|
|
|
|
|
$is_object = $var_id && isset($context->vars_in_scope[$var_id]) && $context->vars_in_scope[$var_id]->hasObjectType();
|
2016-10-14 00:27:23 +02:00
|
|
|
$array_var_id = self::getArrayVarId($stmt->var, $this->absolute_class, $this->namespace, $this->aliased_classes);
|
2016-09-21 03:45:49 +02:00
|
|
|
$keyed_array_var_id = $array_var_id && $stmt->dim instanceof PhpParser\Node\Scalar\String_
|
|
|
|
? $array_var_id . '[\'' . $stmt->dim->value . '\']'
|
|
|
|
: null;
|
2016-09-12 06:02:26 +02:00
|
|
|
|
|
|
|
if ($stmt->dim && $this->checkExpression($stmt->dim, $context) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-06-17 22:58:15 +02:00
|
|
|
|
2016-09-21 03:45:49 +02:00
|
|
|
if ($stmt->dim) {
|
|
|
|
if (isset($stmt->dim->inferredType)) {
|
2016-10-09 23:54:58 +02:00
|
|
|
/** @var Type\Union */
|
2016-09-21 03:45:49 +02:00
|
|
|
$key_type = $stmt->dim->inferredType;
|
2016-10-03 04:00:42 +02:00
|
|
|
|
|
|
|
if ($stmt->dim instanceof PhpParser\Node\Scalar\String_) {
|
|
|
|
$key_value = $stmt->dim->value;
|
|
|
|
}
|
2016-09-21 03:45:49 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$key_type = Type::getMixed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$key_type = Type::getInt();
|
|
|
|
}
|
2016-06-17 22:58:15 +02:00
|
|
|
|
2016-09-21 03:45:49 +02:00
|
|
|
$keyed_assignment_type = null;
|
|
|
|
|
2016-10-03 17:39:32 +02:00
|
|
|
if ($array_assignment && $assignment_key_type && $assignment_value_type) {
|
2016-10-03 04:00:42 +02:00
|
|
|
$keyed_assignment_type =
|
|
|
|
$keyed_array_var_id && isset($context->vars_in_scope[$keyed_array_var_id])
|
|
|
|
? $context->vars_in_scope[$keyed_array_var_id]
|
|
|
|
: null;
|
2016-09-21 03:45:49 +02:00
|
|
|
|
|
|
|
if (!$keyed_assignment_type || $keyed_assignment_type->isEmpty()) {
|
2016-10-03 04:00:42 +02:00
|
|
|
if (!$assignment_key_type->isMixed() && !$assignment_key_type->hasInt() && $assignment_key_value) {
|
|
|
|
$keyed_assignment_type = new Type\Union([
|
|
|
|
new Type\ObjectLike(
|
|
|
|
'object-like',
|
|
|
|
[
|
|
|
|
$assignment_key_value => $assignment_value_type
|
|
|
|
]
|
|
|
|
)
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$keyed_assignment_type = Type::getEmptyArray();
|
|
|
|
$keyed_assignment_type->types['array']->type_params[0] = $assignment_key_type;
|
|
|
|
$keyed_assignment_type->types['array']->type_params[1] = $assignment_value_type;
|
|
|
|
}
|
2016-09-21 03:45:49 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
foreach ($keyed_assignment_type->types as &$type) {
|
|
|
|
if ($type->isScalarType() && !$type->isString()) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidArrayAssignment(
|
2016-10-14 00:27:23 +02:00
|
|
|
'Cannot assign value on variable ' . $var_id . ' of scalar type ' . $type->value,
|
2016-09-21 03:45:49 +02:00
|
|
|
$this->checked_file_name,
|
|
|
|
$stmt->getLine()
|
|
|
|
),
|
|
|
|
$this->suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-10-03 06:43:24 +02:00
|
|
|
if ($type instanceof Type\Generic) {
|
|
|
|
$refined_type = $this->refineArrayType($type, $assignment_key_type, $assignment_value_type, $var_id, $stmt->getLine());
|
2016-09-21 03:45:49 +02:00
|
|
|
|
2016-10-03 06:43:24 +02:00
|
|
|
if ($refined_type === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-09-21 03:45:49 +02:00
|
|
|
|
2016-10-03 06:43:24 +02:00
|
|
|
if ($refined_type === null) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-09-21 03:45:49 +02:00
|
|
|
|
2016-10-03 06:43:24 +02:00
|
|
|
$type = $refined_type;
|
|
|
|
}
|
|
|
|
elseif ($type instanceof Type\ObjectLike && $assignment_key_value) {
|
|
|
|
if (isset($type->properties[$assignment_key_value])) {
|
|
|
|
$type->properties[$assignment_key_value] = Type::combineUnionTypes(
|
|
|
|
$type->properties[$assignment_key_value],
|
|
|
|
$assignment_value_type
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$type->properties[$assignment_key_value] = $assignment_value_type;
|
|
|
|
}
|
|
|
|
}
|
2016-09-21 03:45:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-03 04:00:42 +02:00
|
|
|
if ($this->checkExpression($stmt->var, $context, $array_assignment, $key_type, $keyed_assignment_type, $key_value) === false) {
|
2016-09-21 03:45:49 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-09-12 06:02:26 +02:00
|
|
|
|
2016-06-17 22:58:15 +02:00
|
|
|
if (isset($stmt->var->inferredType)) {
|
2016-10-09 23:54:58 +02:00
|
|
|
/** @var Type\Union */
|
2016-06-17 22:58:15 +02:00
|
|
|
$var_type = $stmt->var->inferredType;
|
|
|
|
|
2016-09-12 06:02:26 +02:00
|
|
|
foreach ($var_type->types as &$type) {
|
2016-10-03 04:00:42 +02:00
|
|
|
if ($type instanceof Type\Generic || $type instanceof Type\ObjectLike) {
|
2016-10-03 17:39:32 +02:00
|
|
|
$value_index = null;
|
2016-09-12 06:02:26 +02:00
|
|
|
|
2016-10-03 04:00:42 +02:00
|
|
|
if ($type instanceof Type\Generic) {
|
|
|
|
// create a union type to pass back to the statement
|
|
|
|
$value_index = count($type->type_params) - 1;
|
|
|
|
|
|
|
|
if ($value_index) {
|
|
|
|
// if we're assigning to an empty array with a key offset, refashion that array
|
|
|
|
if ($array_assignment && $type->type_params[0]->isEmpty()) {
|
|
|
|
if ($key_type) {
|
|
|
|
$type->type_params[0] = $key_type;
|
|
|
|
}
|
2016-09-12 06:02:26 +02:00
|
|
|
}
|
|
|
|
else {
|
2016-10-03 04:00:42 +02:00
|
|
|
if ($key_type) {
|
|
|
|
$key_type = Type::combineUnionTypes($key_type, $type->type_params[0]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$key_type = $type->type_params[0];
|
|
|
|
}
|
2016-09-12 06:02:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-22 19:43:53 +02:00
|
|
|
if ($array_assignment && !$is_object) {
|
2016-09-21 03:45:49 +02:00
|
|
|
// if we're in an array assignment then we need to create some variables
|
|
|
|
// e.g.
|
|
|
|
// $a = [];
|
|
|
|
// $a['b']['c']['d'] = 3;
|
|
|
|
//
|
|
|
|
// means we need add $a['b'], $a['b']['c'] to the current context
|
|
|
|
// (but not $a['b']['c']['d'], which is handled in checkArrayAssignment)
|
|
|
|
if ($keyed_array_var_id && $keyed_assignment_type) {
|
|
|
|
if (isset($context->vars_in_scope[$keyed_array_var_id])) {
|
|
|
|
$context->vars_in_scope[$keyed_array_var_id] = Type::combineUnionTypes(
|
|
|
|
$keyed_assignment_type,
|
|
|
|
$context->vars_in_scope[$keyed_array_var_id]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$context->vars_in_scope[$keyed_array_var_id] = $keyed_assignment_type;
|
|
|
|
}
|
2016-09-12 06:02:26 +02:00
|
|
|
|
2016-09-21 03:45:49 +02:00
|
|
|
$stmt->inferredType = $keyed_assignment_type;
|
|
|
|
}
|
2016-09-12 06:02:26 +02:00
|
|
|
|
2016-09-21 03:45:49 +02:00
|
|
|
if ($array_var_id === $var_id) {
|
2016-10-03 04:00:42 +02:00
|
|
|
if ($type instanceof Type\ObjectLike || ($type->isArray() && !$key_type->hasInt() && $type->type_params[1]->isEmpty())) {
|
|
|
|
$properties = $key_value ? [$key_value => $keyed_assignment_type] : [];
|
|
|
|
|
|
|
|
$assignment_type = new Type\Union([
|
|
|
|
new Type\ObjectLike(
|
|
|
|
'object-like',
|
|
|
|
$properties
|
|
|
|
)
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$assignment_type = new Type\Union([
|
|
|
|
new Type\Generic(
|
|
|
|
'array',
|
|
|
|
[
|
|
|
|
$key_type,
|
|
|
|
$keyed_assignment_type
|
|
|
|
]
|
|
|
|
)
|
|
|
|
]);
|
|
|
|
}
|
2016-09-21 03:45:49 +02:00
|
|
|
|
|
|
|
if (isset($context->vars_in_scope[$var_id])) {
|
|
|
|
$context->vars_in_scope[$var_id] = Type::combineUnionTypes(
|
|
|
|
$context->vars_in_scope[$var_id],
|
|
|
|
$assignment_type
|
|
|
|
);
|
2016-09-12 06:02:26 +02:00
|
|
|
}
|
|
|
|
else {
|
2016-09-21 03:45:49 +02:00
|
|
|
$context->vars_in_scope[$var_id] = $assignment_type;
|
2016-09-12 06:02:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-03 04:00:42 +02:00
|
|
|
if ($type instanceof Type\Generic && $type->type_params[$value_index]->isEmpty()) {
|
2016-09-21 03:45:49 +02:00
|
|
|
$empty_type = Type::getEmptyArray();
|
|
|
|
|
|
|
|
if (!isset($stmt->inferredType)) {
|
|
|
|
// if in array assignment and the referenced variable does not have
|
|
|
|
// an array at this level, create one
|
|
|
|
$stmt->inferredType = $empty_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
$context_type = clone $context->vars_in_scope[$var_id];
|
|
|
|
|
|
|
|
$array_type = $context_type;
|
|
|
|
|
|
|
|
for ($i = 0; $i < $nesting + 1; $i++) {
|
2016-10-03 07:01:24 +02:00
|
|
|
if ($array_type->hasArray()) {
|
|
|
|
if ($i < $nesting) {
|
|
|
|
if ($array_type->types['array']->type_params[1]->isEmpty()) {
|
|
|
|
$new_empty = clone $empty_type;
|
2016-10-03 04:00:42 +02:00
|
|
|
|
2016-10-03 07:01:24 +02:00
|
|
|
$new_empty->types['array']->type_params[0] = $key_type;
|
2016-09-21 03:45:49 +02:00
|
|
|
|
2016-10-03 07:01:24 +02:00
|
|
|
$array_type->types['array']->type_params[1] = $new_empty;
|
|
|
|
continue;
|
|
|
|
}
|
2016-09-21 03:45:49 +02:00
|
|
|
|
2016-10-03 07:01:24 +02:00
|
|
|
$array_type = $array_type->types['array']->type_params[1];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$array_type->types['array']->type_params[0] = $key_type;
|
|
|
|
}
|
2016-09-21 03:45:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$context->vars_in_scope[$var_id] = $context_type;
|
|
|
|
}
|
2016-09-12 06:02:26 +02:00
|
|
|
}
|
2016-10-03 17:39:32 +02:00
|
|
|
elseif ($type instanceof Type\Generic && $value_index !== null) {
|
2016-09-12 06:02:26 +02:00
|
|
|
$stmt->inferredType = $type->type_params[$value_index];
|
|
|
|
}
|
2016-10-03 22:40:46 +02:00
|
|
|
elseif ($type instanceof Type\ObjectLike) {
|
|
|
|
if ($key_value && isset($type->properties[$key_value])) {
|
|
|
|
$stmt->inferredType = clone $type->properties[$key_value];
|
|
|
|
}
|
|
|
|
elseif ($key_type->hasInt()) {
|
2016-10-03 22:47:14 +02:00
|
|
|
$object_like_keys = array_keys($type->properties);
|
|
|
|
if ($object_like_keys) {
|
|
|
|
if (count($object_like_keys) === 1) {
|
|
|
|
$expected_keys_string = '\'' . $object_like_keys[0] . '\'';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$last_key = array_pop($object_like_keys);
|
|
|
|
$expected_keys_string = '\'' . implode('\', \'', $object_like_keys) . '\' or \'' . $last_key . '\'';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$expected_keys_string = 'string';
|
|
|
|
}
|
|
|
|
|
2016-10-03 22:40:46 +02:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidArrayAccess(
|
2016-10-14 00:27:23 +02:00
|
|
|
'Cannot access value on object-like variable ' . $var_id . ' using int offset - expecting ' . $expected_keys_string,
|
2016-10-03 22:40:46 +02:00
|
|
|
$this->checked_file_name,
|
|
|
|
$stmt->getLine()
|
|
|
|
),
|
|
|
|
$this->suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-10-03 16:41:21 +02:00
|
|
|
}
|
2016-09-12 06:02:26 +02:00
|
|
|
}
|
|
|
|
elseif ($type->isString()) {
|
|
|
|
if ($key_type) {
|
|
|
|
$key_type = Type::combineUnionTypes($key_type, Type::getInt());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$key_type = Type::getInt();
|
|
|
|
}
|
2016-09-09 22:21:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-21 22:42:39 +02:00
|
|
|
if ($keyed_array_var_id && isset($context->vars_in_scope[$keyed_array_var_id])) {
|
|
|
|
$stmt->inferredType = $context->vars_in_scope[$keyed_array_var_id];
|
|
|
|
}
|
|
|
|
|
2016-09-10 00:36:35 +02:00
|
|
|
if (!isset($stmt->inferredType)) {
|
|
|
|
$stmt->inferredType = Type::getMixed();
|
|
|
|
}
|
|
|
|
|
2016-09-09 22:21:49 +02:00
|
|
|
if (!$key_type) {
|
|
|
|
$key_type = new Type\Union([
|
|
|
|
new Type\Atomic('int'),
|
|
|
|
new Type\Atomic('string')
|
|
|
|
]);
|
2016-06-17 22:58:15 +02:00
|
|
|
}
|
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
if ($stmt->dim) {
|
2016-09-12 18:23:51 +02:00
|
|
|
if (isset($stmt->dim->inferredType) && $key_type && !$key_type->isEmpty()) {
|
2016-06-17 22:58:15 +02:00
|
|
|
foreach ($stmt->dim->inferredType->types as $at) {
|
2016-10-09 23:54:58 +02:00
|
|
|
if (($at->isMixed() || $at->isEmpty()) && !$key_type->isMixed()) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MixedArrayOffset(
|
2016-10-14 00:27:23 +02:00
|
|
|
'Cannot access value on variable ' . $var_id . ' using mixed offset - expecting ' . $key_type,
|
2016-10-09 23:54:58 +02:00
|
|
|
$this->checked_file_name,
|
|
|
|
$stmt->getLine()
|
|
|
|
),
|
|
|
|
$this->suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-09-10 00:36:35 +02:00
|
|
|
}
|
|
|
|
elseif (!$at->isIn($key_type)) {
|
2016-06-26 21:18:40 +02:00
|
|
|
if (IssueBuffer::accepts(
|
2016-07-22 19:29:46 +02:00
|
|
|
new InvalidArrayAccess(
|
2016-10-14 00:27:23 +02:00
|
|
|
'Cannot access value on variable ' . $var_id . ' using ' . $at . ' offset - expecting ' . $key_type,
|
2016-08-25 01:00:44 +02:00
|
|
|
$this->checked_file_name,
|
2016-07-22 19:29:46 +02:00
|
|
|
$stmt->getLine()
|
|
|
|
),
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->suppressed_issues
|
2016-06-17 22:58:15 +02:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* @param PhpParser\Node\Scalar\Encapsed $stmt
|
|
|
|
* @param Context $context
|
|
|
|
* @return false|null
|
|
|
|
*/
|
2016-08-11 00:10:12 +02:00
|
|
|
protected function checkEncapsulatedString(PhpParser\Node\Scalar\Encapsed $stmt, Context $context)
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
2016-10-09 23:54:58 +02:00
|
|
|
/** @var PhpParser\Node\Expr $part */
|
2016-01-20 00:27:06 +01:00
|
|
|
foreach ($stmt->parts as $part) {
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->checkExpression($part, $context) === false) {
|
2016-06-06 02:25:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
2016-09-12 17:32:44 +02:00
|
|
|
|
|
|
|
$stmt->inferredType = Type::getString();
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* @param string $var_name
|
|
|
|
* @param int $line_number
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-01-20 00:27:06 +01:00
|
|
|
public function registerVariable($var_name, $line_number)
|
|
|
|
{
|
2016-08-11 00:10:12 +02:00
|
|
|
if (!isset($this->all_vars[$var_name])) {
|
|
|
|
$this->all_vars[$var_name] = $line_number;
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* @param PhpParser\Node\Expr $dim
|
|
|
|
* @return Type\Union
|
|
|
|
*/
|
|
|
|
protected static function getArrayTypeFromDim(PhpParser\Node\Expr $dim)
|
2016-09-12 06:34:38 +02:00
|
|
|
{
|
|
|
|
if ($dim) {
|
2016-09-13 01:30:57 +02:00
|
|
|
if (isset($dim->inferredType)) {
|
2016-09-12 06:34:38 +02:00
|
|
|
return $dim->inferredType;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return new Type\Union([Type::getInt()->types['int'], Type::getString()->types['string']]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return Type::getInt();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-27 00:42:48 +02:00
|
|
|
/**
|
2016-10-09 23:54:58 +02:00
|
|
|
* @param string $function_id
|
|
|
|
* @param Context $context
|
2016-08-14 05:26:45 +02:00
|
|
|
* @return bool
|
2016-04-27 00:42:48 +02:00
|
|
|
*/
|
2016-10-09 23:54:58 +02:00
|
|
|
protected function checkFunctionExists($function_id, Context $context, $line_number)
|
2016-03-23 18:05:25 +01:00
|
|
|
{
|
2016-08-15 19:51:43 +02:00
|
|
|
$cased_function_id = $function_id;
|
|
|
|
$function_id = strtolower($function_id);
|
|
|
|
|
2016-08-14 05:26:45 +02:00
|
|
|
if (!isset($this->existing_functions[$function_id])) {
|
|
|
|
$this->existing_functions[$function_id] = FunctionChecker::functionExists($function_id, $context->file_name);
|
2016-03-23 18:05:25 +01:00
|
|
|
}
|
|
|
|
|
2016-08-14 05:26:45 +02:00
|
|
|
if (!$this->existing_functions[$function_id]) {
|
2016-06-26 21:18:40 +02:00
|
|
|
if (IssueBuffer::accepts(
|
2016-10-09 23:54:58 +02:00
|
|
|
new UndefinedFunction('Function ' . $cased_function_id . ' does not exist', $this->checked_file_name, $line_number),
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->suppressed_issues
|
2016-06-06 02:25:16 +02:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-03-23 18:05:25 +01:00
|
|
|
}
|
|
|
|
|
2016-08-14 05:26:45 +02:00
|
|
|
return true;
|
2016-03-23 18:05:25 +01:00
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* @param PhpParser\Node\Expr\Include_ $stmt
|
|
|
|
* @param Context $context
|
|
|
|
* @return false|null
|
|
|
|
*/
|
2016-08-11 00:10:12 +02:00
|
|
|
protected function checkInclude(PhpParser\Node\Expr\Include_ $stmt, Context $context)
|
2016-07-25 21:05:58 +02:00
|
|
|
{
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($this->checkExpression($stmt->expr, $context) === false) {
|
2016-07-25 21:05:58 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$path_to_file = null;
|
|
|
|
|
|
|
|
if ($stmt->expr instanceof PhpParser\Node\Scalar\String_) {
|
|
|
|
$path_to_file = $stmt->expr->value;
|
|
|
|
|
|
|
|
// attempts to resolve using get_include_path dirs
|
2016-08-25 01:00:44 +02:00
|
|
|
$include_path = self::resolveIncludePath($path_to_file, dirname($this->checked_file_name));
|
2016-07-25 21:05:58 +02:00
|
|
|
$path_to_file = $include_path ? $include_path : $path_to_file;
|
|
|
|
|
|
|
|
if ($path_to_file[0] !== '/') {
|
|
|
|
$path_to_file = getcwd() . '/' . $path_to_file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2016-09-15 22:31:36 +02:00
|
|
|
$path_to_file = self::getPathTo($stmt->expr, $this->include_file_name ?: $this->file_name);
|
2016-07-25 21:05:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($path_to_file) {
|
|
|
|
$reduce_pattern = '/\/[^\/]+\/\.\.\//';
|
|
|
|
|
|
|
|
while (preg_match($reduce_pattern, $path_to_file)) {
|
|
|
|
$path_to_file = preg_replace($reduce_pattern, '/', $path_to_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the file is already included, we can't check much more
|
|
|
|
if (in_array($path_to_file, get_included_files())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-10 07:28:38 +02:00
|
|
|
/*
|
2016-07-25 21:05:58 +02:00
|
|
|
if (in_array($path_to_file, FileChecker::getIncludesToIgnore())) {
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->check_classes = false;
|
|
|
|
$this->check_variables = false;
|
2016-07-26 00:30:49 +02:00
|
|
|
|
2016-07-25 21:05:58 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-08-10 07:28:38 +02:00
|
|
|
*/
|
2016-07-25 21:05:58 +02:00
|
|
|
|
|
|
|
if (file_exists($path_to_file)) {
|
2016-08-14 05:26:45 +02:00
|
|
|
$include_stmts = FileChecker::getStatementsForFile($path_to_file);
|
2016-08-25 01:00:44 +02:00
|
|
|
$old_include_file_name = $this->include_file_name;
|
|
|
|
$this->include_file_name = Config::getInstance()->shortenFileName($path_to_file);
|
|
|
|
$this->source->setIncludeFileName($this->include_file_name);
|
2016-08-14 05:26:45 +02:00
|
|
|
$this->check($include_stmts, $context);
|
2016-08-25 01:00:44 +02:00
|
|
|
$this->include_file_name = $old_include_file_name;
|
|
|
|
$this->source->setIncludeFileName($old_include_file_name);
|
2016-07-25 21:05:58 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-08-14 06:38:29 +02:00
|
|
|
|
|
|
|
$this->check_classes = false;
|
|
|
|
$this->check_variables = false;
|
|
|
|
$this->check_functions = false;
|
2016-07-25 21:05:58 +02:00
|
|
|
}
|
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
/**
|
|
|
|
* Parse a docblock comment into its parts.
|
|
|
|
*
|
|
|
|
* Taken from advanced api docmaker
|
|
|
|
* Which was taken from https://github.com/facebook/libphutil/blob/master/src/parser/docblock/PhutilDocblockParser.php
|
|
|
|
*
|
2016-10-09 23:54:58 +02:00
|
|
|
* @param string $docblock
|
2016-01-20 00:27:06 +01:00
|
|
|
* @return array Array of the main comment and specials
|
|
|
|
*/
|
2016-04-16 22:28:25 +02:00
|
|
|
public static function parseDocComment($docblock)
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
|
|
|
// Strip off comments.
|
|
|
|
$docblock = trim($docblock);
|
|
|
|
$docblock = preg_replace('@^/\*\*@', '', $docblock);
|
|
|
|
$docblock = preg_replace('@\*/$@', '', $docblock);
|
|
|
|
$docblock = preg_replace('@^\s*\*@m', '', $docblock);
|
|
|
|
|
|
|
|
// Normalize multi-line @specials.
|
|
|
|
$lines = explode("\n", $docblock);
|
|
|
|
$last = false;
|
|
|
|
foreach ($lines as $k => $line) {
|
|
|
|
if (preg_match('/^\s?@\w/i', $line)) {
|
|
|
|
$last = $k;
|
|
|
|
} elseif (preg_match('/^\s*$/', $line)) {
|
|
|
|
$last = false;
|
|
|
|
} elseif ($last !== false) {
|
|
|
|
$lines[$last] = rtrim($lines[$last]).' '.trim($line);
|
|
|
|
unset($lines[$k]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$docblock = implode("\n", $lines);
|
|
|
|
|
|
|
|
$special = array();
|
|
|
|
|
|
|
|
// Parse @specials.
|
|
|
|
$matches = null;
|
2016-10-04 03:36:42 +02:00
|
|
|
$have_specials = preg_match_all('/^\s?@([\w\-:]+)\s*([^\n]*)/m', $docblock, $matches, PREG_SET_ORDER);
|
2016-01-20 00:27:06 +01:00
|
|
|
if ($have_specials) {
|
2016-10-04 03:36:42 +02:00
|
|
|
$docblock = preg_replace('/^\s?@([\w\-:]+)\s*([^\n]*)/m', '', $docblock);
|
2016-01-20 00:27:06 +01:00
|
|
|
foreach ($matches as $match) {
|
|
|
|
list($_, $type, $data) = $match;
|
|
|
|
|
|
|
|
if (empty($special[$type])) {
|
|
|
|
$special[$type] = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
$special[$type][] = $data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$docblock = str_replace("\t", ' ', $docblock);
|
|
|
|
|
|
|
|
// Smush the whole docblock to the left edge.
|
|
|
|
$min_indent = 80;
|
|
|
|
$indent = 0;
|
|
|
|
foreach (array_filter(explode("\n", $docblock)) as $line) {
|
|
|
|
for ($ii = 0; $ii < strlen($line); $ii++) {
|
|
|
|
if ($line[$ii] != ' ') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$indent++;
|
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/** @var int */
|
2016-01-20 00:27:06 +01:00
|
|
|
$min_indent = min($indent, $min_indent);
|
|
|
|
}
|
|
|
|
|
|
|
|
$docblock = preg_replace('/^' . str_repeat(' ', $min_indent) . '/m', '', $docblock);
|
|
|
|
$docblock = rtrim($docblock);
|
|
|
|
|
|
|
|
// Trim any empty lines off the front, but leave the indent level if there
|
|
|
|
// is one.
|
|
|
|
$docblock = preg_replace('/^\s*\n/', '', $docblock);
|
|
|
|
|
|
|
|
return array('description' => $docblock, 'specials' => $special);
|
|
|
|
}
|
|
|
|
|
2016-04-27 00:42:48 +02:00
|
|
|
/**
|
2016-10-09 23:54:58 +02:00
|
|
|
* @param object-like{description:string,specials:array<string,array<string>>} $parsed_doc_comment
|
2016-04-27 00:42:48 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-04-27 00:17:47 +02:00
|
|
|
public static function renderDocComment(array $parsed_doc_comment)
|
|
|
|
{
|
|
|
|
$doc_comment_text = '/**' . PHP_EOL;
|
|
|
|
|
2016-04-30 20:14:22 +02:00
|
|
|
$description_lines = null;
|
|
|
|
|
2016-04-30 21:43:06 +02:00
|
|
|
$trimmed_description = trim($parsed_doc_comment['description']);
|
|
|
|
|
|
|
|
if (!empty($trimmed_description)) {
|
2016-04-30 20:14:22 +02:00
|
|
|
$description_lines = explode(PHP_EOL, $parsed_doc_comment['description']);
|
2016-04-27 00:17:47 +02:00
|
|
|
|
2016-04-30 20:14:22 +02:00
|
|
|
foreach ($description_lines as $line) {
|
|
|
|
$doc_comment_text .= ' * ' . $line . PHP_EOL;
|
|
|
|
}
|
2016-04-27 00:17:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($description_lines && $parsed_doc_comment['specials']) {
|
|
|
|
$doc_comment_text .= ' *' . PHP_EOL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($parsed_doc_comment['specials']) {
|
|
|
|
$type_lengths = array_map('strlen', array_keys($parsed_doc_comment['specials']));
|
2016-10-09 23:54:58 +02:00
|
|
|
/** @var int */
|
2016-04-27 00:17:47 +02:00
|
|
|
$type_width = max($type_lengths) + 1;
|
|
|
|
|
|
|
|
foreach ($parsed_doc_comment['specials'] as $type => $lines) {
|
|
|
|
foreach ($lines as $line) {
|
|
|
|
$doc_comment_text .= ' * @' . str_pad($type, $type_width) . $line . PHP_EOL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$doc_comment_text .= ' */';
|
|
|
|
|
|
|
|
return $doc_comment_text;
|
|
|
|
}
|
|
|
|
|
2016-04-27 00:42:48 +02:00
|
|
|
/**
|
2016-10-09 23:54:58 +02:00
|
|
|
* @param string $method_id
|
|
|
|
* @param int $argument_offset
|
|
|
|
* @return boolean
|
2016-04-27 00:42:48 +02:00
|
|
|
*/
|
2016-10-09 23:54:58 +02:00
|
|
|
protected function isPassedByReference($method_id, array $args, $argument_offset)
|
2016-02-27 01:11:11 +01:00
|
|
|
{
|
2016-10-09 23:54:58 +02:00
|
|
|
$function_params = FunctionLikeChecker::getParamsById($method_id, $args, $this->file_name);
|
2016-02-27 01:11:11 +01:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
return $argument_offset < count($function_params) && $function_params[$argument_offset]->by_ref;
|
2016-02-27 01:11:11 +01:00
|
|
|
}
|
2016-03-23 18:05:25 +01:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* @param PhpParser\Node\Expr $stmt
|
|
|
|
* @param string $file_name
|
|
|
|
* @return string|null
|
|
|
|
*/
|
2016-08-11 00:10:12 +02:00
|
|
|
protected static function getPathTo(PhpParser\Node\Expr $stmt, $file_name)
|
2016-03-23 18:05:25 +01:00
|
|
|
{
|
|
|
|
if ($file_name[0] !== '/') {
|
|
|
|
$file_name = getcwd() . '/' . $file_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Scalar\String_) {
|
|
|
|
return $stmt->value;
|
|
|
|
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\BinaryOp\Concat) {
|
2016-08-11 00:10:12 +02:00
|
|
|
$left_string = self::getPathTo($stmt->left, $file_name);
|
|
|
|
$right_string = self::getPathTo($stmt->right, $file_name);
|
2016-03-23 18:05:25 +01:00
|
|
|
|
|
|
|
if ($left_string && $right_string) {
|
|
|
|
return $left_string . $right_string;
|
|
|
|
}
|
|
|
|
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\FuncCall &&
|
|
|
|
$stmt->name instanceof PhpParser\Node\Name &&
|
|
|
|
$stmt->name->parts === ['dirname']) {
|
|
|
|
|
|
|
|
if ($stmt->args) {
|
2016-08-11 00:10:12 +02:00
|
|
|
$evaled_path = self::getPathTo($stmt->args[0]->value, $file_name);
|
2016-03-23 18:05:25 +01:00
|
|
|
|
|
|
|
if (!$evaled_path) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return dirname($evaled_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\ConstFetch && $stmt->name instanceof PhpParser\Node\Name) {
|
|
|
|
$const_name = implode('', $stmt->name->parts);
|
|
|
|
|
|
|
|
if (defined($const_name)) {
|
|
|
|
return constant($const_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Scalar\MagicConst\Dir) {
|
|
|
|
return dirname($file_name);
|
|
|
|
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Scalar\MagicConst\File) {
|
|
|
|
return $file_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-04-27 00:42:48 +02:00
|
|
|
/**
|
2016-06-20 07:05:44 +02:00
|
|
|
* @return string|null
|
2016-04-27 00:42:48 +02:00
|
|
|
*/
|
2016-08-11 00:10:12 +02:00
|
|
|
protected static function resolveIncludePath($file_name, $current_directory)
|
2016-03-23 18:05:25 +01:00
|
|
|
{
|
|
|
|
$paths = PATH_SEPARATOR == ':' ?
|
|
|
|
preg_split('#(?<!phar):#', get_include_path()) :
|
|
|
|
explode(PATH_SEPARATOR, get_include_path());
|
|
|
|
|
|
|
|
foreach ($paths as $prefix) {
|
|
|
|
$ds = substr($prefix, -1) == DIRECTORY_SEPARATOR ? '' : DIRECTORY_SEPARATOR;
|
|
|
|
|
|
|
|
if ($prefix === '.') {
|
|
|
|
$prefix = $current_directory;
|
|
|
|
}
|
|
|
|
|
|
|
|
$file = $prefix . $ds . $file_name;
|
|
|
|
|
|
|
|
if (file_exists($file)) {
|
|
|
|
return $file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-01 16:52:43 +02:00
|
|
|
|
2016-04-03 02:27:36 +02:00
|
|
|
public static function setMockInterfaces(array $classes)
|
|
|
|
{
|
2016-08-11 00:10:12 +02:00
|
|
|
self::$mock_interfaces = $classes;
|
2016-04-01 16:52:43 +02:00
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* @param string $absolute_class
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2016-04-16 22:28:25 +02:00
|
|
|
public static function isMock($absolute_class)
|
2016-04-01 16:52:43 +02:00
|
|
|
{
|
2016-06-10 20:47:44 +02:00
|
|
|
return in_array($absolute_class, Config::getInstance()->getMockClasses());
|
2016-04-01 16:52:43 +02:00
|
|
|
}
|
2016-04-03 02:27:36 +02:00
|
|
|
|
2016-04-27 00:42:48 +02:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-08-11 00:10:12 +02:00
|
|
|
protected static function containsBooleanOr(PhpParser\Node\Expr\BinaryOp $stmt)
|
2016-04-03 20:50:57 +02:00
|
|
|
{
|
2016-04-04 07:21:28 +02:00
|
|
|
// we only want to discount expressions where either the whole thing is an or
|
2016-04-03 20:50:57 +02:00
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\BooleanOr) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-16 22:28:25 +02:00
|
|
|
public function getAliasedClasses()
|
2016-04-03 20:25:41 +02:00
|
|
|
{
|
2016-08-11 00:10:12 +02:00
|
|
|
return $this->aliased_classes;
|
2016-04-03 20:25:41 +02:00
|
|
|
}
|
2016-05-09 14:56:07 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* @param string $method_id
|
|
|
|
* @param boolean $include_constructor
|
|
|
|
* @return array<string,Type\Union>
|
|
|
|
*/
|
2016-05-09 14:56:07 +02:00
|
|
|
public static function getThisAssignments($method_id, $include_constructor = false)
|
|
|
|
{
|
|
|
|
$absolute_class = explode('::', $method_id)[0];
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/** @var array<string,Type\Union> */
|
2016-05-09 14:56:07 +02:00
|
|
|
$this_assignments = [];
|
|
|
|
|
2016-08-11 00:10:12 +02:00
|
|
|
if ($include_constructor && isset(self::$this_assignments[$absolute_class . '::__construct'])) {
|
2016-10-09 23:54:58 +02:00
|
|
|
/** @var array<string,Type\Union> */
|
2016-08-11 00:10:12 +02:00
|
|
|
$this_assignments = self::$this_assignments[$absolute_class . '::__construct'];
|
2016-05-09 14:56:07 +02:00
|
|
|
}
|
|
|
|
|
2016-08-11 00:10:12 +02:00
|
|
|
if (isset(self::$this_assignments[$method_id])) {
|
2016-10-09 23:54:58 +02:00
|
|
|
/** @var array<string,Type\Union> */
|
2016-08-11 00:10:12 +02:00
|
|
|
$this_assignments = TypeChecker::combineKeyedTypes($this_assignments, self::$this_assignments[$method_id]);
|
2016-05-09 14:56:07 +02:00
|
|
|
}
|
|
|
|
|
2016-08-11 00:10:12 +02:00
|
|
|
if (isset(self::$this_calls[$method_id])) {
|
|
|
|
foreach (self::$this_calls[$method_id] as $call) {
|
2016-05-09 14:56:07 +02:00
|
|
|
$call_assingments = self::getThisAssignments($absolute_class . '::' . $call);
|
2016-06-14 01:57:32 +02:00
|
|
|
$this_assignments = TypeChecker::combineKeyedTypes($this_assignments, $call_assingments);
|
2016-05-09 14:56:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this_assignments;
|
|
|
|
}
|
2016-10-02 04:47:32 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* @param PhpParser\Node\Expr $stmt
|
|
|
|
* @return PhpParser\Node\Expr|null
|
|
|
|
*/
|
2016-10-02 04:47:32 +02:00
|
|
|
protected static function getFirstFunctionCall(PhpParser\Node\Expr $stmt)
|
|
|
|
{
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\MethodCall
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\StaticCall
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\FuncCall
|
|
|
|
) {
|
|
|
|
return $stmt;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp) {
|
|
|
|
return self::getFirstFunctionCall($stmt->left);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\BooleanNot) {
|
|
|
|
return self::getFirstFunctionCall($stmt->expr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2016-10-02 19:05:49 +02:00
|
|
|
|
|
|
|
protected function isPhantomClass($class_name)
|
|
|
|
{
|
|
|
|
return isset($this->phantom_classes[$class_name]);
|
|
|
|
}
|
2016-01-20 16:57:56 +01:00
|
|
|
}
|