Skip to content

Commit 9384557

Browse files
Merge pull request #210 from CircleCode
allow ThatIs filter to take multivalued options (BC BREAK)
2 parents ed0279f + b213da4 commit 9384557

File tree

2 files changed

+91
-10
lines changed

2 files changed

+91
-10
lines changed

src/Hook/Condition/FileStaged/ThatIs.php

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,16 @@ class ThatIs implements Condition, Constrained
4343
/**
4444
* Directory path to check e.g. 'src/' or 'path/To/Custom/Directory/'
4545
*
46-
* @var string
46+
* @var string[]
4747
*/
48-
private string $directory;
48+
private array $directories;
4949

5050
/**
5151
* File type to check e.g. 'php' or 'html'
5252
*
53-
* @var string
53+
* @var string[]
5454
*/
55-
private string $suffix;
55+
private array $suffixes;
5656

5757
/**
5858
* OfType constructor
@@ -61,8 +61,8 @@ class ThatIs implements Condition, Constrained
6161
*/
6262
public function __construct(array $options)
6363
{
64-
$this->directory = $options['inDirectory'] ?? '';
65-
$this->suffix = $options['ofType'] ?? '';
64+
$this->directories = (array)($options['inDirectory'] ?? []);
65+
$this->suffixes = (array)($options['ofType'] ?? []);
6666
}
6767

6868
/**
@@ -98,10 +98,17 @@ public function isTrue(IO $io, Repository $repository): bool
9898
*/
9999
private function filterFilesByDirectory(array $files): array
100100
{
101-
if (empty($this->directory)) {
101+
if (empty($this->directories)) {
102102
return $files;
103103
}
104-
return array_filter($files, fn($file) => strpos($file, $this->directory) !== false);
104+
return array_filter($files, function ($file) {
105+
foreach ($this->directories as $directory) {
106+
if (str_starts_with($file, $directory)) {
107+
return true;
108+
}
109+
}
110+
return false;
111+
});
105112
}
106113

107114
/**
@@ -112,9 +119,13 @@ private function filterFilesByDirectory(array $files): array
112119
*/
113120
private function filterFilesByType(array $files): array
114121
{
115-
if (empty($this->suffix)) {
122+
if (empty($this->suffixes)) {
116123
return $files;
117124
}
118-
return array_filter($files, fn($file) => strtolower(pathinfo($file, PATHINFO_EXTENSION)) === $this->suffix);
125+
return array_filter($files, fn($file) => in_array(
126+
strtolower(pathinfo($file, PATHINFO_EXTENSION)),
127+
$this->suffixes,
128+
true
129+
));
119130
}
120131
}

tests/unit/Hook/Condition/FileStaged/ThatIsTest.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,34 @@ public function testStagedTrueType(): void
4343
$this->assertTrue($thatIs->isTrue($io, $repo));
4444
}
4545

46+
/**
47+
* Tests ThatIs::isTrue
48+
*/
49+
public function testStagedTrueMultipleType(): void
50+
{
51+
$io = $this->createIOMock();
52+
$repo = $this->createRepositoryMock();
53+
$index = $this->createGitIndexOperator(['foo.php', 'bar.php']);
54+
$repo->expects($this->once())->method('getIndexOperator')->willReturn($index);
55+
56+
$thatIs = new ThatIs(['ofType' => ['php', 'js']]);
57+
$this->assertTrue($thatIs->isTrue($io, $repo));
58+
}
59+
60+
/**
61+
* Tests ThatIs::isTrue
62+
*/
63+
public function testStagedFalseMultipleType(): void
64+
{
65+
$io = $this->createIOMock();
66+
$repo = $this->createRepositoryMock();
67+
$index = $this->createGitIndexOperator(['foo.php', 'bar.php']);
68+
$repo->expects($this->once())->method('getIndexOperator')->willReturn($index);
69+
70+
$thatIs = new ThatIs(['ofType' => ['ts', 'js']]);
71+
$this->assertFalse($thatIs->isTrue($io, $repo));
72+
}
73+
4674
/**
4775
* Tests ThatIs::isTrue
4876
*/
@@ -57,6 +85,48 @@ public function testStagedTrueDirectory(): void
5785
$this->assertTrue($thatIs->isTrue($io, $repo));
5886
}
5987

88+
/**
89+
* Tests ThatIs::isTrue
90+
*/
91+
public function testStagedFalsePartialDirectory(): void
92+
{
93+
$io = $this->createIOMock();
94+
$repo = $this->createRepositoryMock();
95+
$index = $this->createGitIndexOperator(['foo/foo.php', 'foo/bar/bar.js', 'fiz/baz.txt']);
96+
$repo->expects($this->once())->method('getIndexOperator')->willReturn($index);
97+
98+
$thatIs = new ThatIs(['inDirectory' => 'bar/']);
99+
$this->assertFalse($thatIs->isTrue($io, $repo));
100+
}
101+
102+
/**
103+
* Tests ThatIs::isTrue
104+
*/
105+
public function testStagedTrueMultipleDirectory(): void
106+
{
107+
$io = $this->createIOMock();
108+
$repo = $this->createRepositoryMock();
109+
$index = $this->createGitIndexOperator(['foo/foo.php', 'bar/bar.js', 'fiz/baz.txt']);
110+
$repo->expects($this->once())->method('getIndexOperator')->willReturn($index);
111+
112+
$thatIs = new ThatIs(['inDirectory' => ['bar/', 'baz/']]);
113+
$this->assertTrue($thatIs->isTrue($io, $repo));
114+
}
115+
116+
/**
117+
* Tests ThatIs::isTrue
118+
*/
119+
public function testStagedFalseMultipleDirectory(): void
120+
{
121+
$io = $this->createIOMock();
122+
$repo = $this->createRepositoryMock();
123+
$index = $this->createGitIndexOperator(['foo/foo.php', 'bar/bar.js', 'fiz/baz.txt']);
124+
$repo->expects($this->once())->method('getIndexOperator')->willReturn($index);
125+
126+
$thatIs = new ThatIs(['inDirectory' => ['foobar/', 'baz/']]);
127+
$this->assertFalse($thatIs->isTrue($io, $repo));
128+
}
129+
60130
/**
61131
* Tests ThatIs::isTrue
62132
*/

0 commit comments

Comments
 (0)