Skip to content

Fix missing detection of dead code in expressions#4090

Merged
ondrejmirtes merged 28 commits intophpstan:2.1.xfrom
staabm:bug13232
Jul 21, 2025
Merged

Fix missing detection of dead code in expressions#4090
ondrejmirtes merged 28 commits intophpstan:2.1.xfrom
staabm:bug13232

Conversation

@staabm
Copy link
Copy Markdown
Contributor

@staabm staabm commented Jul 11, 2025

@staabm
Copy link
Copy Markdown
Contributor Author

staabm commented Jul 11, 2025

hmm I wonder whether I should add this $isAlwaysTerminating = $isAlwaysTerminating || $result->isAlwaysTerminating(); handling everywhere, where we have

			$hasYield = $hasYield || $result->hasYield();
			$throwPoints = array_merge($throwPoints, $result->getThrowPoints());
			$impurePoints = array_merge($impurePoints, $result->getImpurePoints());

etc...?

edit: I added it in all places where I could think of a example which I was able to test

@staabm
Copy link
Copy Markdown
Contributor Author

staabm commented Jul 12, 2025

I think the new behaviour makes sense but the errors are unfortunate

https://github.com/laravel/framework/blob/43993ed92af54aa8620d8e779a7dcd658f44364c/types/Support/Helpers.php#L44-L60

------ ------------------------------------------------------- 
  Line   Support/Helpers.php                                    
 ------ ------------------------------------------------------- 
  47     Unreachable statement - code above always terminates.  
         🪪  deadCode.unreachable                               
  66     Unreachable statement - code above always terminates.  
         🪪  deadCode.unreachable                               
 ------ ------------------------------------------------------- 

@staabm staabm marked this pull request as ready for review July 12, 2025 06:26
@phpstan-bot
Copy link
Copy Markdown
Collaborator

This pull request has been marked as ready for review.

Copy link
Copy Markdown
Member

@ondrejmirtes ondrejmirtes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is StatementResultTest that tests "is always terminating". Please add a similar test for ExpressionResult. Thank you.

Comment thread src/Analyser/ExpressionResult.php Outdated
Comment thread src/Analyser/NodeScopeResolver.php Outdated
Comment thread src/Analyser/NodeScopeResolver.php
Comment thread src/Analyser/NodeScopeResolver.php
Comment thread src/Analyser/NodeScopeResolver.php
Comment thread src/Analyser/NodeScopeResolver.php Outdated
Comment thread src/Analyser/NodeScopeResolver.php
@staabm
Copy link
Copy Markdown
Contributor Author

staabm commented Jul 17, 2025

I think this should be ready to land.

thanks for the feedback

Comment thread tests/PHPStan/Analyser/ExpressionResultTest.php Outdated
Comment thread tests/PHPStan/Analyser/ExpressionResultTest.php
Comment thread tests/PHPStan/Analyser/ExpressionResultTest.php
Comment thread tests/PHPStan/Analyser/ExpressionResultTest.php
Comment thread src/Analyser/NodeScopeResolver.php Outdated
$leftMergedWithRightScope,
$leftResult->hasYield() || $rightResult->hasYield(),
false,
$leftResult->isAlwaysTerminating() || $rightResult->isAlwaysTerminating(),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The right side might never be executed so this should only mention the left side.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$x && exit(); should be false

Copy link
Copy Markdown
Contributor Author

@staabm staabm Jul 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is false, see https://github.com/phpstan/phpstan-src/pull/4090/files#diff-fa85cdb8d6b73f343f042bbbe433ecc95119bf3dc5e76cc7291563d1c49cbd0dR87

I guess you was looking at an outdated version of the PR while commenting

@ondrejmirtes
Copy link
Copy Markdown
Member

I swear GitHub has some kind of caching problem with the new "Files changed" UI. Sorry.

@ondrejmirtes ondrejmirtes merged commit 89d0610 into phpstan:2.1.x Jul 21, 2025
413 of 419 checks passed
@ondrejmirtes
Copy link
Copy Markdown
Member

Thank you!

@staabm staabm deleted the bug13232 branch July 21, 2025 14:11
phpstan-bot added a commit to phpstan-bot/phpstan-src that referenced this pull request Apr 12, 2026
- #6663: nsrt test for type narrowing in nested if with || condition
- phpstan#4090: nsrt + rule test verifying no "trim expects string, string|false
  given" error when array count is checked via elseif/switch

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
staabm pushed a commit to phpstan-bot/phpstan-src that referenced this pull request Apr 13, 2026
- #6663: nsrt test for type narrowing in nested if with || condition
- phpstan#4090: nsrt + rule test verifying no "trim expects string, string|false
  given" error when array count is checked via elseif/switch

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

never return not detected never return type should lead to error when used

3 participants