[5.0][Finder] add parameter type-hints

This commit is contained in:
smoench 2019-06-28 09:24:44 +02:00
parent a25848b2b5
commit 7f98903d05
No known key found for this signature in database
GPG Key ID: EB008C0F094A779B
10 changed files with 32 additions and 60 deletions

View File

@ -31,12 +31,7 @@ class Comparator
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;
}
@ -54,13 +49,11 @@ class Comparator
/**
* 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 = '==';
}

View File

@ -336,13 +336,11 @@ class Finder implements \IteratorAggregate, \Countable
*
* 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;
@ -358,13 +356,11 @@ class Finder implements \IteratorAggregate, \Countable
*
* 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;
@ -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.
*
* @param bool $useNaturalSort Whether to use natural sort or not, disabled by default
*
* @return $this
*
* @see SortableIterator
@ -563,13 +557,11 @@ class Finder implements \IteratorAggregate, \Countable
*
* 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;
}
@ -577,7 +569,7 @@ class Finder implements \IteratorAggregate, \Countable
/**
* 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
*
@ -644,7 +636,7 @@ class Finder implements \IteratorAggregate, \Countable
*
* @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();
@ -789,11 +781,9 @@ class Finder implements \IteratorAggregate, \Countable
*
* Excluding: (s)ftp:// wrapper
*
* @param string $dir
*
* @return string
*/
private function normalizeDir($dir)
private function normalizeDir(string $dir)
{
$dir = rtrim($dir, '/'.\DIRECTORY_SEPARATOR);

View File

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

View File

@ -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)
{

View File

@ -51,7 +51,7 @@ class FilecontentFilterIterator extends MultiplePcreFilterIterator
*
* @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, '/').'/';
}

View File

@ -40,7 +40,7 @@ class FilenameFilterIterator extends MultiplePcreFilterIterator
*
* @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);
}

View File

@ -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)
{
@ -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
* 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) {
@ -77,11 +75,9 @@ abstract class MultiplePcreFilterIterator extends \FilterIterator
/**
* 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);
@ -104,9 +100,7 @@ abstract class MultiplePcreFilterIterator extends \FilterIterator
/**
* 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);
}

View File

@ -49,7 +49,7 @@ class PathFilterIterator extends MultiplePcreFilterIterator
*
* @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, '/').'/';
}

View File

@ -41,15 +41,15 @@ class SortableIterator implements \IteratorAggregate
$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()) {
@ -59,21 +59,21 @@ class SortableIterator implements \IteratorAggregate
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.');

View File

@ -59,12 +59,12 @@ class TestMultiplePcreFilterIterator extends MultiplePcreFilterIterator
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');
}