coding-standard icon indicating copy to clipboard operation
coding-standard copied to clipboard

Ignore unused variable with SplFileObject when setting it to NULL

Open kwebble opened this issue 1 year ago • 1 comments

To close a file created with SplFileObject, the variable should be set to NULL. For example:

class X {
    public function wrt (
    ): void {
        $sfo = new SplFileObject('junk.txt', FopenMode::WRITE);
        $sfo->fwrite('Some text');

        // Close the file.
        $sfo = NULL;
    }
}

The line with $sfo = NULL; is flagged by SlevomatCodingStandard.Variables.UnusedVariable.UnusedVariable.

It would be nice if the rule could detect this situation and not trigger an error for it.

kwebble avatar Sep 06 '23 21:09 kwebble

Is not possible that unset($sFo) will work too?

kukulich avatar Mar 09 '24 13:03 kukulich