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;
|
2017-11-30 07:07:20 +01:00
|
|
|
use Psalm\Checker\Statements\Block\LoopChecker;
|
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-11-01 16:37:58 +01:00
|
|
|
use Psalm\Checker\Statements\Expression\AssignmentChecker;
|
2016-12-09 18:48:02 +01:00
|
|
|
use Psalm\Checker\Statements\Expression\CallChecker;
|
2017-05-19 06:48:26 +02:00
|
|
|
use Psalm\Checker\Statements\ExpressionChecker;
|
2016-12-04 01:11:30 +01:00
|
|
|
use Psalm\CodeLocation;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\Config;
|
|
|
|
use Psalm\Context;
|
2017-11-15 03:43:31 +01:00
|
|
|
use Psalm\Exception\DocblockParseException;
|
2016-12-06 22:41:42 +01:00
|
|
|
use Psalm\Exception\FileIncludeException;
|
2017-11-07 20:46:53 +01:00
|
|
|
use Psalm\FileManipulation\FileManipulationBuffer;
|
2016-10-17 22:42:23 +02:00
|
|
|
use Psalm\Issue\ContinueOutsideLoop;
|
2017-11-15 03:43:31 +01:00
|
|
|
use Psalm\Issue\InvalidDocblock;
|
2016-11-05 22:53:30 +01:00
|
|
|
use Psalm\Issue\InvalidGlobal;
|
2017-12-07 21:50:25 +01:00
|
|
|
use Psalm\Issue\InvalidReturnStatement;
|
|
|
|
use Psalm\Issue\LessSpecificReturnStatement;
|
2017-10-29 22:36:02 +01:00
|
|
|
use Psalm\Issue\MissingFile;
|
2017-02-12 22:49:32 +01:00
|
|
|
use Psalm\Issue\UnevaluatedCode;
|
2016-11-06 01:53:39 +01:00
|
|
|
use Psalm\Issue\UnrecognizedStatement;
|
2017-10-29 19:39:23 +01:00
|
|
|
use Psalm\Issue\UnresolvableInclude;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\IssueBuffer;
|
2017-12-03 00:28:18 +01:00
|
|
|
use Psalm\Scope\LoopScope;
|
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
|
|
|
|
2017-01-07 20:35:07 +01:00
|
|
|
class StatementsChecker extends SourceChecker implements StatementsSource
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
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
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
/**
|
|
|
|
* @var FileChecker
|
|
|
|
*/
|
|
|
|
protected $file_checker;
|
|
|
|
|
2016-11-01 05:39:41 +01:00
|
|
|
/**
|
2017-02-08 00:09:12 +01:00
|
|
|
* @var array<string, CodeLocation>
|
2016-11-01 05:39:41 +01:00
|
|
|
*/
|
2017-02-12 00:56:38 +01:00
|
|
|
private $all_vars = [];
|
2016-11-01 05:39:41 +01:00
|
|
|
|
2017-07-27 01:54:22 +02:00
|
|
|
/**
|
|
|
|
* @var array<string, Type\Union>
|
|
|
|
*/
|
|
|
|
public static $stub_constants = [];
|
|
|
|
|
2017-08-12 01:05:04 +02:00
|
|
|
/**
|
|
|
|
* @var array<string, FunctionChecker>
|
|
|
|
*/
|
|
|
|
private $function_checkers = [];
|
|
|
|
|
2017-12-07 21:50:25 +01:00
|
|
|
/**
|
|
|
|
* @var Type\Union|null
|
|
|
|
*/
|
|
|
|
public $local_return_type;
|
|
|
|
|
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;
|
2017-03-20 04:30:20 +01:00
|
|
|
$this->file_checker = $source->getFileChecker();
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-06-18 20:45:55 +02:00
|
|
|
/**
|
|
|
|
* Checks an array of statements for validity
|
|
|
|
*
|
2016-12-12 19:50:46 +01:00
|
|
|
* @param array<PhpParser\Node\Stmt|PhpParser\Node\Expr> $stmts
|
|
|
|
* @param Context $context
|
|
|
|
* @param Context|null $global_context
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-06-18 20:45:55 +02:00
|
|
|
* @return null|false
|
|
|
|
*/
|
2017-01-07 21:09:47 +01:00
|
|
|
public function analyze(
|
2017-01-07 20:35:07 +01:00
|
|
|
array $stmts,
|
|
|
|
Context $context,
|
2017-12-03 00:28:18 +01:00
|
|
|
LoopScope $loop_scope = null,
|
2017-01-07 20:35:07 +01:00
|
|
|
Context $global_context = null
|
|
|
|
) {
|
2016-01-20 00:27:06 +01:00
|
|
|
$has_returned = false;
|
|
|
|
|
2016-08-15 06:58:30 +02:00
|
|
|
// hoist functions to the top
|
|
|
|
foreach ($stmts as $stmt) {
|
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\Function_) {
|
2017-01-06 07:07:11 +01:00
|
|
|
$function_checker = new FunctionChecker($stmt, $this->source);
|
2017-08-14 21:46:01 +02:00
|
|
|
$this->function_checkers[strtolower($stmt->name)] = $function_checker;
|
2016-08-15 06:58:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker = $this->getFileChecker()->project_checker;
|
|
|
|
|
2017-12-03 00:28:18 +01:00
|
|
|
$original_context = null;
|
|
|
|
|
|
|
|
if ($loop_scope) {
|
|
|
|
$original_context = clone $context;
|
|
|
|
}
|
|
|
|
|
2017-11-07 20:46:53 +01:00
|
|
|
$plugins = Config::getInstance()->getPlugins();
|
2016-12-06 22:41:42 +01:00
|
|
|
|
2017-11-07 20:46:53 +01:00
|
|
|
foreach ($stmts as $stmt) {
|
2016-11-02 07:29:00 +01:00
|
|
|
if ($has_returned && !($stmt instanceof PhpParser\Node\Stmt\Nop) &&
|
2017-01-07 20:35:07 +01:00
|
|
|
!($stmt instanceof PhpParser\Node\Stmt\InlineHTML)
|
|
|
|
) {
|
2017-02-27 05:09:18 +01:00
|
|
|
if ($context->collect_references) {
|
2017-02-12 22:49:32 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UnevaluatedCode(
|
|
|
|
'Expressions after return/throw/continue',
|
|
|
|
new CodeLocation($this->source, $stmt)
|
|
|
|
),
|
|
|
|
$this->source->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-01-20 16:57:56 +01:00
|
|
|
break;
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2017-01-26 01:09:31 +01:00
|
|
|
/*
|
2017-12-03 00:28:18 +01:00
|
|
|
if (isset($context->vars_in_scope['$tag']) && !$stmt instanceof PhpParser\Node\Stmt\Nop) {
|
|
|
|
var_dump($stmt->getLine() . ' ' . $context->vars_in_scope['$tag']);
|
2016-10-09 23:54:58 +02:00
|
|
|
}
|
2017-01-26 01:09:31 +01:00
|
|
|
*/
|
2016-10-09 23:54:58 +02:00
|
|
|
|
2017-10-27 00:19:19 +02:00
|
|
|
$new_issues = null;
|
|
|
|
|
|
|
|
if ($docblock = $stmt->getDocComment()) {
|
|
|
|
$comments = CommentChecker::parseDocComment((string)$docblock);
|
|
|
|
if (isset($comments['specials']['psalm-suppress'])) {
|
|
|
|
$suppressed = array_filter(
|
|
|
|
array_map(
|
|
|
|
/**
|
|
|
|
* @param string $line
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function ($line) {
|
|
|
|
return explode(' ', trim($line))[0];
|
|
|
|
},
|
|
|
|
$comments['specials']['psalm-suppress']
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($suppressed) {
|
|
|
|
$new_issues = array_diff($suppressed, $this->source->getSuppressedIssues());
|
2017-11-19 19:42:48 +01:00
|
|
|
/** @psalm-suppress MixedTypeCoercion */
|
2017-10-27 00:19:19 +02:00
|
|
|
$this->addSuppressedIssues($new_issues);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-20 00:27:06 +01:00
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\If_) {
|
2017-12-03 00:28:18 +01:00
|
|
|
IfChecker::analyze($this, $stmt, $context, $loop_scope);
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\TryCatch) {
|
2017-12-03 00:28:18 +01:00
|
|
|
TryChecker::analyze($this, $stmt, $context, $loop_scope);
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\For_) {
|
2017-01-07 21:09:47 +01:00
|
|
|
ForChecker::analyze($this, $stmt, $context);
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Foreach_) {
|
2017-01-07 21:09:47 +01:00
|
|
|
ForeachChecker::analyze($this, $stmt, $context);
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\While_) {
|
2017-01-07 21:09:47 +01:00
|
|
|
WhileChecker::analyze($this, $stmt, $context);
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Do_) {
|
2017-01-07 21:09:47 +01:00
|
|
|
$this->analyzeDo($stmt, $context);
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Const_) {
|
2017-01-07 21:09:47 +01:00
|
|
|
$this->analyzeConstAssignment($stmt, $context);
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Unset_) {
|
2017-12-14 01:46:58 +01:00
|
|
|
$suppressed_issues = $this->getSuppressedIssues();
|
|
|
|
|
|
|
|
if (!in_array('PossiblyUndefinedGlobalVariable', $suppressed_issues, true)) {
|
|
|
|
$this->addSuppressedIssues(['PossiblyUndefinedGlobalVariable']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!in_array('PossiblyUndefinedVariable', $suppressed_issues, true)) {
|
|
|
|
$this->addSuppressedIssues(['PossiblyUndefinedVariable']);
|
|
|
|
}
|
|
|
|
|
2016-09-17 17:57:44 +02:00
|
|
|
foreach ($stmt->vars as $var) {
|
2017-02-08 06:28:26 +01:00
|
|
|
ExpressionChecker::analyze($this, $var, $context);
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
$var_id = ExpressionChecker::getArrayVarId(
|
|
|
|
$var,
|
2017-01-07 20:35:07 +01:00
|
|
|
$this->getFQCLN(),
|
|
|
|
$this
|
2016-11-02 07:29:00 +01:00
|
|
|
);
|
2016-09-17 17:57:44 +02:00
|
|
|
|
|
|
|
if ($var_id) {
|
|
|
|
$context->remove($var_id);
|
|
|
|
}
|
|
|
|
}
|
2017-12-14 01:46:58 +01:00
|
|
|
|
|
|
|
if (!in_array('PossiblyUndefinedGlobalVariable', $suppressed_issues, true)) {
|
|
|
|
$this->removeSuppressedIssues(['PossiblyUndefinedGlobalVariable']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!in_array('PossiblyUndefinedVariable', $suppressed_issues, true)) {
|
|
|
|
$this->removeSuppressedIssues(['PossiblyUndefinedVariable']);
|
|
|
|
}
|
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;
|
2017-10-07 16:22:52 +02:00
|
|
|
$this->analyzeReturn($project_checker, $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;
|
2017-01-07 21:09:47 +01:00
|
|
|
$this->analyzeThrow($stmt, $context);
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Switch_) {
|
2017-12-03 00:28:18 +01:00
|
|
|
SwitchChecker::analyze($this, $stmt, $context, $loop_scope);
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Break_) {
|
2017-12-03 00:28:18 +01:00
|
|
|
if ($loop_scope && $original_context) {
|
|
|
|
$loop_scope->final_actions[] = ScopeChecker::ACTION_BREAK;
|
|
|
|
|
|
|
|
$redefined_vars = $context->getRedefinedVars($loop_scope->loop_parent_context->vars_in_scope);
|
|
|
|
|
|
|
|
if ($loop_scope->possibly_redefined_loop_parent_vars === null) {
|
|
|
|
$loop_scope->possibly_redefined_loop_parent_vars = $redefined_vars;
|
|
|
|
} else {
|
|
|
|
foreach ($redefined_vars as $var => $type) {
|
|
|
|
if ($type->isMixed()) {
|
|
|
|
$loop_scope->possibly_redefined_loop_parent_vars[$var] = $type;
|
|
|
|
} elseif (isset($loop_scope->possibly_redefined_loop_parent_vars[$var])) {
|
|
|
|
$loop_scope->possibly_redefined_loop_parent_vars[$var] = Type::combineUnionTypes(
|
|
|
|
$type,
|
|
|
|
$loop_scope->possibly_redefined_loop_parent_vars[$var]
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$loop_scope->possibly_redefined_loop_parent_vars[$var] = $type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$has_returned = true;
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Continue_) {
|
2017-12-03 00:28:18 +01:00
|
|
|
if ($loop_scope === null) {
|
2016-10-17 22:42:23 +02:00
|
|
|
if (IssueBuffer::accepts(
|
2016-11-02 07:29:00 +01:00
|
|
|
new ContinueOutsideLoop(
|
|
|
|
'Continue call outside loop context',
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($this->source, $stmt)
|
2016-11-02 07:29:00 +01:00
|
|
|
),
|
2017-01-07 20:35:07 +01:00
|
|
|
$this->source->getSuppressedIssues()
|
2016-10-17 22:42:23 +02:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-12-03 00:28:18 +01:00
|
|
|
} elseif ($original_context) {
|
|
|
|
$loop_scope->final_actions[] = ScopeChecker::ACTION_CONTINUE;
|
|
|
|
|
|
|
|
$redefined_vars = $context->getRedefinedVars($original_context->vars_in_scope);
|
|
|
|
|
|
|
|
if ($loop_scope->redefined_loop_vars === null) {
|
|
|
|
$loop_scope->redefined_loop_vars = $redefined_vars;
|
|
|
|
} else {
|
|
|
|
foreach ($loop_scope->redefined_loop_vars as $redefined_var => $type) {
|
|
|
|
if (!isset($redefined_vars[$redefined_var])) {
|
|
|
|
unset($loop_scope->redefined_loop_vars[$redefined_var]);
|
|
|
|
} else {
|
|
|
|
$loop_scope->redefined_loop_vars[$redefined_var] = Type::combineUnionTypes(
|
|
|
|
$redefined_vars[$redefined_var],
|
|
|
|
$type
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($loop_scope->possibly_redefined_loop_vars === null) {
|
|
|
|
$loop_scope->possibly_redefined_loop_vars = $redefined_vars;
|
|
|
|
} else {
|
|
|
|
foreach ($redefined_vars as $var => $type) {
|
|
|
|
if ($type->isMixed()) {
|
|
|
|
$loop_scope->possibly_redefined_loop_vars[$var] = $type;
|
|
|
|
} elseif (isset($loop_scope->possibly_redefined_loop_vars[$var])) {
|
|
|
|
$loop_scope->possibly_redefined_loop_vars[$var] = Type::combineUnionTypes(
|
|
|
|
$type,
|
|
|
|
$loop_scope->possibly_redefined_loop_vars[$var]
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$loop_scope->possibly_redefined_loop_vars[$var] = $type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-17 22:42:23 +02:00
|
|
|
}
|
|
|
|
|
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_) {
|
2017-01-07 21:09:47 +01:00
|
|
|
$this->analyzeStatic($stmt, $context);
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Echo_) {
|
2016-12-09 18:48:02 +01:00
|
|
|
foreach ($stmt->exprs as $i => $expr) {
|
2017-01-07 21:09:47 +01:00
|
|
|
ExpressionChecker::analyze($this, $expr, $context);
|
2016-12-09 18:48:02 +01:00
|
|
|
|
|
|
|
if (isset($expr->inferredType)) {
|
|
|
|
if (CallChecker::checkFunctionArgumentType(
|
|
|
|
$this,
|
|
|
|
$expr->inferredType,
|
|
|
|
Type::getString(),
|
|
|
|
'echo',
|
2016-12-09 19:27:59 +01:00
|
|
|
(int)$i,
|
2017-12-11 03:14:30 +01:00
|
|
|
new CodeLocation($this->getSource(), $expr),
|
|
|
|
$expr,
|
|
|
|
$context
|
2016-12-09 18:48:02 +01:00
|
|
|
) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Function_) {
|
2017-03-19 20:41:24 +01:00
|
|
|
if (!$project_checker->register_global_functions) {
|
2017-08-14 21:46:01 +02:00
|
|
|
$function_id = strtolower($stmt->name);
|
2017-03-19 20:41:24 +01:00
|
|
|
$function_context = new Context($context->self);
|
|
|
|
$function_context->collect_references = $project_checker->collect_references;
|
2017-08-14 21:46:01 +02:00
|
|
|
$this->function_checkers[$function_id]->analyze($function_context, $context);
|
2016-12-29 01:57:18 +01:00
|
|
|
|
2017-03-19 20:41:24 +01:00
|
|
|
$config = Config::getInstance();
|
2016-12-29 01:57:18 +01:00
|
|
|
|
2017-03-24 23:34:46 +01:00
|
|
|
if ($config->reportIssueInFile('InvalidReturnType', $this->getFilePath())) {
|
2017-03-19 20:41:24 +01:00
|
|
|
/** @var string */
|
2017-08-14 21:46:01 +02:00
|
|
|
$method_id = $this->function_checkers[$function_id]->getMethodId();
|
2016-12-29 01:57:18 +01:00
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
$function_storage = FunctionChecker::getStorage(
|
2017-08-15 01:30:11 +02:00
|
|
|
$this,
|
|
|
|
$method_id
|
2017-07-29 21:05:06 +02:00
|
|
|
);
|
2016-12-29 01:57:18 +01:00
|
|
|
|
2017-03-19 20:41:24 +01:00
|
|
|
$return_type = $function_storage->return_type;
|
|
|
|
$return_type_location = $function_storage->return_type_location;
|
|
|
|
|
2017-08-14 21:46:01 +02:00
|
|
|
$this->function_checkers[$function_id]->verifyReturnType(
|
2017-09-16 18:45:11 +02:00
|
|
|
$project_checker,
|
2017-03-19 20:41:24 +01:00
|
|
|
$return_type,
|
|
|
|
$this->getFQCLN(),
|
|
|
|
$return_type_location
|
|
|
|
);
|
|
|
|
}
|
2016-12-29 01:57:18 +01:00
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr) {
|
2017-10-13 02:24:22 +02:00
|
|
|
if (ExpressionChecker::analyze($this, $stmt, $context) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
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\Global_) {
|
2017-02-12 19:27:02 +01:00
|
|
|
if (!$context->collect_initializations && !$global_context) {
|
2016-11-05 22:53:30 +01:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidGlobal(
|
|
|
|
'Cannot use global scope here',
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($this->source, $stmt)
|
2016-11-05 22:53:30 +01:00
|
|
|
),
|
2017-01-07 20:35:07 +01:00
|
|
|
$this->source->getSuppressedIssues()
|
2016-11-05 22:53:30 +01:00
|
|
|
)) {
|
2017-02-12 18:23:05 +01:00
|
|
|
// fall through
|
2016-11-05 22:53:30 +01:00
|
|
|
}
|
2017-02-12 19:27:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($stmt->vars as $var) {
|
|
|
|
if ($var instanceof PhpParser\Node\Expr\Variable) {
|
|
|
|
if (is_string($var->name)) {
|
|
|
|
$var_id = '$' . $var->name;
|
2016-11-05 22:53:30 +01:00
|
|
|
|
2017-02-12 19:27:02 +01:00
|
|
|
$context->vars_in_scope[$var_id] =
|
|
|
|
$global_context && $global_context->hasVariable($var_id)
|
2016-11-05 22:53:30 +01:00
|
|
|
? clone $global_context->vars_in_scope[$var_id]
|
|
|
|
: Type::getMixed();
|
|
|
|
|
2017-02-12 19:27:02 +01:00
|
|
|
$context->vars_possibly_in_scope[$var_id] = true;
|
|
|
|
} else {
|
|
|
|
ExpressionChecker::analyze($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) {
|
2017-01-07 21:09:47 +01:00
|
|
|
ExpressionChecker::analyze($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()) {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (AssignmentChecker::analyzePropertyAssignment(
|
2016-11-02 07:29:00 +01:00
|
|
|
$this,
|
|
|
|
$prop,
|
|
|
|
$prop->name,
|
2016-12-07 00:27:22 +01:00
|
|
|
$prop->default,
|
2016-11-02 07:29:00 +01:00
|
|
|
$prop->default->inferredType,
|
|
|
|
$context
|
|
|
|
) === false) {
|
2017-02-12 19:27:02 +01:00
|
|
|
// fall through
|
2016-08-07 21:15:21 +02:00
|
|
|
}
|
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-12-04 07:44:33 +01:00
|
|
|
$const_visibility = \ReflectionProperty::IS_PUBLIC;
|
|
|
|
|
|
|
|
if ($stmt->isProtected()) {
|
|
|
|
$const_visibility = \ReflectionProperty::IS_PROTECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt->isPrivate()) {
|
|
|
|
$const_visibility = \ReflectionProperty::IS_PRIVATE;
|
|
|
|
}
|
2016-12-06 22:41:42 +01:00
|
|
|
|
2016-08-14 03:14:32 +02:00
|
|
|
foreach ($stmt->consts as $const) {
|
2017-01-07 21:09:47 +01:00
|
|
|
ExpressionChecker::analyze($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(
|
2017-07-29 21:05:06 +02:00
|
|
|
$project_checker,
|
2017-01-07 20:35:07 +01:00
|
|
|
(string)$this->getFQCLN(),
|
2016-11-02 07:29:00 +01:00
|
|
|
$const->name,
|
2016-12-04 07:44:33 +01:00
|
|
|
$const->value->inferredType,
|
|
|
|
$const_visibility
|
2016-11-02 07:29:00 +01:00
|
|
|
);
|
2016-08-14 03:14:32 +02:00
|
|
|
}
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Class_) {
|
2017-01-29 19:20:12 +01:00
|
|
|
$class_checker = (new ClassChecker($stmt, $this->source, $stmt->name));
|
|
|
|
$class_checker->analyze(null, $global_context);
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Stmt\Nop) {
|
2017-03-02 04:27:52 +01:00
|
|
|
if ((string)$stmt->getDocComment()) {
|
2017-11-15 03:43:31 +01:00
|
|
|
try {
|
|
|
|
$var_comment = CommentChecker::getTypeFromComment(
|
|
|
|
(string)$stmt->getDocComment(),
|
|
|
|
$this->getSource(),
|
|
|
|
$this->getSource()->getAliases()
|
|
|
|
);
|
|
|
|
} catch (DocblockParseException $e) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidDocblock(
|
|
|
|
(string)$e->getMessage(),
|
|
|
|
new CodeLocation($this->getSource(), $stmt, null, true)
|
|
|
|
)
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
2017-05-25 07:32:34 +02:00
|
|
|
|
|
|
|
if ($var_comment && $var_comment->var_id) {
|
2017-10-07 16:22:52 +02:00
|
|
|
$comment_type = ExpressionChecker::fleshOutType(
|
|
|
|
$project_checker,
|
|
|
|
Type::parseString($var_comment->type),
|
|
|
|
$context->self
|
|
|
|
);
|
|
|
|
|
|
|
|
$context->vars_in_scope[$var_comment->var_id] = $comment_type;
|
2017-05-25 07:32:34 +02:00
|
|
|
}
|
2017-03-02 04:27:52 +01:00
|
|
|
}
|
2016-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-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),
|
2016-12-04 01:11:30 +01:00
|
|
|
new CodeLocation($this->source, $stmt)
|
2016-11-06 01:53:39 +01:00
|
|
|
),
|
|
|
|
$this->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
2017-10-27 00:19:19 +02:00
|
|
|
|
2017-12-03 00:28:18 +01:00
|
|
|
if ($loop_scope
|
|
|
|
&& $loop_scope->final_actions
|
|
|
|
&& !in_array(ScopeChecker::ACTION_NONE, $loop_scope->final_actions, true)
|
|
|
|
) {
|
|
|
|
//$has_returned = true;
|
|
|
|
}
|
|
|
|
|
2017-11-07 20:46:53 +01:00
|
|
|
if ($plugins) {
|
|
|
|
$file_manipulations = [];
|
|
|
|
$code_location = new CodeLocation($this->source, $stmt);
|
|
|
|
|
|
|
|
foreach ($plugins as $plugin) {
|
|
|
|
if ($plugin->afterStatementCheck(
|
|
|
|
$this,
|
|
|
|
$stmt,
|
|
|
|
$context,
|
|
|
|
$code_location,
|
|
|
|
$this->getSuppressedIssues(),
|
|
|
|
$file_manipulations
|
|
|
|
) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($file_manipulations) {
|
|
|
|
FileManipulationBuffer::add($this->getFilePath(), $file_manipulations);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-27 00:19:19 +02:00
|
|
|
if ($new_issues) {
|
2017-11-19 19:42:48 +01:00
|
|
|
/** @psalm-suppress MixedTypeCoercion */
|
2017-10-27 00:19:19 +02:00
|
|
|
$this->removeSuppressedIssues($new_issues);
|
|
|
|
}
|
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
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-11-02 07:29:00 +01:00
|
|
|
* @return false|null
|
2016-06-17 00:52:12 +02:00
|
|
|
*/
|
2017-02-12 00:56:38 +01:00
|
|
|
private function analyzeStatic(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) {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (ExpressionChecker::analyze($this, $var->default, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-06-20 06:38:13 +02:00
|
|
|
|
2016-10-22 19:23:18 +02:00
|
|
|
if ($context->check_variables) {
|
2017-03-15 20:21:00 +01:00
|
|
|
$context->vars_in_scope['$' . $var->name] = Type::getMixed();
|
2016-10-22 19:23:18 +02:00
|
|
|
$context->vars_possibly_in_scope['$' . $var->name] = true;
|
2017-02-08 00:09:12 +01:00
|
|
|
$this->registerVariable('$' . $var->name, new CodeLocation($this, $stmt));
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
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
|
2017-07-25 22:11:02 +02:00
|
|
|
* @param array<string, Type\Union> $existing_class_constants
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-11-02 07:29:00 +01:00
|
|
|
* @return Type\Union|null
|
2016-10-22 19:23:18 +02:00
|
|
|
*/
|
2017-07-25 22:11:02 +02:00
|
|
|
public static function getSimpleType(
|
|
|
|
PhpParser\Node\Expr $stmt,
|
|
|
|
StatementsSource $statements_source = null,
|
|
|
|
array $existing_class_constants = []
|
|
|
|
) {
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp) {
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\Concat) {
|
|
|
|
return Type::getString();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\BooleanAnd
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\BooleanOr
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\LogicalAnd
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\LogicalOr
|
|
|
|
|| $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
|
|
|
|
) {
|
|
|
|
return Type::getBool();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\Coalesce) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\Spaceship) {
|
|
|
|
return Type::getInt();
|
|
|
|
}
|
|
|
|
|
|
|
|
$stmt->left->inferredType = self::getSimpleType(
|
|
|
|
$stmt->left,
|
|
|
|
$statements_source,
|
|
|
|
$existing_class_constants
|
|
|
|
);
|
|
|
|
$stmt->right->inferredType = self::getSimpleType(
|
|
|
|
$stmt->right,
|
|
|
|
$statements_source,
|
|
|
|
$existing_class_constants
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!$stmt->left->inferredType || !$stmt->right->inferredType) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$statements_source) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\Plus ||
|
|
|
|
$stmt instanceof PhpParser\Node\Expr\BinaryOp\Minus ||
|
|
|
|
$stmt instanceof PhpParser\Node\Expr\BinaryOp\Mod ||
|
|
|
|
$stmt instanceof PhpParser\Node\Expr\BinaryOp\Mul ||
|
|
|
|
$stmt instanceof PhpParser\Node\Expr\BinaryOp\Pow
|
|
|
|
) {
|
|
|
|
ExpressionChecker::analyzeNonDivArithmenticOp(
|
|
|
|
$statements_source,
|
|
|
|
$stmt->left,
|
|
|
|
$stmt->right,
|
|
|
|
$stmt,
|
|
|
|
$result_type
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($result_type) {
|
|
|
|
return $result_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\Div
|
|
|
|
&& ($stmt->left->inferredType->hasInt() || $stmt->left->inferredType->hasFloat())
|
|
|
|
&& ($stmt->right->inferredType->hasInt() || $stmt->right->inferredType->hasFloat())
|
|
|
|
) {
|
|
|
|
return Type::combineUnionTypes(Type::getFloat(), Type::getInt());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-22 19:23:18 +02:00
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\ConstFetch) {
|
2017-01-19 07:12:19 +01:00
|
|
|
if (strtolower($stmt->name->parts[0]) === 'false') {
|
|
|
|
return Type::getFalse();
|
|
|
|
} elseif (strtolower($stmt->name->parts[0]) === 'true') {
|
2017-01-13 18:26:10 +01:00
|
|
|
return Type::getBool();
|
|
|
|
} elseif (strtolower($stmt->name->parts[0]) === 'null') {
|
|
|
|
return Type::getNull();
|
|
|
|
}
|
2017-07-25 22:11:02 +02:00
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\ClassConstFetch) {
|
|
|
|
if ($stmt->class instanceof PhpParser\Node\Name
|
|
|
|
&& $stmt->class->parts !== ['static']
|
|
|
|
&& is_string($stmt->name)
|
|
|
|
&& isset($existing_class_constants[$stmt->name])
|
|
|
|
) {
|
|
|
|
return $existing_class_constants[$stmt->name];
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Scalar\String_) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return Type::getString();
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Scalar\LNumber) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return Type::getInt();
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Scalar\DNumber) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return Type::getFloat();
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\Array_) {
|
2017-01-02 07:07:44 +01:00
|
|
|
if (count($stmt->items) === 0) {
|
|
|
|
return Type::getEmptyArray();
|
|
|
|
}
|
|
|
|
|
2016-10-22 19:23:18 +02:00
|
|
|
return Type::getArray();
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\Cast\Int_) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return Type::getInt();
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\Cast\Double) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return Type::getFloat();
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\Cast\Bool_) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return Type::getBool();
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\Cast\String_) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return Type::getString();
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\Cast\Object_) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return Type::getObject();
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\Cast\Array_) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return Type::getArray();
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\UnaryMinus || $stmt instanceof PhpParser\Node\Expr\UnaryPlus) {
|
|
|
|
return self::getSimpleType($stmt->expr, $statements_source, $existing_class_constants);
|
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
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-11-02 07:29:00 +01:00
|
|
|
* @return false|null
|
2016-10-22 19:23:18 +02:00
|
|
|
*/
|
2017-02-12 00:56:38 +01:00
|
|
|
private function analyzeDo(PhpParser\Node\Stmt\Do_ $stmt, Context $context)
|
2016-10-22 19:23:18 +02:00
|
|
|
{
|
2017-02-18 23:49:34 +01:00
|
|
|
$do_context = clone $context;
|
|
|
|
|
2017-12-17 16:58:03 +01:00
|
|
|
$loop_scope = new LoopScope($do_context, $context);
|
|
|
|
$loop_scope->protected_var_ids = $context->protected_var_ids;
|
|
|
|
|
|
|
|
LoopChecker::analyze($this, $stmt->stmts, [], [], $loop_scope, $inner_loop_context);
|
2016-02-10 21:20:53 +01:00
|
|
|
|
2017-02-18 23:49:34 +01:00
|
|
|
foreach ($context->vars_in_scope as $var => $type) {
|
|
|
|
if ($type->isMixed()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($do_context->hasVariable($var)) {
|
|
|
|
if ($do_context->vars_in_scope[$var]->isMixed()) {
|
|
|
|
$context->vars_in_scope[$var] = $do_context->vars_in_scope[$var];
|
|
|
|
}
|
|
|
|
|
2017-12-03 18:44:08 +01:00
|
|
|
if ($do_context->vars_in_scope[$var]->getId() !== $type->getId()) {
|
2017-02-18 23:49:34 +01:00
|
|
|
$context->vars_in_scope[$var] = Type::combineUnionTypes($do_context->vars_in_scope[$var], $type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-13 23:06:56 +01:00
|
|
|
foreach ($do_context->vars_in_scope as $var_id => $type) {
|
|
|
|
if (!isset($context->vars_in_scope[$var_id])) {
|
|
|
|
$context->vars_in_scope[$var_id] = $type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-05 17:05:10 +01:00
|
|
|
// because it's a do {} while, inner loop vars belong to the main context
|
|
|
|
if ($inner_loop_context) {
|
|
|
|
foreach ($inner_loop_context->vars_in_scope as $var_id => $type) {
|
|
|
|
if (!isset($context->vars_in_scope[$var_id])) {
|
|
|
|
$context->vars_in_scope[$var_id] = $type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-21 23:48:12 +01:00
|
|
|
$context->vars_possibly_in_scope = array_merge(
|
|
|
|
$context->vars_possibly_in_scope,
|
|
|
|
$do_context->vars_possibly_in_scope
|
|
|
|
);
|
|
|
|
|
2017-11-24 18:17:28 +01:00
|
|
|
$context->referenced_var_ids = array_merge(
|
|
|
|
$context->referenced_var_ids,
|
|
|
|
$do_context->referenced_var_ids
|
|
|
|
);
|
2017-02-27 05:09:18 +01:00
|
|
|
|
2017-01-07 21:09:47 +01:00
|
|
|
return ExpressionChecker::analyze($this, $stmt->cond, $context);
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @param PhpParser\Node\Stmt\Const_ $stmt
|
|
|
|
* @param Context $context
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-11-02 07:29:00 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
2017-02-12 00:56:38 +01:00
|
|
|
private function analyzeConstAssignment(PhpParser\Node\Stmt\Const_ $stmt, Context $context)
|
2016-10-22 19:23:18 +02:00
|
|
|
{
|
|
|
|
foreach ($stmt->consts as $const) {
|
2017-01-07 21:09:47 +01:00
|
|
|
ExpressionChecker::analyze($this, $const->value, $context);
|
2016-10-19 03:54:08 +02:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
$this->setConstType(
|
|
|
|
$const->name,
|
2017-01-02 21:31:18 +01:00
|
|
|
isset($const->value->inferredType) ? $const->value->inferredType : Type::getMixed(),
|
|
|
|
$context
|
2016-11-02 07:29:00 +01:00
|
|
|
);
|
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
|
2017-01-02 21:31:18 +01:00
|
|
|
* @param Context $context
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-11-05 02:14:04 +01:00
|
|
|
* @return Type\Union|null
|
2016-11-02 07:29:00 +01:00
|
|
|
*/
|
2017-07-28 16:42:30 +02:00
|
|
|
public function getConstType(
|
|
|
|
StatementsChecker $statements_checker,
|
|
|
|
$const_name,
|
|
|
|
$is_fully_qualified,
|
|
|
|
Context $context
|
|
|
|
) {
|
2016-11-21 03:49:06 +01:00
|
|
|
$fq_const_name = null;
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
$aliased_constants = $this->getAliases()->constants;
|
2017-01-07 20:35:07 +01:00
|
|
|
|
|
|
|
if (isset($aliased_constants[$const_name])) {
|
|
|
|
$fq_const_name = $aliased_constants[$const_name];
|
|
|
|
} elseif ($is_fully_qualified) {
|
2016-11-21 03:49:06 +01:00
|
|
|
$fq_const_name = $const_name;
|
2017-01-07 20:35:07 +01:00
|
|
|
} elseif (strpos($const_name, '\\')) {
|
2017-07-25 22:11:02 +02:00
|
|
|
$fq_const_name = ClassLikeChecker::getFQCLNFromString($const_name, $this->getAliases());
|
2016-11-21 03:49:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($fq_const_name) {
|
|
|
|
$const_name_parts = explode('\\', $fq_const_name);
|
2017-11-28 06:46:41 +01:00
|
|
|
/** @var string */
|
2016-11-21 03:49:06 +01:00
|
|
|
$const_name = array_pop($const_name_parts);
|
|
|
|
$namespace_name = implode('\\', $const_name_parts);
|
2017-01-07 20:35:07 +01:00
|
|
|
$namespace_constants = NamespaceChecker::getConstantsForNamespace(
|
|
|
|
$namespace_name,
|
|
|
|
\ReflectionProperty::IS_PUBLIC
|
|
|
|
);
|
2016-11-21 03:49:06 +01:00
|
|
|
|
|
|
|
if (isset($namespace_constants[$const_name])) {
|
|
|
|
return $namespace_constants[$const_name];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-01 05:24:33 +01:00
|
|
|
if ($context->hasVariable($const_name)) {
|
2017-01-02 21:31:18 +01:00
|
|
|
return $context->vars_in_scope[$const_name];
|
2016-11-21 03:49:06 +01:00
|
|
|
}
|
|
|
|
|
2017-07-28 16:42:30 +02:00
|
|
|
$file_path = $statements_checker->getFilePath();
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
$file_storage_provider = $statements_checker->getFileChecker()->project_checker->file_storage_provider;
|
|
|
|
|
|
|
|
$file_storage = $file_storage_provider->get($file_path);
|
2017-07-28 16:42:30 +02:00
|
|
|
|
|
|
|
if (isset($file_storage->declaring_constants[$const_name])) {
|
|
|
|
$constant_file_path = $file_storage->declaring_constants[$const_name];
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
return $file_storage_provider->get($constant_file_path)->constants[$const_name];
|
2017-07-28 16:42:30 +02:00
|
|
|
}
|
|
|
|
|
2016-11-21 03:49:06 +01:00
|
|
|
$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
|
|
|
}
|
|
|
|
|
2017-07-27 01:54:22 +02:00
|
|
|
if (isset(self::$stub_constants[$fq_const_name ?: $const_name])) {
|
|
|
|
return self::$stub_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
|
2017-01-02 21:31:18 +01:00
|
|
|
* @param Context $context
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-11-02 07:29:00 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
2017-01-02 21:31:18 +01:00
|
|
|
public function setConstType($const_name, Type\Union $const_type, Context $context)
|
2016-10-22 19:23:18 +02:00
|
|
|
{
|
2017-01-15 21:58:40 +01:00
|
|
|
$context->vars_in_scope[$const_name] = $const_type;
|
|
|
|
$context->constants[$const_name] = $const_type;
|
|
|
|
|
2016-11-21 03:49:06 +01:00
|
|
|
if ($this->source instanceof NamespaceChecker) {
|
|
|
|
$this->source->setConstType($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
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-06-17 22:58:15 +02:00
|
|
|
* @return false|null
|
|
|
|
*/
|
2017-10-07 16:22:52 +02:00
|
|
|
private function analyzeReturn(
|
|
|
|
ProjectChecker $project_checker,
|
|
|
|
PhpParser\Node\Stmt\Return_ $stmt,
|
|
|
|
Context $context
|
|
|
|
) {
|
2017-03-02 04:27:52 +01:00
|
|
|
$doc_comment_text = (string)$stmt->getDocComment();
|
|
|
|
|
2017-05-25 07:32:34 +02:00
|
|
|
$var_comment = null;
|
|
|
|
|
2017-03-02 04:27:52 +01:00
|
|
|
if ($doc_comment_text) {
|
2017-11-15 03:43:31 +01:00
|
|
|
try {
|
|
|
|
$var_comment = CommentChecker::getTypeFromComment(
|
|
|
|
$doc_comment_text,
|
|
|
|
$this->source,
|
|
|
|
$this->source->getAliases()
|
|
|
|
);
|
|
|
|
} catch (DocblockParseException $e) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidDocblock(
|
|
|
|
(string)$e->getMessage(),
|
|
|
|
new CodeLocation($this->source, $stmt)
|
|
|
|
)
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
2017-05-25 07:32:34 +02:00
|
|
|
|
|
|
|
if ($var_comment && $var_comment->var_id) {
|
2017-10-07 16:22:52 +02:00
|
|
|
$comment_type = ExpressionChecker::fleshOutType(
|
|
|
|
$project_checker,
|
|
|
|
Type::parseString($var_comment->type),
|
|
|
|
$context->self
|
|
|
|
);
|
|
|
|
|
|
|
|
$context->vars_in_scope[$var_comment->var_id] = $comment_type;
|
2017-05-25 07:32:34 +02:00
|
|
|
}
|
2017-03-02 04:27:52 +01:00
|
|
|
}
|
2016-06-17 22:58:15 +02:00
|
|
|
|
2016-10-22 19:23:18 +02:00
|
|
|
if ($stmt->expr) {
|
2017-01-07 21:09:47 +01:00
|
|
|
if (ExpressionChecker::analyze($this, $stmt->expr, $context) === false) {
|
2016-10-22 19:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-10-03 04:00:42 +02:00
|
|
|
|
2017-05-25 07:32:34 +02:00
|
|
|
if ($var_comment && !$var_comment->var_id) {
|
2017-07-25 22:11:02 +02:00
|
|
|
$stmt->inferredType = Type::parseString($var_comment->type);
|
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;
|
2017-12-14 18:50:02 +01:00
|
|
|
|
|
|
|
if ($stmt->inferredType->isVoid()) {
|
|
|
|
$stmt->inferredType = Type::getNull();
|
|
|
|
}
|
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
|
|
|
|
2017-12-07 21:50:25 +01:00
|
|
|
if ($this->source instanceof FunctionLikeChecker
|
|
|
|
&& !($this->source->getSource() instanceof TraitChecker)
|
|
|
|
) {
|
2016-10-22 19:23:18 +02:00
|
|
|
$this->source->addReturnTypes($stmt->expr ? (string) $stmt->inferredType : '', $context);
|
2017-12-07 21:50:25 +01:00
|
|
|
|
|
|
|
if ($stmt->expr && !$stmt->inferredType->isMixed()) {
|
|
|
|
$storage = $this->source->getFunctionLikeStorage($this);
|
|
|
|
$cased_method_id = $this->source->getCorrectlyCasedMethodId();
|
|
|
|
|
|
|
|
if ($storage->return_type
|
|
|
|
&& !$storage->return_type->isMixed()
|
|
|
|
&& !$project_checker->update_docblocks
|
|
|
|
) {
|
|
|
|
$inferred_type = ExpressionChecker::fleshOutType(
|
|
|
|
$project_checker,
|
|
|
|
$stmt->inferredType,
|
|
|
|
$this->source->getFQCLN(),
|
|
|
|
''
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!$this->local_return_type) {
|
|
|
|
$this->local_return_type = ExpressionChecker::fleshOutType(
|
|
|
|
$project_checker,
|
|
|
|
$storage->return_type,
|
|
|
|
$this->source->getFQCLN(),
|
|
|
|
''
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->local_return_type->isGenerator()
|
|
|
|
&& !TypeChecker::isContainedBy(
|
|
|
|
$this->source->getFileChecker()->project_checker,
|
|
|
|
$inferred_type,
|
|
|
|
$this->local_return_type,
|
2017-12-07 22:08:09 +01:00
|
|
|
$stmt->inferredType->ignore_nullable_issues,
|
2017-12-07 21:50:25 +01:00
|
|
|
false,
|
|
|
|
$has_scalar_match,
|
|
|
|
$type_coerced,
|
|
|
|
$type_coerced_from_mixed
|
|
|
|
)) {
|
|
|
|
// is the declared return type more specific than the inferred one?
|
|
|
|
if ($type_coerced) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new LessSpecificReturnStatement(
|
|
|
|
'The type \'' . $stmt->inferredType . '\' is more general than the declared '
|
|
|
|
. 'return type \'' . $this->local_return_type . '\' for ' . $cased_method_id,
|
|
|
|
new CodeLocation($this->source, $stmt)
|
|
|
|
),
|
|
|
|
$this->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidReturnStatement(
|
|
|
|
'The type \'' . $stmt->inferredType . '\' does not match the declared return '
|
|
|
|
. 'type \'' . $this->local_return_type . '\' for ' . $cased_method_id,
|
|
|
|
new CodeLocation($this->source, $stmt)
|
|
|
|
),
|
|
|
|
$this->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
|
|
|
|
return null;
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
2016-09-21 03:45:49 +02:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @param PhpParser\Node\Stmt\Throw_ $stmt
|
|
|
|
* @param Context $context
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-11-02 07:29:00 +01:00
|
|
|
* @return false|null
|
|
|
|
*/
|
2017-02-12 00:56:38 +01:00
|
|
|
private function analyzeThrow(PhpParser\Node\Stmt\Throw_ $stmt, Context $context)
|
2016-10-22 19:23:18 +02:00
|
|
|
{
|
2017-01-07 21:09:47 +01:00
|
|
|
return ExpressionChecker::analyze($this, $stmt->expr, $context);
|
2016-10-22 19:23:18 +02:00
|
|
|
}
|
2016-09-21 03:45:49 +02:00
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
2017-02-08 00:09:12 +01:00
|
|
|
* @param string $var_name
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-02-08 00:09:12 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function hasVariable($var_name)
|
|
|
|
{
|
|
|
|
return isset($this->all_vars[$var_name]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $var_name
|
|
|
|
* @param CodeLocation $location
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-10-09 23:54:58 +02:00
|
|
|
* @return void
|
|
|
|
*/
|
2017-02-08 00:09:12 +01:00
|
|
|
public function registerVariable($var_name, CodeLocation $location)
|
2016-01-20 00:27:06 +01:00
|
|
|
{
|
2017-02-08 00:09:12 +01:00
|
|
|
$this->all_vars[$var_name] = $location;
|
2016-01-20 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* @param PhpParser\Node\Expr\Include_ $stmt
|
|
|
|
* @param Context $context
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-10-09 23:54:58 +02:00
|
|
|
* @return false|null
|
|
|
|
*/
|
2017-01-07 21:09:47 +01:00
|
|
|
public function analyzeInclude(PhpParser\Node\Expr\Include_ $stmt, Context $context)
|
2016-07-25 21:05:58 +02:00
|
|
|
{
|
2016-12-06 22:41:42 +01:00
|
|
|
$config = Config::getInstance();
|
|
|
|
|
|
|
|
if (!$config->allow_includes) {
|
2017-04-28 06:31:55 +02:00
|
|
|
throw new FileIncludeException(
|
|
|
|
'File includes are not allowed per your Psalm config - check the allowFileIncludes flag.'
|
|
|
|
);
|
2016-12-06 22:41:42 +01:00
|
|
|
}
|
|
|
|
|
2017-01-07 21:09:47 +01:00
|
|
|
if (ExpressionChecker::analyze($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
|
2017-01-07 20:35:07 +01:00
|
|
|
$include_path = self::resolveIncludePath($path_to_file, dirname($this->getCheckedFileName()));
|
2016-07-25 21:05:58 +02:00
|
|
|
$path_to_file = $include_path ? $include_path : $path_to_file;
|
|
|
|
|
2017-01-18 04:10:21 +01:00
|
|
|
if ($path_to_file[0] !== DIRECTORY_SEPARATOR) {
|
|
|
|
$path_to_file = getcwd() . DIRECTORY_SEPARATOR . $path_to_file;
|
2016-07-25 21:05:58 +02:00
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} else {
|
2017-04-05 20:37:22 +02:00
|
|
|
$path_to_file = self::getPathTo($stmt->expr, $this->getCheckedFileName());
|
2016-07-25 21:05:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($path_to_file) {
|
|
|
|
$reduce_pattern = '/\/[^\/]+\/\.\.\//';
|
|
|
|
|
|
|
|
while (preg_match($reduce_pattern, $path_to_file)) {
|
2017-01-18 04:10:21 +01:00
|
|
|
$path_to_file = preg_replace($reduce_pattern, DIRECTORY_SEPARATOR, $path_to_file);
|
2016-07-25 21:05:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// if the file is already included, we can't check much more
|
2017-05-27 02:05:57 +02:00
|
|
|
if (in_array($path_to_file, get_included_files(), true)) {
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-07-25 21:05:58 +02:00
|
|
|
}
|
|
|
|
|
2017-06-05 22:46:04 +02:00
|
|
|
if ($this->getFilePath() === $path_to_file) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-01-08 01:07:58 +01:00
|
|
|
$current_file_checker = $this->getFileChecker();
|
|
|
|
|
2017-07-27 01:36:41 +02:00
|
|
|
if ($current_file_checker->project_checker->fileExists($path_to_file)) {
|
|
|
|
if (is_subclass_of($current_file_checker, 'Psalm\\Checker\\FileChecker')) {
|
|
|
|
$include_file_checker = new FileChecker(
|
|
|
|
$path_to_file,
|
|
|
|
$current_file_checker->project_checker,
|
|
|
|
false
|
|
|
|
);
|
|
|
|
$this->analyze($include_file_checker->getStatements(), $context);
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-07-25 21:05:58 +02:00
|
|
|
}
|
2017-10-29 19:39:23 +01:00
|
|
|
|
|
|
|
if (IssueBuffer::accepts(
|
2017-10-29 22:36:02 +01:00
|
|
|
new MissingFile(
|
2017-10-29 19:39:23 +01:00
|
|
|
'Cannot find file ' . $path_to_file . ' to include',
|
|
|
|
new CodeLocation($this->source, $stmt)
|
|
|
|
),
|
|
|
|
$this->source->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new UnresolvableInclude(
|
|
|
|
'Cannot resolve the given expression to a file path',
|
|
|
|
new CodeLocation($this->source, $stmt)
|
|
|
|
),
|
|
|
|
$this->source->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
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;
|
2017-05-25 04:07:49 +02:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
return null;
|
2016-07-25 21:05:58 +02:00
|
|
|
}
|
|
|
|
|
2016-10-09 23:54:58 +02:00
|
|
|
/**
|
|
|
|
* @param PhpParser\Node\Expr $stmt
|
|
|
|
* @param string $file_name
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-10-09 23:54:58 +02:00
|
|
|
* @return string|null
|
2016-12-17 06:48:31 +01:00
|
|
|
* @psalm-suppress MixedAssignment
|
2016-10-09 23:54:58 +02:00
|
|
|
*/
|
2017-07-25 22:11:02 +02:00
|
|
|
public static function getPathTo(PhpParser\Node\Expr $stmt, $file_name)
|
2016-03-23 18:05:25 +01:00
|
|
|
{
|
2017-01-18 04:10:21 +01:00
|
|
|
if ($file_name[0] !== DIRECTORY_SEPARATOR) {
|
|
|
|
$file_name = getcwd() . DIRECTORY_SEPARATOR . $file_name;
|
2016-03-23 18:05:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @param string $file_name
|
|
|
|
* @param string $current_directory
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-11-02 07:29:00 +01:00
|
|
|
* @return string|null
|
|
|
|
*/
|
2017-07-25 22:11:02 +02:00
|
|
|
public static function resolveIncludePath($file_name, $current_directory)
|
2016-03-23 18:05:25 +01:00
|
|
|
{
|
2017-01-08 01:07:58 +01:00
|
|
|
if (!$current_directory) {
|
|
|
|
return $file_name;
|
|
|
|
}
|
|
|
|
|
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-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
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-02-08 00:09:12 +01:00
|
|
|
* @return CodeLocation|null
|
2016-10-22 19:23:18 +02:00
|
|
|
*/
|
|
|
|
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
|
|
|
|
2017-11-26 22:03:17 +01:00
|
|
|
/**
|
|
|
|
* @return FileChecker
|
|
|
|
*/
|
2017-07-29 21:05:06 +02:00
|
|
|
public function getFileChecker()
|
|
|
|
{
|
|
|
|
return $this->file_checker;
|
|
|
|
}
|
|
|
|
|
2017-08-12 01:05:04 +02:00
|
|
|
/**
|
|
|
|
* @return array<string, FunctionChecker>
|
|
|
|
*/
|
|
|
|
public function getFunctionCheckers()
|
|
|
|
{
|
|
|
|
return $this->function_checkers;
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-10-21 00:16:17 +02:00
|
|
|
public static function clearCache()
|
|
|
|
{
|
2017-07-27 01:54:22 +02:00
|
|
|
self::$stub_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
|
|
|
}
|