Skip to content

Commit bc98c0e

Browse files
committed
Adds missing test
1 parent ee3fbc3 commit bc98c0e

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

src/Glob.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,21 +64,23 @@ public static function glob($pattern, $flags = 0, $forceFallback = false)
6464
* @param int $flags
6565
* @return array
6666
*/
67-
public static function globArray(string $pattern, array $array, int $flags = FNM_CASEFOLD)
67+
public static function globArray(string $pattern, array $array, int $flags = FNM_CASEFOLD): array
6868
{
6969
$pattern = \str_replace(['{', '}'], '', $pattern);
7070

7171
$patternParts = \explode(',', $pattern);
7272

73-
foreach ($patternParts as $index => $patternPart) {
74-
$matches[] = \array_filter($array, function ($val) use ($patternPart, $flags) {
75-
return \fnmatch($patternPart, $val, $flags);
73+
foreach ($patternParts as $patternPart) {
74+
$matches[] = \array_filter($array, function ($value) use ($patternPart, $flags) {
75+
return \fnmatch($patternPart, $value, $flags);
7676
});
7777
}
7878

7979
$excludes = [];
80-
foreach ($matches as $index => $value) {
81-
$excludes[] = \array_values($value)[0];
80+
foreach ($matches as $match) {
81+
if (\count(\array_values($match)) > 0) {
82+
$excludes[] = \array_values($match)[0];
83+
}
8284
}
8385

8486
return $excludes;

tests/GlobTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Stolt\LeanPackage\Tests;
6+
7+
use PHPUnit\Framework\Attributes\Test;
8+
use Stolt\LeanPackage\Glob;
9+
10+
class GlobTest extends TestCase
11+
{
12+
#[Test]
13+
public function globArrayWorksAsExpected(): void
14+
{
15+
$expectedResult = ['README.md', 'LICENSE.md', 'docs/'];
16+
17+
$actualResult = Glob::globArray(
18+
'{READ*.md,LICENSE.md,docs*}',
19+
['spec.dist.yml', 'phpunit.dist.xml', 'README.md', 'LICENSE.md', 'docs/']
20+
);
21+
$this->assertEquals($expectedResult, $actualResult);
22+
23+
$actualResult = Glob::globArray(
24+
'{composer.lock}',
25+
['spec.dist.yml', 'phpunit.dist.xml', 'README.md', 'LICENSE.md', 'docs/']
26+
);
27+
$this->assertEquals([], $actualResult);
28+
}
29+
}

0 commit comments

Comments
 (0)