Phpinspectionsea: [false-positive] "This statement seems to be disconnected from its parent foreach."

Created on 11 Apr 2018  路  3Comments  路  Source: kalessil/phpinspectionsea

Quite related to #621 , using the progress bar in the Symfony framework results in the warning "This statement seems to be disconnected from its parent foreach."

(Dummy) Example:

    $progressBar = new ProgressBar($output);
    $progressBar->start();

    foreach ($rows as $row) {
        $this->addRowToCSV($row);
        $progressBar->advance(); // Inspection warning here
    }
    $progressBar->finish();
bug / false-positive wontfix

Most helpful comment

Or introducing a link to the loop scope:

    foreach ($rows as $index => $row) {
        $this->addRowToCSV($row);
        $progressBar->setProgress($index);
    }

All 3 comments

Thank you for reporting @excentrik. Unfortunately, I don't a solution for this case, so a suppression is recommended here.

In cases like that, if you prefer avoid warning supressions, you could refactor your looping and give more meaning to what happen.

public function start() {
    $progressBar = new ProgressBar($output);
    $progressBar->start();

    foreach ($rows as $row) {
        $this->advanceWithRow($progressBar, $row);
    }

    $progressBar->finish();
}

private function advanceWithRow(ProgressBar $progressBar, $row) {
    $this->addRowToCSV($row);
    $progressBar->advance();
}

Or introducing a link to the loop scope:

    foreach ($rows as $index => $row) {
        $this->addRowToCSV($row);
        $progressBar->setProgress($index);
    }
Was this page helpful?
0 / 5 - 0 ratings