-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from faissaloux/deep-scan
Scan deep directories
- Loading branch information
Showing
6 changed files
with
104 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,46 @@ | ||
<?php | ||
|
||
function isDirOrPhp(string $maybeFile): bool | ||
{ | ||
return isDir($maybeFile) || isPhp($maybeFile); | ||
} | ||
|
||
function isDir(string $maybeDir): bool | ||
{ | ||
return ! str_contains($maybeDir, '.'); | ||
} | ||
|
||
function isPhp(string $file): bool | ||
{ | ||
$exploded = explode('.', $file); | ||
|
||
return end($exploded) === 'php'; | ||
} | ||
|
||
/** | ||
* @return array<string> | ||
*/ | ||
function getFilesIn(string $directory, int $depth = -1): array | ||
{ | ||
$allFiles = []; | ||
|
||
if ($files = scandir($directory)) { | ||
$files = array_diff($files, ['.', '..']); | ||
$files = array_filter($files, 'isDirOrPhp'); | ||
|
||
foreach ($files as $file) { | ||
if (isDir($file)) { | ||
if ($depth !== 0) { | ||
$deeperFiles = getFilesIn($directory.DIRECTORY_SEPARATOR.$file, $depth < 0 ? $depth : $depth - 1); | ||
$allFiles = array_merge($deeperFiles, $allFiles); | ||
} | ||
} else { | ||
$allFiles[] = $directory.DIRECTORY_SEPARATOR.$file; | ||
} | ||
} | ||
|
||
return $allFiles; | ||
} | ||
|
||
return []; | ||
} |
14 changes: 14 additions & 0 deletions
14
tests/Fixtures/shouldAllBeLowercase/subdirectory/notAllLowerCase.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
return [ | ||
'', | ||
'f@issa!oux', | ||
'pest', | ||
'plugin', | ||
'inside', | ||
'lowercase', | ||
'loWer', | ||
'case', | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
uses()->group('helpers'); | ||
|
||
it('gets all php files in directory and subdirectories', function (): void { | ||
$files = getFilesIn('tests/Fixtures'); | ||
|
||
expect($files)->toBeArray()->toHaveCount(7); | ||
}); | ||
|
||
it('gets all direct php files in directory', function (): void { | ||
$files = getFilesIn('tests/Fixtures', depth: 0); | ||
|
||
expect($files)->toBeArray()->toHaveCount(2); | ||
}); | ||
|
||
it('gets all php files in directory depth 1', function (): void { | ||
$files = getFilesIn('tests/Fixtures', depth: 1); | ||
|
||
expect($files)->toBeArray()->toHaveCount(6); | ||
}); | ||
|
||
it('gets all php files in directory and subdirectories on negative depth', function (): void { | ||
$files = getFilesIn('tests/Fixtures', depth: -4); | ||
|
||
expect($files)->toBeArray()->toHaveCount(7); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters