Skip to content

Commit 74b5d06

Browse files
committed
[+]: "PhpCodeParser" -> allow to exclude some files
1 parent 9bd47bb commit 74b5d06

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed

src/voku/SimplePhpParser/Parsers/PhpCodeChecker.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public static function checkFromString(
3434
* @param bool $skipFunctionsWithLeadingUnderscore
3535
* @param bool $skipParseErrorsAsError
3636
* @param string[] $composerAutoloaderProjectPaths
37+
* @param string[] $pathExcludeRegex
3738
*
3839
* @return string[][]
3940
*/
@@ -44,11 +45,13 @@ public static function checkPhpFiles(
4445
bool $skipDeprecatedFunctions = false,
4546
bool $skipFunctionsWithLeadingUnderscore = false,
4647
bool $skipParseErrorsAsError = true,
47-
array $composerAutoloaderProjectPaths = []
48+
array $composerAutoloaderProjectPaths = [],
49+
array $pathExcludeRegex = []
4850
): array {
4951
$phpInfo = PhpCodeParser::getPhpFiles(
5052
$path,
51-
$composerAutoloaderProjectPaths
53+
$composerAutoloaderProjectPaths,
54+
$pathExcludeRegex
5255
);
5356

5457
$errors = $phpInfo->getParseErrors();

src/voku/SimplePhpParser/Parsers/PhpCodeParser.php

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,26 @@
2323

2424
final class PhpCodeParser
2525
{
26-
public static function getFromString(string $code): ParserContainer
27-
{
28-
return self::getPhpFiles($code);
26+
/**
27+
* @param string $code
28+
* @param string[] $composerAutoloaderProjectPaths
29+
*
30+
* @return \voku\SimplePhpParser\Parsers\Helper\ParserContainer
31+
*/
32+
public static function getFromString(
33+
string $code,
34+
array $composerAutoloaderProjectPaths = []
35+
): ParserContainer {
36+
return self::getPhpFiles(
37+
$code,
38+
$composerAutoloaderProjectPaths
39+
);
2940
}
3041

3142
/**
3243
* @param string $pathOrCode
3344
* @param string[] $composerAutoloaderProjectPaths
45+
* @param string[] $pathExcludeRegex
3446
*
3547
* @return \voku\SimplePhpParser\Parsers\Helper\ParserContainer
3648
*
@@ -39,9 +51,13 @@ public static function getFromString(string $code): ParserContainer
3951
*/
4052
public static function getPhpFiles(
4153
string $pathOrCode,
42-
array $composerAutoloaderProjectPaths = []
54+
array $composerAutoloaderProjectPaths = [],
55+
array $pathExcludeRegex = []
4356
): ParserContainer {
44-
$phpCodes = self::getCode($pathOrCode);
57+
$phpCodes = self::getCode(
58+
$pathOrCode,
59+
$pathExcludeRegex
60+
);
4561

4662
$parserContainer = new ParserContainer();
4763
$visitor = new ASTVisitor($parserContainer);
@@ -235,14 +251,17 @@ public static function file_get_contents_with_cache(string $fileName, Cache $cac
235251
}
236252

237253
/**
238-
* @param string $pathOrCode
254+
* @param string $pathOrCode
255+
* @param string[] $pathExcludeRegex
239256
*
240257
* @return array
241258
*
242259
* @psalm-return array<string, array{content: string, fileName: null|string}>
243260
*/
244-
private static function getCode(string $pathOrCode): array
245-
{
261+
private static function getCode(
262+
string $pathOrCode,
263+
array $pathExcludeRegex = []
264+
): array {
246265
// init
247266
$phpCodes = [];
248267
/** @var SplFileInfo[] $phpFileIterators */
@@ -271,6 +290,12 @@ private static function getCode(string $pathOrCode): array
271290
continue;
272291
}
273292

293+
foreach ($pathExcludeRegex as $regex) {
294+
if (\preg_match($regex, $path)) {
295+
continue 2;
296+
}
297+
}
298+
274299
$phpFilePromises[] = Worker\enqueueCallable(
275300
[self::class, 'file_get_contents_with_cache'],
276301
$path,

tests/ParserTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ public function testSimpleOneClassV2(): void
4343

4444
public function testSimpleDirectory(): void
4545
{
46-
$phpCode = PhpCodeParser::getPhpFiles(__DIR__ . '/');
46+
$phpCode = PhpCodeParser::getPhpFiles(
47+
__DIR__ . '/',
48+
[],
49+
['/Dummy5/']
50+
);
4751

4852
$phpClasses = $phpCode->getClasses();
4953

0 commit comments

Comments
 (0)