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-10-22 23:35:59 +02:00
|
|
|
use Psalm\Checker\Statements\Block\ForChecker;
|
2016-10-22 19:23:18 +02:00
|
|
|
use Psalm\Checker\Statements\Block\ForeachChecker;
|
|
|
|
use Psalm\Checker\Statements\Block\IfChecker;
|
2016-10-22 19:37:06 +02:00
|
|
|
use Psalm\Checker\Statements\Block\SwitchChecker;
|
2016-10-22 23:35:59 +02:00
|
|
|
use Psalm\Checker\Statements\Block\TryChecker;
|
|
|
|
use Psalm\Checker\Statements\Block\WhileChecker;
|
2016-10-22 19:23:18 +02:00
|
|
|
use Psalm\Checker\Statements\ExpressionChecker;
|
2016-11-01 16:37:58 +01:00
|
|
|
use Psalm\Checker\Statements\Expression\AssignmentChecker;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\Config;
|
|
|
|
use Psalm\Context;
|
2016-10-17 22:42:23 +02:00
|
|
|
use Psalm\Issue\ContinueOutsideLoop;
|
2016-11-05 22:53:30 +01:00
|
|
|
use Psalm\Issue\InvalidGlobal;
|
2016-10-09 23:54:58 +02:00
|
|
|
use Psalm\Issue\InvalidNamespace;
|
2016-11-06 01:53:39 +01:00
|
|
|
use Psalm\Issue\UnrecognizedStatement;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\IssueBuffer;
|
2016-08-13 20:20:46 +02:00
|
|
|
use Psalm\StatementsSource;
|
2016-10-22 19:23:18 +02:00
|
|
|
use Psalm\Type;
|
2016-08-13 20:20:46 +02:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
class StatementsChecker
|
|
|
|
{
|
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-11-01 05:39:41 +01:00
|
|
|
/**
|
|
|
|
* @var array<string, int>
|
|
|
|
*/
|
2016-08-11 00:10:12 +02:00
|
|
|
protected $all_vars = [];
|
2016-11-01 05:39:41 +01:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @var string|null
|
|
|
|
*/
|
2016-08-11 00:10:12 +02:00
|
|
|
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-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-08-11 00:10:12 +02:00
|
|
|
protected $namespace;
|
2016-10-09 23:54:58 +02:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @var array<string,string>
|
|
|
|
*/
|
2016-11-21 03:49:06 +01:00
|
|
|
protected $aliased_classes = [];
|
2016-10-09 23:54:58 +02:00
|
|
|
|
2016-11-21 04:02:26 +01:00
|
|
|
/**
|
|
|
|
* @var array<string,string>
|
|
|
|
*/
|
|
|
|
protected $aliased_classes_flipped = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array<string,string>
|
|
|
|
*/
|
|
|
|
protected $aliased_constants = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array<string,string>
|
|
|
|
*/
|
|
|
|
protected $aliased_functions = [];
|
|
|
|
|
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-11-07 23:29:51 +01:00
|
|
|
protected $fq_class_name;
|
2016-10-09 23:54:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var TypeChecker
|
|
|
|
*/
|
2016-08-11 00:10:12 +02:00
|
|
|
protected $type_checker;
|
|
|
|
|
2016-07-22 19:29:46 +02:00
|
|
|
/**
|
|
|
|
* A list of suppressed issues
|
2016-11-02 07:29:00 +01:00
|
|
|
*
|
2016-11-01 05:39:41 +01:00
|
|
|
* @var array<string>
|
2016-07-22 19:29:46 +02:00
|
|
|
*/
|
2016-08-11 00:10:12 +02:00
|
|
|
protected $suppressed_issues;
|
2016-07-22 19:29:46 +02:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @var array<string, array<string, Type\Union>>
|
|
|
|
*/
|
2016-08-14 06:38:29 +02:00
|
|
|
protected static $user_constants = [];
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @param StatementsSource $source
|
|
|
|
*/
|
2016-10-18 22:28:51 +02:00
|
|
|
public function __construct(StatementsSource $source)
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->source = $source;
|
|
|
|
$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->namespace = $this->source->getNamespace();
|
|
|
|
$this->is_static = $this->source->isStatic();
|
2016-11-08 01:16:51 +01:00
|
|
|
$this->fq_class_name = $this->source->getFQCLN();
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->class_name = $this->source->getClassName();
|
|
|
|
$this->parent_class = $this->source->getParentClass();
|
2016-11-21 03:49:06 +01:00
|
|
|
$this->aliased_classes = $this->source->getAliasedClasses();
|
2016-11-21 04:02:26 +01:00
|
|
|
$this->aliased_classes_flipped = $this->source->getAliasedClassesFlipped();
|
2016-11-21 03:49:06 +01:00
|
|
|
$this->aliased_constants = $this->source->getAliasedConstants();
|
|
|
|
$this->aliased_functions = $this->source->getAliasedFunctions();
|
2016-08-11 00:10:12 +02:00
|
|
|
$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-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-11-05 22:53:30 +01:00
|
|
|
* @param Context|null $global_context
|
2016-06-18 20:45:55 +02:00
|
|
|
* @return null|false
|
|
|
|
*/
|
2016-11-05 22:53:30 +01:00
|
|
|
public function check(array $stmts, Context $context, Context $loop_context = null, Context $global_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-11-02 07:29:00 +01:00
|
|
|
if ($plugin->checkStatement(
|
|
|
|
$stmt,
|
|
|
|
$context,
|
|
|
|
$this->checked_file_name,
|
|
|
|
$this->getSuppressedIssues()
|
|
|
|
) === false) {
|
2016-06-18 20:45:55 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
if ($has_returned && !($stmt instanceof PhpParser\Node\Stmt\Nop) &&
|
|
|
|
!($stmt instanceof PhpParser\Node\Stmt\InlineHTML)) {
|
|
|
|
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-11-13 17:54:40 +01:00
|
|
|
/*
|
2016-11-13 17:24:46 +01:00
|
|
|
if (isset($context->vars_in_scope['$value_types'])) {
|
|
|
|
var_dump($stmt->getLine() . ' ' . $context->vars_in_scope['$value_types']);
|
2016-10-09 23:54:58 +02:00
|
|
|
}
|
2016-11-13 17:54:40 +01:00
|
|
|
*/
|
2016-10-09 23:54:58 +02:00
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\If_) {
|
2016-10-22 19:23:18 +02:00
|
|
|
IfChecker::check($this, $stmt, $context, $loop_context);
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\TryCatch) {
|
2016-10-22 23:35:59 +02:00
|
|
|
TryChecker::check($this, $stmt, $context, $loop_context);
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\For_) {
|
2016-10-22 23:35:59 +02:00
|
|
|
ForChecker::check($this, $stmt, $context);
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Foreach_) {
|
2016-10-22 19:23:18 +02:00
|
|
|
ForeachChecker::check($this, $stmt, $context);
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\While_) {
|
2016-10-22 23:35:59 +02:00
|
|
|
WhileChecker::check($this, $stmt, $context);
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Do_) {
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->checkDo($stmt, $context);
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Const_) {
|
2016-10-19 06:00:49 +02:00
|
|
|
$this->checkConstAssignment($stmt, $context);
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Unset_) {
|
2016-09-17 17:57:44 +02:00
|
|
|
foreach ($stmt->vars as $var) {
|
2016-11-02 07:29:00 +01:00
|
|
|
$var_id = ExpressionChecker::getArrayVarId(
|
|
|
|
$var,
|
2016-11-07 23:29:51 +01:00
|
|
|
$this->fq_class_name,
|
2016-11-02 07:29:00 +01:00
|
|
|
$this->namespace,
|
|
|
|
$this->aliased_classes
|
|
|
|
);
|
2016-09-17 17:57:44 +02:00
|
|
|
|
|
|
|
if ($var_id) {
|
|
|
|
$context->remove($var_id);
|
|
|
|
}
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Return_) {
|
2016-01-20 00:27:06 +01:00
|
|
|
$has_returned = true;
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->checkReturn($stmt, $context);
|
2016-11-02 07:29:00 +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-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Switch_) {
|
2016-10-22 19:37:06 +02:00
|
|
|
SwitchChecker::check($this, $stmt, $context, $loop_context);
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Break_) {
|
2016-01-20 00:27:06 +01:00
|
|
|
// do nothing
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Continue_) {
|
2016-10-17 22:42:23 +02:00
|
|
|
if ($loop_context === null) {
|
|
|
|
if (IssueBuffer::accepts(
|
2016-11-02 07:29:00 +01:00
|
|
|
new ContinueOutsideLoop(
|
|
|
|
'Continue call outside loop context',
|
|
|
|
$this->checked_file_name,
|
|
|
|
$stmt->getLine()
|
|
|
|
),
|
2016-10-17 22:42:23 +02:00
|
|
|
$this->suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-03 02:27:36 +02:00
|
|
|
$has_returned = true;
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Static_) {
|
2016-08-11 00:10:12 +02:00
|
|
|
$this->checkStatic($stmt, $context);
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Echo_) {
|
2016-01-20 00:27:06 +01:00
|
|
|
foreach ($stmt->exprs as $expr) {
|
2016-10-22 19:23:18 +02:00
|
|
|
ExpressionChecker::check($this, $expr, $context);
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
2016-11-02 07:29:00 +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);
|
2016-11-06 01:17:22 +01:00
|
|
|
$function_checkers[$stmt->name]->check($function_context, $context);
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr) {
|
2016-10-22 19:23:18 +02:00
|
|
|
ExpressionChecker::check($this, $stmt, $context);
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\InlineHTML) {
|
2016-01-20 00:27:06 +01:00
|
|
|
// do nothing
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Use_) {
|
2016-11-21 04:40:19 +01:00
|
|
|
foreach ($stmt->uses as $use) {
|
|
|
|
$use_path = implode('\\', $use->name->parts);
|
|
|
|
|
|
|
|
switch ($use->type !== PhpParser\Node\Stmt\Use_::TYPE_UNKNOWN ? $use->type : $stmt->type) {
|
|
|
|
case PhpParser\Node\Stmt\Use_::TYPE_FUNCTION:
|
|
|
|
$this->aliased_functions[strtolower($use->alias)] = $use_path;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PhpParser\Node\Stmt\Use_::TYPE_CONSTANT:
|
|
|
|
$this->aliased_constants[$use->alias] = $use_path;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PhpParser\Node\Stmt\Use_::TYPE_NORMAL:
|
|
|
|
$this->aliased_classes[strtolower($use->alias)] = $use_path;
|
|
|
|
$this->aliased_classes_flipped[$use_path] = strtolower($use->alias);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\GroupUse) {
|
|
|
|
$use_prefix = implode('\\', $stmt->prefix->parts);
|
2016-11-21 03:49:06 +01:00
|
|
|
|
2016-11-21 04:40:19 +01:00
|
|
|
foreach ($stmt->uses as $use) {
|
|
|
|
$use_path = $use_prefix . '\\' . implode('\\', $use->name->parts);
|
2016-11-21 03:49:06 +01:00
|
|
|
|
2016-11-21 04:40:19 +01:00
|
|
|
switch ($use->type !== PhpParser\Node\Stmt\Use_::TYPE_UNKNOWN ? $use->type : $stmt->type) {
|
|
|
|
case PhpParser\Node\Stmt\Use_::TYPE_FUNCTION:
|
|
|
|
$this->aliased_functions[strtolower($use->alias)] = $use_path;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PhpParser\Node\Stmt\Use_::TYPE_CONSTANT:
|
|
|
|
$this->aliased_constants[$use->alias] = $use_path;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PhpParser\Node\Stmt\Use_::TYPE_NORMAL:
|
|
|
|
$this->aliased_classes[strtolower($use->alias)] = $use_path;
|
|
|
|
$this->aliased_classes_flipped[$use_path] = strtolower($use->alias);
|
|
|
|
break;
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Global_) {
|
2016-11-05 22:53:30 +01:00
|
|
|
if (!$global_context) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidGlobal(
|
|
|
|
'Cannot use global scope here',
|
|
|
|
$this->checked_file_name,
|
|
|
|
$stmt->getLine()
|
|
|
|
),
|
|
|
|
$this->suppressed_issues
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
foreach ($stmt->vars as $var) {
|
|
|
|
if ($var instanceof PhpParser\Node\Expr\Variable) {
|
|
|
|
if (is_string($var->name)) {
|
|
|
|
$var_id = '$' . $var->name;
|
|
|
|
|
|
|
|
$context->vars_in_scope[$var_id] = isset($global_context->vars_in_scope[$var_id])
|
|
|
|
? clone $global_context->vars_in_scope[$var_id]
|
|
|
|
: Type::getMixed();
|
|
|
|
|
|
|
|
$context->vars_possibly_in_scope[$var_id] = true;
|
|
|
|
} else {
|
|
|
|
ExpressionChecker::check($this, $var, $context);
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Property) {
|
2016-01-26 20:11:56 +01:00
|
|
|
foreach ($stmt->props as $prop) {
|
|
|
|
if ($prop->default) {
|
2016-10-22 19:23:18 +02:00
|
|
|
ExpressionChecker::check($this, $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-11-02 07:29:00 +01:00
|
|
|
if (AssignmentChecker::checkPropertyAssignment(
|
|
|
|
$this,
|
|
|
|
$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-01-26 20:11:56 +01:00
|
|
|
}
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\ClassConst) {
|
2016-08-14 03:14:32 +02:00
|
|
|
foreach ($stmt->consts as $const) {
|
2016-10-22 19:23:18 +02:00
|
|
|
ExpressionChecker::check($this, $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()) {
|
2016-11-02 07:29:00 +01:00
|
|
|
ClassLikeChecker::setConstantType(
|
2016-11-07 23:29:51 +01:00
|
|
|
$this->fq_class_name,
|
2016-11-02 07:29:00 +01:00
|
|
|
$const->name,
|
|
|
|
$const->value->inferredType
|
|
|
|
);
|
2016-08-14 03:14:32 +02:00
|
|
|
}
|
|
|
|
}
|
2016-11-02 07:29:00 +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-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Nop) {
|
2016-03-14 17:38:08 +01:00
|
|
|
// do nothing
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Goto_) {
|
2016-10-22 19:23:18 +02:00
|
|
|
// do nothing
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Label) {
|
2016-10-22 19:23:18 +02:00
|
|
|
// do nothing
|
2016-11-02 07:29:00 +01: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-11-01 05:39:41 +01:00
|
|
|
new InvalidNamespace(
|
|
|
|
'Cannot redeclare namespace',
|
|
|
|
$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-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-11-20 08:52:34 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Declare_) {
|
|
|
|
// do nothing
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-11-06 01:53:39 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UnrecognizedStatement(
|
|
|
|
'Psalm does not understand ' . get_class($stmt),
|
|
|
|
$this->getCheckedFileName(),
|
|
|
|
$stmt->getLine()
|
|
|
|
),
|
|
|
|
$this->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
|
|
|
|
return null;
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-06-17 00:52:12 +02:00
|
|
|
/**
|
2016-11-02 07:29:00 +01:00
|
|
|
* @param PhpParser\Node\Stmt\Static_ $stmt
|
|
|
|
* @param Context $context
|
|
|
|
* @return false|null
|
2016-06-17 00:52:12 +02:00
|
|
|
*/
|
2016-10-22 19:23:18 +02:00
|
|
|
protected function checkStatic(PhpParser\Node\Stmt\Static_ $stmt, Context $context)
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
2016-10-22 19:23:18 +02:00
|
|
|
foreach ($stmt->vars as $var) {
|
|
|
|
if ($var->default) {
|
|
|
|
if (ExpressionChecker::check($this, $var->default, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-06-20 06:38:13 +02:00
|
|
|
|
2016-10-22 19:23:18 +02:00
|
|
|
if ($context->check_variables) {
|
|
|
|
$context->vars_in_scope['$' . $var->name] = $var->default && isset($var->default->inferredType)
|
2016-11-02 07:29:00 +01:00
|
|
|
? $var->default->inferredType
|
|
|
|
: Type::getMixed();
|
2016-06-20 06:38:13 +02:00
|
|
|
|
2016-10-22 19:23:18 +02:00
|
|
|
$context->vars_possibly_in_scope['$' . $var->name] = true;
|
|
|
|
$this->registerVariable('$' . $var->name, $var->getLine());
|
|
|
|
}
|
2016-06-06 02:25:16 +02:00
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
|
|
|
|
return null;
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-10-22 19:23:18 +02:00
|
|
|
/**
|
2016-11-02 07:29:00 +01:00
|
|
|
* @param PhpParser\Node\Expr $stmt
|
|
|
|
* @return Type\Union|null
|
2016-10-22 19:23:18 +02:00
|
|
|
*/
|
|
|
|
public static function getSimpleType(PhpParser\Node\Expr $stmt)
|
|
|
|
{
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\ConstFetch) {
|
|
|
|
// @todo support this
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\ClassConstFetch) {
|
2016-10-22 19:23:18 +02:00
|
|
|
// @todo support this as well
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Scalar\String_) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return Type::getString();
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Scalar\LNumber) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return Type::getInt();
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Scalar\DNumber) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return Type::getFloat();
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Array_) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return Type::getArray();
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Cast\Int_) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return Type::getInt();
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Cast\Double) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return Type::getFloat();
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Cast\Bool_) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return Type::getBool();
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Cast\String_) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return Type::getString();
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Cast\Object_) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return Type::getObject();
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\Cast\Array_) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return Type::getArray();
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\UnaryMinus || $stmt instanceof PhpParser\Node\Expr\UnaryPlus) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return self::getSimpleType($stmt->expr);
|
2016-06-28 21:28:05 +02:00
|
|
|
}
|
2016-10-30 17:46:18 +01:00
|
|
|
|
|
|
|
return null;
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
|
2016-10-22 19:23:18 +02:00
|
|
|
/**
|
2016-11-02 07:29:00 +01:00
|
|
|
* @param PhpParser\Node\Stmt\Do_ $stmt
|
|
|
|
* @param Context $context
|
|
|
|
* @return false|null
|
2016-10-22 19:23:18 +02:00
|
|
|
*/
|
|
|
|
protected function checkDo(PhpParser\Node\Stmt\Do_ $stmt, Context $context)
|
|
|
|
{
|
|
|
|
// do not clone context for do, because it executes in current scope always
|
|
|
|
if ($this->check($stmt->stmts, $context, $context) === false) {
|
|
|
|
return false;
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
2016-02-10 21:20:53 +01:00
|
|
|
|
2016-10-22 19:23:18 +02:00
|
|
|
return ExpressionChecker::check($this, $stmt->cond, $context);
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-08-22 21:00:12 +02:00
|
|
|
/**
|
2016-10-22 19:23:18 +02:00
|
|
|
* @param string $method_id
|
|
|
|
* @param Context $context
|
|
|
|
* @return void
|
2016-08-22 21:00:12 +02:00
|
|
|
*/
|
2016-10-23 07:57:11 +02:00
|
|
|
public function checkInsideMethod($method_id, Context $context)
|
2016-05-24 18:11:17 +02:00
|
|
|
{
|
2016-10-22 19:23:18 +02:00
|
|
|
$method_checker = ClassLikeChecker::getMethodChecker($method_id);
|
2016-06-29 16:57:15 +02:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
if ($method_checker &&
|
|
|
|
$this->source instanceof FunctionLikeChecker &&
|
|
|
|
$method_checker->getMethodId() !== $this->source->getMethodId()
|
|
|
|
) {
|
2016-10-22 19:23:18 +02:00
|
|
|
$this_context = new Context($this->file_name, (string) $context->vars_in_scope['$this']);
|
2016-10-09 23:54:58 +02:00
|
|
|
|
2016-10-22 19:23:18 +02:00
|
|
|
foreach ($context->vars_possibly_in_scope as $var => $type) {
|
|
|
|
if (strpos($var, '$this->') === 0) {
|
|
|
|
$this_context->vars_possibly_in_scope[$var] = true;
|
|
|
|
}
|
|
|
|
}
|
2016-06-29 16:57:15 +02:00
|
|
|
|
2016-10-22 19:23:18 +02:00
|
|
|
foreach ($context->vars_in_scope as $var => $type) {
|
|
|
|
if (strpos($var, '$this->') === 0) {
|
|
|
|
$this_context->vars_in_scope[$var] = $type;
|
|
|
|
}
|
2016-06-29 16:57:15 +02:00
|
|
|
}
|
|
|
|
|
2016-10-22 19:23:18 +02:00
|
|
|
$this_context->vars_in_scope['$this'] = $context->vars_in_scope['$this'];
|
2016-10-09 23:54:58 +02:00
|
|
|
|
2016-10-22 19:23:18 +02:00
|
|
|
$method_checker->check($this_context);
|
2016-07-10 22:09:09 +02:00
|
|
|
|
2016-10-22 19:23:18 +02:00
|
|
|
foreach ($this_context->vars_in_scope as $var => $type) {
|
|
|
|
$context->vars_possibly_in_scope[$var] = true;
|
2016-05-24 18:11:17 +02:00
|
|
|
}
|
2016-10-22 19:23:18 +02:00
|
|
|
|
|
|
|
foreach ($this_context->vars_in_scope as $var => $type) {
|
|
|
|
$context->vars_in_scope[$var] = $type;
|
2016-08-22 21:00:12 +02:00
|
|
|
}
|
2016-05-24 18:11:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @param PhpParser\Node\Stmt\Const_ $stmt
|
|
|
|
* @param Context $context
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-10-22 19:23:18 +02:00
|
|
|
protected function checkConstAssignment(PhpParser\Node\Stmt\Const_ $stmt, Context $context)
|
|
|
|
{
|
|
|
|
foreach ($stmt->consts as $const) {
|
|
|
|
ExpressionChecker::check($this, $const->value, $context);
|
2016-10-19 03:54:08 +02:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
$this->setConstType(
|
|
|
|
$const->name,
|
|
|
|
isset($const->value->inferredType) ? $const->value->inferredType : Type::getMixed()
|
|
|
|
);
|
2016-08-11 23:36:22 +02:00
|
|
|
}
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
2016-08-11 23:36:22 +02:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @param string $const_name
|
2016-11-21 03:49:06 +01:00
|
|
|
* @param bool $is_fully_qualified
|
2016-11-05 02:14:04 +01:00
|
|
|
* @return Type\Union|null
|
2016-11-02 07:29:00 +01:00
|
|
|
*/
|
2016-11-21 03:49:06 +01:00
|
|
|
public function getConstType($const_name, $is_fully_qualified)
|
2016-10-22 19:23:18 +02:00
|
|
|
{
|
2016-11-21 03:49:06 +01:00
|
|
|
$fq_const_name = null;
|
|
|
|
|
|
|
|
if (isset($this->aliased_constants[$const_name])) {
|
|
|
|
$fq_const_name = $this->aliased_constants[$const_name];
|
|
|
|
}
|
|
|
|
elseif ($is_fully_qualified) {
|
|
|
|
$fq_const_name = $const_name;
|
|
|
|
}
|
|
|
|
elseif (strpos($const_name, '\\')) {
|
|
|
|
$fq_const_name = ClassLikeChecker::getFQCLNFromString($const_name, $this->namespace, $this->aliased_classes);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($fq_const_name) {
|
|
|
|
$const_name_parts = explode('\\', $fq_const_name);
|
|
|
|
$const_name = array_pop($const_name_parts);
|
|
|
|
$namespace_name = implode('\\', $const_name_parts);
|
|
|
|
$namespace_constants = NamespaceChecker::getConstantsForNamespace($namespace_name, \ReflectionProperty::IS_PUBLIC);
|
|
|
|
|
|
|
|
if (isset($namespace_constants[$const_name])) {
|
|
|
|
return $namespace_constants[$const_name];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset(self::$user_constants[$this->file_name][$const_name])) {
|
|
|
|
return self::$user_constants[$this->file_name][$const_name];
|
|
|
|
}
|
|
|
|
|
|
|
|
$predefined_constants = Config::getInstance()->getPredefinedConstants();
|
|
|
|
|
2016-11-21 04:40:19 +01:00
|
|
|
if (isset($predefined_constants[$fq_const_name ?: $const_name])) {
|
|
|
|
return ClassLikeChecker::getTypeFromValue($predefined_constants[$fq_const_name ?: $const_name]);
|
2016-11-21 03:49:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
2016-08-10 00:10:46 +02:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @param string $const_name
|
|
|
|
* @param Type\Union $const_type
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-10-22 19:23:18 +02:00
|
|
|
public function setConstType($const_name, Type\Union $const_type)
|
|
|
|
{
|
2016-11-21 03:49:06 +01:00
|
|
|
if ($this->source instanceof NamespaceChecker) {
|
|
|
|
$this->source->setConstType($const_name, $const_type);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
self::$user_constants[$this->file_name][$const_name] = $const_type;
|
|
|
|
}
|
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-06-17 22:58:15 +02:00
|
|
|
/**
|
2016-10-22 19:23:18 +02:00
|
|
|
* @param PhpParser\Node\Stmt\Return_ $stmt
|
|
|
|
* @param Context $context
|
2016-06-17 22:58:15 +02:00
|
|
|
* @return false|null
|
|
|
|
*/
|
2016-10-22 19:23:18 +02:00
|
|
|
protected function checkReturn(PhpParser\Node\Stmt\Return_ $stmt, Context $context)
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$type_in_comments = CommentChecker::getTypeFromComment(
|
|
|
|
(string) $stmt->getDocComment(),
|
|
|
|
$context,
|
|
|
|
$this->source
|
|
|
|
);
|
2016-06-17 22:58:15 +02:00
|
|
|
|
2016-10-22 19:23:18 +02:00
|
|
|
if ($stmt->expr) {
|
|
|
|
if (ExpressionChecker::check($this, $stmt->expr, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-10-03 04:00:42 +02:00
|
|
|
|
2016-10-22 19:23:18 +02:00
|
|
|
if ($type_in_comments) {
|
|
|
|
$stmt->inferredType = $type_in_comments;
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif (isset($stmt->expr->inferredType)) {
|
2016-10-22 19:23:18 +02:00
|
|
|
$stmt->inferredType = $stmt->expr->inferredType;
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-10-22 19:23:18 +02:00
|
|
|
$stmt->inferredType = Type::getMixed();
|
2016-09-21 03:45:49 +02:00
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-10-22 19:23:18 +02:00
|
|
|
$stmt->inferredType = Type::getVoid();
|
2016-09-21 03:45:49 +02:00
|
|
|
}
|
2016-06-17 22:58:15 +02:00
|
|
|
|
2016-10-22 19:23:18 +02:00
|
|
|
if ($this->source instanceof FunctionLikeChecker) {
|
|
|
|
$this->source->addReturnTypes($stmt->expr ? (string) $stmt->inferredType : '', $context);
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
|
|
|
|
return null;
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
2016-09-21 03:45:49 +02:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @param PhpParser\Node\Stmt\Throw_ $stmt
|
|
|
|
* @param Context $context
|
|
|
|
* @return false|null
|
|
|
|
*/
|
2016-10-22 19:23:18 +02:00
|
|
|
protected function checkThrow(PhpParser\Node\Stmt\Throw_ $stmt, Context $context)
|
|
|
|
{
|
|
|
|
return ExpressionChecker::check($this, $stmt->expr, $context);
|
|
|
|
}
|
2016-09-21 03:45:49 +02: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;
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-09-12 06:34:38 +02:00
|
|
|
return new Type\Union([Type::getInt()->types['int'], Type::getString()->types['string']]);
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2016-09-12 06:34:38 +02:00
|
|
|
return Type::getInt();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* @param PhpParser\Node\Expr\Include_ $stmt
|
|
|
|
* @param Context $context
|
|
|
|
* @return false|null
|
|
|
|
*/
|
2016-10-22 19:23:18 +02:00
|
|
|
public function checkInclude(PhpParser\Node\Expr\Include_ $stmt, Context $context)
|
2016-07-25 21:05:58 +02:00
|
|
|
{
|
2016-10-22 19:23:18 +02:00
|
|
|
if (ExpressionChecker::check($this, $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;
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} 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())) {
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-07-25 21:05:58 +02:00
|
|
|
}
|
|
|
|
|
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-10-18 22:14:52 +02:00
|
|
|
$context->check_classes = false;
|
|
|
|
$context->check_variables = false;
|
2016-07-26 00:30:49 +02:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-07-25 21:05:58 +02:00
|
|
|
}
|
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-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-07-25 21:05:58 +02:00
|
|
|
}
|
|
|
|
}
|
2016-08-14 06:38:29 +02:00
|
|
|
|
2016-10-18 22:14:52 +02:00
|
|
|
$context->check_classes = false;
|
|
|
|
$context->check_variables = false;
|
|
|
|
$context->check_functions = false;
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-07-25 21:05:58 +02:00
|
|
|
}
|
|
|
|
|
2016-04-27 00:42:48 +02:00
|
|
|
/**
|
2016-10-09 23:54:58 +02:00
|
|
|
* @param string $method_id
|
2016-11-21 19:37:27 +01:00
|
|
|
* @param array $args
|
2016-10-09 23:54:58 +02:00
|
|
|
* @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 &&
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmt->name->parts === ['dirname']
|
|
|
|
) {
|
2016-03-23 18:05:25 +01:00
|
|
|
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) {
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-03-23 18:05:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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)) {
|
2016-11-05 22:53:30 +01:00
|
|
|
$constant_value = constant($const_name);
|
|
|
|
|
|
|
|
if (is_string($constant_value)) {
|
|
|
|
return $constant_value;
|
|
|
|
}
|
2016-03-23 18:05:25 +01:00
|
|
|
}
|
|
|
|
} 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-11-02 07:29:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $file_name
|
|
|
|
* @param string $current_directory
|
|
|
|
* @return string|null
|
|
|
|
*/
|
2016-08-11 00:10:12 +02:00
|
|
|
protected static function resolveIncludePath($file_name, $current_directory)
|
2016-03-23 18:05:25 +01:00
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$paths = PATH_SEPARATOR == ':'
|
|
|
|
? preg_split('#(?<!phar):#', get_include_path())
|
|
|
|
: explode(PATH_SEPARATOR, get_include_path());
|
2016-03-23 18:05:25 +01:00
|
|
|
|
|
|
|
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-11-02 07:29:00 +01:00
|
|
|
|
|
|
|
return null;
|
2016-03-23 18:05:25 +01:00
|
|
|
}
|
2016-04-01 16:52:43 +02:00
|
|
|
|
2016-10-15 06:12:57 +02:00
|
|
|
/**
|
|
|
|
* @return array<string>
|
|
|
|
*/
|
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-11-21 03:49:06 +01:00
|
|
|
/**
|
|
|
|
* @return array<string>
|
|
|
|
*/
|
|
|
|
public function getAliasedConstants()
|
|
|
|
{
|
|
|
|
return $this->aliased_constants;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array<string>
|
|
|
|
*/
|
|
|
|
public function getAliasedFunctions()
|
|
|
|
{
|
|
|
|
return $this->aliased_functions;
|
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
2016-10-22 19:23:18 +02:00
|
|
|
* @return array<string>
|
2016-10-09 23:54:58 +02:00
|
|
|
*/
|
2016-10-22 19:23:18 +02:00
|
|
|
public function getSuppressedIssues()
|
2016-05-09 14:56:07 +02:00
|
|
|
{
|
2016-10-22 19:23:18 +02:00
|
|
|
return $this->suppressed_issues;
|
|
|
|
}
|
2016-05-09 14:56:07 +02:00
|
|
|
|
2016-10-22 19:23:18 +02:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getCheckedFileName()
|
|
|
|
{
|
|
|
|
return $this->checked_file_name;
|
|
|
|
}
|
2016-05-09 14:56:07 +02:00
|
|
|
|
2016-10-22 19:23:18 +02:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getFileName()
|
|
|
|
{
|
|
|
|
return $this->file_name;
|
|
|
|
}
|
2016-05-09 14:56:07 +02:00
|
|
|
|
2016-10-22 19:23:18 +02:00
|
|
|
/**
|
2016-10-23 07:57:11 +02:00
|
|
|
* @return string|null
|
2016-10-22 19:23:18 +02:00
|
|
|
*/
|
|
|
|
public function getParentClass()
|
|
|
|
{
|
|
|
|
return $this->parent_class;
|
|
|
|
}
|
2016-05-09 14:56:07 +02:00
|
|
|
|
2016-10-22 19:23:18 +02:00
|
|
|
/**
|
2016-11-01 05:39:41 +01:00
|
|
|
* @return string|null
|
2016-10-22 19:23:18 +02:00
|
|
|
*/
|
|
|
|
public function getClassName()
|
|
|
|
{
|
|
|
|
return $this->class_name;
|
2016-05-09 14:56:07 +02:00
|
|
|
}
|
2016-10-02 04:47:32 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
2016-10-22 19:23:18 +02:00
|
|
|
* @return string
|
2016-10-09 23:54:58 +02:00
|
|
|
*/
|
2016-11-08 01:16:51 +01:00
|
|
|
public function getFQCLN()
|
2016-10-02 04:47:32 +02:00
|
|
|
{
|
2016-11-07 23:29:51 +01:00
|
|
|
return $this->fq_class_name;
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
2016-10-02 04:47:32 +02:00
|
|
|
|
2016-10-22 19:23:18 +02:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getNamespace()
|
|
|
|
{
|
|
|
|
return $this->namespace;
|
|
|
|
}
|
2016-10-02 04:47:32 +02:00
|
|
|
|
2016-10-22 19:23:18 +02:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isStatic()
|
|
|
|
{
|
|
|
|
return $this->is_static;
|
|
|
|
}
|
2016-10-02 04:47:32 +02:00
|
|
|
|
2016-10-22 19:23:18 +02:00
|
|
|
/**
|
|
|
|
* @return StatementsSource
|
|
|
|
*/
|
|
|
|
public function getSource()
|
|
|
|
{
|
|
|
|
return $this->source;
|
2016-10-02 04:47:32 +02:00
|
|
|
}
|
2016-10-02 19:05:49 +02:00
|
|
|
|
2016-10-22 19:23:18 +02:00
|
|
|
/**
|
|
|
|
* The first appearance of the variable in this set of statements being evaluated
|
2016-11-02 07:29:00 +01:00
|
|
|
*
|
2016-10-22 19:23:18 +02:00
|
|
|
* @param string $var_name
|
|
|
|
* @return int|null
|
|
|
|
*/
|
|
|
|
public function getFirstAppearance($var_name)
|
2016-10-02 19:05:49 +02:00
|
|
|
{
|
2016-10-22 19:23:18 +02:00
|
|
|
return isset($this->all_vars[$var_name]) ? $this->all_vars[$var_name] : null;
|
2016-10-02 19:05:49 +02:00
|
|
|
}
|
2016-10-21 00:16:17 +02:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-10-21 00:16:17 +02:00
|
|
|
public static function clearCache()
|
|
|
|
{
|
|
|
|
self::$user_constants = [];
|
2016-10-22 23:35:59 +02:00
|
|
|
|
|
|
|
ExpressionChecker::clearCache();
|
2016-10-21 00:16:17 +02:00
|
|
|
}
|
2016-01-20 16:57:56 +01:00
|
|
|
}
|