diff --git a/src/Psalm/Context.php b/src/Psalm/Context.php index 0e9cbc4540b..1101b8fe555 100644 --- a/src/Psalm/Context.php +++ b/src/Psalm/Context.php @@ -831,4 +831,15 @@ public function mergeFunctionExceptions( $this->possibly_thrown_exceptions[$possibly_thrown_exception][$hash] = $codelocation; } } + + public function insideUse(): bool + { + return $this->inside_assignment + || $this->inside_return + || $this->inside_call + || $this->inside_general_use + || $this->inside_conditional + || $this->inside_throw + || $this->inside_isset; + } } diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArgumentAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArgumentAnalyzer.php index 10a84fce58e..c43a1025e48 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArgumentAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArgumentAnalyzer.php @@ -935,12 +935,7 @@ public static function verifyType( $statements_analyzer, $statements_analyzer->getFilePath(), true, - $context->inside_return - || $context->inside_call - || $context->inside_general_use - || $context->inside_assignment - || $context->inside_conditional - || $context->inside_throw + $context->insideUse() ); } } diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/FunctionCallAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/FunctionCallAnalyzer.php index d2673653b9d..3e3f6ae4d5a 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/FunctionCallAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/FunctionCallAnalyzer.php @@ -992,11 +992,7 @@ private static function checkFunctionCallPurity( * If a function is pure, and has the return type of 'no-return', * it's okay to dismiss it's return value. */ - if (!$context->inside_assignment - && !$context->inside_call - && !$context->inside_return - && !$context->inside_general_use - && !$context->inside_throw + if (!$context->insideUse() && !self::callUsesByReferenceArguments($function_call_info, $stmt) && !( $function_call_info->function_storage && diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/AtomicMethodCallAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/AtomicMethodCallAnalyzer.php index 5c3464c9bcb..edb83774b58 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/AtomicMethodCallAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/AtomicMethodCallAnalyzer.php @@ -202,12 +202,7 @@ public static function analyze( : null, $statements_analyzer->getFilePath(), false, - $context->inside_return - || $context->inside_call - || $context->inside_general_use - || $context->inside_assignment - || $context->inside_conditional - || $context->inside_throw + $context->insideUse() ); $fake_method_exists = false; @@ -327,12 +322,7 @@ public static function analyze( : null, $statements_analyzer->getFilePath(), true, - $context->inside_return - || $context->inside_call - || $context->inside_general_use - || $context->inside_assignment - || $context->inside_conditional - || $context->inside_throw + $context->insideUse() ) ) { $new_call_context = MissingMethodCallHandler::handleMagicMethod( @@ -734,12 +724,7 @@ private static function handleTemplatedMixins( : null, $statements_analyzer->getFilePath(), true, - $context->inside_return - || $context->inside_call - || $context->inside_general_use - || $context->inside_assignment - || $context->inside_conditional - || $context->inside_throw + $context->insideUse() )) { $lhs_type_part = clone $lhs_type_part_new; $class_storage = $mixin_class_storage; @@ -807,12 +792,7 @@ private static function handleRegularMixins( : null, $statements_analyzer->getFilePath(), true, - $context->inside_return - || $context->inside_call - || $context->inside_general_use - || $context->inside_assignment - || $context->inside_conditional - || $context->inside_throw + $context->insideUse() )) { $mixin_declaring_class_storage = $codebase->classlike_storage_provider->get( $class_storage->mixin_declaring_fqcln diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/MethodCallAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/MethodCallAnalyzer.php index 76a9c48102a..df3657f02c0 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/MethodCallAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/MethodCallAnalyzer.php @@ -44,9 +44,6 @@ public static function analyze( $context->inside_call = true; - $was_inside_general_use = $context->inside_general_use; - $context->inside_general_use = true; - $existing_stmt_var_type = null; if (!$real_method_call) { @@ -70,7 +67,6 @@ public static function analyze( } $context->inside_call = $was_inside_call; - $context->inside_general_use = $was_inside_general_use; if ($stmt->var instanceof PhpParser\Node\Expr\Variable) { if (is_string($stmt->var->name) && $stmt->var->name === 'this' && !$statements_analyzer->getFQCLN()) { diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/StaticMethod/AtomicStaticCallAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/StaticMethod/AtomicStaticCallAnalyzer.php index 4f5f13c1358..18d26cb7cd1 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/StaticMethod/AtomicStaticCallAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/StaticMethod/AtomicStaticCallAnalyzer.php @@ -301,12 +301,7 @@ private static function handleNamedCall( $statements_analyzer, $statements_analyzer->getFilePath(), false, - $context->inside_return - || $context->inside_call - || $context->inside_general_use - || $context->inside_assignment - || $context->inside_conditional - || $context->inside_throw + $context->insideUse() ); $fake_method_exists = false; @@ -348,12 +343,7 @@ private static function handleNamedCall( : null, $statements_analyzer->getFilePath(), true, - $context->inside_return - || $context->inside_call - || $context->inside_general_use - || $context->inside_assignment - || $context->inside_conditional - || $context->inside_throw + $context->insideUse() )) { $mixin_candidates = []; foreach ($class_storage->templatedMixins as $mixin_candidate) { @@ -473,12 +463,7 @@ private static function handleNamedCall( : null, $statements_analyzer->getFilePath(), true, - $context->inside_return - || $context->inside_call - || $context->inside_general_use - || $context->inside_assignment - || $context->inside_conditional - || $context->inside_throw + $context->insideUse() )) { if ($codebase->methods->return_type_provider->has($fq_class_name)) { $return_type_candidate = $codebase->methods->return_type_provider->getReturnType( diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/VariableFetchAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/VariableFetchAnalyzer.php index 058775a0820..c8f57a7bcb9 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/VariableFetchAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/VariableFetchAnalyzer.php @@ -423,12 +423,12 @@ private static function addDataFlowToVariable( if ($statements_analyzer->data_flow_graph && $codebase->find_unused_variables - && ($context->inside_call - || $context->inside_return - || $context->inside_conditional + && ($context->inside_return + || $context->inside_call || $context->inside_general_use - || $context->inside_isset - || $context->inside_throw) + || $context->inside_conditional + || $context->inside_throw + || $context->inside_isset) ) { if (!$stmt_type->parent_nodes) { $assignment_node = DataFlowNode::getForAssignment(