Skip to content

Commit 585e20e

Browse files
Improve type safety
1 parent 39b34bb commit 585e20e

File tree

4 files changed

+46
-7
lines changed

4 files changed

+46
-7
lines changed

.psalm/baseline.xml

-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,5 @@
44
<MissingTemplateParam>
55
<code>Iterator</code>
66
</MissingTemplateParam>
7-
<PossiblyFalseArgument>
8-
<code>$this-&gt;basePath</code>
9-
</PossiblyFalseArgument>
107
</file>
118
</files>

src/Facade.php

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
final class Facade
2121
{
2222
/**
23+
* @psalm-param list<string>|string $suffixes
24+
* @psalm-param list<string>|string $prefixes
25+
* @psalm-param list<string> $exclude
26+
*
2327
* @psalm-return list<string>
2428
*/
2529
public function getFilesAsArray(string $path, array|string $suffixes = '', array|string $prefixes = '', array $exclude = []): array

src/Factory.php

+13-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use function array_filter;
1414
use function array_map;
1515
use function array_merge;
16+
use function array_values;
1617
use function glob;
1718
use function is_dir;
1819
use function is_string;
@@ -27,6 +28,12 @@
2728
*/
2829
final class Factory
2930
{
31+
/**
32+
* @psalm-param list<string>|string $paths
33+
* @psalm-param list<string>|string $suffixes
34+
* @psalm-param list<string>|string $prefixes
35+
* @psalm-param list<string> $exclude
36+
*/
3037
public function getFileIterator(array|string $paths, array|string $suffixes = '', array|string $prefixes = '', array $exclude = []): AppendIterator
3138
{
3239
if (is_string($paths)) {
@@ -73,6 +80,11 @@ public function getFileIterator(array|string $paths, array|string $suffixes = ''
7380
return $iterator;
7481
}
7582

83+
/**
84+
* @psalm-param list<string> $paths
85+
*
86+
* @psalm-return list<string>
87+
*/
7688
private function resolveWildcards(array $paths): array
7789
{
7890
$_paths = [[]];
@@ -87,6 +99,6 @@ private function resolveWildcards(array $paths): array
8799
}
88100
}
89101

90-
return array_filter(array_merge(...$_paths));
102+
return array_values(array_filter(array_merge(...$_paths)));
91103
}
92104
}

src/Iterator.php

+29-3
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@
1111

1212
use function array_filter;
1313
use function array_map;
14+
use function array_values;
15+
use function assert;
1416
use function preg_match;
1517
use function realpath;
1618
use function str_ends_with;
1719
use function str_replace;
1820
use function str_starts_with;
1921
use FilterIterator;
22+
use SplFileInfo;
2023

2124
/**
2225
* @internal This class is not covered by the backward compatibility promise for phpunit/php-file-iterator
@@ -27,23 +30,43 @@ final class Iterator extends FilterIterator
2730

2831
public const SUFFIX = 1;
2932
private string|false $basePath;
33+
34+
/**
35+
* @psalm-var list<string>
36+
*/
3037
private array $suffixes;
38+
39+
/**
40+
* @psalm-var list<string>
41+
*/
3142
private array $prefixes;
43+
44+
/**
45+
* @psalm-var list<string>
46+
*/
3247
private array $exclude;
3348

49+
/**
50+
* @psalm-param list<string> $suffixes
51+
* @psalm-param list<string> $prefixes
52+
* @psalm-param list<string> $exclude
53+
*/
3454
public function __construct(string $basePath, \Iterator $iterator, array $suffixes = [], array $prefixes = [], array $exclude = [])
3555
{
3656
$this->basePath = realpath($basePath);
3757
$this->prefixes = $prefixes;
3858
$this->suffixes = $suffixes;
39-
$this->exclude = array_filter(array_map('realpath', $exclude));
59+
$this->exclude = array_values(array_filter(array_map('realpath', $exclude)));
4060

4161
parent::__construct($iterator);
4262
}
4363

4464
public function accept(): bool
4565
{
46-
$current = $this->getInnerIterator()->current();
66+
$current = $this->getInnerIterator()->current();
67+
68+
assert($current instanceof SplFileInfo);
69+
4770
$filename = $current->getFilename();
4871
$realPath = $current->getRealPath();
4972

@@ -61,7 +84,7 @@ public function accept(): bool
6184
private function acceptPath(string $path): bool
6285
{
6386
// Filter files in hidden directories by checking path that is relative to the base path.
64-
if (preg_match('=/\.[^/]*/=', str_replace($this->basePath, '', $path))) {
87+
if (preg_match('=/\.[^/]*/=', str_replace((string) $this->basePath, '', $path))) {
6588
return false;
6689
}
6790

@@ -84,6 +107,9 @@ private function acceptSuffix(string $filename): bool
84107
return $this->acceptSubString($filename, $this->suffixes, self::SUFFIX);
85108
}
86109

110+
/**
111+
* @psalm-param list<string> $subStrings
112+
*/
87113
private function acceptSubString(string $filename, array $subStrings, int $type): bool
88114
{
89115
if (empty($subStrings)) {

0 commit comments

Comments
 (0)