phan / phan/phan

A reached statement is declared unreachable despite `break 2;`

Open
#4,411 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.