Skip to content

Commit 6a7aab8

Browse files
authored
Merge pull request #12 from faissaloux/support-nested-arrays
Support nested arrays
2 parents c5a2bf7 + 09654d4 commit 6a7aab8

12 files changed

+196
-9
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ return [
3737
'lower',
3838
'case',
3939
'lowercase',
40+
'array' => [
41+
'lower',
42+
'case',
43+
],
4044
];
4145

4246
```

src/Investigator.php

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
trait Investigator
88
{
99
/**
10-
* @param array<string> $array
10+
* @param array<string|array<string>> $array
1111
* @return array<string>
1212
*/
1313
private function lowercasesIn(array $array): array
@@ -19,6 +19,12 @@ private function lowercasesIn(array $array): array
1919
continue;
2020
}
2121

22+
if (is_array($word)) {
23+
array_push($unwanted, ...$this->lowercasesIn($word));
24+
25+
continue;
26+
}
27+
2228
if (! ctype_lower(preg_replace('/[^A-Za-z]/', '', $word))) {
2329
$unwanted[] = $word;
2430
}
@@ -28,20 +34,51 @@ private function lowercasesIn(array $array): array
2834
}
2935

3036
/**
31-
* @param array<string> $array
37+
* @param array<string|array<string>> $array
3238
* @return array<string>
3339
*/
3440
private function duplicatesIn(array $array): array
3541
{
36-
return array_diff_assoc($array, array_unique($array));
42+
$unwanted = [];
43+
$unique = [];
44+
45+
foreach ($array as $word) {
46+
if (is_array($word)) {
47+
array_push($unwanted, ...$this->duplicatesIn($word));
48+
49+
continue;
50+
}
51+
52+
if (! in_array($word, $unique)) {
53+
$unique[] = $word;
54+
} else {
55+
$unwanted[] = $word;
56+
}
57+
}
58+
59+
return $unwanted;
3760
}
3861

3962
/**
40-
* @param array<string> $array
63+
* @param array<string|array<string>> $array
4164
* @return array<string>
4265
*/
4366
private function singleWordsIn(array $array): array
4467
{
45-
return array_filter($array, fn (string $word): bool => str_contains(trim($word), ' '));
68+
$unwanted = [];
69+
70+
foreach ($array as $word) {
71+
if (is_array($word)) {
72+
array_push($unwanted, ...$this->singleWordsIn($word));
73+
74+
continue;
75+
}
76+
77+
if (str_contains(trim((string) $word), ' ')) {
78+
$unwanted[] = $word;
79+
}
80+
}
81+
82+
return $unwanted;
4683
}
4784
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
return [
6+
'',
7+
'f@issa!oux',
8+
'pestphp',
9+
'pest',
10+
'plugin inside',
11+
'plugin' => [
12+
'pest',
13+
'inside',
14+
],
15+
'inside',
16+
'lowercase',
17+
'lower',
18+
'case',
19+
];
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
return [
6+
'',
7+
'f@issa!oux',
8+
'pestphp',
9+
'pest',
10+
'plugin inside',
11+
'plugin' => [
12+
'pest',
13+
'inSide',
14+
],
15+
'inside',
16+
'lowercase',
17+
'lower',
18+
'case',
19+
];
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
return [
6+
'',
7+
'f@issa!oux',
8+
'pestphp',
9+
'pest',
10+
'plugin inside',
11+
'plugin' => [
12+
'pest',
13+
'inside',
14+
'not single word',
15+
],
16+
'inside',
17+
'lowercase',
18+
'lower',
19+
'case',
20+
];
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
return [
6+
'',
7+
'f@issa!oux',
8+
'pestphp',
9+
'pest',
10+
'plugin inside',
11+
'plugin' => [
12+
'pest',
13+
'inSide',
14+
'pest',
15+
],
16+
'plugin',
17+
'inside',
18+
'unique',
19+
'inside',
20+
'word',
21+
];
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
return [
6+
'',
7+
'f@issa!oux',
8+
'pestphp',
9+
'pest',
10+
'plugin' => [
11+
'pest',
12+
'inside',
13+
],
14+
'inside',
15+
'lowercase',
16+
'lower',
17+
'case',
18+
];
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
return [
6+
'',
7+
'f@issa!oux',
8+
'pestphp',
9+
'pest',
10+
'plugin inside',
11+
'plugin' => [
12+
'pest',
13+
'inSide',
14+
],
15+
'plugin',
16+
'inside',
17+
'unique',
18+
'word',
19+
];

tests/Unit/helpers/getFilesIn.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@
55
it('gets all php files in directory and subdirectories', function (): void {
66
$files = getFilesIn('tests/Fixtures');
77

8-
expect($files)->toBeArray()->toHaveCount(12);
8+
expect($files)->toBeArray()->toHaveCount(18);
99
});
1010

1111
it('gets all direct php files in directory', function (): void {
1212
$files = getFilesIn('tests/Fixtures', depth: 0);
1313

14-
expect($files)->toBeArray()->toHaveCount(5);
14+
expect($files)->toBeArray()->toHaveCount(11);
1515
});
1616

1717
it('gets all php files in directory depth 1', function (): void {
1818
$files = getFilesIn('tests/Fixtures', depth: 1);
1919

20-
expect($files)->toBeArray()->toHaveCount(10);
20+
expect($files)->toBeArray()->toHaveCount(16);
2121
});
2222

2323
it('gets all php files in directory and subdirectories on negative depth', function (): void {
2424
$files = getFilesIn('tests/Fixtures', depth: -4);
2525

26-
expect($files)->toBeArray()->toHaveCount(12);
26+
expect($files)->toBeArray()->toHaveCount(18);
2727
});

tests/toReturnLowercase.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
->not->toReturnLowercase();
1313
});
1414

15+
it('passes when all nested arrays content is lowercase', function (): void {
16+
expect('tests/Fixtures/returnsNestedLowercase.php')
17+
->toReturnLowercase();
18+
});
19+
1520
it('passes when directory is empty', function (): void {
1621
expect('tests/Fixtures/empty')
1722
->toReturnLowercase();
@@ -32,6 +37,11 @@
3237
->not->toReturnLowercase();
3338
})->throws(ExpectationFailedException::class);
3439

40+
it('fails when not all nested arrays content is lowercase', function (): void {
41+
expect('tests/Fixtures/returnsNestedNotAllLowercase.php')
42+
->toReturnLowercase();
43+
})->throws(ExpectationFailedException::class, 'Not lowercase detected: inSide');
44+
3545
it('fails when file does not exist', function (): void {
3646
expect('tests/Fixtures/notExist.php')
3747
->toReturnLowercase();

0 commit comments

Comments
 (0)