Skip to content

Commit cb607ea

Browse files
authored
Merge pull request #18 from faissaloux/support-text-files
Support text files
2 parents 0c95af0 + 1a2b346 commit cb607ea

24 files changed

+466
-226
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ This plugin checks what's inside the files.
1313
| ^3.0 | ^8.2 | ^1.2.0 |
1414
| ^4.0 | ^8.3 | ^1.7.0 |
1515

16+
## Supported files
17+
- php
18+
- txt
19+
- stub
20+
1621
## Available Expectations
1722
### toReturnLowercase
1823
Make sure a file or directory files returns an array with all lowercase values.

src/Content.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Faissaloux\PestInside;
6+
7+
use ArrayIterator;
8+
use Faissaloux\PestInside\Contracts\Content as ContentContract;
9+
use Traversable;
10+
11+
final class Content implements ContentContract
12+
{
13+
/**
14+
* @var array<int|string, string|array<string, string>>
15+
*/
16+
private array $content = [];
17+
18+
private string $extension;
19+
20+
public function __construct(private string $file)
21+
{
22+
$this->extension = pathinfo($this->file, PATHINFO_EXTENSION);
23+
24+
$this->content = $this->extension === 'php'
25+
? include $this->file
26+
: explode("\n", file_get_contents($this->file) ?: '');
27+
}
28+
29+
public function getExtension(): string
30+
{
31+
return $this->extension;
32+
}
33+
34+
public function count(): int
35+
{
36+
return count($this->content);
37+
}
38+
39+
public function getIterator(): Traversable
40+
{
41+
return new ArrayIterator($this->content);
42+
}
43+
44+
public function offsetExists(mixed $offset): bool
45+
{
46+
return isset($this->content[$offset]);
47+
}
48+
49+
public function offsetGet(mixed $offset): mixed
50+
{
51+
return $this->content[$offset];
52+
}
53+
54+
public function offsetSet(mixed $offset, mixed $value): void
55+
{
56+
if (is_null($offset)) {
57+
$this->content[] = $value;
58+
} else {
59+
$this->content[$offset] = $value;
60+
}
61+
}
62+
63+
public function offsetUnset(mixed $offset): void
64+
{
65+
unset($this->content[$offset]);
66+
}
67+
}

src/Contracts/Content.php

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+
namespace Faissaloux\PestInside\Contracts;
6+
7+
use ArrayAccess;
8+
use Countable;
9+
use IteratorAggregate;
10+
11+
/**
12+
* @extends IteratorAggregate<int|string, string|array<string, string>>
13+
* @extends ArrayAccess<int|string, string|array<string, string>>
14+
*/
15+
interface Content extends ArrayAccess, Countable, IteratorAggregate
16+
{
17+
public function getExtension(): string;
18+
}

src/Exceptions/NotSupported.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Faissaloux\PestInside;
6+
7+
use Exception;
8+
9+
class NotSupported extends Exception {}

src/Expectation.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Faissaloux\PestInside;
66

7+
use Faissaloux\PestInside\Contracts\Content;
8+
79
final class Expectation extends Inside
810
{
911
use Investigator;
@@ -12,7 +14,7 @@ public function toReturnLowercase(int $depth = -1): void
1214
{
1315
$this->applyOnDirectory(
1416
$depth,
15-
fn (array $content): array => $this->notLowercasesIn($content),
17+
fn (Content $content): array => $this->notLowercasesIn($content),
1618
'Not lowercase detected'
1719
);
1820
}
@@ -21,7 +23,7 @@ public function toReturnUnique(int $depth = -1): void
2123
{
2224
$this->applyOnDirectory(
2325
$depth,
24-
fn (array $content): array => $this->duplicatesIn($content),
26+
fn (Content $content): array => $this->duplicatesIn($content),
2527
'Duplicates detected'
2628
);
2729
}
@@ -30,7 +32,7 @@ public function toReturnSingleWords(int $depth = -1): void
3032
{
3133
$this->applyOnDirectory(
3234
$depth,
33-
fn (array $content): array => $this->multipleWordsIn($content),
35+
fn (Content $content): array => $this->multipleWordsIn($content),
3436
'Not single words detected'
3537
);
3638
}
@@ -39,7 +41,7 @@ public function toBeOrdered(int $depth = -1): void
3941
{
4042
$this->applyOnDirectory(
4143
$depth,
42-
fn (array $content): array => $this->dataNotOrderedIn($content),
44+
fn (Content $content): array => $this->dataNotOrderedIn($content),
4345
'Your data is not ordered'
4446
);
4547
}
@@ -48,7 +50,7 @@ public function toReturnStrings(int $depth = -1): void
4850
{
4951
$this->applyOnDirectory(
5052
$depth,
51-
fn (array $content): array => $this->notStringsIn($content),
53+
fn (Content $content): array => $this->notStringsIn($content),
5254
'Not string detected'
5355
);
5456
}

src/Inside.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,6 @@ protected function checkFileExistence(string $file): void
3232
}
3333
}
3434

35-
/**
36-
* @return array<string>
37-
*/
38-
protected function getContentFrom(string $file): array
39-
{
40-
$content = include $file;
41-
42-
expect($content)->toBeArray();
43-
44-
return $content;
45-
}
46-
4735
/**
4836
* @return PestExpectation<string>
4937
*/
@@ -60,7 +48,7 @@ protected function applyOnDirectory(int $depth, callable $callback, string $mess
6048
foreach ($this->files as $file) {
6149
$this->checkFileExistence($file);
6250

63-
$content = $this->getContentFrom($file);
51+
$content = new Content($file);
6452

6553
$unwanted = $callback($content);
6654

src/Investigator.php

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,22 @@
44

55
namespace Faissaloux\PestInside;
66

7+
use Faissaloux\PestInside\Contracts\Content;
8+
79
/**
810
* @internal
911
*/
1012
trait Investigator
1113
{
1214
/**
13-
* @param array<string|array<string>> $array
15+
* @param Content|array<int|string, string|array<string, string>> $content
1416
* @return array<string>
1517
*/
16-
private function notLowercasesIn(array $array): array
18+
private function notLowercasesIn(Content|array $content): array
1719
{
1820
$unwanted = [];
1921

20-
foreach ($array as $word) {
22+
foreach ($content as $word) {
2123
if (is_array($word)) {
2224
array_push($unwanted, ...$this->notLowercasesIn($word));
2325

@@ -35,15 +37,15 @@ private function notLowercasesIn(array $array): array
3537
}
3638

3739
/**
38-
* @param array<string|array<string>> $array
40+
* @param Content|array<int|string, string|array<string, string>> $content
3941
* @return array<string>
4042
*/
41-
private function duplicatesIn(array $array): array
43+
private function duplicatesIn(Content|array $content): array
4244
{
4345
$unwanted = [];
4446
$unique = [];
4547

46-
foreach ($array as $word) {
48+
foreach ($content as $word) {
4749
if (is_array($word)) {
4850
array_push($unwanted, ...$this->duplicatesIn($word));
4951

@@ -61,14 +63,14 @@ private function duplicatesIn(array $array): array
6163
}
6264

6365
/**
64-
* @param array<string|array<string>> $array
66+
* @param Content|array<int|string, string|array<string, string>> $content
6567
* @return array<string>
6668
*/
67-
private function multipleWordsIn(array $array): array
69+
private function multipleWordsIn(Content|array $content): array
6870
{
6971
$unwanted = [];
7072

71-
foreach ($array as $word) {
73+
foreach ($content as $word) {
7274
if (is_array($word)) {
7375
array_push($unwanted, ...$this->multipleWordsIn($word));
7476

@@ -84,19 +86,19 @@ private function multipleWordsIn(array $array): array
8486
}
8587

8688
/**
87-
* @param array<string|array<string>> $array
89+
* @param Content|array<int|string, string|array<string, string>> $content
8890
* @return array<string>
8991
*/
90-
private function dataNotOrderedIn(array $array): array
92+
private function dataNotOrderedIn(Content|array $content): array
9193
{
92-
if (count($array) < 2) {
94+
if (count($content) < 2) {
9395
return [];
9496
}
9597

9698
$unwanted = [];
97-
$lastWord = $array[0];
99+
$lastWord = $content[0];
98100

99-
foreach ($array as $key => $value) {
101+
foreach ($content as $key => $value) {
100102
if ($key === 0) {
101103
continue;
102104
}
@@ -124,14 +126,18 @@ private function dataNotOrderedIn(array $array): array
124126
}
125127

126128
/**
127-
* @param array<string|array<string>> $array
129+
* @param Content|array<int|string, string|array<string, string>> $content
128130
* @return array<string>
129131
*/
130-
private function notStringsIn(array $array): array
132+
private function notStringsIn(Content|array $content): array
131133
{
134+
if ($content instanceof Content && ($extension = $content->getExtension()) !== 'php') {
135+
throw new NotSupported("toReturnStrings is not supported on $extension files.");
136+
}
137+
132138
$unwanted = [];
133139

134-
foreach ($array as $word) {
140+
foreach ($content as $word) {
135141
if (is_array($word)) {
136142
array_push($unwanted, ...$this->notStringsIn($word));
137143

src/helpers.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<?php
22

3-
function isDirOrPhp(string $maybeFile): bool
3+
function isDirOrSupportedFile(string $maybeFile): bool
44
{
5-
return isDir($maybeFile) || isPhp($maybeFile);
5+
return isDir($maybeFile)
6+
|| isPhp($maybeFile)
7+
|| isText($maybeFile);
68
}
79

810
function isDir(string $maybeDir): bool
@@ -17,6 +19,13 @@ function isPhp(string $file): bool
1719
return end($exploded) === 'php';
1820
}
1921

22+
function isText(string $file): bool
23+
{
24+
$exploded = explode('.', $file);
25+
26+
return end($exploded) === 'txt' || end($exploded) === 'stub';
27+
}
28+
2029
/**
2130
* @return array<string>
2231
*/
@@ -26,7 +35,7 @@ function getFilesIn(string $directory, int $depth = -1): array
2635

2736
if ($files = scandir($directory)) {
2837
$files = array_diff($files, ['.', '..']);
29-
$files = array_filter($files, 'isDirOrPhp');
38+
$files = array_filter($files, 'isDirOrSupportedFile');
3039

3140
foreach ($files as $file) {
3241
if (isDir($file)) {

test.php

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
f@issA!oux
3+
pest
4+
plugin
5+
inside
6+
lowercase
7+
lower
8+
case

0 commit comments

Comments
 (0)