Skip to content

Commit

Permalink
Fix performance issue and baseline key count
Browse files Browse the repository at this point in the history
  • Loading branch information
dmason30 committed Nov 23, 2023
1 parent 2ff1153 commit b8a9450
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 33 deletions.
11 changes: 7 additions & 4 deletions src/Collections/ApplicationFileCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
});
}
}
7 changes: 6 additions & 1 deletion src/Collections/ResultObjectCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/Commands/MissingCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function handle(
$results = $linter->execute();

if ($baseline) {
$results = $results->whereShouldReport($filters);
$results = $results->whereShouldReport($filters)->uniqueForLocale();

$writer->execute($results);

Expand Down
2 changes: 1 addition & 1 deletion src/Commands/UnusedCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function handle(
$results = $linter->execute();

if ($baseline) {
$results = $results->whereShouldReport($filters);
$results = $results->whereShouldReport($filters)->uniqueForLocale();

$writer->execute($results);

Expand Down
2 changes: 2 additions & 0 deletions src/Contracts/Collections/ApplicationFileCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 2 additions & 0 deletions src/Contracts/Collections/ResultObjectCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ public function toBaselineJson(): string;
public function toCommandTableOutputArray(FieldCollectionContract $fields): array;

public function whereShouldReport(FilterCollectionContract $filters): self;

public function uniqueForLocale(): self;
}
4 changes: 2 additions & 2 deletions src/Contracts/Parsers/ApplicationFileParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
28 changes: 8 additions & 20 deletions src/Parsers/ApplicationFileParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
}
}
14 changes: 10 additions & 4 deletions src/Readers/ApplicationFileReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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();
}
}

0 comments on commit b8a9450

Please sign in to comment.