slevomat / coding-standard

Slevomat Coding Standard for PHP_CodeSniffer provides many useful sniffs
MIT License
1.39k stars 171 forks source link

Ignore unused variable with SplFileObject when setting it to NULL #1616

Open kwebble opened 1 year ago

kwebble commented 1 year ago

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.

kukulich commented 6 months ago

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