Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mark file resource functions as having side effects
Fixes phpstan/phpstan#5461
- Loading branch information
Showing
3 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace FileResourceFunctions; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
class Bug5461 | ||
{ | ||
|
||
public function run(): void | ||
{ | ||
$fh = fopen('foo.txt', 'r'); | ||
if ($fh === false) { | ||
throw new \InvalidArgumentException('Could not open file'); | ||
} | ||
|
||
while (!feof($fh)) { | ||
$data = (string)fread($fh, 1024); | ||
assertType('bool', feof($fh)); | ||
} | ||
|
||
fclose($fh); | ||
} | ||
|
||
} | ||
|
||
class MoreTests | ||
{ | ||
|
||
/** | ||
* @param resource $fh | ||
*/ | ||
public function test1($fh): void | ||
{ | ||
if (fread($fh, 4) === 'data') { | ||
assertType('string|false', fread($fh, 4)); | ||
} | ||
} | ||
|
||
public function test2(): void | ||
{ | ||
if (fopen('foo.txt', 'r') === false) { | ||
assertType('resource|false', fopen('foo.txt', 'r')); | ||
} | ||
} | ||
|
||
/** | ||
* @param resource $fh | ||
*/ | ||
public function test3($fh): void | ||
{ | ||
if (ftell($fh) === 0) { | ||
fseek($fh, 10); | ||
assertType('int|false', ftell($fh)); | ||
} | ||
} | ||
|
||
} |