Skip to content

Commit 3a29877

Browse files
committed
minor #32243 [5.0][Finder] add parameter type-hints (smoench)
This PR was merged into the 5.0-dev branch. Discussion ---------- [5.0][Finder] 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 Finder component. Commits ------- 7f98903 [5.0][Finder] add parameter type-hints
2 parents b057243 + 7f98903 commit 3a29877

File tree

9 files changed

+30
-58
lines changed

9 files changed

+30
-58
lines changed

src/Symfony/Component/Finder/Comparator/Comparator.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,7 @@ public function getTarget()
3131
return $this->target;
3232
}
3333

34-
/**
35-
* Sets the target value.
36-
*
37-
* @param string $target The target value
38-
*/
39-
public function setTarget($target)
34+
public function setTarget(string $target)
4035
{
4136
$this->target = $target;
4237
}
@@ -54,13 +49,11 @@ public function getOperator()
5449
/**
5550
* Sets the comparison operator.
5651
*
57-
* @param string $operator A valid operator
58-
*
5952
* @throws \InvalidArgumentException
6053
*/
61-
public function setOperator($operator)
54+
public function setOperator(string $operator)
6255
{
63-
if (!$operator) {
56+
if ('' === $operator) {
6457
$operator = '==';
6558
}
6659

src/Symfony/Component/Finder/Finder.php

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -336,13 +336,11 @@ public function exclude($dirs)
336336
*
337337
* This option is enabled by default.
338338
*
339-
* @param bool $ignoreDotFiles Whether to exclude "hidden" files or not
340-
*
341339
* @return $this
342340
*
343341
* @see ExcludeDirectoryFilterIterator
344342
*/
345-
public function ignoreDotFiles($ignoreDotFiles)
343+
public function ignoreDotFiles(bool $ignoreDotFiles)
346344
{
347345
if ($ignoreDotFiles) {
348346
$this->ignore |= static::IGNORE_DOT_FILES;
@@ -358,13 +356,11 @@ public function ignoreDotFiles($ignoreDotFiles)
358356
*
359357
* This option is enabled by default.
360358
*
361-
* @param bool $ignoreVCS Whether to exclude VCS files or not
362-
*
363359
* @return $this
364360
*
365361
* @see ExcludeDirectoryFilterIterator
366362
*/
367-
public function ignoreVCS($ignoreVCS)
363+
public function ignoreVCS(bool $ignoreVCS)
368364
{
369365
if ($ignoreVCS) {
370366
$this->ignore |= static::IGNORE_VCS_FILES;
@@ -432,8 +428,6 @@ public function sort(\Closure $closure)
432428
*
433429
* This can be slow as all the matching files and directories must be retrieved for comparison.
434430
*
435-
* @param bool $useNaturalSort Whether to use natural sort or not, disabled by default
436-
*
437431
* @return $this
438432
*
439433
* @see SortableIterator
@@ -563,13 +557,11 @@ public function followLinks()
563557
*
564558
* By default, scanning unreadable directories content throws an AccessDeniedException.
565559
*
566-
* @param bool $ignore
567-
*
568560
* @return $this
569561
*/
570-
public function ignoreUnreadableDirs($ignore = true)
562+
public function ignoreUnreadableDirs(bool $ignore = true)
571563
{
572-
$this->ignoreUnreadableDirs = (bool) $ignore;
564+
$this->ignoreUnreadableDirs = $ignore;
573565

574566
return $this;
575567
}
@@ -644,7 +636,7 @@ public function getIterator()
644636
*
645637
* @throws \InvalidArgumentException when the given argument is not iterable
646638
*/
647-
public function append($iterator)
639+
public function append(iterable $iterator)
648640
{
649641
if ($iterator instanceof \IteratorAggregate) {
650642
$this->iterators[] = $iterator->getIterator();
@@ -789,11 +781,9 @@ private function searchInDirectory(string $dir): \Iterator
789781
*
790782
* Excluding: (s)ftp:// wrapper
791783
*
792-
* @param string $dir
793-
*
794784
* @return string
795785
*/
796-
private function normalizeDir($dir)
786+
private function normalizeDir(string $dir)
797787
{
798788
$dir = rtrim($dir, '/'.\DIRECTORY_SEPARATOR);
799789

src/Symfony/Component/Finder/Glob.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,9 @@ class Glob
3838
/**
3939
* Returns a regexp which is the equivalent of the glob pattern.
4040
*
41-
* @param string $glob The glob pattern
42-
* @param bool $strictLeadingDot
43-
* @param bool $strictWildcardSlash
44-
* @param string $delimiter Optional delimiter
45-
*
46-
* @return string regex The regexp
41+
* @return string
4742
*/
48-
public static function toRegex($glob, $strictLeadingDot = true, $strictWildcardSlash = true, $delimiter = '#')
43+
public static function toRegex(string $glob, bool $strictLeadingDot = true, bool $strictWildcardSlash = true, string $delimiter = '#')
4944
{
5045
$firstByte = true;
5146
$escaping = false;

src/Symfony/Component/Finder/Iterator/FilecontentFilterIterator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function accept()
5151
*
5252
* @return string regexp corresponding to a given string or regexp
5353
*/
54-
protected function toRegex($str)
54+
protected function toRegex(string $str)
5555
{
5656
return $this->isRegex($str) ? $str : '/'.preg_quote($str, '/').'/';
5757
}

src/Symfony/Component/Finder/Iterator/FilenameFilterIterator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function accept()
4040
*
4141
* @return string regexp corresponding to a given glob or regexp
4242
*/
43-
protected function toRegex($str)
43+
protected function toRegex(string $str)
4444
{
4545
return $this->isRegex($str) ? $str : Glob::toRegex($str);
4646
}

src/Symfony/Component/Finder/Iterator/MultiplePcreFilterIterator.php

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ abstract class MultiplePcreFilterIterator extends \FilterIterator
2323

2424
/**
2525
* @param \Iterator $iterator The Iterator to filter
26-
* @param array $matchPatterns An array of patterns that need to match
27-
* @param array $noMatchPatterns An array of patterns that need to not match
26+
* @param string[] $matchPatterns An array of patterns that need to match
27+
* @param string[] $noMatchPatterns An array of patterns that need to not match
2828
*/
2929
public function __construct(\Iterator $iterator, array $matchPatterns, array $noMatchPatterns)
3030
{
@@ -46,11 +46,9 @@ public function __construct(\Iterator $iterator, array $matchPatterns, array $no
4646
* Such case can be handled by child classes before calling the method if they want to
4747
* apply a different behavior.
4848
*
49-
* @param string $string The string to be matched against filters
50-
*
5149
* @return bool
5250
*/
53-
protected function isAccepted($string)
51+
protected function isAccepted(string $string)
5452
{
5553
// should at least not match one rule to exclude
5654
foreach ($this->noMatchRegexps as $regex) {
@@ -77,11 +75,9 @@ protected function isAccepted($string)
7775
/**
7876
* Checks whether the string is a regex.
7977
*
80-
* @param string $str
81-
*
82-
* @return bool Whether the given string is a regex
78+
* @return bool
8379
*/
84-
protected function isRegex($str)
80+
protected function isRegex(string $str)
8581
{
8682
if (preg_match('/^(.{3,}?)[imsxuADU]*$/', $str, $m)) {
8783
$start = substr($m[1], 0, 1);
@@ -104,9 +100,7 @@ protected function isRegex($str)
104100
/**
105101
* Converts string into regexp.
106102
*
107-
* @param string $str Pattern
108-
*
109-
* @return string regexp corresponding to a given string
103+
* @return string
110104
*/
111-
abstract protected function toRegex($str);
105+
abstract protected function toRegex(string $str);
112106
}

src/Symfony/Component/Finder/Iterator/PathFilterIterator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function accept()
4949
*
5050
* @return string regexp corresponding to a given string or regexp
5151
*/
52-
protected function toRegex($str)
52+
protected function toRegex(string $str)
5353
{
5454
return $this->isRegex($str) ? $str : '/'.preg_quote($str, '/').'/';
5555
}

src/Symfony/Component/Finder/Iterator/SortableIterator.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ public function __construct(\Traversable $iterator, $sort, bool $reverseOrder =
4141
$order = $reverseOrder ? -1 : 1;
4242

4343
if (self::SORT_BY_NAME === $sort) {
44-
$this->sort = function ($a, $b) use ($order) {
44+
$this->sort = function (\SplFileInfo $a, \SplFileInfo $b) use ($order) {
4545
return $order * strcmp($a->getRealPath() ?: $a->getPathname(), $b->getRealPath() ?: $b->getPathname());
4646
};
4747
} elseif (self::SORT_BY_NAME_NATURAL === $sort) {
48-
$this->sort = function ($a, $b) use ($order) {
48+
$this->sort = function (\SplFileInfo $a, \SplFileInfo $b) use ($order) {
4949
return $order * strnatcmp($a->getRealPath() ?: $a->getPathname(), $b->getRealPath() ?: $b->getPathname());
5050
};
5151
} elseif (self::SORT_BY_TYPE === $sort) {
52-
$this->sort = function ($a, $b) use ($order) {
52+
$this->sort = function (\SplFileInfo $a, \SplFileInfo $b) use ($order) {
5353
if ($a->isDir() && $b->isFile()) {
5454
return -$order;
5555
} elseif ($a->isFile() && $b->isDir()) {
@@ -59,21 +59,21 @@ public function __construct(\Traversable $iterator, $sort, bool $reverseOrder =
5959
return $order * strcmp($a->getRealPath() ?: $a->getPathname(), $b->getRealPath() ?: $b->getPathname());
6060
};
6161
} elseif (self::SORT_BY_ACCESSED_TIME === $sort) {
62-
$this->sort = function ($a, $b) use ($order) {
62+
$this->sort = function (\SplFileInfo $a, \SplFileInfo $b) use ($order) {
6363
return $order * ($a->getATime() - $b->getATime());
6464
};
6565
} elseif (self::SORT_BY_CHANGED_TIME === $sort) {
66-
$this->sort = function ($a, $b) use ($order) {
66+
$this->sort = function (\SplFileInfo $a, \SplFileInfo $b) use ($order) {
6767
return $order * ($a->getCTime() - $b->getCTime());
6868
};
6969
} elseif (self::SORT_BY_MODIFIED_TIME === $sort) {
70-
$this->sort = function ($a, $b) use ($order) {
70+
$this->sort = function (\SplFileInfo $a, \SplFileInfo $b) use ($order) {
7171
return $order * ($a->getMTime() - $b->getMTime());
7272
};
7373
} elseif (self::SORT_BY_NONE === $sort) {
7474
$this->sort = $order;
7575
} elseif (\is_callable($sort)) {
76-
$this->sort = $reverseOrder ? function ($a, $b) use ($sort) { return -$sort($a, $b); }
76+
$this->sort = $reverseOrder ? function (\SplFileInfo $a, \SplFileInfo $b) use ($sort) { return -$sort($a, $b); }
7777
: $sort;
7878
} else {
7979
throw new \InvalidArgumentException('The SortableIterator takes a PHP callable or a valid built-in sort algorithm as an argument.');

src/Symfony/Component/Finder/Tests/Iterator/MultiplePcreFilterIteratorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ public function accept()
5959
throw new \BadFunctionCallException('Not implemented');
6060
}
6161

62-
public function isRegex($str)
62+
public function isRegex(string $str)
6363
{
6464
return parent::isRegex($str);
6565
}
6666

67-
public function toRegex($str)
67+
public function toRegex(string $str)
6868
{
6969
throw new \BadFunctionCallException('Not implemented');
7070
}

0 commit comments

Comments
 (0)