Skip to content

Commit 141e201

Browse files
authored
Fix performance issue and baseline key count (#12)
1 parent 2ff1153 commit 141e201

File tree

9 files changed

+39
-33
lines changed

9 files changed

+39
-33
lines changed

src/Collections/ApplicationFileCollection.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
use Fidum\LaravelTranslationLinter\Data\ApplicationFileObject;
77
use Illuminate\Support\Collection;
88

9-
/**
10-
* @method self __construct(ApplicationFileObject[] $items = null)
11-
* @method self push(ApplicationFileObject $object)
12-
*/
139
class ApplicationFileCollection extends Collection implements ApplicationFileCollectionContract
1410
{
1511
public function containsKey(string $key): bool
@@ -23,4 +19,11 @@ public function doesntContainKey(string $key): bool
2319
{
2420
return ! $this->containsKey($key);
2521
}
22+
23+
public function uniqueForFile(): ApplicationFileCollectionContract
24+
{
25+
return $this->unique(function (ApplicationFileObject $object) {
26+
return $object->namespaceHintedKey.$object->file->getPathname();
27+
});
28+
}
2629
}

src/Collections/ResultObjectCollection.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function toBaselineJson(): string
2020
{
2121
return $this
2222
->groupBy('locale')
23-
->map(fn (ResultObjectCollection $collection) => $collection->pluck('namespaceHintedKey')->values())
23+
->map(fn (ResultObjectCollection $collection) => $collection->pluck('namespaceHintedKey')->unique()->values())
2424
->toJson(JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
2525
}
2626

@@ -35,4 +35,9 @@ public function whereShouldReport(FilterCollectionContract $filters): ResultObje
3535
{
3636
return $this->filter($filters->shouldReport(...));
3737
}
38+
39+
public function uniqueForLocale(): ResultObjectCollectionContract
40+
{
41+
return $this->unique(fn (ResultObject $object) => $object->locale.$object->namespaceHintedKey);
42+
}
3843
}

src/Commands/MissingCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function handle(
2929
$results = $linter->execute();
3030

3131
if ($baseline) {
32-
$results = $results->whereShouldReport($filters);
32+
$results = $results->whereShouldReport($filters)->uniqueForLocale();
3333

3434
$writer->execute($results);
3535

src/Commands/UnusedCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function handle(
2727
$results = $linter->execute();
2828

2929
if ($baseline) {
30-
$results = $results->whereShouldReport($filters);
30+
$results = $results->whereShouldReport($filters)->uniqueForLocale();
3131

3232
$writer->execute($results);
3333

src/Contracts/Collections/ApplicationFileCollection.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@ interface ApplicationFileCollection extends Arrayable, Enumerable
1515
public function containsKey(string $key): bool;
1616

1717
public function doesntContainKey(string $key): bool;
18+
19+
public function uniqueForFile(): self;
1820
}

src/Contracts/Collections/ResultObjectCollection.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,6 @@ public function toBaselineJson(): string;
2121
public function toCommandTableOutputArray(FieldCollectionContract $fields): array;
2222

2323
public function whereShouldReport(FilterCollectionContract $filters): self;
24+
25+
public function uniqueForLocale(): self;
2426
}

src/Contracts/Parsers/ApplicationFileParser.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
namespace Fidum\LaravelTranslationLinter\Contracts\Parsers;
44

5-
use Fidum\LaravelTranslationLinter\Contracts\Collections\ApplicationFileCollection as ApplicationFileCollectionContract;
5+
use Illuminate\Support\Collection;
66
use Symfony\Component\Finder\SplFileInfo;
77

88
interface ApplicationFileParser
99
{
10-
public function execute(SplFileInfo $file): ApplicationFileCollectionContract;
10+
public function execute(SplFileInfo $file): Collection;
1111
}

src/Parsers/ApplicationFileParser.php

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22

33
namespace Fidum\LaravelTranslationLinter\Parsers;
44

5-
use Fidum\LaravelTranslationLinter\Contracts\Collections\ApplicationFileCollection as ApplicationFileCollectionContract;
65
use Fidum\LaravelTranslationLinter\Contracts\Parsers\ApplicationFileParser as ApplicationFileParserContract;
7-
use Fidum\LaravelTranslationLinter\Data\ApplicationFileObject;
8-
use Illuminate\Support\Str;
6+
use Illuminate\Support\Collection;
97
use Symfony\Component\Finder\SplFileInfo;
108

119
readonly class ApplicationFileParser implements ApplicationFileParserContract
@@ -14,38 +12,28 @@
1412

1513
protected string $pattern;
1614

17-
public function __construct(
18-
protected ApplicationFileCollectionContract $collection,
19-
array $functions
20-
) {
15+
public function __construct(array $functions)
16+
{
2117
$this->pattern = str_replace('[FUNCTIONS]', implode('|', $functions), static::REGEX);
2218
}
2319

24-
public function execute(SplFileInfo $file): ApplicationFileCollectionContract
20+
public function execute(SplFileInfo $file): Collection
2521
{
2622
$data = $file->getContents();
23+
$collection = new Collection();
2724

2825
if (! preg_match_all($this->pattern, $data, $matches, PREG_OFFSET_CAPTURE)) {
2926
// If pattern not found return
30-
return $this->collection;
27+
return $collection;
3128
}
3229

3330
foreach (current($matches) as $match) {
3431
preg_match($this->pattern, $match[0], $string);
3532

36-
$namespaceHintedKey = $string[2];
37-
38-
$this->collection->push(new ApplicationFileObject(
39-
file: $file,
40-
key: Str::after($namespaceHintedKey, '::') ?: null,
41-
namespaceHint: Str::before($namespaceHintedKey, '::') ?: null,
42-
namespaceHintedKey: $namespaceHintedKey,
43-
));
33+
$collection->push($string[2]);
4434
}
4535

4636
// Remove duplicates.
47-
return $this->collection->unique(function (ApplicationFileObject $object) {
48-
return $object->namespaceHintedKey;
49-
});
37+
return $collection->unique();
5038
}
5139
}

src/Readers/ApplicationFileReader.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Fidum\LaravelTranslationLinter\Contracts\Parsers\ApplicationFileParser;
88
use Fidum\LaravelTranslationLinter\Contracts\Readers\ApplicationFileReader as ApplicationFileReaderContract;
99
use Fidum\LaravelTranslationLinter\Data\ApplicationFileObject;
10+
use Illuminate\Support\Str;
1011

1112
class ApplicationFileReader implements ApplicationFileReaderContract
1213
{
@@ -23,11 +24,16 @@ public function execute(): ApplicationFileCollectionContract
2324

2425
// Get all translatable strings from files
2526
foreach ($files as $file) {
26-
$this->collection->push(...$this->parser->execute($file));
27+
foreach ($this->parser->execute($file) as $namespaceHintedKey) {
28+
$this->collection->push(new ApplicationFileObject(
29+
file: $file,
30+
key: Str::after($namespaceHintedKey, '::') ?: null,
31+
namespaceHint: Str::before($namespaceHintedKey, '::') ?: null,
32+
namespaceHintedKey: $namespaceHintedKey,
33+
));
34+
}
2735
}
2836

29-
return $this->collection->unique(function (ApplicationFileObject $object) {
30-
return $object->namespaceHintedKey.$object->file->getPathname();
31-
});
37+
return $this->collection->uniqueForFile();
3238
}
3339
}

0 commit comments

Comments
 (0)