diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArgumentAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArgumentAnalyzer.php index 4b3c69d83..501395894 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArgumentAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArgumentAnalyzer.php @@ -140,7 +140,7 @@ class ArgumentAnalyzer && !$arg->value instanceof PhpParser\Node\Expr\ConstFetch ) { $values = \preg_split('//u', $arg_value_type->getSingleStringLiteral()->value, -1, \PREG_SPLIT_NO_EMPTY); - + if ($values !== false) { $prev_ord = 0; @@ -940,6 +940,7 @@ class ArgumentAnalyzer || $context->inside_use || $context->inside_assignment || $context->inside_conditional + || $context->inside_throw ); } } 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 1e701134e..f26b87977 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/AtomicMethodCallAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/AtomicMethodCallAnalyzer.php @@ -207,6 +207,7 @@ class AtomicMethodCallAnalyzer extends CallAnalyzer || $context->inside_use || $context->inside_assignment || $context->inside_conditional + || $context->inside_throw ); $fake_method_exists = false; @@ -331,6 +332,7 @@ class AtomicMethodCallAnalyzer extends CallAnalyzer || $context->inside_use || $context->inside_assignment || $context->inside_conditional + || $context->inside_throw ) ) { $new_call_context = MissingMethodCallHandler::handleMagicMethod( @@ -737,6 +739,7 @@ class AtomicMethodCallAnalyzer extends CallAnalyzer || $context->inside_use || $context->inside_assignment || $context->inside_conditional + || $context->inside_throw )) { $lhs_type_part = clone $lhs_type_part_new; $class_storage = $mixin_class_storage; @@ -809,6 +812,7 @@ class AtomicMethodCallAnalyzer extends CallAnalyzer || $context->inside_use || $context->inside_assignment || $context->inside_conditional + || $context->inside_throw )) { $mixin_declaring_class_storage = $codebase->classlike_storage_provider->get( $class_storage->mixin_declaring_fqcln 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 dce6017e7..28d3e3f24 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/StaticMethod/AtomicStaticCallAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/StaticMethod/AtomicStaticCallAnalyzer.php @@ -306,6 +306,7 @@ class AtomicStaticCallAnalyzer || $context->inside_use || $context->inside_assignment || $context->inside_conditional + || $context->inside_throw ); $fake_method_exists = false; @@ -352,6 +353,7 @@ class AtomicStaticCallAnalyzer || $context->inside_use || $context->inside_assignment || $context->inside_conditional + || $context->inside_throw )) { $mixin_candidates = []; foreach ($class_storage->templatedMixins as $mixin_candidate) { @@ -476,6 +478,7 @@ class AtomicStaticCallAnalyzer || $context->inside_use || $context->inside_assignment || $context->inside_conditional + || $context->inside_throw )) { 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/Codebase/Methods.php b/src/Psalm/Internal/Codebase/Methods.php index 85759f05f..1db76469f 100644 --- a/src/Psalm/Internal/Codebase/Methods.php +++ b/src/Psalm/Internal/Codebase/Methods.php @@ -81,6 +81,9 @@ class Methods /** * Whether or not a given method exists + * + * If you pass true in $is_used argument the method return is considered used + * * @param lowercase-string|null $calling_method_id */ public function methodExists( diff --git a/tests/UnusedCodeTest.php b/tests/UnusedCodeTest.php index c2c94d17e..b597e0df4 100644 --- a/tests/UnusedCodeTest.php +++ b/tests/UnusedCodeTest.php @@ -1008,6 +1008,26 @@ class UnusedCodeTest extends TestCase f(new Worker()); f(new AnotherWorker());', ], + 'methodReturnValueUsedInThrow' => [ + 'foo(); + ' + ], + 'staticMethodReturnValueUsedInThrow' => [ + '