A reached statement is declared unreachable despite `break 2;`
Nobody has claimed this yet.
- Dominant language
- PHP
- Stars
- 5.6k
- Forks
- 365
- Avg merge
- 1h 25m
- Merged PRs (30d)
- 6
Description
Phan 4.0.6
php-ast version 1.0.11
PHP version used to run Phan: 7.4.16
Running the following code:
<?php declare(strict_types=1);
$text = 'Some "text"';
$output = '';
$textLen = strlen($text);
$startPos = 0;
$spacePos = strpos( $text, ' ', $startPos );
$quotePos = strpos( $text, '"', $startPos );
$pos = ( $spacePos === false && $quotePos === false )
? false
: min(
$spacePos === false ? $textLen : $spacePos
, $quotePos === false ? $textLen : $quotePos
);
while (true)
{
if ($pos === false)
{
trigger_error('bad',E_USER_ERROR);
}
switch ($text[$pos])
{
case ' ':
if ( $pos == $textLen -1 )
{
trigger_error('bad',E_USER_ERROR);
}
$output .= substr( $text, $startPos, $pos-$startPos) . $text[$pos];
$startPos = $pos + 2;
break;
case '"':
$output .= substr( $text, 0, $pos );
break 2;
default:
trigger_error('unexpected',E_USER_ERROR);
}
$spacePos = strpos( $text, ' ', $startPos );
$quotePos = strpos( $text, '"', $startPos );
$pos = ( $spacePos === false && $quotePos === false )
? false
: min(
$spacePos === false ? $textLen : $spacePos
, $quotePos === false ? $textLen : $quotePos
);
}
echo $output . PHP_EOL;
will output Some Some "text via the statement on last line (47). On running this through phan, it reports:
phan-test.php:47 PhanPluginUnreachableCode Unreachable statement detected
This is incorrect. phan seems to be failing to properly account for the break 2, which exits the while loop and makes the last line reachable.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running Phan 4.0.6 or the latest release on the PHP reproduction in this issue and confirm the unreachable-code report on the final echo statement. Trace the control-flow handling for the switch's break 2, which should exit the surrounding while loop. Done means Phan no longer reports that reached statement as unreachable while preserving valid unreachable-code diagnostics.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100