Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d38f4b0

Browse files
committed
minor #32247 [5.0][Filesystem] add parameter type hints (smoench)
This PR was merged into the 5.0-dev branch. Discussion ---------- [5.0][Filesystem] add parameter type hints | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | #32179 | License | MIT | Doc PR | N/A This PR adds parameter type hints to the Filesystem component. Commits ------- ba7dab6 [5.0][Filesystem] add parameter type hints
2 parents 3a29877 + ba7dab6 commit d38f4b0

File tree

2 files changed

+28
-77
lines changed

2 files changed

+28
-77
lines changed

src/Symfony/Component/Filesystem/Filesystem.php

Lines changed: 28 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,10 @@ class Filesystem
3131
* If the target file is newer, it is overwritten only when the
3232
* $overwriteNewerFiles option is set to true.
3333
*
34-
* @param string $originFile The original filename
35-
* @param string $targetFile The target filename
36-
* @param bool $overwriteNewerFiles If true, target files newer than origin files are overwritten
37-
*
3834
* @throws FileNotFoundException When originFile doesn't exist
3935
* @throws IOException When copy fails
4036
*/
41-
public function copy($originFile, $targetFile, $overwriteNewerFiles = false)
37+
public function copy(string $originFile, string $targetFile, bool $overwriteNewerFiles = false)
4238
{
4339
$originIsLocal = stream_is_local($originFile) || 0 === stripos($originFile, 'file://');
4440
if ($originIsLocal && !is_file($originFile)) {
@@ -87,11 +83,10 @@ public function copy($originFile, $targetFile, $overwriteNewerFiles = false)
8783
* Creates a directory recursively.
8884
*
8985
* @param string|iterable $dirs The directory path
90-
* @param int $mode The directory mode
9186
*
9287
* @throws IOException On any directory creation failure
9388
*/
94-
public function mkdir($dirs, $mode = 0777)
89+
public function mkdir($dirs, int $mode = 0777)
9590
{
9691
foreach ($this->toIterable($dirs) as $dir) {
9792
if (is_dir($dir)) {
@@ -143,7 +138,7 @@ public function exists($files)
143138
*
144139
* @throws IOException When touch fails
145140
*/
146-
public function touch($files, $time = null, $atime = null)
141+
public function touch($files, int $time = null, int $atime = null)
147142
{
148143
foreach ($this->toIterable($files) as $file) {
149144
$touch = $time ? @touch($file, $time, $atime) : @touch($file);
@@ -196,7 +191,7 @@ public function remove($files)
196191
*
197192
* @throws IOException When the change fails
198193
*/
199-
public function chmod($files, $mode, $umask = 0000, $recursive = false)
194+
public function chmod($files, int $mode, int $umask = 0000, bool $recursive = false)
200195
{
201196
foreach ($this->toIterable($files) as $file) {
202197
if (true !== @chmod($file, $mode & ~$umask)) {
@@ -211,13 +206,11 @@ public function chmod($files, $mode, $umask = 0000, $recursive = false)
211206
/**
212207
* Change the owner of an array of files or directories.
213208
*
214-
* @param string|iterable $files A filename, an array of files, or a \Traversable instance to change owner
215-
* @param string $user The new owner user name
216-
* @param bool $recursive Whether change the owner recursively or not
209+
* @param string|iterable $files A filename, an array of files, or a \Traversable instance to change owner
217210
*
218211
* @throws IOException When the change fails
219212
*/
220-
public function chown($files, $user, $recursive = false)
213+
public function chown($files, string $user, bool $recursive = false)
221214
{
222215
foreach ($this->toIterable($files) as $file) {
223216
if ($recursive && is_dir($file) && !is_link($file)) {
@@ -238,13 +231,11 @@ public function chown($files, $user, $recursive = false)
238231
/**
239232
* Change the group of an array of files or directories.
240233
*
241-
* @param string|iterable $files A filename, an array of files, or a \Traversable instance to change group
242-
* @param string $group The group name
243-
* @param bool $recursive Whether change the group recursively or not
234+
* @param string|iterable $files A filename, an array of files, or a \Traversable instance to change group
244235
*
245236
* @throws IOException When the change fails
246237
*/
247-
public function chgrp($files, $group, $recursive = false)
238+
public function chgrp($files, string $group, bool $recursive = false)
248239
{
249240
foreach ($this->toIterable($files) as $file) {
250241
if ($recursive && is_dir($file) && !is_link($file)) {
@@ -265,14 +256,10 @@ public function chgrp($files, $group, $recursive = false)
265256
/**
266257
* Renames a file or a directory.
267258
*
268-
* @param string $origin The origin filename or directory
269-
* @param string $target The new filename or directory
270-
* @param bool $overwrite Whether to overwrite the target if it already exists
271-
*
272259
* @throws IOException When target file or directory already exists
273260
* @throws IOException When origin cannot be renamed
274261
*/
275-
public function rename($origin, $target, $overwrite = false)
262+
public function rename(string $origin, string $target, bool $overwrite = false)
276263
{
277264
// we check that target does not exist
278265
if (!$overwrite && $this->isReadable($target)) {
@@ -294,13 +281,11 @@ public function rename($origin, $target, $overwrite = false)
294281
/**
295282
* Tells whether a file exists and is readable.
296283
*
297-
* @param string $filename Path to the file
298-
*
299284
* @return bool
300285
*
301286
* @throws IOException When windows path is longer than 258 characters
302287
*/
303-
private function isReadable($filename)
288+
private function isReadable(string $filename)
304289
{
305290
$maxPathLength = PHP_MAXPATHLEN - 2;
306291

@@ -314,13 +299,9 @@ private function isReadable($filename)
314299
/**
315300
* Creates a symbolic link or copy a directory.
316301
*
317-
* @param string $originDir The origin directory path
318-
* @param string $targetDir The symbolic link name
319-
* @param bool $copyOnWindows Whether to copy files if on Windows
320-
*
321302
* @throws IOException When symlink fails
322303
*/
323-
public function symlink($originDir, $targetDir, $copyOnWindows = false)
304+
public function symlink(string $originDir, string $targetDir, bool $copyOnWindows = false)
324305
{
325306
if ('\\' === \DIRECTORY_SEPARATOR) {
326307
$originDir = strtr($originDir, '/', '\\');
@@ -350,13 +331,12 @@ public function symlink($originDir, $targetDir, $copyOnWindows = false)
350331
/**
351332
* Creates a hard link, or several hard links to a file.
352333
*
353-
* @param string $originFile The original file
354334
* @param string|string[] $targetFiles The target file(s)
355335
*
356336
* @throws FileNotFoundException When original file is missing or not a file
357337
* @throws IOException When link fails, including if link already exists
358338
*/
359-
public function hardlink($originFile, $targetFiles)
339+
public function hardlink(string $originFile, $targetFiles)
360340
{
361341
if (!$this->exists($originFile)) {
362342
throw new FileNotFoundException(null, 0, null, $originFile);
@@ -381,11 +361,9 @@ public function hardlink($originFile, $targetFiles)
381361
}
382362

383363
/**
384-
* @param string $origin
385-
* @param string $target
386364
* @param string $linkType Name of the link type, typically 'symbolic' or 'hard'
387365
*/
388-
private function linkException($origin, $target, $linkType)
366+
private function linkException(string $origin, string $target, string $linkType)
389367
{
390368
if (self::$lastError) {
391369
if ('\\' === \DIRECTORY_SEPARATOR && false !== strpos(self::$lastError, 'error code(1314)')) {
@@ -406,12 +384,9 @@ private function linkException($origin, $target, $linkType)
406384
* - if $path does not exist, returns null
407385
* - if $path exists, returns its absolute fully resolved final version
408386
*
409-
* @param string $path A filesystem path
410-
* @param bool $canonicalize Whether or not to return a canonicalized path
411-
*
412387
* @return string|null
413388
*/
414-
public function readlink($path, $canonicalize = false)
389+
public function readlink(string $path, bool $canonicalize = false)
415390
{
416391
if (!$canonicalize && !is_link($path)) {
417392
return;
@@ -439,12 +414,9 @@ public function readlink($path, $canonicalize = false)
439414
/**
440415
* Given an existing path, convert it to a path relative to a given starting path.
441416
*
442-
* @param string $endPath Absolute path of target
443-
* @param string $startPath Absolute path where traversal begins
444-
*
445417
* @return string Path of target relative to starting path
446418
*/
447-
public function makePathRelative($endPath, $startPath)
419+
public function makePathRelative(string $endPath, string $startPath)
448420
{
449421
if (!$this->isAbsolutePath($startPath)) {
450422
throw new InvalidArgumentException(sprintf('The start path "%s" is not absolute.', $startPath));
@@ -524,18 +496,16 @@ public function makePathRelative($endPath, $startPath)
524496
* - existing files in the target directory will be overwritten, except if they are newer (see the `override` option)
525497
* - files in the target directory that do not exist in the source directory will not be deleted (see the `delete` option)
526498
*
527-
* @param string $originDir The origin directory
528-
* @param string $targetDir The target directory
529-
* @param \Traversable|null $iterator Iterator that filters which files and directories to copy, if null a recursive iterator is created
530-
* @param array $options An array of boolean options
531-
* Valid options are:
532-
* - $options['override'] If true, target files newer than origin files are overwritten (see copy(), defaults to false)
533-
* - $options['copy_on_windows'] Whether to copy files instead of links on Windows (see symlink(), defaults to false)
534-
* - $options['delete'] Whether to delete files that are not in the source directory (defaults to false)
499+
* @param \Traversable|null $iterator Iterator that filters which files and directories to copy, if null a recursive iterator is created
500+
* @param array $options An array of boolean options
501+
* Valid options are:
502+
* - $options['override'] If true, target files newer than origin files are overwritten (see copy(), defaults to false)
503+
* - $options['copy_on_windows'] Whether to copy files instead of links on Windows (see symlink(), defaults to false)
504+
* - $options['delete'] Whether to delete files that are not in the source directory (defaults to false)
535505
*
536506
* @throws IOException When file type is unknown
537507
*/
538-
public function mirror($originDir, $targetDir, \Traversable $iterator = null, $options = [])
508+
public function mirror(string $originDir, string $targetDir, \Traversable $iterator = null, array $options = [])
539509
{
540510
$targetDir = rtrim($targetDir, '/\\');
541511
$originDir = rtrim($originDir, '/\\');
@@ -594,11 +564,9 @@ public function mirror($originDir, $targetDir, \Traversable $iterator = null, $o
594564
/**
595565
* Returns whether the file path is an absolute path.
596566
*
597-
* @param string $file A file path
598-
*
599567
* @return bool
600568
*/
601-
public function isAbsolutePath($file)
569+
public function isAbsolutePath(string $file)
602570
{
603571
return strspn($file, '/\\', 0, 1)
604572
|| (\strlen($file) > 3 && ctype_alpha($file[0])
@@ -612,13 +580,12 @@ public function isAbsolutePath($file)
612580
/**
613581
* Creates a temporary file with support for custom stream wrappers.
614582
*
615-
* @param string $dir The directory where the temporary filename will be created
616583
* @param string $prefix The prefix of the generated temporary filename
617584
* Note: Windows uses only the first three characters of prefix
618585
*
619586
* @return string The new temporary filename (with path), or throw an exception on failure
620587
*/
621-
public function tempnam($dir, $prefix)
588+
public function tempnam(string $dir, string $prefix)
622589
{
623590
list($scheme, $hierarchy) = $this->getSchemeAndHierarchy($dir);
624591

@@ -664,12 +631,11 @@ public function tempnam($dir, $prefix)
664631
/**
665632
* Atomically dumps content into a file.
666633
*
667-
* @param string $filename The file to be written to
668-
* @param string|resource $content The data to write into the file
634+
* @param string|resource $content The data to write into the file
669635
*
670636
* @throws IOException if the file cannot be written to
671637
*/
672-
public function dumpFile($filename, $content)
638+
public function dumpFile(string $filename, $content)
673639
{
674640
if (\is_array($content)) {
675641
throw new \TypeError(sprintf('Argument 2 passed to %s() must be string or resource, %s given.', __METHOD__, $content));
@@ -701,12 +667,11 @@ public function dumpFile($filename, $content)
701667
/**
702668
* Appends content to an existing file.
703669
*
704-
* @param string $filename The file to which to append content
705-
* @param string|resource $content The content to append
670+
* @param string|resource $content The content to append
706671
*
707672
* @throws IOException If the file is not writable
708673
*/
709-
public function appendToFile($filename, $content)
674+
public function appendToFile(string $filename, $content)
710675
{
711676
if (\is_array($content)) {
712677
throw new \TypeError(sprintf('Argument 2 passed to %s() must be string or resource, %s given.', __METHOD__, $content));

src/Symfony/Component/Filesystem/Tests/FilesystemTest.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -456,20 +456,6 @@ public function testChmodChangesFileMode()
456456
$this->assertFilePermissions(400, $file);
457457
}
458458

459-
public function testChmodWithWrongModLeavesPreviousPermissionsUntouched()
460-
{
461-
$this->markAsSkippedIfChmodIsMissing();
462-
463-
$dir = $this->workspace.\DIRECTORY_SEPARATOR.'file';
464-
touch($dir);
465-
466-
$permissions = fileperms($dir);
467-
468-
$this->filesystem->chmod($dir, 'Wrongmode');
469-
470-
$this->assertSame($permissions, fileperms($dir));
471-
}
472-
473459
public function testChmodRecursive()
474460
{
475461
$this->markAsSkippedIfChmodIsMissing();

0 commit comments

Comments
 (0)