Skip to content

[5.0][Finder] add parameter type-hints #32243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions src/Symfony/Component/Finder/Comparator/Comparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,7 @@ public function getTarget()
return $this->target;
}

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

Expand Down
24 changes: 7 additions & 17 deletions src/Symfony/Component/Finder/Finder.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,13 +336,11 @@ public function exclude($dirs)
*
* This option is enabled by default.
*
* @param bool $ignoreDotFiles Whether to exclude "hidden" files or not
*
* @return $this
*
* @see ExcludeDirectoryFilterIterator
*/
public function ignoreDotFiles($ignoreDotFiles)
public function ignoreDotFiles(bool $ignoreDotFiles)
{
if ($ignoreDotFiles) {
$this->ignore |= static::IGNORE_DOT_FILES;
Expand All @@ -358,13 +356,11 @@ public function ignoreDotFiles($ignoreDotFiles)
*
* This option is enabled by default.
*
* @param bool $ignoreVCS Whether to exclude VCS files or not
*
* @return $this
*
* @see ExcludeDirectoryFilterIterator
*/
public function ignoreVCS($ignoreVCS)
public function ignoreVCS(bool $ignoreVCS)
{
if ($ignoreVCS) {
$this->ignore |= static::IGNORE_VCS_FILES;
Expand Down Expand Up @@ -432,8 +428,6 @@ public function sort(\Closure $closure)
*
* 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
*
* @see SortableIterator
Expand Down Expand Up @@ -563,21 +557,19 @@ public function followLinks()
*
* By default, scanning unreadable directories content throws an AccessDeniedException.
*
* @param bool $ignore
*
* @return $this
*/
public function ignoreUnreadableDirs($ignore = true)
public function ignoreUnreadableDirs(bool $ignore = true)
{
$this->ignoreUnreadableDirs = (bool) $ignore;
$this->ignoreUnreadableDirs = $ignore;

return $this;
}

/**
* Searches files and directories which match defined rules.
*
* @param string|array $dirs A directory path or an array of directories
* @param string|string[] $dirs A directory path or an array of directories
*
* @return $this
*
Expand Down Expand Up @@ -644,7 +636,7 @@ public function getIterator()
*
* @throws \InvalidArgumentException when the given argument is not iterable
*/
public function append($iterator)
public function append(iterable $iterator)
{
if ($iterator instanceof \IteratorAggregate) {
$this->iterators[] = $iterator->getIterator();
Expand Down Expand Up @@ -789,11 +781,9 @@ private function searchInDirectory(string $dir): \Iterator
*
* Excluding: (s)ftp:// wrapper
*
* @param string $dir
*
* @return string
*/
private function normalizeDir($dir)
private function normalizeDir(string $dir)
{
$dir = rtrim($dir, '/'.\DIRECTORY_SEPARATOR);

Expand Down
9 changes: 2 additions & 7 deletions src/Symfony/Component/Finder/Glob.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,9 @@ class Glob
/**
* Returns a regexp which is the equivalent of the glob pattern.
*
* @param string $glob The glob pattern
* @param bool $strictLeadingDot
* @param bool $strictWildcardSlash
* @param string $delimiter Optional delimiter
*
* @return string regex The regexp
* @return string
*/
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;
$escaping = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ExcludeDirectoryFilterIterator extends \FilterIterator implements \Recursi

/**
* @param \Iterator $iterator The Iterator to filter
* @param array $directories An array of directories to exclude
* @param string[] $directories An array of directories to exclude
*/
public function __construct(\Iterator $iterator, array $directories)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function accept()
*
* @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, '/').'/';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function accept()
*
* @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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ abstract class MultiplePcreFilterIterator extends \FilterIterator

/**
* @param \Iterator $iterator The Iterator to filter
* @param array $matchPatterns An array of patterns that need to match
* @param array $noMatchPatterns An array of patterns that need to not match
* @param string[] $matchPatterns An array of patterns that need to match
* @param string[] $noMatchPatterns An array of patterns that need to not match
*/
public function __construct(\Iterator $iterator, array $matchPatterns, array $noMatchPatterns)
{
Expand All @@ -46,11 +46,9 @@ public function __construct(\Iterator $iterator, array $matchPatterns, array $no
* Such case can be handled by child classes before calling the method if they want to
* apply a different behavior.
*
* @param string $string The string to be matched against filters
*
* @return bool
*/
protected function isAccepted($string)
protected function isAccepted(string $string)
{
// should at least not match one rule to exclude
foreach ($this->noMatchRegexps as $regex) {
Expand All @@ -77,11 +75,9 @@ protected function isAccepted($string)
/**
* Checks whether the string is a regex.
*
* @param string $str
*
* @return bool Whether the given string is a regex
* @return bool
*/
protected function isRegex($str)
protected function isRegex(string $str)
{
if (preg_match('/^(.{3,}?)[imsxuADU]*$/', $str, $m)) {
$start = substr($m[1], 0, 1);
Expand All @@ -104,9 +100,7 @@ protected function isRegex($str)
/**
* Converts string into regexp.
*
* @param string $str Pattern
*
* @return string regexp corresponding to a given string
* @return string
*/
abstract protected function toRegex($str);
abstract protected function toRegex(string $str);
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function accept()
*
* @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, '/').'/';
}
Expand Down
14 changes: 7 additions & 7 deletions src/Symfony/Component/Finder/Iterator/SortableIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ public function __construct(\Traversable $iterator, $sort, bool $reverseOrder =
$order = $reverseOrder ? -1 : 1;

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());
};
} 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());
};
} 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()) {
return -$order;
} elseif ($a->isFile() && $b->isDir()) {
Expand All @@ -59,21 +59,21 @@ public function __construct(\Traversable $iterator, $sort, bool $reverseOrder =
return $order * strcmp($a->getRealPath() ?: $a->getPathname(), $b->getRealPath() ?: $b->getPathname());
};
} 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());
};
} 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());
};
} 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());
};
} elseif (self::SORT_BY_NONE === $sort) {
$this->sort = $order;
} 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;
} else {
throw new \InvalidArgumentException('The SortableIterator takes a PHP callable or a valid built-in sort algorithm as an argument.');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ public function accept()
throw new \BadFunctionCallException('Not implemented');
}

public function isRegex($str)
public function isRegex(string $str)
{
return parent::isRegex($str);
}

public function toRegex($str)
public function toRegex(string $str)
{
throw new \BadFunctionCallException('Not implemented');
}
Expand Down