From 4ec5d447d100d9c66c0d8b2e8ee5e4bd289efc9d Mon Sep 17 00:00:00 2001 From: Roland Franssen Date: Sat, 11 Nov 2017 20:29:06 +0100 Subject: [PATCH] [Filesystem] Replace remaing docblocks by type-hints + simplify toIterator() --- .../Component/Filesystem/Filesystem.php | 74 +++++++------------ 1 file changed, 28 insertions(+), 46 deletions(-) diff --git a/src/Symfony/Component/Filesystem/Filesystem.php b/src/Symfony/Component/Filesystem/Filesystem.php index d8cf9ddba5aba..c9667531779ac 100644 --- a/src/Symfony/Component/Filesystem/Filesystem.php +++ b/src/Symfony/Component/Filesystem/Filesystem.php @@ -84,14 +84,14 @@ public function copy($originFile, $targetFile, $overwriteNewerFiles = false) /** * Creates a directory recursively. * - * @param string|array|\Traversable $dirs The directory path - * @param int $mode The directory mode + * @param string|iterable $dirs The directory path + * @param int $mode The directory mode * * @throws IOException On any directory creation failure */ public function mkdir($dirs, $mode = 0777) { - foreach ($this->toIterator($dirs) as $dir) { + foreach ($this->toIterable($dirs) as $dir) { if (is_dir($dir)) { continue; } @@ -112,7 +112,7 @@ public function mkdir($dirs, $mode = 0777) /** * Checks the existence of files or directories. * - * @param string|array|\Traversable $files A filename, an array of files, or a \Traversable instance to check + * @param string|iterable $files A filename, an array of files, or a \Traversable instance to check * * @return bool true if the file exists, false otherwise */ @@ -120,7 +120,7 @@ public function exists($files) { $maxPathLength = PHP_MAXPATHLEN - 2; - foreach ($this->toIterator($files) as $file) { + foreach ($this->toIterable($files) as $file) { if (strlen($file) > $maxPathLength) { throw new IOException(sprintf('Could not check if file exist because path length exceeds %d characters.', $maxPathLength), 0, null, $file); } @@ -136,15 +136,15 @@ public function exists($files) /** * Sets access and modification time of file. * - * @param string|array|\Traversable $files A filename, an array of files, or a \Traversable instance to create - * @param int $time The touch time as a Unix timestamp - * @param int $atime The access time as a Unix timestamp + * @param string|iterable $files A filename, an array of files, or a \Traversable instance to create + * @param int $time The touch time as a Unix timestamp + * @param int $atime The access time as a Unix timestamp * * @throws IOException When touch fails */ public function touch($files, $time = null, $atime = null) { - foreach ($this->toIterator($files) as $file) { + foreach ($this->toIterable($files) as $file) { $touch = $time ? @touch($file, $time, $atime) : @touch($file); if (true !== $touch) { throw new IOException(sprintf('Failed to touch "%s".', $file), 0, null, $file); @@ -155,7 +155,7 @@ public function touch($files, $time = null, $atime = null) /** * Removes files or directories. * - * @param string|array|\Traversable $files A filename, an array of files, or a \Traversable instance to remove + * @param string|iterable $files A filename, an array of files, or a \Traversable instance to remove * * @throws IOException When removal fails */ @@ -191,16 +191,16 @@ public function remove($files) /** * Change mode for an array of files or directories. * - * @param string|array|\Traversable $files A filename, an array of files, or a \Traversable instance to change mode - * @param int $mode The new mode (octal) - * @param int $umask The mode mask (octal) - * @param bool $recursive Whether change the mod recursively or not + * @param string|iterable $files A filename, an array of files, or a \Traversable instance to change mode + * @param int $mode The new mode (octal) + * @param int $umask The mode mask (octal) + * @param bool $recursive Whether change the mod recursively or not * * @throws IOException When the change fail */ public function chmod($files, $mode, $umask = 0000, $recursive = false) { - foreach ($this->toIterator($files) as $file) { + foreach ($this->toIterable($files) as $file) { if (true !== @chmod($file, $mode & ~$umask)) { throw new IOException(sprintf('Failed to chmod file "%s".', $file), 0, null, $file); } @@ -213,15 +213,15 @@ public function chmod($files, $mode, $umask = 0000, $recursive = false) /** * Change the owner of an array of files or directories. * - * @param string|array|\Traversable $files A filename, an array of files, or a \Traversable instance to change owner - * @param string $user The new owner user name - * @param bool $recursive Whether change the owner recursively or not + * @param string|iterable $files A filename, an array of files, or a \Traversable instance to change owner + * @param string $user The new owner user name + * @param bool $recursive Whether change the owner recursively or not * * @throws IOException When the change fail */ public function chown($files, $user, $recursive = false) { - foreach ($this->toIterator($files) as $file) { + foreach ($this->toIterable($files) as $file) { if ($recursive && is_dir($file) && !is_link($file)) { $this->chown(new \FilesystemIterator($file), $user, true); } @@ -240,15 +240,15 @@ public function chown($files, $user, $recursive = false) /** * Change the group of an array of files or directories. * - * @param string|array|\Traversable $files A filename, an array of files, or a \Traversable instance to change group - * @param string $group The group name - * @param bool $recursive Whether change the group recursively or not + * @param string|iterable $files A filename, an array of files, or a \Traversable instance to change group + * @param string $group The group name + * @param bool $recursive Whether change the group recursively or not * * @throws IOException When the change fail */ public function chgrp($files, $group, $recursive = false) { - foreach ($this->toIterator($files) as $file) { + foreach ($this->toIterable($files) as $file) { if ($recursive && is_dir($file) && !is_link($file)) { $this->chgrp(new \FilesystemIterator($file), $group, true); } @@ -296,13 +296,9 @@ public function rename($origin, $target, $overwrite = false) /** * Tells whether a file exists and is readable. * - * @param string $filename Path to the file - * - * @return bool - * * @throws IOException When windows path is longer than 258 characters */ - private function isReadable($filename) + private function isReadable(string $filename): bool { $maxPathLength = PHP_MAXPATHLEN - 2; @@ -370,7 +366,7 @@ public function hardlink($originFile, $targetFiles) throw new FileNotFoundException(sprintf('Origin file "%s" is not a file', $originFile)); } - foreach ($this->toIterator($targetFiles) as $targetFile) { + foreach ($this->toIterable($targetFiles) as $targetFile) { if (is_file($targetFile)) { if (fileinode($originFile) === fileinode($targetFile)) { continue; @@ -384,12 +380,7 @@ public function hardlink($originFile, $targetFiles) } } - /** - * @param string $origin - * @param string $target - * @param string $linkType Name of the link type, typically 'symbolic' or 'hard' - */ - private function linkException($origin, $target, $linkType) + private function linkException(string $origin, string $target, string $linkType): void { $report = error_get_last(); if (is_array($report)) { @@ -724,18 +715,9 @@ public function appendToFile($filename, $content) } } - /** - * @param mixed $files - * - * @return \Traversable - */ - private function toIterator($files) + private function toIterable($files): iterable { - if (!$files instanceof \Traversable) { - $files = new \ArrayObject(is_array($files) ? $files : array($files)); - } - - return $files; + return is_iterable($files) ? $files : array($files); } /**