From c11b32d41412a99817dde6ac15a8a2829d245109 Mon Sep 17 00:00:00 2001 From: orklah Date: Sun, 26 Sep 2021 22:25:15 +0200 Subject: [PATCH] redundant variable check --- .../Statements/Block/ForeachAnalyzer.php | 2 +- .../Analyzer/Statements/BreakAnalyzer.php | 5 +---- .../Analyzer/Statements/ContinueAnalyzer.php | 5 +---- .../Expression/AssignmentAnalyzer.php | 5 ++--- .../Expression/Call/ArgumentsAnalyzer.php | 5 ++--- .../Call/NamedFunctionCallHandler.php | 4 +--- .../Statements/Expression/CastAnalyzer.php | 18 ++++++------------ src/Psalm/Internal/Type/ParseTreeCreator.php | 12 ++++++------ 8 files changed, 20 insertions(+), 36 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/Statements/Block/ForeachAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Block/ForeachAnalyzer.php index 124381ebd..713de3d8d 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Block/ForeachAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Block/ForeachAnalyzer.php @@ -250,7 +250,7 @@ class ForeachAnalyzer $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(); AssignmentAnalyzer::analyze( diff --git a/src/Psalm/Internal/Analyzer/Statements/BreakAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/BreakAnalyzer.php index 97c2246fb..2295dff5c 100644 --- a/src/Psalm/Internal/Analyzer/Statements/BreakAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/BreakAnalyzer.php @@ -21,10 +21,7 @@ class BreakAnalyzer if ($loop_scope) { if ($context->break_types && \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; } else { diff --git a/src/Psalm/Internal/Analyzer/Statements/ContinueAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/ContinueAnalyzer.php index 49759c6a4..f13daeec0 100644 --- a/src/Psalm/Internal/Analyzer/Statements/ContinueAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/ContinueAnalyzer.php @@ -17,10 +17,7 @@ class ContinueAnalyzer PhpParser\Node\Stmt\Continue_ $stmt, Context $context ): void { - $count = $stmt->num - && $stmt->num instanceof PhpParser\Node\Scalar\LNumber - ? $stmt->num->value - : 1; + $count = $stmt->num instanceof PhpParser\Node\Scalar\LNumber? $stmt->num->value : 1; $loop_scope = $context->loop_scope; diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/AssignmentAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/AssignmentAnalyzer.php index 8296219dc..aed3c70c5 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/AssignmentAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/AssignmentAnalyzer.php @@ -1349,9 +1349,8 @@ class AssignmentAnalyzer $can_be_empty = !$assign_value_atomic_type instanceof Type\Atomic\TNonEmptyList; } elseif ($assign_value_atomic_type instanceof Type\Atomic\TKeyedArray) { - if ($assign_var_item->key - && ($assign_var_item->key instanceof PhpParser\Node\Scalar\String_ - || $assign_var_item->key instanceof PhpParser\Node\Scalar\LNumber) + if (($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 = diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArgumentsAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArgumentsAnalyzer.php index 5cfba7e5e..998b6882e 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArgumentsAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArgumentsAnalyzer.php @@ -70,8 +70,7 @@ class ArgumentsAnalyzer : null; // if this modifies the array type based on further args - if ($method_id - && in_array($method_id, ['array_push', 'array_unshift'], true) + if (in_array($method_id, ['array_push', 'array_unshift'], true) && $function_params && isset($args[0]) && isset($args[1]) @@ -89,7 +88,7 @@ class ArgumentsAnalyzer 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) { return false; } diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/NamedFunctionCallHandler.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/NamedFunctionCallHandler.php index 2c687821c..a55dd9a3d 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/NamedFunctionCallHandler.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/NamedFunctionCallHandler.php @@ -223,9 +223,7 @@ class NamedFunctionCallHandler if ($function_id === 'func_get_args') { $source = $statements_analyzer->getSource(); - if ($statements_analyzer->data_flow_graph - && $source instanceof \Psalm\Internal\Analyzer\FunctionLikeAnalyzer - ) { + if ($source instanceof \Psalm\Internal\Analyzer\FunctionLikeAnalyzer) { if ($statements_analyzer->data_flow_graph instanceof \Psalm\Internal\Codebase\VariableUseGraph) { foreach ($source->param_nodes as $param_node) { $statements_analyzer->data_flow_graph->addPath( diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/CastAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/CastAnalyzer.php index 81c2f902c..56e1ea91e 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/CastAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/CastAnalyzer.php @@ -66,8 +66,7 @@ class CastAnalyzer new Type\Atomic\TLiteralInt(1), ]); - if ($statements_analyzer->data_flow_graph - && $statements_analyzer->data_flow_graph instanceof \Psalm\Internal\Codebase\VariableUseGraph + if ($statements_analyzer->data_flow_graph instanceof \Psalm\Internal\Codebase\VariableUseGraph ) { $type->parent_nodes = $maybe_type->parent_nodes; } @@ -79,8 +78,7 @@ class CastAnalyzer if ($as_int) { $type = $valid_int_type ?? Type::getInt(); - if ($statements_analyzer->data_flow_graph - && $statements_analyzer->data_flow_graph instanceof \Psalm\Internal\Codebase\VariableUseGraph + if ($statements_analyzer->data_flow_graph instanceof \Psalm\Internal\Codebase\VariableUseGraph ) { $type->parent_nodes = $maybe_type ? $maybe_type->parent_nodes : []; } @@ -106,8 +104,7 @@ class CastAnalyzer $type = Type::getFloat(); - if ($statements_analyzer->data_flow_graph - && $statements_analyzer->data_flow_graph instanceof \Psalm\Internal\Codebase\VariableUseGraph + if ($statements_analyzer->data_flow_graph instanceof \Psalm\Internal\Codebase\VariableUseGraph ) { $type->parent_nodes = $maybe_type ? $maybe_type->parent_nodes : []; } @@ -132,8 +129,7 @@ class CastAnalyzer $type = Type::getBool(); - if ($statements_analyzer->data_flow_graph - && $statements_analyzer->data_flow_graph instanceof \Psalm\Internal\Codebase\VariableUseGraph + if ($statements_analyzer->data_flow_graph instanceof \Psalm\Internal\Codebase\VariableUseGraph ) { $type->parent_nodes = $maybe_type ? $maybe_type->parent_nodes : []; } @@ -183,8 +179,7 @@ class CastAnalyzer $maybe_type = $statements_analyzer->node_data->getType($stmt->expr); - if ($statements_analyzer->data_flow_graph - && $statements_analyzer->data_flow_graph instanceof \Psalm\Internal\Codebase\VariableUseGraph + if ($statements_analyzer->data_flow_graph instanceof \Psalm\Internal\Codebase\VariableUseGraph ) { $type->parent_nodes = $maybe_type ? $maybe_type->parent_nodes : []; } @@ -237,8 +232,7 @@ class CastAnalyzer $type = Type::getArray(); } - if ($statements_analyzer->data_flow_graph - && $statements_analyzer->data_flow_graph instanceof \Psalm\Internal\Codebase\VariableUseGraph + if ($statements_analyzer->data_flow_graph instanceof \Psalm\Internal\Codebase\VariableUseGraph ) { $type->parent_nodes = $stmt_expr_type ? $stmt_expr_type->parent_nodes : []; } diff --git a/src/Psalm/Internal/Type/ParseTreeCreator.php b/src/Psalm/Internal/Type/ParseTreeCreator.php index 4c70e4ff3..eb0e680a7 100644 --- a/src/Psalm/Internal/Type/ParseTreeCreator.php +++ b/src/Psalm/Internal/Type/ParseTreeCreator.php @@ -450,7 +450,7 @@ class ParseTreeCreator return; } - if ($current_parent && $current_parent instanceof ParseTree\KeyedArrayPropertyTree) { + if ($current_parent instanceof ParseTree\KeyedArrayPropertyTree) { return; } @@ -462,7 +462,7 @@ class ParseTreeCreator $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) { throw new TypeParseTreeException('Cannot process colon in conditional twice'); } @@ -608,12 +608,12 @@ class ParseTreeCreator throw new TypeParseTreeException('Unexpected token |'); } - if ($current_parent && $current_parent instanceof ParseTree\UnionTree) { + if ($current_parent instanceof ParseTree\UnionTree) { $this->current_leaf = $current_parent; return; } - if ($current_parent && $current_parent instanceof ParseTree\IntersectionTree) { + if ($current_parent instanceof ParseTree\IntersectionTree) { $this->current_leaf = $current_parent; $current_parent = $this->current_leaf->parent; } @@ -649,12 +649,12 @@ class ParseTreeCreator $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); return; } - if ($current_parent && $current_parent instanceof ParseTree\IntersectionTree) { + if ($current_parent instanceof ParseTree\IntersectionTree) { $this->current_leaf = $current_parent; return; }