Skip to content

Commit b61ad44

Browse files
authored
fix: skip files not having extraction functions to improve speed (#32)
1 parent 595c0f1 commit b61ad44

5 files changed

Lines changed: 84 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,28 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
66
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
77

8+
## 0.3.2 - 2021-12-17
9+
10+
### Added
11+
12+
- Nothing.
13+
14+
### Changed
15+
16+
- Nothing.
17+
18+
### Deprecated
19+
20+
- Nothing.
21+
22+
### Removed
23+
24+
- Nothing.
25+
26+
### Fixed
27+
28+
- Check the contents of the file before parsing, to see if any of the formatting functions exist; if not, skip parsing the file
29+
830
## 0.3.1 - 2021-12-17
931

1032
### Added

src/Console/Command/ExtractCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
use Symfony\Component\Console\Output\OutputInterface;
4646
use Symfony\Component\Console\Style\SymfonyStyle;
4747

48+
use function array_filter;
4849
use function array_map;
4950
use function array_merge;
5051
use function array_unique;
@@ -275,7 +276,7 @@ private function buildOptions(InputInterface $input): MessageExtractorOptions
275276

276277
/** @var string $inputFunctionNames */
277278
$inputFunctionNames = $input->getOption('addl-func') ?? '';
278-
$additionalFunctionNames = array_map('trim', explode(',', $inputFunctionNames));
279+
$additionalFunctionNames = array_filter(array_map('trim', explode(',', $inputFunctionNames)));
279280
$options->functionNames = array_unique(array_merge($options->functionNames, $additionalFunctionNames));
280281

281282
return $options;

src/Extractor/Parser/Descriptor/PhpParser.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use FormatPHP\Extractor\MessageExtractorOptions;
3131
use FormatPHP\Extractor\Parser\DescriptorParserInterface;
3232
use FormatPHP\Extractor\Parser\ParserErrorCollection;
33+
use FormatPHP\Icu\MessageFormat\Parser;
3334
use FormatPHP\Util\FileSystemHelper;
3435
use LogicException;
3536
use PhpParser\Lexer;
@@ -43,6 +44,7 @@
4344
use function assert;
4445
use function count;
4546
use function in_array;
47+
use function mb_strpos;
4648
use function pathinfo;
4749

4850
use const PATHINFO_EXTENSION;
@@ -88,9 +90,14 @@ public function __invoke(
8890
return new DescriptorCollection();
8991
}
9092

93+
$fileContents = $this->fileSystemHelper->getContents($filePath);
94+
if (!$this->hasFormattingFunctions($fileContents, $options->functionNames)) {
95+
return new DescriptorCollection();
96+
}
97+
9198
$lexer = new Emulative(self::LEXER_OPTIONS);
9299
$parser = new Php7Parser($lexer);
93-
$statements = $parser->parse($this->fileSystemHelper->getContents($filePath));
100+
$statements = $parser->parse($fileContents);
94101

95102
$descriptorCollector = new DescriptorCollectorVisitor(
96103
$filePath,
@@ -199,4 +206,18 @@ private function isPhpFile(string $filePath): bool
199206

200207
return in_array($extension, self::PHP_PATH_EXTENSIONS);
201208
}
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+
}
202223
}

tests/Extractor/Parser/Descriptor/PhpParserTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,36 @@ public function testParse11(): void
423423
$this->assertCount(0, $receivedErrors);
424424
}
425425

426+
/**
427+
* This test covers situations where the formatting functions are not
428+
* present in the source code being analyzed, so we should short-circuit
429+
* and skip parsing the file.
430+
*/
431+
public function testParse12(): void
432+
{
433+
$fileSystemHelper = $this->mockery(FileSystemHelper::class);
434+
435+
$fileSystemHelper
436+
->expects()
437+
->getContents(__DIR__ . '/fixtures/php-parser-12.php')
438+
->andReturn(file_get_contents(__DIR__ . '/fixtures/php-parser-12.php'));
439+
440+
$fileSystemHelper->shouldNotReceive('writeContents');
441+
442+
$errors = new ParserErrorCollection();
443+
444+
$options = new MessageExtractorOptions();
445+
$options->functionNames = ['formatMessage', 'translate', 't'];
446+
447+
$parser = new PhpParser($fileSystemHelper);
448+
$descriptors = $parser(__DIR__ . '/fixtures/php-parser-12.php', $options, $errors);
449+
$receivedErrors = $this->compileErrors($errors);
450+
451+
$this->assertContainsOnlyInstancesOf(DescriptorInterface::class, $descriptors);
452+
$this->assertCount(0, $descriptors);
453+
$this->assertCount(0, $receivedErrors);
454+
}
455+
426456
/**
427457
* @return string[]
428458
*/
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
/**
4+
* This PHP script has no FormatPHP functions in it, on purpose.
5+
*/
6+
foreach (['foo', 'bar', 'baz'] as $item) {
7+
// Do nothing.
8+
}

0 commit comments

Comments
 (0)