|
30 | 30 | use FormatPHP\Extractor\MessageExtractorOptions; |
31 | 31 | use FormatPHP\Extractor\Parser\DescriptorParserInterface; |
32 | 32 | use FormatPHP\Extractor\Parser\ParserErrorCollection; |
| 33 | +use FormatPHP\Icu\MessageFormat\Parser; |
33 | 34 | use FormatPHP\Util\FileSystemHelper; |
34 | 35 | use LogicException; |
35 | 36 | use PhpParser\Lexer; |
|
43 | 44 | use function assert; |
44 | 45 | use function count; |
45 | 46 | use function in_array; |
| 47 | +use function mb_strpos; |
46 | 48 | use function pathinfo; |
47 | 49 |
|
48 | 50 | use const PATHINFO_EXTENSION; |
@@ -88,9 +90,14 @@ public function __invoke( |
88 | 90 | return new DescriptorCollection(); |
89 | 91 | } |
90 | 92 |
|
| 93 | + $fileContents = $this->fileSystemHelper->getContents($filePath); |
| 94 | + if (!$this->hasFormattingFunctions($fileContents, $options->functionNames)) { |
| 95 | + return new DescriptorCollection(); |
| 96 | + } |
| 97 | + |
91 | 98 | $lexer = new Emulative(self::LEXER_OPTIONS); |
92 | 99 | $parser = new Php7Parser($lexer); |
93 | | - $statements = $parser->parse($this->fileSystemHelper->getContents($filePath)); |
| 100 | + $statements = $parser->parse($fileContents); |
94 | 101 |
|
95 | 102 | $descriptorCollector = new DescriptorCollectorVisitor( |
96 | 103 | $filePath, |
@@ -199,4 +206,18 @@ private function isPhpFile(string $filePath): bool |
199 | 206 |
|
200 | 207 | return in_array($extension, self::PHP_PATH_EXTENSIONS); |
201 | 208 | } |
| 209 | + |
| 210 | + /** |
| 211 | + * @param string[] $functions |
| 212 | + */ |
| 213 | + private function hasFormattingFunctions(string $code, array $functions): bool |
| 214 | + { |
| 215 | + foreach ($functions as $function) { |
| 216 | + if (mb_strpos($code, $function, 0, Parser::ENCODING) !== false) { |
| 217 | + return true; |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + return false; |
| 222 | + } |
202 | 223 | } |
0 commit comments