Skip to content

Commit 9708b44

Browse files
committed
allow to exclude paths
1 parent 0ecc905 commit 9708b44

File tree

8 files changed

+54
-20
lines changed

8 files changed

+54
-20
lines changed

config/translator.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,13 @@
4343
'searchcode' => [
4444
'service' => 'php-parser',
4545

46-
'services' => [
47-
'php-parser' => [
48-
'paths' => [
49-
app_path(),
50-
resource_path(),
51-
],
52-
],
46+
'paths' => [
47+
app_path(),
48+
resource_path(),
5349
],
50+
51+
'excluded_paths' => [],
52+
5453
],
5554

5655
];

src/Services/SearchCode/PhpParserService.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
class PhpParserService implements SearchCodeServiceInterface
1616
{
1717
public function __construct(
18-
public array $paths
18+
public array $paths,
19+
public array $excludedPaths = [],
1920
) {
2021
//
2122
}
@@ -24,13 +25,16 @@ public function finder(): Finder
2425
{
2526
return Finder::create()
2627
->in($this->paths)
27-
->followLinks()
28+
// ->exclude($this->excludedPaths)
29+
->notPath($this->excludedPaths)
30+
->exclude('vendor')
31+
->exclude('node_modules')
2832
->ignoreDotFiles(true)
2933
->ignoreVCS(true)
34+
->ignoreVCSIgnored(true)
3035
->ignoreUnreadableDirs(true)
31-
->exclude('vendor')
32-
->exclude('node_modules')
3336
->name('*.php')
37+
->followLinks()
3438
->files();
3539
}
3640

src/TranslatorServiceProvider.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ public static function getSearchcodeServiceFromConfig(?string $serviceName = nul
9191

9292
return match ($service) {
9393
'php-parser', PhpParserService::class => new PhpParserService(
94-
config('translator.searchcode.services.php-parser.paths')
94+
paths: config('translator.searchcode.paths'),
95+
excludedPaths: config('translator.searchcode.excluded_paths', [])
9596
),
9697
'', null => null,
9798
default => new $service,

tests/Feature/TranslatorTest.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,13 @@
8888
it('finds dead translations in a namespace', function () {
8989
$translator = new Translator(
9090
storage: $this->getStorage(),
91-
searchcodeService: new PhpParserService([
92-
$this->getAppPath(),
93-
$this->getResourcesPath(),
94-
])
91+
searchcodeService: new PhpParserService(
92+
paths: [
93+
$this->getAppPath(),
94+
$this->getResourcesPath(),
95+
],
96+
excludedPaths: $this->getExcludedPaths()
97+
)
9598
);
9699

97100
$dead = $translator->getDeadTranslations(
@@ -113,10 +116,13 @@
113116
it('finds all dead translations', function () {
114117
$translator = new Translator(
115118
storage: $this->getStorage(),
116-
searchcodeService: new PhpParserService([
117-
$this->getAppPath(),
118-
$this->getResourcesPath(),
119-
])
119+
searchcodeService: new PhpParserService(
120+
paths: [
121+
$this->getAppPath(),
122+
$this->getResourcesPath(),
123+
],
124+
excludedPaths: $this->getExcludedPaths()
125+
)
120126
);
121127

122128
$deadTranslations = $translator->getAllDeadTranslations();

tests/TestCase.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010

1111
class TestCase extends Orchestra
1212
{
13+
public function getExcludedPaths(): array
14+
{
15+
return [
16+
'ignored',
17+
'ignored.blade.php',
18+
];
19+
}
20+
1321
public function getAppPath(): string
1422
{
1523
return __DIR__.'/src/app';

tests/Unit/PhpParserServiceTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
$appPath,
2727
$resourcesPath,
2828
],
29+
excludedPaths: $this->getExcludedPaths()
2930
);
3031

3132
expect($service->translationsByFiles())->toBe([
@@ -53,6 +54,7 @@
5354
$appPath,
5455
$resourcesPath,
5556
],
57+
excludedPaths: $this->getExcludedPaths()
5658
);
5759

5860
expect($service->filesByTranslations())->toBe([
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Elegantly\Translator\Tests\Src\App\Ignored;
4+
5+
class IgnoredDummyClass
6+
{
7+
public function getLabel()
8+
{
9+
return __(key: 'messages.hello');
10+
}
11+
}

tests/src/resources/ignored.blade.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div>
2+
{{ __('messages.hello') }}
3+
</div>

0 commit comments

Comments
 (0)