phpinspectionsea icon indicating copy to clipboard operation
phpinspectionsea copied to clipboard

Inspection "This statement seems to be disconnected from its parent foreach" with objects

Open nickvergessen opened this issue 6 years ago • 6 comments

Subject Details
Plugin Php Inspections (EA Extended) v3.0.16.2
Language level PHP 7.1

Current behaviour

Code:

		foreach ($participants as $participant) {
			$notification->setUser($participant['userId']);
			$this->notificationManager->notify($notification);
		}

Bildschirmfoto von 2019-10-16 15-03-25

Expected behaviour

No warning should be generated because the object used in the function call is modified within the foreach.

nickvergessen avatar Oct 16 '19 13:10 nickvergessen

Thank you for reporting @nickvergessen, I'll check what we can do here.

kalessil avatar Nov 25 '19 09:11 kalessil

For myself: respond to https://github.com/kalessil/phpinspectionsea/issues/173#issuecomment-553714839 as well

kalessil avatar Nov 25 '19 10:11 kalessil

Hi everyone,

Here another example if it could help :

		foreach ($addresses as $address) {
			[
				'_id' => $id,
				'_source' => ['adresse' => $adresse]
			] = $address;

			dump($id, $adresse);
			$this->addressConnector->deleteDocument($id, $this->newIndex);
			file_put_contents($this->deletedLogFile . '-temp', $adresse . "\n", FILE_APPEND);

		}

ben-grandin avatar Apr 27 '21 19:04 ben-grandin

Subject Details
Plugin Php Inspections (EA Extended) v4.0.6.4
Language level PHP 7.4

Here is another example using the Symfony ProgressBar object.

$progressBar = $this->io->createProgressBar();
$progressBar->start();

foreach ($foo as $bar) {
    ...

    //This statement seems to be disconnected from its parent foreach
    $progressBar->advance();
}

colonelclick avatar Aug 31 '21 20:08 colonelclick

is there a way to ignore this in phpstorm for now?

tebeso avatar Oct 16 '22 19:10 tebeso

Use this to ignore the warning: /** @noinspection DisconnectedForeachInstructionInspection */

Example before the line:

$progressBar = $this->io->createProgressBar();
$progressBar->start();

foreach ($foo as $bar) {
    ...

    // To ignore: "This statement seems to be disconnected from its parent foreach"
    /** @noinspection DisconnectedForeachInstructionInspection */
    $progressBar->advance();
}

Example before a method:

class MyHappyClass 
{
    /** @noinspection DisconnectedForeachInstructionInspection */
    public function myHappyMethod(): Happiness
    {
        // Any code to make you happy
    }
}

Example in the file's header:

<?php
/** @noinspection DisconnectedForeachInstructionInspection */

// ... Your code :-)

Hope I was helpful :smile:

kevinoo avatar Nov 17 '22 15:11 kevinoo