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
-------

7f98903d05 [5.0][Finder] add parameter type-hints
This commit is contained in:
Fabien Potencier 2019-07-08 08:27:20 +02:00
commit 3a2987764f
9 changed files with 30 additions and 58 deletions

View File

@ -31,12 +31,7 @@ class Comparator
return $this->target; return $this->target;
} }
/** public function setTarget(string $target)
* Sets the target value.
*
* @param string $target The target value
*/
public function setTarget($target)
{ {
$this->target = $target; $this->target = $target;
} }
@ -54,13 +49,11 @@ class Comparator
/** /**
* Sets the comparison operator. * Sets the comparison operator.
* *
* @param string $operator A valid operator
*
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */
public function setOperator($operator) public function setOperator(string $operator)
{ {
if (!$operator) { if ('' === $operator) {
$operator = '=='; $operator = '==';
} }

View File

@ -336,13 +336,11 @@ class Finder implements \IteratorAggregate, \Countable
* *
* This option is enabled by default. * This option is enabled by default.
* *
* @param bool $ignoreDotFiles Whether to exclude "hidden" files or not
*
* @return $this * @return $this
* *
* @see ExcludeDirectoryFilterIterator * @see ExcludeDirectoryFilterIterator
*/ */
public function ignoreDotFiles($ignoreDotFiles) public function ignoreDotFiles(bool $ignoreDotFiles)
{ {
if ($ignoreDotFiles) { if ($ignoreDotFiles) {
$this->ignore |= static::IGNORE_DOT_FILES; $this->ignore |= static::IGNORE_DOT_FILES;
@ -358,13 +356,11 @@ class Finder implements \IteratorAggregate, \Countable
* *
* This option is enabled by default. * This option is enabled by default.
* *
* @param bool $ignoreVCS Whether to exclude VCS files or not
*
* @return $this * @return $this
* *
* @see ExcludeDirectoryFilterIterator * @see ExcludeDirectoryFilterIterator
*/ */
public function ignoreVCS($ignoreVCS) public function ignoreVCS(bool $ignoreVCS)
{ {
if ($ignoreVCS) { if ($ignoreVCS) {
$this->ignore |= static::IGNORE_VCS_FILES; $this->ignore |= static::IGNORE_VCS_FILES;
@ -432,8 +428,6 @@ class Finder implements \IteratorAggregate, \Countable
* *
* This can be slow as all the matching files and directories must be retrieved for comparison. * This can be slow as all the matching files and directories must be retrieved for comparison.
* *
* @param bool $useNaturalSort Whether to use natural sort or not, disabled by default
*
* @return $this * @return $this
* *
* @see SortableIterator * @see SortableIterator
@ -563,13 +557,11 @@ class Finder implements \IteratorAggregate, \Countable
* *
* By default, scanning unreadable directories content throws an AccessDeniedException. * By default, scanning unreadable directories content throws an AccessDeniedException.
* *
* @param bool $ignore
*
* @return $this * @return $this
*/ */
public function ignoreUnreadableDirs($ignore = true) public function ignoreUnreadableDirs(bool $ignore = true)
{ {
$this->ignoreUnreadableDirs = (bool) $ignore; $this->ignoreUnreadableDirs = $ignore;
return $this; return $this;
} }
@ -644,7 +636,7 @@ class Finder implements \IteratorAggregate, \Countable
* *
* @throws \InvalidArgumentException when the given argument is not iterable * @throws \InvalidArgumentException when the given argument is not iterable
*/ */
public function append($iterator) public function append(iterable $iterator)
{ {
if ($iterator instanceof \IteratorAggregate) { if ($iterator instanceof \IteratorAggregate) {
$this->iterators[] = $iterator->getIterator(); $this->iterators[] = $iterator->getIterator();
@ -789,11 +781,9 @@ class Finder implements \IteratorAggregate, \Countable
* *
* Excluding: (s)ftp:// wrapper * Excluding: (s)ftp:// wrapper
* *
* @param string $dir
*
* @return string * @return string
*/ */
private function normalizeDir($dir) private function normalizeDir(string $dir)
{ {
$dir = rtrim($dir, '/'.\DIRECTORY_SEPARATOR); $dir = rtrim($dir, '/'.\DIRECTORY_SEPARATOR);

View File

@ -38,14 +38,9 @@ class Glob
/** /**
* Returns a regexp which is the equivalent of the glob pattern. * Returns a regexp which is the equivalent of the glob pattern.
* *
* @param string $glob The glob pattern * @return string
* @param bool $strictLeadingDot
* @param bool $strictWildcardSlash
* @param string $delimiter Optional delimiter
*
* @return string regex The regexp
*/ */
public static function toRegex($glob, $strictLeadingDot = true, $strictWildcardSlash = true, $delimiter = '#') public static function toRegex(string $glob, bool $strictLeadingDot = true, bool $strictWildcardSlash = true, string $delimiter = '#')
{ {
$firstByte = true; $firstByte = true;
$escaping = false; $escaping = false;

View File

@ -51,7 +51,7 @@ class FilecontentFilterIterator extends MultiplePcreFilterIterator
* *
* @return string regexp corresponding to a given string or regexp * @return string regexp corresponding to a given string or regexp
*/ */
protected function toRegex($str) protected function toRegex(string $str)
{ {
return $this->isRegex($str) ? $str : '/'.preg_quote($str, '/').'/'; return $this->isRegex($str) ? $str : '/'.preg_quote($str, '/').'/';
} }

View File

@ -40,7 +40,7 @@ class FilenameFilterIterator extends MultiplePcreFilterIterator
* *
* @return string regexp corresponding to a given glob or regexp * @return string regexp corresponding to a given glob or regexp
*/ */
protected function toRegex($str) protected function toRegex(string $str)
{ {
return $this->isRegex($str) ? $str : Glob::toRegex($str); return $this->isRegex($str) ? $str : Glob::toRegex($str);
} }

View File

@ -23,8 +23,8 @@ abstract class MultiplePcreFilterIterator extends \FilterIterator
/** /**
* @param \Iterator $iterator The Iterator to filter * @param \Iterator $iterator The Iterator to filter
* @param array $matchPatterns An array of patterns that need to match * @param string[] $matchPatterns An array of patterns that need to match
* @param array $noMatchPatterns An array of patterns that need to not match * @param string[] $noMatchPatterns An array of patterns that need to not match
*/ */
public function __construct(\Iterator $iterator, array $matchPatterns, array $noMatchPatterns) public function __construct(\Iterator $iterator, array $matchPatterns, array $noMatchPatterns)
{ {
@ -46,11 +46,9 @@ abstract class MultiplePcreFilterIterator extends \FilterIterator
* Such case can be handled by child classes before calling the method if they want to * Such case can be handled by child classes before calling the method if they want to
* apply a different behavior. * apply a different behavior.
* *
* @param string $string The string to be matched against filters
*
* @return bool * @return bool
*/ */
protected function isAccepted($string) protected function isAccepted(string $string)
{ {
// should at least not match one rule to exclude // should at least not match one rule to exclude
foreach ($this->noMatchRegexps as $regex) { foreach ($this->noMatchRegexps as $regex) {
@ -77,11 +75,9 @@ abstract class MultiplePcreFilterIterator extends \FilterIterator
/** /**
* Checks whether the string is a regex. * Checks whether the string is a regex.
* *
* @param string $str * @return bool
*
* @return bool Whether the given string is a regex
*/ */
protected function isRegex($str) protected function isRegex(string $str)
{ {
if (preg_match('/^(.{3,}?)[imsxuADU]*$/', $str, $m)) { if (preg_match('/^(.{3,}?)[imsxuADU]*$/', $str, $m)) {
$start = substr($m[1], 0, 1); $start = substr($m[1], 0, 1);
@ -104,9 +100,7 @@ abstract class MultiplePcreFilterIterator extends \FilterIterator
/** /**
* Converts string into regexp. * Converts string into regexp.
* *
* @param string $str Pattern * @return string
*
* @return string regexp corresponding to a given string
*/ */
abstract protected function toRegex($str); abstract protected function toRegex(string $str);
} }

View File

@ -49,7 +49,7 @@ class PathFilterIterator extends MultiplePcreFilterIterator
* *
* @return string regexp corresponding to a given string or regexp * @return string regexp corresponding to a given string or regexp
*/ */
protected function toRegex($str) protected function toRegex(string $str)
{ {
return $this->isRegex($str) ? $str : '/'.preg_quote($str, '/').'/'; return $this->isRegex($str) ? $str : '/'.preg_quote($str, '/').'/';
} }

View File

@ -41,15 +41,15 @@ class SortableIterator implements \IteratorAggregate
$order = $reverseOrder ? -1 : 1; $order = $reverseOrder ? -1 : 1;
if (self::SORT_BY_NAME === $sort) { if (self::SORT_BY_NAME === $sort) {
$this->sort = function ($a, $b) use ($order) { $this->sort = function (\SplFileInfo $a, \SplFileInfo $b) use ($order) {
return $order * strcmp($a->getRealPath() ?: $a->getPathname(), $b->getRealPath() ?: $b->getPathname()); return $order * strcmp($a->getRealPath() ?: $a->getPathname(), $b->getRealPath() ?: $b->getPathname());
}; };
} elseif (self::SORT_BY_NAME_NATURAL === $sort) { } elseif (self::SORT_BY_NAME_NATURAL === $sort) {
$this->sort = function ($a, $b) use ($order) { $this->sort = function (\SplFileInfo $a, \SplFileInfo $b) use ($order) {
return $order * strnatcmp($a->getRealPath() ?: $a->getPathname(), $b->getRealPath() ?: $b->getPathname()); return $order * strnatcmp($a->getRealPath() ?: $a->getPathname(), $b->getRealPath() ?: $b->getPathname());
}; };
} elseif (self::SORT_BY_TYPE === $sort) { } elseif (self::SORT_BY_TYPE === $sort) {
$this->sort = function ($a, $b) use ($order) { $this->sort = function (\SplFileInfo $a, \SplFileInfo $b) use ($order) {
if ($a->isDir() && $b->isFile()) { if ($a->isDir() && $b->isFile()) {
return -$order; return -$order;
} elseif ($a->isFile() && $b->isDir()) { } elseif ($a->isFile() && $b->isDir()) {
@ -59,21 +59,21 @@ class SortableIterator implements \IteratorAggregate
return $order * strcmp($a->getRealPath() ?: $a->getPathname(), $b->getRealPath() ?: $b->getPathname()); return $order * strcmp($a->getRealPath() ?: $a->getPathname(), $b->getRealPath() ?: $b->getPathname());
}; };
} elseif (self::SORT_BY_ACCESSED_TIME === $sort) { } elseif (self::SORT_BY_ACCESSED_TIME === $sort) {
$this->sort = function ($a, $b) use ($order) { $this->sort = function (\SplFileInfo $a, \SplFileInfo $b) use ($order) {
return $order * ($a->getATime() - $b->getATime()); return $order * ($a->getATime() - $b->getATime());
}; };
} elseif (self::SORT_BY_CHANGED_TIME === $sort) { } elseif (self::SORT_BY_CHANGED_TIME === $sort) {
$this->sort = function ($a, $b) use ($order) { $this->sort = function (\SplFileInfo $a, \SplFileInfo $b) use ($order) {
return $order * ($a->getCTime() - $b->getCTime()); return $order * ($a->getCTime() - $b->getCTime());
}; };
} elseif (self::SORT_BY_MODIFIED_TIME === $sort) { } elseif (self::SORT_BY_MODIFIED_TIME === $sort) {
$this->sort = function ($a, $b) use ($order) { $this->sort = function (\SplFileInfo $a, \SplFileInfo $b) use ($order) {
return $order * ($a->getMTime() - $b->getMTime()); return $order * ($a->getMTime() - $b->getMTime());
}; };
} elseif (self::SORT_BY_NONE === $sort) { } elseif (self::SORT_BY_NONE === $sort) {
$this->sort = $order; $this->sort = $order;
} elseif (\is_callable($sort)) { } elseif (\is_callable($sort)) {
$this->sort = $reverseOrder ? function ($a, $b) use ($sort) { return -$sort($a, $b); } $this->sort = $reverseOrder ? function (\SplFileInfo $a, \SplFileInfo $b) use ($sort) { return -$sort($a, $b); }
: $sort; : $sort;
} else { } else {
throw new \InvalidArgumentException('The SortableIterator takes a PHP callable or a valid built-in sort algorithm as an argument.'); throw new \InvalidArgumentException('The SortableIterator takes a PHP callable or a valid built-in sort algorithm as an argument.');

View File

@ -59,12 +59,12 @@ class TestMultiplePcreFilterIterator extends MultiplePcreFilterIterator
throw new \BadFunctionCallException('Not implemented'); throw new \BadFunctionCallException('Not implemented');
} }
public function isRegex($str) public function isRegex(string $str)
{ {
return parent::isRegex($str); return parent::isRegex($str);
} }
public function toRegex($str) public function toRegex(string $str)
{ {
throw new \BadFunctionCallException('Not implemented'); throw new \BadFunctionCallException('Not implemented');
} }