Skip to content

Commit 41fe6da

Browse files
Add union types
1 parent 3348900 commit 41fe6da

File tree

2 files changed

+24
-39
lines changed

2 files changed

+24
-39
lines changed

Filesystem.php

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,9 @@ public function copy(string $originFile, string $targetFile, bool $overwriteNewe
8282
/**
8383
* Creates a directory recursively.
8484
*
85-
* @param string|iterable $dirs The directory path
86-
*
8785
* @throws IOException On any directory creation failure
8886
*/
89-
public function mkdir($dirs, int $mode = 0777)
87+
public function mkdir(string|iterable $dirs, int $mode = 0777)
9088
{
9189
foreach ($this->toIterable($dirs) as $dir) {
9290
if (is_dir($dir)) {
@@ -102,11 +100,9 @@ public function mkdir($dirs, int $mode = 0777)
102100
/**
103101
* Checks the existence of files or directories.
104102
*
105-
* @param string|iterable $files A filename, an array of files, or a \Traversable instance to check
106-
*
107103
* @return bool true if the file exists, false otherwise
108104
*/
109-
public function exists($files)
105+
public function exists(string|iterable $files)
110106
{
111107
$maxPathLength = \PHP_MAXPATHLEN - 2;
112108

@@ -126,13 +122,12 @@ public function exists($files)
126122
/**
127123
* Sets access and modification time of file.
128124
*
129-
* @param string|iterable $files A filename, an array of files, or a \Traversable instance to create
130-
* @param int|null $time The touch time as a Unix timestamp, if not supplied the current system time is used
131-
* @param int|null $atime The access time as a Unix timestamp, if not supplied the current system time is used
125+
* @param int|null $time The touch time as a Unix timestamp, if not supplied the current system time is used
126+
* @param int|null $atime The access time as a Unix timestamp, if not supplied the current system time is used
132127
*
133128
* @throws IOException When touch fails
134129
*/
135-
public function touch($files, int $time = null, int $atime = null)
130+
public function touch(string|iterable $files, int $time = null, int $atime = null)
136131
{
137132
foreach ($this->toIterable($files) as $file) {
138133
if (!($time ? self::box('touch', $file, $time, $atime) : self::box('touch', $file))) {
@@ -144,11 +139,9 @@ public function touch($files, int $time = null, int $atime = null)
144139
/**
145140
* Removes files or directories.
146141
*
147-
* @param string|iterable $files A filename, an array of files, or a \Traversable instance to remove
148-
*
149142
* @throws IOException When removal fails
150143
*/
151-
public function remove($files)
144+
public function remove(string|iterable $files)
152145
{
153146
if ($files instanceof \Traversable) {
154147
$files = iterator_to_array($files, false);
@@ -208,14 +201,13 @@ private static function doRemove(array $files, bool $isRecursive): void
208201
/**
209202
* Change mode for an array of files or directories.
210203
*
211-
* @param string|iterable $files A filename, an array of files, or a \Traversable instance to change mode
212-
* @param int $mode The new mode (octal)
213-
* @param int $umask The mode mask (octal)
214-
* @param bool $recursive Whether change the mod recursively or not
204+
* @param int $mode The new mode (octal)
205+
* @param int $umask The mode mask (octal)
206+
* @param bool $recursive Whether change the mod recursively or not
215207
*
216208
* @throws IOException When the change fails
217209
*/
218-
public function chmod($files, int $mode, int $umask = 0000, bool $recursive = false)
210+
public function chmod(string|iterable $files, int $mode, int $umask = 0000, bool $recursive = false)
219211
{
220212
foreach ($this->toIterable($files) as $file) {
221213
if (\is_int($mode) && !self::box('chmod', $file, $mode & ~$umask)) {
@@ -230,13 +222,12 @@ public function chmod($files, int $mode, int $umask = 0000, bool $recursive = fa
230222
/**
231223
* Change the owner of an array of files or directories.
232224
*
233-
* @param string|iterable $files A filename, an array of files, or a \Traversable instance to change owner
234-
* @param string|int $user A user name or number
235-
* @param bool $recursive Whether change the owner recursively or not
225+
* @param string|int $user A user name or number
226+
* @param bool $recursive Whether change the owner recursively or not
236227
*
237228
* @throws IOException When the change fails
238229
*/
239-
public function chown($files, $user, bool $recursive = false)
230+
public function chown(string|iterable $files, string|int $user, bool $recursive = false)
240231
{
241232
foreach ($this->toIterable($files) as $file) {
242233
if ($recursive && is_dir($file) && !is_link($file)) {
@@ -257,13 +248,12 @@ public function chown($files, $user, bool $recursive = false)
257248
/**
258249
* Change the group of an array of files or directories.
259250
*
260-
* @param string|iterable $files A filename, an array of files, or a \Traversable instance to change group
261-
* @param string|int $group A group name or number
262-
* @param bool $recursive Whether change the group recursively or not
251+
* @param string|int $group A group name or number
252+
* @param bool $recursive Whether change the group recursively or not
263253
*
264254
* @throws IOException When the change fails
265255
*/
266-
public function chgrp($files, $group, bool $recursive = false)
256+
public function chgrp(string|iterable $files, string|int $group, bool $recursive = false)
267257
{
268258
foreach ($this->toIterable($files) as $file) {
269259
if ($recursive && is_dir($file) && !is_link($file)) {
@@ -362,7 +352,7 @@ public function symlink(string $originDir, string $targetDir, bool $copyOnWindow
362352
* @throws FileNotFoundException When original file is missing or not a file
363353
* @throws IOException When link fails, including if link already exists
364354
*/
365-
public function hardlink(string $originFile, $targetFiles)
355+
public function hardlink(string $originFile, string|iterable $targetFiles)
366356
{
367357
if (!$this->exists($originFile)) {
368358
throw new FileNotFoundException(null, 0, null, $originFile);
@@ -712,9 +702,9 @@ public function appendToFile(string $filename, $content)
712702
}
713703
}
714704

715-
private function toIterable($files): iterable
705+
private function toIterable(string|iterable $files): iterable
716706
{
717-
return \is_array($files) || $files instanceof \Traversable ? $files : [$files];
707+
return \is_string($files) ? [$files] : $files;
718708
}
719709

720710
/**
@@ -727,12 +717,7 @@ private function getSchemeAndHierarchy(string $filename): array
727717
return 2 === \count($components) ? [$components[0], $components[1]] : [null, $components[0]];
728718
}
729719

730-
/**
731-
* @param mixed ...$args
732-
*
733-
* @return mixed
734-
*/
735-
private static function box(callable $func, ...$args)
720+
private static function box(callable $func, mixed ...$args): mixed
736721
{
737722
self::$lastError = null;
738723
set_error_handler(__CLASS__.'::handleError');
@@ -751,7 +736,7 @@ private static function box(callable $func, ...$args)
751736
/**
752737
* @internal
753738
*/
754-
public static function handleError($type, $msg)
739+
public static function handleError(int $type, string $msg)
755740
{
756741
self::$lastError = $msg;
757742
}

Tests/Fixtures/MockStream/MockStream.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,18 @@ class MockStream
2626
* @param string $opened_path If the path is opened successfully, and STREAM_USE_PATH is set in options,
2727
* opened_path should be set to the full path of the file/resource that was actually opened
2828
*/
29-
public function stream_open($path, $mode, $options, &$opened_path): bool
29+
public function stream_open(string $path, string $mode, int $options, string &$opened_path = null): bool
3030
{
3131
return true;
3232
}
3333

3434
/**
3535
* @param string $path The file path or URL to stat
36-
* @param array $flags Holds additional flags set by the streams API
36+
* @param int $flags Holds additional flags set by the streams API
3737
*
3838
* @return array File stats
3939
*/
40-
public function url_stat($path, $flags): array
40+
public function url_stat(string $path, int $flags): array
4141
{
4242
return [];
4343
}

0 commit comments

Comments
 (0)