1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-12 01:09:38 +01:00

redundant variable check

This commit is contained in:
orklah 2021-09-26 22:25:15 +02:00
parent 998f91aa65
commit c11b32d414
8 changed files with 20 additions and 36 deletions

View File

@ -250,7 +250,7 @@ class ForeachAnalyzer
$foreach_context->branch_point ?: (int) $stmt->getAttribute('startFilePos'); $foreach_context->branch_point ?: (int) $stmt->getAttribute('startFilePos');
} }
if ($stmt->keyVar && $stmt->keyVar instanceof PhpParser\Node\Expr\Variable && is_string($stmt->keyVar->name)) { if ($stmt->keyVar instanceof PhpParser\Node\Expr\Variable && is_string($stmt->keyVar->name)) {
$key_type = $key_type ?: Type::getMixed(); $key_type = $key_type ?: Type::getMixed();
AssignmentAnalyzer::analyze( AssignmentAnalyzer::analyze(

View File

@ -21,10 +21,7 @@ class BreakAnalyzer
if ($loop_scope) { if ($loop_scope) {
if ($context->break_types if ($context->break_types
&& \end($context->break_types) === 'switch' && \end($context->break_types) === 'switch'
&& (!$stmt->num && (!$stmt->num instanceof PhpParser\Node\Scalar\LNumber || $stmt->num->value < 2)
|| !$stmt->num instanceof PhpParser\Node\Scalar\LNumber
|| $stmt->num->value < 2
)
) { ) {
$loop_scope->final_actions[] = ScopeAnalyzer::ACTION_LEAVE_SWITCH; $loop_scope->final_actions[] = ScopeAnalyzer::ACTION_LEAVE_SWITCH;
} else { } else {

View File

@ -17,10 +17,7 @@ class ContinueAnalyzer
PhpParser\Node\Stmt\Continue_ $stmt, PhpParser\Node\Stmt\Continue_ $stmt,
Context $context Context $context
): void { ): void {
$count = $stmt->num $count = $stmt->num instanceof PhpParser\Node\Scalar\LNumber? $stmt->num->value : 1;
&& $stmt->num instanceof PhpParser\Node\Scalar\LNumber
? $stmt->num->value
: 1;
$loop_scope = $context->loop_scope; $loop_scope = $context->loop_scope;

View File

@ -1349,9 +1349,8 @@ class AssignmentAnalyzer
$can_be_empty = !$assign_value_atomic_type instanceof Type\Atomic\TNonEmptyList; $can_be_empty = !$assign_value_atomic_type instanceof Type\Atomic\TNonEmptyList;
} elseif ($assign_value_atomic_type instanceof Type\Atomic\TKeyedArray) { } elseif ($assign_value_atomic_type instanceof Type\Atomic\TKeyedArray) {
if ($assign_var_item->key if (($assign_var_item->key instanceof PhpParser\Node\Scalar\String_
&& ($assign_var_item->key instanceof PhpParser\Node\Scalar\String_ || $assign_var_item->key instanceof PhpParser\Node\Scalar\LNumber)
|| $assign_var_item->key instanceof PhpParser\Node\Scalar\LNumber)
&& isset($assign_value_atomic_type->properties[$assign_var_item->key->value]) && isset($assign_value_atomic_type->properties[$assign_var_item->key->value])
) { ) {
$new_assign_type = $new_assign_type =

View File

@ -70,8 +70,7 @@ class ArgumentsAnalyzer
: null; : null;
// if this modifies the array type based on further args // if this modifies the array type based on further args
if ($method_id if (in_array($method_id, ['array_push', 'array_unshift'], true)
&& in_array($method_id, ['array_push', 'array_unshift'], true)
&& $function_params && $function_params
&& isset($args[0]) && isset($args[0])
&& isset($args[1]) && isset($args[1])
@ -89,7 +88,7 @@ class ArgumentsAnalyzer
return null; return null;
} }
if ($method_id && $method_id === 'array_splice' && $function_params && count($args) > 1) { if ($method_id === 'array_splice' && $function_params && count($args) > 1) {
if (ArrayFunctionArgumentsAnalyzer::handleSplice($statements_analyzer, $args, $context) === false) { if (ArrayFunctionArgumentsAnalyzer::handleSplice($statements_analyzer, $args, $context) === false) {
return false; return false;
} }

View File

@ -223,9 +223,7 @@ class NamedFunctionCallHandler
if ($function_id === 'func_get_args') { if ($function_id === 'func_get_args') {
$source = $statements_analyzer->getSource(); $source = $statements_analyzer->getSource();
if ($statements_analyzer->data_flow_graph if ($source instanceof \Psalm\Internal\Analyzer\FunctionLikeAnalyzer) {
&& $source instanceof \Psalm\Internal\Analyzer\FunctionLikeAnalyzer
) {
if ($statements_analyzer->data_flow_graph instanceof \Psalm\Internal\Codebase\VariableUseGraph) { if ($statements_analyzer->data_flow_graph instanceof \Psalm\Internal\Codebase\VariableUseGraph) {
foreach ($source->param_nodes as $param_node) { foreach ($source->param_nodes as $param_node) {
$statements_analyzer->data_flow_graph->addPath( $statements_analyzer->data_flow_graph->addPath(

View File

@ -66,8 +66,7 @@ class CastAnalyzer
new Type\Atomic\TLiteralInt(1), new Type\Atomic\TLiteralInt(1),
]); ]);
if ($statements_analyzer->data_flow_graph if ($statements_analyzer->data_flow_graph instanceof \Psalm\Internal\Codebase\VariableUseGraph
&& $statements_analyzer->data_flow_graph instanceof \Psalm\Internal\Codebase\VariableUseGraph
) { ) {
$type->parent_nodes = $maybe_type->parent_nodes; $type->parent_nodes = $maybe_type->parent_nodes;
} }
@ -79,8 +78,7 @@ class CastAnalyzer
if ($as_int) { if ($as_int) {
$type = $valid_int_type ?? Type::getInt(); $type = $valid_int_type ?? Type::getInt();
if ($statements_analyzer->data_flow_graph if ($statements_analyzer->data_flow_graph instanceof \Psalm\Internal\Codebase\VariableUseGraph
&& $statements_analyzer->data_flow_graph instanceof \Psalm\Internal\Codebase\VariableUseGraph
) { ) {
$type->parent_nodes = $maybe_type ? $maybe_type->parent_nodes : []; $type->parent_nodes = $maybe_type ? $maybe_type->parent_nodes : [];
} }
@ -106,8 +104,7 @@ class CastAnalyzer
$type = Type::getFloat(); $type = Type::getFloat();
if ($statements_analyzer->data_flow_graph if ($statements_analyzer->data_flow_graph instanceof \Psalm\Internal\Codebase\VariableUseGraph
&& $statements_analyzer->data_flow_graph instanceof \Psalm\Internal\Codebase\VariableUseGraph
) { ) {
$type->parent_nodes = $maybe_type ? $maybe_type->parent_nodes : []; $type->parent_nodes = $maybe_type ? $maybe_type->parent_nodes : [];
} }
@ -132,8 +129,7 @@ class CastAnalyzer
$type = Type::getBool(); $type = Type::getBool();
if ($statements_analyzer->data_flow_graph if ($statements_analyzer->data_flow_graph instanceof \Psalm\Internal\Codebase\VariableUseGraph
&& $statements_analyzer->data_flow_graph instanceof \Psalm\Internal\Codebase\VariableUseGraph
) { ) {
$type->parent_nodes = $maybe_type ? $maybe_type->parent_nodes : []; $type->parent_nodes = $maybe_type ? $maybe_type->parent_nodes : [];
} }
@ -183,8 +179,7 @@ class CastAnalyzer
$maybe_type = $statements_analyzer->node_data->getType($stmt->expr); $maybe_type = $statements_analyzer->node_data->getType($stmt->expr);
if ($statements_analyzer->data_flow_graph if ($statements_analyzer->data_flow_graph instanceof \Psalm\Internal\Codebase\VariableUseGraph
&& $statements_analyzer->data_flow_graph instanceof \Psalm\Internal\Codebase\VariableUseGraph
) { ) {
$type->parent_nodes = $maybe_type ? $maybe_type->parent_nodes : []; $type->parent_nodes = $maybe_type ? $maybe_type->parent_nodes : [];
} }
@ -237,8 +232,7 @@ class CastAnalyzer
$type = Type::getArray(); $type = Type::getArray();
} }
if ($statements_analyzer->data_flow_graph if ($statements_analyzer->data_flow_graph instanceof \Psalm\Internal\Codebase\VariableUseGraph
&& $statements_analyzer->data_flow_graph instanceof \Psalm\Internal\Codebase\VariableUseGraph
) { ) {
$type->parent_nodes = $stmt_expr_type ? $stmt_expr_type->parent_nodes : []; $type->parent_nodes = $stmt_expr_type ? $stmt_expr_type->parent_nodes : [];
} }

View File

@ -450,7 +450,7 @@ class ParseTreeCreator
return; return;
} }
if ($current_parent && $current_parent instanceof ParseTree\KeyedArrayPropertyTree) { if ($current_parent instanceof ParseTree\KeyedArrayPropertyTree) {
return; return;
} }
@ -462,7 +462,7 @@ class ParseTreeCreator
$current_parent = $this->current_leaf->parent; $current_parent = $this->current_leaf->parent;
} }
if ($current_parent && $current_parent instanceof ParseTree\ConditionalTree) { if ($current_parent instanceof ParseTree\ConditionalTree) {
if (count($current_parent->children) > 1) { if (count($current_parent->children) > 1) {
throw new TypeParseTreeException('Cannot process colon in conditional twice'); throw new TypeParseTreeException('Cannot process colon in conditional twice');
} }
@ -608,12 +608,12 @@ class ParseTreeCreator
throw new TypeParseTreeException('Unexpected token |'); throw new TypeParseTreeException('Unexpected token |');
} }
if ($current_parent && $current_parent instanceof ParseTree\UnionTree) { if ($current_parent instanceof ParseTree\UnionTree) {
$this->current_leaf = $current_parent; $this->current_leaf = $current_parent;
return; return;
} }
if ($current_parent && $current_parent instanceof ParseTree\IntersectionTree) { if ($current_parent instanceof ParseTree\IntersectionTree) {
$this->current_leaf = $current_parent; $this->current_leaf = $current_parent;
$current_parent = $this->current_leaf->parent; $current_parent = $this->current_leaf->parent;
} }
@ -649,12 +649,12 @@ class ParseTreeCreator
$current_parent = $this->current_leaf->parent; $current_parent = $this->current_leaf->parent;
if ($current_parent && $current_parent instanceof ParseTree\MethodTree) { if ($current_parent instanceof ParseTree\MethodTree) {
$this->createMethodParam($this->type_tokens[$this->t], $current_parent); $this->createMethodParam($this->type_tokens[$this->t], $current_parent);
return; return;
} }
if ($current_parent && $current_parent instanceof ParseTree\IntersectionTree) { if ($current_parent instanceof ParseTree\IntersectionTree) {
$this->current_leaf = $current_parent; $this->current_leaf = $current_parent;
return; return;
} }