Skip to content

Commit 88e5898

Browse files
authored
Bleeding edge - More precise flock() operation flags
1 parent fcf1939 commit 88e5898

3 files changed

Lines changed: 58 additions & 0 deletions

File tree

‎resources/functionMap_bleedingEdge.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
'max' => ['', '...arg1'=>'non-empty-array'],
109109
'min' => ['', '...arg1'=>'non-empty-array'],
110110
'file' => ['list<string>|false', 'filename'=>'string', 'flags='=>'0|FILE_USE_INCLUDE_PATH|FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES', 'context='=>'resource'],
111+
'flock' => ['bool', 'fp'=>'resource', 'operation'=>'int-mask<LOCK_SH, LOCK_EX, LOCK_UN, LOCK_NB>', '&w_wouldblock='=>'int'],
111112
],
112113
'old' => [
113114

‎tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,4 +1378,14 @@ public function testFileParams(): void
13781378
]);
13791379
}
13801380

1381+
public function testFlockParams(): void
1382+
{
1383+
$this->analyse([__DIR__ . '/data/flock.php'], [
1384+
[
1385+
'Parameter #2 $operation of function flock expects int<0, 7>, 8 given.',
1386+
45,
1387+
],
1388+
]);
1389+
}
1390+
13811391
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace flockFlags;
4+
5+
class flock {
6+
/** @param resource $fp */
7+
public function ok($fp):void {
8+
9+
if (flock($fp, LOCK_EX)) { // acquire an exclusive lock
10+
ftruncate($fp, 0); // truncate file
11+
fwrite($fp, "Write something here\n");
12+
fflush($fp); // flush output before releasing the lock
13+
flock($fp, LOCK_UN); // release the lock
14+
} else {
15+
echo "Couldn't get the lock!";
16+
}
17+
18+
fclose($fp);
19+
}
20+
21+
/** @param resource $fp */
22+
public function ok1($fp):void {
23+
24+
/* Activate the LOCK_NB option on an LOCK_EX operation */
25+
if(!flock($fp, LOCK_EX | LOCK_NB)) {
26+
echo 'Unable to obtain lock';
27+
exit(-1);
28+
}
29+
if(!flock($fp, LOCK_SH | LOCK_NB)) {
30+
echo 'Unable to obtain lock';
31+
exit(-1);
32+
}
33+
if(!flock($fp, LOCK_UN | LOCK_NB)) {
34+
echo 'Unable to obtain lock';
35+
exit(-1);
36+
}
37+
38+
/* ... */
39+
40+
fclose($fp);
41+
}
42+
43+
/** @param resource $fp */
44+
public function error($fp):void {
45+
$f = flock($fp, FILE_APPEND);
46+
}
47+
}

0 commit comments

Comments
 (0)