-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathPhpFunctionsScanner.php
More file actions
41 lines (31 loc) · 1.02 KB
/
PhpFunctionsScanner.php
File metadata and controls
41 lines (31 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
declare(strict_types = 1);
namespace Gettext\Scanner;
use PhpParser\NodeTraverser;
use PhpParser\Parser;
use PhpParser\ParserFactory;
class PhpFunctionsScanner implements FunctionsScannerInterface
{
protected Parser $parser;
protected ?array $validFunctions;
public function __construct(?array $validFunctions = null, ?Parser $parser = null)
{
$this->validFunctions = $validFunctions;
$this->parser = $parser ?: (new ParserFactory())->createForNewestSupportedVersion();
}
public function scan(string $code, string $filename): array
{
$ast = $this->parser->parse($code);
if (empty($ast)) {
return [];
}
$visitor = $this->createNodeVisitor($filename);
$traverser = new NodeTraverser($visitor);
$traverser->traverse($ast);
return $visitor->getFunctions();
}
protected function createNodeVisitor(string $filename): PhpNodeVisitor
{
return new PhpNodeVisitor($filename, $this->validFunctions);
}
}