From 141e201a6cedb63163b5f8c3e2671fe4bb3e56e1 Mon Sep 17 00:00:00 2001 From: Daniel Mason Date: Thu, 23 Nov 2023 15:38:46 +0000 Subject: [PATCH] Fix performance issue and baseline key count (#12) --- src/Collections/ApplicationFileCollection.php | 11 +++++--- src/Collections/ResultObjectCollection.php | 7 ++++- src/Commands/MissingCommand.php | 2 +- src/Commands/UnusedCommand.php | 2 +- .../Collections/ApplicationFileCollection.php | 2 ++ .../Collections/ResultObjectCollection.php | 2 ++ .../Parsers/ApplicationFileParser.php | 4 +-- src/Parsers/ApplicationFileParser.php | 28 ++++++------------- src/Readers/ApplicationFileReader.php | 14 +++++++--- 9 files changed, 39 insertions(+), 33 deletions(-) diff --git a/src/Collections/ApplicationFileCollection.php b/src/Collections/ApplicationFileCollection.php index 4487faa..5cd0f05 100644 --- a/src/Collections/ApplicationFileCollection.php +++ b/src/Collections/ApplicationFileCollection.php @@ -6,10 +6,6 @@ use Fidum\LaravelTranslationLinter\Data\ApplicationFileObject; use Illuminate\Support\Collection; -/** - * @method self __construct(ApplicationFileObject[] $items = null) - * @method self push(ApplicationFileObject $object) - */ class ApplicationFileCollection extends Collection implements ApplicationFileCollectionContract { public function containsKey(string $key): bool @@ -23,4 +19,11 @@ public function doesntContainKey(string $key): bool { return ! $this->containsKey($key); } + + public function uniqueForFile(): ApplicationFileCollectionContract + { + return $this->unique(function (ApplicationFileObject $object) { + return $object->namespaceHintedKey.$object->file->getPathname(); + }); + } } diff --git a/src/Collections/ResultObjectCollection.php b/src/Collections/ResultObjectCollection.php index 1f4af07..8293aca 100644 --- a/src/Collections/ResultObjectCollection.php +++ b/src/Collections/ResultObjectCollection.php @@ -20,7 +20,7 @@ public function toBaselineJson(): string { return $this ->groupBy('locale') - ->map(fn (ResultObjectCollection $collection) => $collection->pluck('namespaceHintedKey')->values()) + ->map(fn (ResultObjectCollection $collection) => $collection->pluck('namespaceHintedKey')->unique()->values()) ->toJson(JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); } @@ -35,4 +35,9 @@ public function whereShouldReport(FilterCollectionContract $filters): ResultObje { return $this->filter($filters->shouldReport(...)); } + + public function uniqueForLocale(): ResultObjectCollectionContract + { + return $this->unique(fn (ResultObject $object) => $object->locale.$object->namespaceHintedKey); + } } diff --git a/src/Commands/MissingCommand.php b/src/Commands/MissingCommand.php index 54d90b9..8275e21 100644 --- a/src/Commands/MissingCommand.php +++ b/src/Commands/MissingCommand.php @@ -29,7 +29,7 @@ public function handle( $results = $linter->execute(); if ($baseline) { - $results = $results->whereShouldReport($filters); + $results = $results->whereShouldReport($filters)->uniqueForLocale(); $writer->execute($results); diff --git a/src/Commands/UnusedCommand.php b/src/Commands/UnusedCommand.php index cd12eda..2d97f3c 100644 --- a/src/Commands/UnusedCommand.php +++ b/src/Commands/UnusedCommand.php @@ -27,7 +27,7 @@ public function handle( $results = $linter->execute(); if ($baseline) { - $results = $results->whereShouldReport($filters); + $results = $results->whereShouldReport($filters)->uniqueForLocale(); $writer->execute($results); diff --git a/src/Contracts/Collections/ApplicationFileCollection.php b/src/Contracts/Collections/ApplicationFileCollection.php index 2bdaa0e..82c8368 100644 --- a/src/Contracts/Collections/ApplicationFileCollection.php +++ b/src/Contracts/Collections/ApplicationFileCollection.php @@ -15,4 +15,6 @@ interface ApplicationFileCollection extends Arrayable, Enumerable public function containsKey(string $key): bool; public function doesntContainKey(string $key): bool; + + public function uniqueForFile(): self; } diff --git a/src/Contracts/Collections/ResultObjectCollection.php b/src/Contracts/Collections/ResultObjectCollection.php index c72fd8c..7d55246 100644 --- a/src/Contracts/Collections/ResultObjectCollection.php +++ b/src/Contracts/Collections/ResultObjectCollection.php @@ -21,4 +21,6 @@ public function toBaselineJson(): string; public function toCommandTableOutputArray(FieldCollectionContract $fields): array; public function whereShouldReport(FilterCollectionContract $filters): self; + + public function uniqueForLocale(): self; } diff --git a/src/Contracts/Parsers/ApplicationFileParser.php b/src/Contracts/Parsers/ApplicationFileParser.php index 0f481d8..cca4003 100644 --- a/src/Contracts/Parsers/ApplicationFileParser.php +++ b/src/Contracts/Parsers/ApplicationFileParser.php @@ -2,10 +2,10 @@ namespace Fidum\LaravelTranslationLinter\Contracts\Parsers; -use Fidum\LaravelTranslationLinter\Contracts\Collections\ApplicationFileCollection as ApplicationFileCollectionContract; +use Illuminate\Support\Collection; use Symfony\Component\Finder\SplFileInfo; interface ApplicationFileParser { - public function execute(SplFileInfo $file): ApplicationFileCollectionContract; + public function execute(SplFileInfo $file): Collection; } diff --git a/src/Parsers/ApplicationFileParser.php b/src/Parsers/ApplicationFileParser.php index 9ebd0ee..47509e4 100644 --- a/src/Parsers/ApplicationFileParser.php +++ b/src/Parsers/ApplicationFileParser.php @@ -2,10 +2,8 @@ namespace Fidum\LaravelTranslationLinter\Parsers; -use Fidum\LaravelTranslationLinter\Contracts\Collections\ApplicationFileCollection as ApplicationFileCollectionContract; use Fidum\LaravelTranslationLinter\Contracts\Parsers\ApplicationFileParser as ApplicationFileParserContract; -use Fidum\LaravelTranslationLinter\Data\ApplicationFileObject; -use Illuminate\Support\Str; +use Illuminate\Support\Collection; use Symfony\Component\Finder\SplFileInfo; readonly class ApplicationFileParser implements ApplicationFileParserContract @@ -14,38 +12,28 @@ protected string $pattern; - public function __construct( - protected ApplicationFileCollectionContract $collection, - array $functions - ) { + public function __construct(array $functions) + { $this->pattern = str_replace('[FUNCTIONS]', implode('|', $functions), static::REGEX); } - public function execute(SplFileInfo $file): ApplicationFileCollectionContract + public function execute(SplFileInfo $file): Collection { $data = $file->getContents(); + $collection = new Collection(); if (! preg_match_all($this->pattern, $data, $matches, PREG_OFFSET_CAPTURE)) { // If pattern not found return - return $this->collection; + return $collection; } foreach (current($matches) as $match) { preg_match($this->pattern, $match[0], $string); - $namespaceHintedKey = $string[2]; - - $this->collection->push(new ApplicationFileObject( - file: $file, - key: Str::after($namespaceHintedKey, '::') ?: null, - namespaceHint: Str::before($namespaceHintedKey, '::') ?: null, - namespaceHintedKey: $namespaceHintedKey, - )); + $collection->push($string[2]); } // Remove duplicates. - return $this->collection->unique(function (ApplicationFileObject $object) { - return $object->namespaceHintedKey; - }); + return $collection->unique(); } } diff --git a/src/Readers/ApplicationFileReader.php b/src/Readers/ApplicationFileReader.php index 039a2d2..ff1557c 100644 --- a/src/Readers/ApplicationFileReader.php +++ b/src/Readers/ApplicationFileReader.php @@ -7,6 +7,7 @@ use Fidum\LaravelTranslationLinter\Contracts\Parsers\ApplicationFileParser; use Fidum\LaravelTranslationLinter\Contracts\Readers\ApplicationFileReader as ApplicationFileReaderContract; use Fidum\LaravelTranslationLinter\Data\ApplicationFileObject; +use Illuminate\Support\Str; class ApplicationFileReader implements ApplicationFileReaderContract { @@ -23,11 +24,16 @@ public function execute(): ApplicationFileCollectionContract // Get all translatable strings from files foreach ($files as $file) { - $this->collection->push(...$this->parser->execute($file)); + foreach ($this->parser->execute($file) as $namespaceHintedKey) { + $this->collection->push(new ApplicationFileObject( + file: $file, + key: Str::after($namespaceHintedKey, '::') ?: null, + namespaceHint: Str::before($namespaceHintedKey, '::') ?: null, + namespaceHintedKey: $namespaceHintedKey, + )); + } } - return $this->collection->unique(function (ApplicationFileObject $object) { - return $object->namespaceHintedKey.$object->file->getPathname(); - }); + return $this->collection->uniqueForFile(); } }