2016-11-01 16:37:58 +01:00
|
|
|
<?php
|
2018-11-06 03:57:36 +01:00
|
|
|
namespace Psalm\Internal\Analyzer\Statements\Expression;
|
2016-11-01 16:37:58 +01:00
|
|
|
|
|
|
|
use PhpParser;
|
2018-11-06 03:57:36 +01:00
|
|
|
use Psalm\Internal\Analyzer\CommentAnalyzer;
|
2019-10-22 17:17:42 +02:00
|
|
|
use Psalm\Internal\Analyzer\Statements\Block\ForeachAnalyzer;
|
2018-11-06 03:57:36 +01:00
|
|
|
use Psalm\Internal\Analyzer\Statements\Expression\Assignment\ArrayAssignmentAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\Statements\Expression\Assignment\PropertyAssignmentAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\StatementsAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\TypeAnalyzer;
|
2016-12-04 01:11:30 +01:00
|
|
|
use Psalm\CodeLocation;
|
2016-11-01 16:37:58 +01:00
|
|
|
use Psalm\Context;
|
2017-11-15 03:43:31 +01:00
|
|
|
use Psalm\Exception\DocblockParseException;
|
|
|
|
use Psalm\Exception\IncorrectDocblockException;
|
2019-09-26 21:08:05 +02:00
|
|
|
use Psalm\Internal\FileManipulation\FileManipulationBuffer;
|
2017-11-28 06:46:41 +01:00
|
|
|
use Psalm\Issue\AssignmentToVoid;
|
2019-07-18 07:31:48 +02:00
|
|
|
use Psalm\Issue\ImpurePropertyAssignment;
|
2019-10-15 19:55:30 +02:00
|
|
|
use Psalm\Issue\InvalidArrayOffset;
|
2017-11-15 03:43:31 +01:00
|
|
|
use Psalm\Issue\InvalidDocblock;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\Issue\InvalidScope;
|
2017-12-17 16:58:03 +01:00
|
|
|
use Psalm\Issue\LoopInvalidation;
|
2017-11-15 03:43:31 +01:00
|
|
|
use Psalm\Issue\MissingDocblockType;
|
2016-12-17 00:56:23 +01:00
|
|
|
use Psalm\Issue\MixedAssignment;
|
2019-01-02 23:05:39 +01:00
|
|
|
use Psalm\Issue\NoValue;
|
2018-11-30 19:21:08 +01:00
|
|
|
use Psalm\Issue\PossiblyUndefinedArrayOffset;
|
2017-02-23 06:25:28 +01:00
|
|
|
use Psalm\Issue\ReferenceConstraintViolation;
|
2019-09-19 17:59:43 +02:00
|
|
|
use Psalm\Issue\UnnecessaryVarAnnotation;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\IssueBuffer;
|
2016-11-01 16:37:58 +01:00
|
|
|
use Psalm\Type;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function is_string;
|
|
|
|
use function strpos;
|
|
|
|
use function strtolower;
|
2016-11-01 16:37:58 +01:00
|
|
|
|
2018-12-02 00:37:49 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
class AssignmentAnalyzer
|
2016-11-01 16:37:58 +01:00
|
|
|
{
|
|
|
|
/**
|
2018-11-11 18:01:14 +01:00
|
|
|
* @param StatementsAnalyzer $statements_analyzer
|
2016-12-04 20:14:00 +01:00
|
|
|
* @param PhpParser\Node\Expr $assign_var
|
2016-12-07 00:27:22 +01:00
|
|
|
* @param PhpParser\Node\Expr|null $assign_value This has to be null to support list destructuring
|
2016-12-04 20:14:00 +01:00
|
|
|
* @param Type\Union|null $assign_value_type
|
|
|
|
* @param Context $context
|
2019-06-01 17:53:32 +02:00
|
|
|
* @param ?PhpParser\Comment\Doc $doc_comment
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-11-01 16:37:58 +01:00
|
|
|
* @return false|Type\Union
|
|
|
|
*/
|
2017-01-07 21:09:47 +01:00
|
|
|
public static function analyze(
|
2018-11-11 18:01:14 +01:00
|
|
|
StatementsAnalyzer $statements_analyzer,
|
2016-11-01 16:37:58 +01:00
|
|
|
PhpParser\Node\Expr $assign_var,
|
2017-05-05 03:57:26 +02:00
|
|
|
$assign_value,
|
|
|
|
$assign_value_type,
|
2016-11-01 16:37:58 +01:00
|
|
|
Context $context,
|
2019-06-01 17:53:32 +02:00
|
|
|
?PhpParser\Comment\Doc $doc_comment
|
2016-11-01 16:37:58 +01:00
|
|
|
) {
|
2018-11-06 03:57:36 +01:00
|
|
|
$var_id = ExpressionAnalyzer::getVarId(
|
2016-11-01 16:37:58 +01:00
|
|
|
$assign_var,
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getFQCLN(),
|
|
|
|
$statements_analyzer
|
2016-11-01 16:37:58 +01:00
|
|
|
);
|
|
|
|
|
2017-08-19 05:14:38 +02:00
|
|
|
// gets a variable id that *may* contain array keys
|
2018-11-06 03:57:36 +01:00
|
|
|
$array_var_id = ExpressionAnalyzer::getArrayVarId(
|
2016-11-01 16:37:58 +01:00
|
|
|
$assign_var,
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getFQCLN(),
|
|
|
|
$statements_analyzer
|
2016-11-01 16:37:58 +01:00
|
|
|
);
|
|
|
|
|
2018-02-08 05:33:31 +01:00
|
|
|
$var_comments = [];
|
2017-10-07 16:22:52 +02:00
|
|
|
$comment_type = null;
|
2019-09-26 21:08:05 +02:00
|
|
|
$comment_type_location = null;
|
2017-05-25 07:32:34 +02:00
|
|
|
|
2019-08-13 19:15:23 +02:00
|
|
|
$was_in_assignment = $context->inside_assignment;
|
|
|
|
|
|
|
|
$context->inside_assignment = true;
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
2018-11-06 03:57:36 +01:00
|
|
|
|
2019-10-15 22:25:27 +02:00
|
|
|
$remove_taint = false;
|
|
|
|
|
2017-03-02 04:27:52 +01:00
|
|
|
if ($doc_comment) {
|
2018-11-11 18:01:14 +01:00
|
|
|
$file_path = $statements_analyzer->getRootFilePath();
|
2018-09-24 19:08:23 +02:00
|
|
|
|
2018-11-11 18:19:53 +01:00
|
|
|
$file_storage_provider = $codebase->file_storage_provider;
|
2018-09-24 19:08:23 +02:00
|
|
|
|
|
|
|
$file_storage = $file_storage_provider->get($file_path);
|
|
|
|
|
2018-12-18 05:29:27 +01:00
|
|
|
$template_type_map = $statements_analyzer->getTemplateTypeMap();
|
|
|
|
|
2017-11-15 03:43:31 +01:00
|
|
|
try {
|
2018-11-06 03:57:36 +01:00
|
|
|
$var_comments = CommentAnalyzer::getTypeFromComment(
|
2017-11-15 03:43:31 +01:00
|
|
|
$doc_comment,
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSource(),
|
|
|
|
$statements_analyzer->getAliases(),
|
2018-12-18 05:29:27 +01:00
|
|
|
$template_type_map,
|
2019-06-01 18:25:57 +02:00
|
|
|
$file_storage->type_aliases
|
2017-11-15 03:43:31 +01:00
|
|
|
);
|
|
|
|
} catch (IncorrectDocblockException $e) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MissingDocblockType(
|
|
|
|
(string)$e->getMessage(),
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $assign_var)
|
2017-11-15 03:43:31 +01:00
|
|
|
)
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
} catch (DocblockParseException $e) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidDocblock(
|
|
|
|
(string)$e->getMessage(),
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $assign_var)
|
2017-11-15 03:43:31 +01:00
|
|
|
)
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
2017-05-25 07:32:34 +02:00
|
|
|
|
2018-02-08 05:33:31 +01:00
|
|
|
foreach ($var_comments as $var_comment) {
|
2019-10-15 22:25:27 +02:00
|
|
|
if ($var_comment->remove_taint) {
|
|
|
|
$remove_taint = true;
|
|
|
|
}
|
|
|
|
|
2019-08-21 17:25:08 +02:00
|
|
|
if (!$var_comment->type) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-03-27 17:34:48 +02:00
|
|
|
try {
|
2018-11-06 03:57:36 +01:00
|
|
|
$var_comment_type = ExpressionAnalyzer::fleshOutType(
|
|
|
|
$codebase,
|
2018-03-27 17:34:48 +02:00
|
|
|
$var_comment->type,
|
|
|
|
$context->self,
|
2019-05-25 17:51:09 +02:00
|
|
|
$context->self,
|
|
|
|
$statements_analyzer->getParentFQCLN()
|
2018-03-27 17:34:48 +02:00
|
|
|
);
|
2017-10-07 16:22:52 +02:00
|
|
|
|
2018-03-27 17:34:48 +02:00
|
|
|
$var_comment_type->setFromDocblock();
|
2017-10-07 16:22:52 +02:00
|
|
|
|
2019-01-26 23:30:44 +01:00
|
|
|
$var_comment_type->check(
|
|
|
|
$statements_analyzer,
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $assign_var),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
);
|
|
|
|
|
2019-09-26 21:08:05 +02:00
|
|
|
$type_location = null;
|
|
|
|
|
|
|
|
if ($var_comment->type_start
|
2019-06-01 18:25:57 +02:00
|
|
|
&& $var_comment->type_end
|
|
|
|
&& $var_comment->line_number
|
|
|
|
) {
|
2019-06-04 22:36:32 +02:00
|
|
|
$type_location = new CodeLocation\DocblockTypeLocation(
|
|
|
|
$statements_analyzer,
|
|
|
|
$var_comment->type_start,
|
|
|
|
$var_comment->type_end,
|
2019-06-05 06:00:42 +02:00
|
|
|
$var_comment->line_number
|
2019-06-04 22:36:32 +02:00
|
|
|
);
|
2019-06-01 18:25:57 +02:00
|
|
|
|
2019-09-26 21:08:05 +02:00
|
|
|
if ($codebase->alter_code) {
|
|
|
|
$codebase->classlikes->handleDocblockTypeInMigration(
|
|
|
|
$codebase,
|
|
|
|
$statements_analyzer,
|
|
|
|
$var_comment_type,
|
|
|
|
$type_location,
|
2019-12-29 00:37:55 +01:00
|
|
|
$context->calling_function_id
|
2019-09-26 21:08:05 +02:00
|
|
|
);
|
|
|
|
}
|
2019-06-01 18:25:57 +02:00
|
|
|
}
|
|
|
|
|
2018-03-27 17:34:48 +02:00
|
|
|
if (!$var_comment->var_id || $var_comment->var_id === $var_id) {
|
|
|
|
$comment_type = $var_comment_type;
|
2019-09-26 21:08:05 +02:00
|
|
|
$comment_type_location = $type_location;
|
2018-03-27 17:34:48 +02:00
|
|
|
continue;
|
|
|
|
}
|
2018-02-08 05:33:31 +01:00
|
|
|
|
2019-09-19 17:59:43 +02:00
|
|
|
if ($codebase->find_unused_variables
|
2019-09-26 21:08:05 +02:00
|
|
|
&& $type_location
|
2019-09-19 17:59:43 +02:00
|
|
|
&& isset($context->vars_in_scope[$var_comment->var_id])
|
|
|
|
&& $context->vars_in_scope[$var_comment->var_id]->getId() === $var_comment_type->getId()
|
2019-12-01 16:33:27 +01:00
|
|
|
&& !$var_comment_type->isMixed()
|
2019-09-19 17:59:43 +02:00
|
|
|
) {
|
2019-09-26 21:08:05 +02:00
|
|
|
$project_analyzer = $statements_analyzer->getProjectAnalyzer();
|
|
|
|
|
|
|
|
if ($codebase->alter_code
|
|
|
|
&& isset($project_analyzer->getIssuesToFix()['UnnecessaryVarAnnotation'])
|
|
|
|
) {
|
|
|
|
FileManipulationBuffer::addVarAnnotationToRemove($type_location);
|
|
|
|
} elseif (IssueBuffer::accepts(
|
2019-09-19 17:59:43 +02:00
|
|
|
new UnnecessaryVarAnnotation(
|
2019-12-01 16:33:27 +01:00
|
|
|
'The @var ' . $var_comment_type . ' annotation for '
|
2019-12-01 16:19:36 +01:00
|
|
|
. $var_comment->var_id . ' is unnecessary',
|
2019-09-26 21:08:05 +02:00
|
|
|
$type_location
|
2019-12-02 21:24:01 +01:00
|
|
|
),
|
|
|
|
[],
|
|
|
|
true
|
2019-09-19 17:59:43 +02:00
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-27 17:34:48 +02:00
|
|
|
$context->vars_in_scope[$var_comment->var_id] = $var_comment_type;
|
|
|
|
} catch (\UnexpectedValueException $e) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidDocblock(
|
|
|
|
(string)$e->getMessage(),
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $assign_var)
|
2018-03-27 17:34:48 +02:00
|
|
|
)
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
2017-05-25 07:32:34 +02:00
|
|
|
}
|
2017-03-02 04:27:52 +01:00
|
|
|
}
|
2016-11-01 16:37:58 +01:00
|
|
|
|
2019-07-04 23:35:33 +02:00
|
|
|
if ($array_var_id) {
|
|
|
|
unset($context->referenced_var_ids[$array_var_id]);
|
2019-12-22 13:36:16 +01:00
|
|
|
$context->assigned_var_ids[$array_var_id] = true;
|
|
|
|
$context->possibly_assigned_var_ids[$array_var_id] = true;
|
2019-07-04 23:35:33 +02:00
|
|
|
}
|
|
|
|
|
2018-10-04 22:42:40 +02:00
|
|
|
if ($assign_value) {
|
|
|
|
if ($var_id && $assign_value instanceof PhpParser\Node\Expr\Closure) {
|
|
|
|
foreach ($assign_value->uses as $closure_use) {
|
|
|
|
if ($closure_use->byRef
|
|
|
|
&& is_string($closure_use->var->name)
|
|
|
|
&& $var_id === '$' . $closure_use->var->name
|
|
|
|
) {
|
|
|
|
$context->vars_in_scope[$var_id] = Type::getClosure();
|
|
|
|
$context->vars_possibly_in_scope[$var_id] = true;
|
|
|
|
}
|
2017-04-02 01:04:49 +02:00
|
|
|
}
|
2017-02-24 01:36:51 +01:00
|
|
|
}
|
2016-11-01 16:37:58 +01:00
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
if (ExpressionAnalyzer::analyze($statements_analyzer, $assign_value, $context) === false) {
|
2018-10-04 22:42:40 +02:00
|
|
|
if ($var_id) {
|
|
|
|
if ($array_var_id) {
|
|
|
|
$context->removeDescendents($array_var_id, null, $assign_value_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we're not exiting immediately, make everything mixed
|
|
|
|
$context->vars_in_scope[$var_id] = $comment_type ?: Type::getMixed();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-01 16:37:58 +01:00
|
|
|
}
|
|
|
|
|
2019-09-26 21:08:05 +02:00
|
|
|
if ($comment_type && $comment_type_location) {
|
2019-11-25 17:44:54 +01:00
|
|
|
$temp_assign_value_type = $assign_value_type
|
|
|
|
? $assign_value_type
|
|
|
|
: ($assign_value ? $statements_analyzer->node_data->getType($assign_value) : null);
|
2019-09-19 17:59:43 +02:00
|
|
|
|
|
|
|
if ($codebase->find_unused_variables
|
|
|
|
&& $temp_assign_value_type
|
|
|
|
&& $array_var_id
|
|
|
|
&& $temp_assign_value_type->getId() === $comment_type->getId()
|
2019-12-01 16:19:36 +01:00
|
|
|
&& !$comment_type->isMixed()
|
2019-09-19 17:59:43 +02:00
|
|
|
) {
|
2019-09-26 21:08:05 +02:00
|
|
|
if ($codebase->alter_code
|
|
|
|
&& isset($statements_analyzer->getProjectAnalyzer()->getIssuesToFix()['UnnecessaryVarAnnotation'])
|
|
|
|
) {
|
|
|
|
FileManipulationBuffer::addVarAnnotationToRemove($comment_type_location);
|
|
|
|
} elseif (IssueBuffer::accepts(
|
2019-09-19 17:59:43 +02:00
|
|
|
new UnnecessaryVarAnnotation(
|
2019-12-01 16:19:36 +01:00
|
|
|
'The @var ' . $comment_type . ' annotation for '
|
|
|
|
. $array_var_id . ' is unnecessary',
|
2019-09-26 21:08:05 +02:00
|
|
|
$comment_type_location
|
2019-09-19 17:59:43 +02:00
|
|
|
)
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-07 16:22:52 +02:00
|
|
|
$assign_value_type = $comment_type;
|
2016-12-17 01:22:30 +01:00
|
|
|
} elseif (!$assign_value_type) {
|
2019-11-25 17:44:54 +01:00
|
|
|
$assign_value_type = $assign_value
|
|
|
|
? ($statements_analyzer->node_data->getType($assign_value) ?: Type::getMixed())
|
|
|
|
: Type::getMixed();
|
2016-11-01 16:37:58 +01:00
|
|
|
}
|
|
|
|
|
2017-04-02 01:04:49 +02:00
|
|
|
if ($array_var_id && isset($context->vars_in_scope[$array_var_id])) {
|
2019-12-05 19:37:03 +01:00
|
|
|
if ($context->vars_in_scope[$array_var_id]->by_ref) {
|
|
|
|
$assign_value_type->by_ref = true;
|
|
|
|
}
|
|
|
|
|
2017-04-02 21:26:10 +02:00
|
|
|
// removes dependennt vars from $context
|
|
|
|
$context->removeDescendents(
|
|
|
|
$array_var_id,
|
|
|
|
$context->vars_in_scope[$array_var_id],
|
|
|
|
$assign_value_type,
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer
|
2017-04-02 21:26:10 +02:00
|
|
|
);
|
2017-10-11 05:01:52 +02:00
|
|
|
} else {
|
2018-11-06 03:57:36 +01:00
|
|
|
$root_var_id = ExpressionAnalyzer::getRootVarId(
|
2017-10-11 05:01:52 +02:00
|
|
|
$assign_var,
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getFQCLN(),
|
|
|
|
$statements_analyzer
|
2017-10-11 05:01:52 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
if ($root_var_id && isset($context->vars_in_scope[$root_var_id])) {
|
|
|
|
$context->removeVarFromConflictingClauses(
|
|
|
|
$root_var_id,
|
|
|
|
$context->vars_in_scope[$root_var_id],
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer
|
2017-10-11 05:01:52 +02:00
|
|
|
);
|
|
|
|
}
|
2017-04-02 01:04:49 +02:00
|
|
|
}
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$codebase = $statements_analyzer->getCodebase();
|
2018-01-31 22:08:52 +01:00
|
|
|
|
2018-12-08 19:18:55 +01:00
|
|
|
if ($assign_value_type->hasMixed()) {
|
2019-02-05 06:36:57 +01:00
|
|
|
$root_var_id = ExpressionAnalyzer::getRootVarId(
|
|
|
|
$assign_var,
|
|
|
|
$statements_analyzer->getFQCLN(),
|
|
|
|
$statements_analyzer
|
|
|
|
);
|
|
|
|
|
2019-03-23 14:50:47 +01:00
|
|
|
if (!$context->collect_initializations
|
|
|
|
&& !$context->collect_mutations
|
|
|
|
&& $statements_analyzer->getFilePath() === $statements_analyzer->getRootFilePath()
|
|
|
|
&& (!(($parent_source = $statements_analyzer->getSource())
|
|
|
|
instanceof \Psalm\Internal\Analyzer\FunctionLikeAnalyzer)
|
|
|
|
|| !$parent_source->getSource() instanceof \Psalm\Internal\Analyzer\TraitAnalyzer)
|
|
|
|
) {
|
|
|
|
$codebase->analyzer->incrementMixedCount($statements_analyzer->getFilePath());
|
|
|
|
}
|
2018-01-31 22:08:52 +01:00
|
|
|
|
2019-02-05 06:36:57 +01:00
|
|
|
if (!$assign_var instanceof PhpParser\Node\Expr\PropertyFetch
|
|
|
|
&& !strpos($root_var_id ?? '', '->')
|
2019-08-17 17:22:43 +02:00
|
|
|
&& !$comment_type
|
2019-02-05 06:36:57 +01:00
|
|
|
) {
|
2018-04-07 18:13:30 +02:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new MixedAssignment(
|
2019-03-16 17:53:54 +01:00
|
|
|
'Cannot assign' . ($var_id ? ' ' . $var_id . ' ' : ' ') . 'to a mixed type',
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $assign_var)
|
2018-04-07 18:13:30 +02:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-04-07 18:13:30 +02:00
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
2016-12-17 00:56:23 +01:00
|
|
|
}
|
2018-01-31 22:08:52 +01:00
|
|
|
} else {
|
2019-03-23 14:50:47 +01:00
|
|
|
if (!$context->collect_initializations
|
|
|
|
&& !$context->collect_mutations
|
|
|
|
&& $statements_analyzer->getFilePath() === $statements_analyzer->getRootFilePath()
|
|
|
|
&& (!(($parent_source = $statements_analyzer->getSource())
|
|
|
|
instanceof \Psalm\Internal\Analyzer\FunctionLikeAnalyzer)
|
|
|
|
|| !$parent_source->getSource() instanceof \Psalm\Internal\Analyzer\TraitAnalyzer)
|
|
|
|
) {
|
|
|
|
$codebase->analyzer->incrementNonMixedCount($statements_analyzer->getFilePath());
|
|
|
|
}
|
2018-01-31 22:08:52 +01:00
|
|
|
|
|
|
|
if ($var_id
|
|
|
|
&& isset($context->byref_constraints[$var_id])
|
|
|
|
&& ($outer_constraint_type = $context->byref_constraints[$var_id]->type)
|
2017-02-23 06:25:28 +01:00
|
|
|
) {
|
2018-11-06 03:57:36 +01:00
|
|
|
if (!TypeAnalyzer::isContainedBy(
|
2018-02-01 06:50:01 +01:00
|
|
|
$codebase,
|
2018-01-31 22:08:52 +01:00
|
|
|
$assign_value_type,
|
|
|
|
$outer_constraint_type,
|
|
|
|
$assign_value_type->ignore_nullable_issues,
|
|
|
|
$assign_value_type->ignore_falsable_issues
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new ReferenceConstraintViolation(
|
|
|
|
'Variable ' . $var_id . ' is limited to values of type '
|
|
|
|
. $context->byref_constraints[$var_id]->type
|
2019-02-10 21:01:10 +01:00
|
|
|
. ' because it is passed by reference, '
|
|
|
|
. $assign_value_type->getId() . ' type found',
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $assign_var)
|
2018-01-31 22:08:52 +01:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2018-01-31 22:08:52 +01:00
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
2017-02-23 06:25:28 +01:00
|
|
|
}
|
|
|
|
}
|
2016-12-17 00:56:23 +01:00
|
|
|
}
|
|
|
|
|
2016-12-12 05:40:46 +01:00
|
|
|
if ($var_id === '$this' && IssueBuffer::accepts(
|
|
|
|
new InvalidScope(
|
|
|
|
'Cannot re-assign ' . $var_id,
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $assign_var)
|
2016-12-12 05:40:46 +01:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2016-12-12 05:40:46 +01:00
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-17 16:58:03 +01:00
|
|
|
if (isset($context->protected_var_ids[$var_id])) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new LoopInvalidation(
|
|
|
|
'Variable ' . $var_id . ' has already been assigned in a for/foreach loop',
|
2018-11-11 18:01:14 +01:00
|
|
|
new CodeLocation($statements_analyzer->getSource(), $assign_var)
|
2017-12-17 16:58:03 +01:00
|
|
|
),
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getSuppressedIssues()
|
2017-12-17 16:58:03 +01:00
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-01 16:37:58 +01:00
|
|
|
if ($assign_var instanceof PhpParser\Node\Expr\Variable && is_string($assign_var->name) && $var_id) {
|
2016-12-04 20:14:00 +01:00
|
|
|
$context->vars_in_scope[$var_id] = $assign_value_type;
|
2016-11-01 16:37:58 +01:00
|
|
|
$context->vars_possibly_in_scope[$var_id] = true;
|
2017-02-08 00:09:12 +01:00
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$location = new CodeLocation($statements_analyzer, $assign_var);
|
2018-01-28 23:28:34 +01:00
|
|
|
|
|
|
|
if ($context->collect_references) {
|
2018-06-17 02:01:33 +02:00
|
|
|
$context->unreferenced_vars[$var_id] = [$location->getHash() => $location];
|
2018-01-25 07:04:26 +01:00
|
|
|
}
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
if (!$statements_analyzer->hasVariable($var_id)) {
|
|
|
|
$statements_analyzer->registerVariable(
|
2018-01-21 22:24:20 +01:00
|
|
|
$var_id,
|
2018-01-28 23:28:34 +01:00
|
|
|
$location,
|
2018-01-21 22:24:20 +01:00
|
|
|
$context->branch_point
|
|
|
|
);
|
2018-01-28 23:28:34 +01:00
|
|
|
} else {
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->registerVariableAssignment(
|
2018-01-28 23:28:34 +01:00
|
|
|
$var_id,
|
|
|
|
$location
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-12-05 19:37:03 +01:00
|
|
|
if (isset($context->byref_constraints[$var_id]) || $assign_value_type->by_ref) {
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->registerVariableUses([$location->getHash() => $location]);
|
2017-02-08 00:09:12 +01:00
|
|
|
}
|
2016-12-04 19:35:38 +01:00
|
|
|
} elseif ($assign_var instanceof PhpParser\Node\Expr\List_
|
2017-12-19 05:13:18 +01:00
|
|
|
|| $assign_var instanceof PhpParser\Node\Expr\Array_
|
2016-12-04 19:35:38 +01:00
|
|
|
) {
|
2019-10-22 17:17:42 +02:00
|
|
|
if (!$assign_value_type->hasArray()
|
|
|
|
&& !$assign_value_type->isMixed()
|
|
|
|
&& !$assign_value_type->hasArrayAccessInterface($codebase)
|
|
|
|
) {
|
2019-10-15 19:55:30 +02:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidArrayOffset(
|
|
|
|
'Cannot destructure non-array of type ' . $assign_value_type->getId(),
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $assign_var)
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-25 21:38:54 +01:00
|
|
|
$can_be_empty = true;
|
|
|
|
|
2016-12-04 04:41:45 +01:00
|
|
|
foreach ($assign_var->items as $offset => $assign_var_item) {
|
2017-01-02 21:31:18 +01:00
|
|
|
// $assign_var_item can be null e.g. list($a, ) = ['a', 'b']
|
|
|
|
if (!$assign_var_item) {
|
2016-11-01 16:37:58 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$var = $assign_var_item->value;
|
|
|
|
|
2016-11-01 19:32:19 +01:00
|
|
|
if ($assign_value instanceof PhpParser\Node\Expr\Array_
|
2019-11-25 17:44:54 +01:00
|
|
|
&& $statements_analyzer->node_data->getType($assign_var_item->value)
|
2016-11-01 19:32:19 +01:00
|
|
|
) {
|
2017-01-07 21:09:47 +01:00
|
|
|
self::analyze(
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer,
|
2016-11-02 07:29:00 +01:00
|
|
|
$var,
|
2018-01-14 00:33:32 +01:00
|
|
|
$assign_var_item->value,
|
2016-12-04 20:14:00 +01:00
|
|
|
null,
|
2016-11-02 07:29:00 +01:00
|
|
|
$context,
|
|
|
|
$doc_comment
|
|
|
|
);
|
|
|
|
|
2017-01-25 08:11:24 +01:00
|
|
|
continue;
|
2017-11-28 06:46:41 +01:00
|
|
|
}
|
|
|
|
|
2019-10-22 17:17:42 +02:00
|
|
|
$list_var_id = ExpressionAnalyzer::getArrayVarId(
|
|
|
|
$var,
|
|
|
|
$statements_analyzer->getFQCLN(),
|
|
|
|
$statements_analyzer
|
|
|
|
);
|
2019-09-19 17:59:43 +02:00
|
|
|
|
2019-10-22 17:17:42 +02:00
|
|
|
$new_assign_type = null;
|
2019-10-15 19:55:30 +02:00
|
|
|
|
2019-10-22 17:17:42 +02:00
|
|
|
foreach ($assign_value_type->getTypes() as $assign_value_atomic_type) {
|
|
|
|
if ($assign_value_atomic_type instanceof Type\Atomic\ObjectLike
|
|
|
|
&& !$assign_var_item->key
|
|
|
|
&& isset($assign_value_atomic_type->properties[$offset]) // if object-like has int offsets
|
|
|
|
) {
|
|
|
|
$offset_type = $assign_value_atomic_type->properties[(string)$offset];
|
2018-11-30 19:21:08 +01:00
|
|
|
|
2019-10-22 17:17:42 +02:00
|
|
|
if ($offset_type->possibly_undefined) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new PossiblyUndefinedArrayOffset(
|
|
|
|
'Possibly undefined array key',
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $var)
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
|
|
|
|
$offset_type = clone $offset_type;
|
|
|
|
$offset_type->possibly_undefined = false;
|
2018-11-30 19:21:08 +01:00
|
|
|
}
|
|
|
|
|
2019-10-22 17:17:42 +02:00
|
|
|
self::analyze(
|
|
|
|
$statements_analyzer,
|
|
|
|
$var,
|
|
|
|
null,
|
|
|
|
$offset_type,
|
|
|
|
$context,
|
|
|
|
$doc_comment
|
|
|
|
);
|
|
|
|
|
|
|
|
continue 2;
|
2018-11-30 19:21:08 +01:00
|
|
|
}
|
|
|
|
|
2019-10-22 17:17:42 +02:00
|
|
|
if ($var instanceof PhpParser\Node\Expr\List_
|
|
|
|
|| $var instanceof PhpParser\Node\Expr\Array_
|
|
|
|
) {
|
|
|
|
if ($assign_value_atomic_type instanceof Type\Atomic\ObjectLike) {
|
|
|
|
$assign_value_atomic_type = $assign_value_atomic_type->getGenericArrayType();
|
|
|
|
}
|
2017-01-25 08:11:24 +01:00
|
|
|
|
2019-10-22 17:17:42 +02:00
|
|
|
if ($assign_value_atomic_type instanceof Type\Atomic\TList) {
|
|
|
|
$assign_value_atomic_type = new Type\Atomic\TArray([
|
|
|
|
Type::getInt(),
|
|
|
|
$assign_value_atomic_type->type_param
|
|
|
|
]);
|
|
|
|
}
|
2016-11-01 19:32:19 +01:00
|
|
|
|
2019-10-22 17:17:42 +02:00
|
|
|
self::analyze(
|
|
|
|
$statements_analyzer,
|
|
|
|
$var,
|
|
|
|
null,
|
|
|
|
$assign_value_atomic_type instanceof Type\Atomic\TArray
|
|
|
|
? clone $assign_value_atomic_type->type_params[1]
|
|
|
|
: Type::getMixed(),
|
|
|
|
$context,
|
|
|
|
$doc_comment
|
|
|
|
);
|
2018-03-21 13:48:30 +01:00
|
|
|
}
|
|
|
|
|
2019-10-22 17:17:42 +02:00
|
|
|
if ($list_var_id) {
|
|
|
|
$context->vars_possibly_in_scope[$list_var_id] = true;
|
|
|
|
$context->assigned_var_ids[$list_var_id] = true;
|
|
|
|
$context->possibly_assigned_var_ids[$list_var_id] = true;
|
2019-10-09 00:44:46 +02:00
|
|
|
|
2019-12-06 18:47:27 +01:00
|
|
|
$already_in_scope = isset($context->vars_in_scope[$list_var_id]);
|
2017-12-19 05:13:18 +01:00
|
|
|
|
2019-10-22 17:17:42 +02:00
|
|
|
if (strpos($list_var_id, '-') === false && strpos($list_var_id, '[') === false) {
|
|
|
|
$location = new CodeLocation($statements_analyzer, $var);
|
2016-11-01 16:37:58 +01:00
|
|
|
|
2019-10-22 17:17:42 +02:00
|
|
|
if ($context->collect_references) {
|
|
|
|
$context->unreferenced_vars[$list_var_id] = [$location->getHash() => $location];
|
|
|
|
}
|
2017-02-08 00:09:12 +01:00
|
|
|
|
2019-10-22 17:17:42 +02:00
|
|
|
if (!$statements_analyzer->hasVariable($list_var_id)) {
|
|
|
|
$statements_analyzer->registerVariable(
|
|
|
|
$list_var_id,
|
|
|
|
$location,
|
|
|
|
$context->branch_point
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$statements_analyzer->registerVariableAssignment(
|
|
|
|
$list_var_id,
|
|
|
|
$location
|
|
|
|
);
|
|
|
|
}
|
2018-01-28 23:28:34 +01:00
|
|
|
|
2019-10-22 17:17:42 +02:00
|
|
|
if (isset($context->byref_constraints[$list_var_id])) {
|
|
|
|
$statements_analyzer->registerVariableUses([$location->getHash() => $location]);
|
|
|
|
}
|
2018-01-28 23:28:34 +01:00
|
|
|
}
|
|
|
|
|
2019-10-22 17:17:42 +02:00
|
|
|
if ($assign_value_atomic_type instanceof Type\Atomic\TArray) {
|
|
|
|
$new_assign_type = clone $assign_value_atomic_type->type_params[1];
|
2019-11-25 21:38:54 +01:00
|
|
|
|
|
|
|
$can_be_empty = !$assign_value_atomic_type instanceof Type\Atomic\TNonEmptyArray;
|
2019-10-22 17:17:42 +02:00
|
|
|
} elseif ($assign_value_atomic_type instanceof Type\Atomic\TList) {
|
|
|
|
$new_assign_type = clone $assign_value_atomic_type->type_param;
|
2019-11-25 21:38:54 +01:00
|
|
|
|
|
|
|
$can_be_empty = !$assign_value_atomic_type instanceof Type\Atomic\TNonEmptyList;
|
2019-10-22 17:17:42 +02:00
|
|
|
} elseif ($assign_value_atomic_type instanceof Type\Atomic\ObjectLike) {
|
|
|
|
if ($assign_var_item->key
|
|
|
|
&& ($assign_var_item->key instanceof PhpParser\Node\Scalar\String_
|
|
|
|
|| $assign_var_item->key instanceof PhpParser\Node\Scalar\LNumber)
|
|
|
|
&& isset($assign_value_atomic_type->properties[$assign_var_item->key->value])
|
|
|
|
) {
|
|
|
|
$new_assign_type =
|
|
|
|
clone $assign_value_atomic_type->properties[$assign_var_item->key->value];
|
|
|
|
|
|
|
|
if ($new_assign_type->possibly_undefined) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new PossiblyUndefinedArrayOffset(
|
|
|
|
'Possibly undefined array key',
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $var)
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
|
|
|
|
$new_assign_type->possibly_undefined = false;
|
|
|
|
}
|
|
|
|
}
|
2019-11-25 21:38:54 +01:00
|
|
|
|
|
|
|
$can_be_empty = !$assign_value_atomic_type->sealed;
|
2019-10-22 17:17:42 +02:00
|
|
|
} elseif ($assign_value_atomic_type->hasArrayAccessInterface($codebase)) {
|
|
|
|
ForeachAnalyzer::getKeyValueParamsForTraversableObject(
|
|
|
|
$assign_value_atomic_type,
|
|
|
|
$codebase,
|
|
|
|
$array_access_key_type,
|
|
|
|
$array_access_value_type
|
2018-01-28 23:28:34 +01:00
|
|
|
);
|
|
|
|
|
2019-10-22 17:17:42 +02:00
|
|
|
$new_assign_type = $array_access_value_type;
|
2018-01-28 23:28:34 +01:00
|
|
|
}
|
2016-12-04 20:14:00 +01:00
|
|
|
|
2019-10-22 17:17:42 +02:00
|
|
|
if ($already_in_scope) {
|
|
|
|
// removes dependennt vars from $context
|
|
|
|
$context->removeDescendents(
|
|
|
|
$list_var_id,
|
|
|
|
$context->vars_in_scope[$list_var_id],
|
|
|
|
$new_assign_type,
|
|
|
|
$statements_analyzer
|
|
|
|
);
|
2016-12-04 20:14:00 +01:00
|
|
|
}
|
|
|
|
}
|
2019-10-22 17:17:42 +02:00
|
|
|
}
|
2016-12-04 20:14:00 +01:00
|
|
|
|
2019-10-22 17:17:42 +02:00
|
|
|
foreach ($var_comments as $var_comment) {
|
|
|
|
if (!$var_comment->type) {
|
|
|
|
continue;
|
2017-08-19 05:14:38 +02:00
|
|
|
}
|
|
|
|
|
2019-10-22 17:17:42 +02:00
|
|
|
try {
|
|
|
|
if ($var_comment->var_id === $list_var_id) {
|
|
|
|
$var_comment_type = ExpressionAnalyzer::fleshOutType(
|
|
|
|
$codebase,
|
|
|
|
$var_comment->type,
|
|
|
|
$context->self,
|
|
|
|
$context->self,
|
|
|
|
$statements_analyzer->getParentFQCLN()
|
|
|
|
);
|
2018-08-21 04:11:01 +02:00
|
|
|
|
2019-10-22 17:17:42 +02:00
|
|
|
$var_comment_type->setFromDocblock();
|
2018-08-21 04:11:01 +02:00
|
|
|
|
2019-10-22 17:17:42 +02:00
|
|
|
$new_assign_type = $var_comment_type;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} catch (\UnexpectedValueException $e) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new InvalidDocblock(
|
|
|
|
(string)$e->getMessage(),
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $assign_var)
|
|
|
|
)
|
|
|
|
)) {
|
|
|
|
// fall through
|
2018-08-21 04:11:01 +02:00
|
|
|
}
|
|
|
|
}
|
2019-10-22 17:17:42 +02:00
|
|
|
}
|
2018-08-21 04:11:01 +02:00
|
|
|
|
2019-10-22 17:17:42 +02:00
|
|
|
if ($list_var_id) {
|
2017-08-19 05:14:38 +02:00
|
|
|
$context->vars_in_scope[$list_var_id] = $new_assign_type ?: Type::getMixed();
|
2019-10-22 16:40:37 +02:00
|
|
|
|
2019-11-25 21:38:54 +01:00
|
|
|
if ($context->error_suppressing && ($offset || $can_be_empty)) {
|
2019-10-22 16:40:37 +02:00
|
|
|
$context->vars_in_scope[$list_var_id]->addType(new Type\Atomic\TNull);
|
|
|
|
}
|
2016-11-01 16:37:58 +01:00
|
|
|
}
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($assign_var instanceof PhpParser\Node\Expr\ArrayDimFetch) {
|
2018-11-06 03:57:36 +01:00
|
|
|
ArrayAssignmentAnalyzer::analyze(
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer,
|
2017-01-07 21:09:47 +01:00
|
|
|
$assign_var,
|
|
|
|
$context,
|
2019-01-31 18:45:47 +01:00
|
|
|
$assign_value,
|
2017-01-07 21:09:47 +01:00
|
|
|
$assign_value_type
|
2018-03-07 17:16:56 +01:00
|
|
|
);
|
2017-12-19 04:35:03 +01:00
|
|
|
} elseif ($assign_var instanceof PhpParser\Node\Expr\PropertyFetch) {
|
2018-05-09 03:21:22 +02:00
|
|
|
if (!$assign_var->name instanceof PhpParser\Node\Identifier) {
|
2018-10-26 22:17:15 +02:00
|
|
|
// this can happen when the user actually means to type $this-><autocompleted>, but there's
|
|
|
|
// a variable on the next line
|
2018-11-11 18:01:14 +01:00
|
|
|
if (ExpressionAnalyzer::analyze($statements_analyzer, $assign_var->var, $context) === false) {
|
2018-10-26 22:17:15 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
if (ExpressionAnalyzer::analyze($statements_analyzer, $assign_var->name, $context) === false) {
|
2018-05-09 03:21:22 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-17 18:16:25 +02:00
|
|
|
if ($assign_var->name instanceof PhpParser\Node\Identifier) {
|
2018-05-09 03:21:22 +02:00
|
|
|
$prop_name = $assign_var->name->name;
|
2019-11-25 17:44:54 +01:00
|
|
|
} elseif (($assign_var_name_type = $statements_analyzer->node_data->getType($assign_var->name))
|
|
|
|
&& $assign_var_name_type->isSingleStringLiteral()
|
2018-05-09 03:21:22 +02:00
|
|
|
) {
|
2019-11-25 17:44:54 +01:00
|
|
|
$prop_name = $assign_var_name_type->getSingleStringLiteral()->value;
|
2018-05-09 03:21:22 +02:00
|
|
|
} else {
|
|
|
|
$prop_name = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($prop_name) {
|
2018-11-06 03:57:36 +01:00
|
|
|
PropertyAssignmentAnalyzer::analyzeInstance(
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer,
|
2017-12-19 04:35:03 +01:00
|
|
|
$assign_var,
|
2018-05-09 03:21:22 +02:00
|
|
|
$prop_name,
|
2017-12-19 04:35:03 +01:00
|
|
|
$assign_value,
|
|
|
|
$assign_value_type,
|
|
|
|
$context
|
|
|
|
);
|
|
|
|
} else {
|
2018-11-11 18:01:14 +01:00
|
|
|
if (ExpressionAnalyzer::analyze($statements_analyzer, $assign_var->var, $context) === false) {
|
2017-12-19 04:35:03 +01:00
|
|
|
return false;
|
|
|
|
}
|
2019-04-18 20:47:58 +02:00
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
if (($assign_var_type = $statements_analyzer->node_data->getType($assign_var->var))
|
|
|
|
&& !$context->ignore_variable_property
|
|
|
|
) {
|
|
|
|
$stmt_var_type = $assign_var_type;
|
2019-04-18 20:47:58 +02:00
|
|
|
|
|
|
|
if ($stmt_var_type->hasObjectType()) {
|
|
|
|
foreach ($stmt_var_type->getTypes() as $type) {
|
|
|
|
if ($type instanceof Type\Atomic\TNamedObject) {
|
2019-04-27 23:38:24 +02:00
|
|
|
$codebase->analyzer->addMixedMemberName(
|
|
|
|
strtolower($type->value) . '::$',
|
2019-12-29 00:37:55 +01:00
|
|
|
$context->calling_function_id ?: $statements_analyzer->getFileName()
|
2019-04-27 23:38:24 +02:00
|
|
|
);
|
2019-04-18 20:47:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-19 04:35:03 +01:00
|
|
|
}
|
2016-11-01 16:37:58 +01:00
|
|
|
|
2017-02-24 01:36:51 +01:00
|
|
|
if ($var_id) {
|
|
|
|
$context->vars_possibly_in_scope[$var_id] = true;
|
|
|
|
}
|
2019-09-09 17:14:40 +02:00
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
$method_pure_compatible = $statements_analyzer->node_data->isPureCompatible($assign_var->var);
|
2019-09-09 17:14:40 +02:00
|
|
|
|
2019-09-09 17:20:47 +02:00
|
|
|
if (($context->mutation_free || $context->external_mutation_free)
|
|
|
|
&& !$method_pure_compatible
|
2019-09-09 17:14:40 +02:00
|
|
|
&& !$context->collect_mutations
|
|
|
|
&& !$context->collect_initializations
|
|
|
|
) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new ImpurePropertyAssignment(
|
|
|
|
'Cannot assign to a property from a mutation-free context',
|
|
|
|
new CodeLocation($statements_analyzer, $assign_var)
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
2016-11-02 07:29:00 +01:00
|
|
|
} elseif ($assign_var instanceof PhpParser\Node\Expr\StaticPropertyFetch &&
|
2018-04-17 18:16:25 +02:00
|
|
|
$assign_var->class instanceof PhpParser\Node\Name
|
2016-11-01 16:37:58 +01:00
|
|
|
) {
|
2018-11-11 18:01:14 +01:00
|
|
|
if (ExpressionAnalyzer::analyze($statements_analyzer, $assign_var, $context) === false) {
|
2016-11-01 16:37:58 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-01-18 05:34:17 +01:00
|
|
|
if ($context->check_classes) {
|
2018-11-06 03:57:36 +01:00
|
|
|
PropertyAssignmentAnalyzer::analyzeStatic(
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer,
|
2017-01-18 05:34:17 +01:00
|
|
|
$assign_var,
|
|
|
|
$assign_value,
|
|
|
|
$assign_value_type,
|
|
|
|
$context
|
|
|
|
);
|
|
|
|
}
|
2016-11-01 16:37:58 +01:00
|
|
|
|
2017-02-24 01:36:51 +01:00
|
|
|
if ($var_id) {
|
|
|
|
$context->vars_possibly_in_scope[$var_id] = true;
|
|
|
|
}
|
2016-11-01 16:37:58 +01:00
|
|
|
}
|
|
|
|
|
2019-01-02 23:05:39 +01:00
|
|
|
if ($var_id && isset($context->vars_in_scope[$var_id])) {
|
|
|
|
if ($context->vars_in_scope[$var_id]->isVoid()) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new AssignmentToVoid(
|
|
|
|
'Cannot assign ' . $var_id . ' to type void',
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $assign_var)
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
|
|
|
|
$context->vars_in_scope[$var_id] = Type::getNull();
|
|
|
|
|
2019-08-13 19:15:23 +02:00
|
|
|
if (!$was_in_assignment) {
|
|
|
|
$context->inside_assignment = false;
|
|
|
|
}
|
|
|
|
|
2019-01-02 23:05:39 +01:00
|
|
|
return $context->vars_in_scope[$var_id];
|
2016-11-01 16:37:58 +01:00
|
|
|
}
|
2018-01-09 06:29:43 +01:00
|
|
|
|
2019-01-02 23:05:39 +01:00
|
|
|
if ($context->vars_in_scope[$var_id]->isNever()) {
|
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new NoValue(
|
|
|
|
'This function or method call never returns output',
|
|
|
|
new CodeLocation($statements_analyzer->getSource(), $assign_var)
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$context->vars_in_scope[$var_id] = Type::getEmpty();
|
2018-01-09 06:29:43 +01:00
|
|
|
|
2019-08-13 19:15:23 +02:00
|
|
|
if (!$was_in_assignment) {
|
|
|
|
$context->inside_assignment = false;
|
|
|
|
}
|
|
|
|
|
2019-01-02 23:05:39 +01:00
|
|
|
return $context->vars_in_scope[$var_id];
|
|
|
|
}
|
2019-10-15 22:25:27 +02:00
|
|
|
|
|
|
|
if ($remove_taint && $context->vars_in_scope[$var_id]->sources) {
|
|
|
|
$context->vars_in_scope[$var_id]->sources = null;
|
|
|
|
|
|
|
|
$context->vars_in_scope[$var_id]->tainted = null;
|
|
|
|
}
|
2016-11-01 16:37:58 +01:00
|
|
|
}
|
|
|
|
|
2019-08-13 19:15:23 +02:00
|
|
|
if (!$was_in_assignment) {
|
|
|
|
$context->inside_assignment = false;
|
|
|
|
}
|
|
|
|
|
2016-12-04 20:14:00 +01:00
|
|
|
return $assign_value_type;
|
2016-11-01 16:37:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-11-11 18:01:14 +01:00
|
|
|
* @param StatementsAnalyzer $statements_analyzer
|
2016-11-02 07:29:00 +01:00
|
|
|
* @param PhpParser\Node\Expr\AssignOp $stmt
|
|
|
|
* @param Context $context
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-11-02 07:29:00 +01:00
|
|
|
* @return false|null
|
2016-11-01 16:37:58 +01:00
|
|
|
*/
|
2017-01-07 21:09:47 +01:00
|
|
|
public static function analyzeAssignmentOperation(
|
2018-11-11 18:01:14 +01:00
|
|
|
StatementsAnalyzer $statements_analyzer,
|
2016-11-01 16:37:58 +01:00
|
|
|
PhpParser\Node\Expr\AssignOp $stmt,
|
|
|
|
Context $context
|
|
|
|
) {
|
2019-08-13 19:15:23 +02:00
|
|
|
$was_in_assignment = $context->inside_assignment;
|
|
|
|
|
|
|
|
$context->inside_assignment = true;
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->var, $context) === false) {
|
2016-11-01 16:37:58 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->expr, $context) === false) {
|
2016-11-13 21:39:16 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
$array_var_id = ExpressionAnalyzer::getArrayVarId(
|
2016-11-13 21:39:16 +01:00
|
|
|
$stmt->var,
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getFQCLN(),
|
|
|
|
$statements_analyzer
|
2016-11-13 21:39:16 +01:00
|
|
|
);
|
|
|
|
|
2019-10-01 04:07:20 +02:00
|
|
|
if ($array_var_id
|
|
|
|
&& $context->mutation_free
|
|
|
|
&& $stmt->var instanceof PhpParser\Node\Expr\PropertyFetch
|
2019-11-25 17:44:54 +01:00
|
|
|
&& ($stmt_var_var_type = $statements_analyzer->node_data->getType($stmt->var->var))
|
|
|
|
&& (!$stmt_var_var_type->external_mutation_free
|
|
|
|
|| $stmt_var_var_type->mutation_free)
|
2019-10-01 04:07:20 +02:00
|
|
|
) {
|
2019-07-18 07:31:48 +02:00
|
|
|
if (IssueBuffer::accepts(
|
|
|
|
new ImpurePropertyAssignment(
|
2019-09-03 18:16:31 +02:00
|
|
|
'Cannot assign to a property from a mutation-free context',
|
2019-07-18 07:31:48 +02:00
|
|
|
new CodeLocation($statements_analyzer, $stmt->var)
|
|
|
|
),
|
|
|
|
$statements_analyzer->getSuppressedIssues()
|
|
|
|
)) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-22 13:36:16 +01:00
|
|
|
if ($array_var_id) {
|
2018-06-17 05:40:25 +02:00
|
|
|
$context->assigned_var_ids[$array_var_id] = true;
|
|
|
|
$context->possibly_assigned_var_ids[$array_var_id] = true;
|
2019-12-22 13:36:16 +01:00
|
|
|
|
|
|
|
if ($context->collect_references && $stmt->var instanceof PhpParser\Node\Expr\Variable) {
|
|
|
|
$location = new CodeLocation($statements_analyzer, $stmt->var);
|
|
|
|
$statements_analyzer->registerVariableAssignment(
|
|
|
|
$array_var_id,
|
|
|
|
$location
|
|
|
|
);
|
|
|
|
$context->unreferenced_vars[$array_var_id] = [$location->getHash() => $location];
|
|
|
|
}
|
2018-06-17 05:40:25 +02:00
|
|
|
}
|
|
|
|
|
2019-11-25 17:44:54 +01:00
|
|
|
$stmt_var_type = $statements_analyzer->node_data->getType($stmt->var);
|
|
|
|
$stmt_var_type = $stmt_var_type ? clone $stmt_var_type: null;
|
|
|
|
|
|
|
|
$stmt_expr_type = $statements_analyzer->node_data->getType($stmt->expr);
|
2019-12-29 14:36:38 +01:00
|
|
|
$result_type = null;
|
2016-11-13 21:39:16 +01:00
|
|
|
|
2019-02-18 18:53:55 +01:00
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\AssignOp\Plus
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\AssignOp\Minus
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\AssignOp\Mod
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\AssignOp\Mul
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\AssignOp\Pow
|
2016-12-08 22:37:14 +01:00
|
|
|
) {
|
2019-03-25 02:22:52 +01:00
|
|
|
BinaryOpAnalyzer::analyzeNonDivArithmeticOp(
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer,
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data,
|
2016-12-24 03:30:32 +01:00
|
|
|
$stmt->var,
|
|
|
|
$stmt->expr,
|
|
|
|
$stmt,
|
2017-09-07 03:44:26 +02:00
|
|
|
$result_type,
|
|
|
|
$context
|
2016-12-24 03:30:32 +01:00
|
|
|
);
|
2016-11-13 21:39:16 +01:00
|
|
|
|
2018-05-03 02:10:08 +02:00
|
|
|
if ($stmt->var instanceof PhpParser\Node\Expr\ArrayDimFetch) {
|
2018-11-06 03:57:36 +01:00
|
|
|
ArrayAssignmentAnalyzer::analyze(
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer,
|
2018-05-03 02:10:08 +02:00
|
|
|
$stmt->var,
|
|
|
|
$context,
|
2019-01-31 18:45:47 +01:00
|
|
|
$stmt->expr,
|
2018-12-08 19:18:55 +01:00
|
|
|
$result_type ?: Type::getMixed($context->inside_loop)
|
2018-05-03 02:10:08 +02:00
|
|
|
);
|
|
|
|
} elseif ($result_type && $array_var_id) {
|
2017-11-19 18:33:43 +01:00
|
|
|
$context->vars_in_scope[$array_var_id] = $result_type;
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, clone $context->vars_in_scope[$array_var_id]);
|
2016-11-13 21:39:16 +01:00
|
|
|
}
|
2016-12-08 22:37:14 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\AssignOp\Div
|
2019-11-25 17:44:54 +01:00
|
|
|
&& $stmt_var_type
|
|
|
|
&& $stmt_expr_type
|
|
|
|
&& $stmt_var_type->hasDefinitelyNumericType()
|
|
|
|
&& $stmt_expr_type->hasDefinitelyNumericType()
|
2017-11-19 18:33:43 +01:00
|
|
|
&& $array_var_id
|
2016-12-08 22:37:14 +01:00
|
|
|
) {
|
2017-11-19 18:33:43 +01:00
|
|
|
$context->vars_in_scope[$array_var_id] = Type::combineUnionTypes(Type::getFloat(), Type::getInt());
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, clone $context->vars_in_scope[$array_var_id]);
|
2016-12-24 03:30:32 +01:00
|
|
|
} elseif ($stmt instanceof PhpParser\Node\Expr\AssignOp\Concat) {
|
2018-11-06 03:57:36 +01:00
|
|
|
BinaryOpAnalyzer::analyzeConcatOp(
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer,
|
2016-12-24 03:30:32 +01:00
|
|
|
$stmt->var,
|
|
|
|
$stmt->expr,
|
2017-09-03 01:23:00 +02:00
|
|
|
$context,
|
2016-12-24 03:30:32 +01:00
|
|
|
$result_type
|
|
|
|
);
|
|
|
|
|
2019-02-18 18:53:55 +01:00
|
|
|
if ($result_type && $array_var_id) {
|
|
|
|
$context->vars_in_scope[$array_var_id] = $result_type;
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, clone $context->vars_in_scope[$array_var_id]);
|
2019-02-18 18:53:55 +01:00
|
|
|
}
|
2019-11-25 17:44:54 +01:00
|
|
|
} elseif ($stmt_var_type
|
|
|
|
&& $stmt_expr_type
|
|
|
|
&& ($stmt_var_type->hasInt() || $stmt_expr_type->hasInt())
|
2019-02-18 18:53:55 +01:00
|
|
|
&& ($stmt instanceof PhpParser\Node\Expr\AssignOp\BitwiseOr
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\AssignOp\BitwiseXor
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\AssignOp\BitwiseAnd
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\AssignOp\ShiftLeft
|
|
|
|
|| $stmt instanceof PhpParser\Node\Expr\AssignOp\ShiftRight
|
|
|
|
)
|
|
|
|
) {
|
2019-03-25 02:22:52 +01:00
|
|
|
BinaryOpAnalyzer::analyzeNonDivArithmeticOp(
|
2019-02-18 18:53:55 +01:00
|
|
|
$statements_analyzer,
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data,
|
2019-02-18 18:53:55 +01:00
|
|
|
$stmt->var,
|
|
|
|
$stmt->expr,
|
|
|
|
$stmt,
|
|
|
|
$result_type,
|
|
|
|
$context
|
|
|
|
);
|
|
|
|
|
2017-11-19 18:33:43 +01:00
|
|
|
if ($result_type && $array_var_id) {
|
|
|
|
$context->vars_in_scope[$array_var_id] = $result_type;
|
2019-11-25 17:44:54 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, clone $context->vars_in_scope[$array_var_id]);
|
2016-12-24 03:30:32 +01:00
|
|
|
}
|
2016-11-13 21:39:16 +01:00
|
|
|
}
|
|
|
|
|
2019-12-29 14:36:38 +01:00
|
|
|
if ($stmt->var instanceof PhpParser\Node\Expr\ArrayDimFetch) {
|
|
|
|
ArrayAssignmentAnalyzer::analyze(
|
|
|
|
$statements_analyzer,
|
|
|
|
$stmt->var,
|
|
|
|
$context,
|
|
|
|
null,
|
|
|
|
$result_type ?: Type::getEmpty()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-11-27 06:34:23 +01:00
|
|
|
if ($stmt instanceof PhpParser\Node\Expr\AssignOp\Coalesce) {
|
|
|
|
$old_data_provider = $statements_analyzer->node_data;
|
|
|
|
|
|
|
|
$statements_analyzer->node_data = clone $statements_analyzer->node_data;
|
|
|
|
|
|
|
|
$fake_coalesce_expr = new PhpParser\Node\Expr\BinaryOp\Coalesce(
|
|
|
|
$stmt->var,
|
|
|
|
$stmt->expr,
|
|
|
|
$stmt->getAttributes()
|
|
|
|
);
|
|
|
|
|
|
|
|
if (BinaryOpAnalyzer::analyze(
|
|
|
|
$statements_analyzer,
|
|
|
|
$fake_coalesce_expr,
|
|
|
|
$context
|
|
|
|
) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$fake_coalesce_type = $statements_analyzer->node_data->getType($fake_coalesce_expr);
|
|
|
|
|
|
|
|
$statements_analyzer->node_data = $old_data_provider;
|
|
|
|
|
|
|
|
if ($fake_coalesce_type) {
|
|
|
|
if ($array_var_id) {
|
|
|
|
$context->vars_in_scope[$array_var_id] = $fake_coalesce_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
$statements_analyzer->node_data->setType($stmt, $fake_coalesce_type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-13 20:07:45 +02:00
|
|
|
if (!$was_in_assignment) {
|
|
|
|
$context->inside_assignment = false;
|
|
|
|
}
|
|
|
|
|
2016-11-13 21:39:16 +01:00
|
|
|
return null;
|
2016-11-01 16:37:58 +01:00
|
|
|
}
|
|
|
|
|
2017-02-04 04:07:14 +01:00
|
|
|
/**
|
2018-11-11 18:01:14 +01:00
|
|
|
* @param StatementsAnalyzer $statements_analyzer
|
2017-02-04 04:07:14 +01:00
|
|
|
* @param PhpParser\Node\Expr\AssignRef $stmt
|
|
|
|
* @param Context $context
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-02-04 04:07:14 +01:00
|
|
|
* @return false|null
|
|
|
|
*/
|
|
|
|
public static function analyzeAssignmentRef(
|
2018-11-11 18:01:14 +01:00
|
|
|
StatementsAnalyzer $statements_analyzer,
|
2017-02-04 04:07:14 +01:00
|
|
|
PhpParser\Node\Expr\AssignRef $stmt,
|
|
|
|
Context $context
|
|
|
|
) {
|
2019-08-28 05:19:10 +02:00
|
|
|
$assignment_type = self::analyze(
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer,
|
2017-02-04 04:07:14 +01:00
|
|
|
$stmt->var,
|
2017-02-23 06:25:28 +01:00
|
|
|
$stmt->expr,
|
|
|
|
null,
|
2017-02-04 04:07:14 +01:00
|
|
|
$context,
|
2019-06-01 17:53:32 +02:00
|
|
|
$stmt->getDocComment()
|
2019-08-28 05:19:10 +02:00
|
|
|
);
|
|
|
|
if ($assignment_type === false) {
|
2017-02-04 04:07:14 +01:00
|
|
|
return false;
|
|
|
|
}
|
2018-02-11 16:39:21 +01:00
|
|
|
|
2019-12-05 19:37:03 +01:00
|
|
|
$assignment_type->by_ref = true;
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
$lhs_var_id = ExpressionAnalyzer::getVarId(
|
2018-02-11 16:39:21 +01:00
|
|
|
$stmt->var,
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getFQCLN(),
|
|
|
|
$statements_analyzer
|
2018-02-11 16:39:21 +01:00
|
|
|
);
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
$rhs_var_id = ExpressionAnalyzer::getVarId(
|
2018-02-11 16:39:21 +01:00
|
|
|
$stmt->expr,
|
2018-11-11 18:01:14 +01:00
|
|
|
$statements_analyzer->getFQCLN(),
|
|
|
|
$statements_analyzer
|
2018-02-11 16:39:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
if ($lhs_var_id) {
|
2019-08-28 05:19:10 +02:00
|
|
|
$context->vars_in_scope[$lhs_var_id] = $assignment_type;
|
2019-07-24 22:53:14 +02:00
|
|
|
$context->hasVariable($lhs_var_id, $statements_analyzer);
|
2018-02-11 16:39:21 +01:00
|
|
|
}
|
|
|
|
|
2019-08-28 05:19:10 +02:00
|
|
|
if ($rhs_var_id && !isset($context->vars_in_scope[$rhs_var_id])) {
|
2018-02-11 16:39:21 +01:00
|
|
|
$context->vars_in_scope[$rhs_var_id] = Type::getMixed();
|
|
|
|
}
|
2017-02-04 04:07:14 +01:00
|
|
|
}
|
2016-11-01 16:37:58 +01:00
|
|
|
}
|