Closed
Description
Description
The following code:
<?php
$fp = fopen("/tmp/lock.txt", "r+");
if (!flock($fp, LOCK_EX)) {
echo "Couldn't get the lock!";
} else {
fwrite($fp, "Write something here\n");
flock($fp, LOCK_UN);
}
fclose($fp);
Resulted in this output:
Warning: The return value of function flock() should either be used or intentionally ignored by casting it as (void), as locking the stream might have failed
But I expected this output instead:
No error
While flock()
for LOCK_EX
/LOCK_SH
operations should definitely be checked for safety, the addition of the #[NoDiscard]
attribute there is a bit unfortunate as the LOCK_UN
operation doesn't really require checking.
symfony/symfony#60128 is a first symptom of this problem, and I am afraid more will come through. As many projects turn warnings into exceptions this is in practice very close to a BC break.
The new attribute makes sense for cases where not using the return value is definitely going to cause a bug, but for flock it's currently more a strictness check than catching bugs.
PHP Version
8.5
Operating System
No response