Skip to content

Commit 47277f7

Browse files
committed
Make further use of PHP 8.1 features
1 parent db002ce commit 47277f7

12 files changed

+96
-107
lines changed

src/Driver/BlockingFile.php

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ final class BlockingFile implements File, \IteratorAggregate
1818

1919
/** @var resource|null */
2020
private $handle;
21-
private string $path;
22-
private string $mode;
21+
22+
private int $id;
2323

2424
private readonly DeferredFuture $onClose;
2525

@@ -28,11 +28,13 @@ final class BlockingFile implements File, \IteratorAggregate
2828
* @param string $path File path.
2929
* @param string $mode File open mode.
3030
*/
31-
public function __construct($handle, string $path, string $mode)
32-
{
31+
public function __construct(
32+
$handle,
33+
private readonly string $path,
34+
private readonly string $mode,
35+
) {
3336
$this->handle = $handle;
34-
$this->path = $path;
35-
$this->mode = $mode;
37+
$this->id = (int) $handle;
3638

3739
if ($mode[0] === 'a') {
3840
\fseek($this->handle, 0, \SEEK_END);
@@ -44,7 +46,7 @@ public function __construct($handle, string $path, string $mode)
4446
public function __destruct()
4547
{
4648
if ($this->handle !== null) {
47-
@\fclose($this->handle);
49+
\fclose($this->handle);
4850
}
4951

5052
if (!$this->onClose->isComplete()) {
@@ -59,7 +61,7 @@ public function read(?Cancellation $cancellation = null, int $length = self::DEF
5961
}
6062

6163
try {
62-
\set_error_handler(function ($type, $message) {
64+
\set_error_handler(function (int $type, string $message): never {
6365
throw new StreamException("Failed reading from file '{$this->path}': {$message}");
6466
});
6567

@@ -81,7 +83,7 @@ public function write(string $bytes): void
8183
}
8284

8385
try {
84-
\set_error_handler(function ($type, $message) {
86+
\set_error_handler(function (int $type, string $message): never {
8587
throw new StreamException("Failed writing to file '{$this->path}': {$message}");
8688
});
8789

@@ -117,7 +119,7 @@ public function close(): void
117119
}
118120

119121
try {
120-
\set_error_handler(function ($type, $message) {
122+
\set_error_handler(function (int $type, string $message): never {
121123
throw new StreamException("Failed closing file '{$this->path}': {$message}");
122124
});
123125

@@ -148,7 +150,7 @@ public function truncate(int $size): void
148150
}
149151

150152
try {
151-
\set_error_handler(function ($type, $message) {
153+
\set_error_handler(function (int $type, string $message): never {
152154
throw new StreamException("Could not truncate file '{$this->path}': {$message}");
153155
});
154156

@@ -171,7 +173,7 @@ public function seek(int $position, int $whence = self::SEEK_SET): int
171173
case self::SEEK_CUR:
172174
case self::SEEK_END:
173175
try {
174-
\set_error_handler(function ($type, $message) {
176+
\set_error_handler(function (int $type, string $message): never {
175177
throw new StreamException("Could not seek in file '{$this->path}': {$message}");
176178
});
177179

@@ -230,4 +232,9 @@ public function isWritable(): bool
230232
{
231233
return $this->handle !== null && $this->mode[0] !== 'r';
232234
}
235+
236+
public function getId(): int
237+
{
238+
return $this->id;
239+
}
233240
}

src/Driver/BlockingFilesystemDriver.php

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@
22

33
namespace Amp\File\Driver;
44

5-
use Amp\File\File;
65
use Amp\File\FilesystemDriver;
76
use Amp\File\FilesystemException;
87

98
final class BlockingFilesystemDriver implements FilesystemDriver
109
{
11-
public function openFile(string $path, string $mode): File
10+
private readonly \Closure $errorHandler;
11+
12+
public function __construct()
13+
{
14+
$this->errorHandler = static fn () => true;
15+
}
16+
17+
public function openFile(string $path, string $mode): BlockingFile
1218
{
1319
$mode = \str_replace(['b', 't', 'e'], '', $mode);
1420

@@ -30,7 +36,7 @@ public function openFile(string $path, string $mode): File
3036
}
3137

3238
try {
33-
\set_error_handler(static function ($type, $message) use ($path, $mode) {
39+
\set_error_handler(static function (int $type, string $message) use ($path, $mode): never {
3440
throw new FilesystemException("Failed to open '{$path}' in mode '{$mode}': {$message}");
3541
});
3642

@@ -47,21 +53,31 @@ public function openFile(string $path, string $mode): File
4753
public function getStatus(string $path): ?array
4854
{
4955
\clearstatcache(true, $path);
56+
\set_error_handler($this->errorHandler);
5057

51-
return @\stat($path) ?: null;
58+
try {
59+
return \stat($path) ?: null;
60+
} finally {
61+
\restore_error_handler();
62+
}
5263
}
5364

5465
public function getLinkStatus(string $path): ?array
5566
{
5667
\clearstatcache(true, $path);
68+
\set_error_handler($this->errorHandler);
5769

58-
return @\lstat($path) ?: null;
70+
try {
71+
return \lstat($path) ?: null;
72+
} finally {
73+
\restore_error_handler();
74+
}
5975
}
6076

6177
public function createSymlink(string $target, string $link): void
6278
{
6379
try {
64-
\set_error_handler(static function ($type, $message) use ($target, $link) {
80+
\set_error_handler(static function (int $type, string $message) use ($target, $link): never {
6581
throw new FilesystemException("Could not create symbolic link '{$link}' to '{$target}': {$message}");
6682
});
6783

@@ -76,7 +92,7 @@ public function createSymlink(string $target, string $link): void
7692
public function createHardlink(string $target, string $link): void
7793
{
7894
try {
79-
\set_error_handler(static function ($type, $message) use ($target, $link) {
95+
\set_error_handler(static function (int $type, string $message) use ($target, $link): never {
8096
throw new FilesystemException("Could not create hard link '{$link}' to '{$target}': {$message}");
8197
});
8298

@@ -91,7 +107,7 @@ public function createHardlink(string $target, string $link): void
91107
public function resolveSymlink(string $target): string
92108
{
93109
try {
94-
\set_error_handler(static function ($type, $message) use ($target) {
110+
\set_error_handler(static function (int $type, string $message) use ($target): never {
95111
throw new FilesystemException("Could not resolve symbolic link '{$target}': {$message}");
96112
});
97113

@@ -108,7 +124,7 @@ public function resolveSymlink(string $target): string
108124
public function move(string $from, string $to): void
109125
{
110126
try {
111-
\set_error_handler(static function ($type, $message) use ($from, $to) {
127+
\set_error_handler(static function (int $type, string $message) use ($from, $to): never {
112128
throw new FilesystemException("Could not move file from '{$from}' to '{$to}': {$message}");
113129
});
114130

@@ -123,7 +139,7 @@ public function move(string $from, string $to): void
123139
public function deleteFile(string $path): void
124140
{
125141
try {
126-
\set_error_handler(static function ($type, $message) use ($path) {
142+
\set_error_handler(static function (int $type, string $message) use ($path): never {
127143
throw new FilesystemException("Could not delete file '{$path}': {$message}");
128144
});
129145

@@ -138,7 +154,7 @@ public function deleteFile(string $path): void
138154
public function createDirectory(string $path, int $mode = 0777): void
139155
{
140156
try {
141-
\set_error_handler(static function ($type, $message) use ($path) {
157+
\set_error_handler(static function (int $type, string $message) use ($path): never {
142158
throw new FilesystemException("Could not create directory '{$path}': {$message}");
143159
});
144160

@@ -154,7 +170,7 @@ public function createDirectory(string $path, int $mode = 0777): void
154170
public function createDirectoryRecursively(string $path, int $mode = 0777): void
155171
{
156172
try {
157-
\set_error_handler(static function ($type, $message) use ($path) {
173+
\set_error_handler(static function (int $type, string $message) use ($path): bool {
158174
if (!\is_dir($path)) {
159175
throw new FilesystemException("Could not create directory '{$path}': {$message}");
160176
}
@@ -182,7 +198,7 @@ public function createDirectoryRecursively(string $path, int $mode = 0777): void
182198
public function deleteDirectory(string $path): void
183199
{
184200
try {
185-
\set_error_handler(static function ($type, $message) use ($path) {
201+
\set_error_handler(static function (int $type, string $message) use ($path): never {
186202
throw new FilesystemException("Could not remove directory '{$path}': {$message}");
187203
});
188204

@@ -197,7 +213,7 @@ public function deleteDirectory(string $path): void
197213
public function listFiles(string $path): array
198214
{
199215
try {
200-
\set_error_handler(static function ($type, $message) use ($path) {
216+
\set_error_handler(static function (int $type, string $message) use ($path): never {
201217
throw new FilesystemException("Failed to list files in '{$path}': {$message}");
202218
});
203219

@@ -208,7 +224,7 @@ public function listFiles(string $path): array
208224
if ($arr = \scandir($path)) {
209225
\clearstatcache(true, $path);
210226

211-
return \array_values(\array_filter($arr, static function ($el) {
227+
return \array_values(\array_filter($arr, static function ($el): bool {
212228
return $el !== "." && $el !== "..";
213229
}));
214230
}
@@ -222,7 +238,7 @@ public function listFiles(string $path): array
222238
public function changePermissions(string $path, int $mode): void
223239
{
224240
try {
225-
\set_error_handler(static function ($type, $message) use ($path) {
241+
\set_error_handler(static function (int $type, string $message) use ($path): never {
226242
throw new FilesystemException("Failed to change permissions for '{$path}': {$message}");
227243
});
228244

@@ -237,7 +253,7 @@ public function changePermissions(string $path, int $mode): void
237253
public function changeOwner(string $path, ?int $uid, ?int $gid): void
238254
{
239255
try {
240-
\set_error_handler(static function ($type, $message) use ($path) {
256+
\set_error_handler(static function (int $type, string $message) use ($path): never {
241257
throw new FilesystemException("Failed to change owner for '{$path}': {$message}");
242258
});
243259

@@ -259,7 +275,7 @@ public function changeOwner(string $path, ?int $uid, ?int $gid): void
259275
public function touch(string $path, ?int $modificationTime, ?int $accessTime): void
260276
{
261277
try {
262-
\set_error_handler(static function ($type, $message) use ($path) {
278+
\set_error_handler(static function (int $type, string $message) use ($path): never {
263279
throw new FilesystemException("Failed to touch '{$path}': {$message}");
264280
});
265281

@@ -277,7 +293,7 @@ public function touch(string $path, ?int $modificationTime, ?int $accessTime): v
277293
public function read(string $path): string
278294
{
279295
try {
280-
\set_error_handler(static function ($type, $message) use ($path) {
296+
\set_error_handler(static function (int $type, string $message) use ($path): never {
281297
throw new FilesystemException("Failed to read '{$path}': {$message}");
282298
});
283299

@@ -294,7 +310,7 @@ public function read(string $path): string
294310
public function write(string $path, string $contents): void
295311
{
296312
try {
297-
\set_error_handler(static function ($type, $message) use ($path) {
313+
\set_error_handler(static function (int $type, string $message) use ($path): never {
298314
throw new FilesystemException("Failed to write to '{$path}': {$message}");
299315
});
300316

src/Driver/EioFilesystemDriver.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Amp\File\Driver;
44

55
use Amp\DeferredFuture;
6-
use Amp\File\File;
76
use Amp\File\FilesystemDriver;
87
use Amp\File\FilesystemException;
98
use Amp\File\Internal;
@@ -26,7 +25,7 @@ public function __construct(EventLoopDriver $driver)
2625
$this->poll = new Internal\EioPoll($driver);
2726
}
2827

29-
public function openFile(string $path, string $mode): File
28+
public function openFile(string $path, string $mode): EioFile
3029
{
3130
$flags = \EIO_O_NONBLOCK | $this->parseMode($mode);
3231
if (\defined('\EIO_O_FSYNC')) {

src/Driver/ParallelFile.php

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,37 +23,33 @@ final class ParallelFile implements File, \IteratorAggregate
2323
{
2424
use ReadableStreamIteratorAggregate;
2525

26-
private readonly Internal\FileWorker $worker;
27-
2826
private ?int $id;
2927

30-
private string $path;
31-
3228
private int $position;
3329

3430
private int $size;
3531

36-
private string $mode;
37-
3832
/** @var bool True if an operation is pending. */
3933
private bool $busy = false;
4034

4135
/** @var int Number of pending write operations. */
4236
private int $pendingWrites = 0;
4337

44-
private bool $writable = true;
38+
private bool $writable;
4539

4640
private ?Future $closing = null;
4741

4842
private readonly DeferredFuture $onClose;
4943

50-
public function __construct(Internal\FileWorker $worker, int $id, string $path, int $size, string $mode)
51-
{
52-
$this->worker = $worker;
44+
public function __construct(
45+
private readonly Internal\FileWorker $worker,
46+
int $id,
47+
private readonly string $path,
48+
int $size,
49+
private readonly string $mode,
50+
) {
5351
$this->id = $id;
54-
$this->path = $path;
5552
$this->size = $size;
56-
$this->mode = $mode;
5753
$this->writable = $this->mode[0] !== 'r';
5854
$this->position = $this->mode[0] === 'a' ? $this->size : 0;
5955

@@ -158,7 +154,7 @@ public function read(?Cancellation $cancellation = null, int $length = self::DEF
158154
$this->busy = true;
159155

160156
try {
161-
$data = $this->worker->execute(new Internal\FileTask('fread', [null, $length], $this->id), $cancellation);
157+
$data = $this->worker->execute(new Internal\FileTask('fread', [$length], $this->id), $cancellation);
162158

163159
if ($data !== null) {
164160
$this->position += \strlen($data);

src/Driver/ParallelFilesystemDriver.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Amp\File\Driver;
44

5-
use Amp\File\File;
65
use Amp\File\FilesystemDriver;
76
use Amp\File\FilesystemException;
87
use Amp\File\Internal;
@@ -40,7 +39,7 @@ public function __construct(WorkerPool $pool = null, int $workerLimit = self::DE
4039
$this->pendingWorker = Future::complete();
4140
}
4241

43-
public function openFile(string $path, string $mode): File
42+
public function openFile(string $path, string $mode): ParallelFile
4443
{
4544
$worker = $this->selectWorker();
4645

src/Driver/StatusCachingFilesystemDriver.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Amp\File\Driver;
44

5-
use Amp\File\File;
65
use Amp\File\FilesystemDriver;
76
use Amp\File\Internal\Cache;
87

@@ -18,7 +17,7 @@ public function __construct(FilesystemDriver $driver)
1817
$this->statusCache = new Cache(1000, 1024);
1918
}
2019

21-
public function openFile(string $path, string $mode): File
20+
public function openFile(string $path, string $mode): StatusCachingFile
2221
{
2322
$file = $this->driver->openFile($path, $mode);
2423

0 commit comments

Comments
 (0)