phpinspectionsea
phpinspectionsea copied to clipboard
Inspection "This statement seems to be disconnected from its parent foreach" with objects
| 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);
}

Expected behaviour
No warning should be generated because the object used in the function call is modified within the foreach.
Thank you for reporting @nickvergessen, I'll check what we can do here.
For myself: respond to https://github.com/kalessil/phpinspectionsea/issues/173#issuecomment-553714839 as well
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);
}
| 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();
}
is there a way to ignore this in phpstorm for now?
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: