-
Notifications
You must be signed in to change notification settings - Fork 1
/
LostInTranslation.php
80 lines (65 loc) · 2.03 KB
/
LostInTranslation.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
namespace CodingSocks\LostInTranslation;
use CodingSocks\LostInTranslation\NodeFinder\BladeNowdocFinder;
use CodingSocks\LostInTranslation\NodeFinder\TranslationFinder;
use Illuminate\Support\Str;
use Illuminate\View\Compilers\BladeCompiler;
use Symfony\Component\Finder\SplFileInfo;
class LostInTranslation
{
/** @var \Illuminate\View\Compilers\BladeCompiler */
private BladeCompiler $compiler;
/** @var array */
private array $detect;
public function __construct(
BladeCompiler $compiler,
array $detect,
) {
$this->compiler = $compiler;
$this->detect = $detect;
}
/**
* Find occurrences of translations in a file.
*
* @param \Symfony\Component\Finder\SplFileInfo $file
* @return \PhpParser\Node[]
*/
public function findInFile(SplFileInfo $file): array
{
$finder = new TranslationFinder($this->detect);
$str = $file->getContents();
if ($this->isBladeFile($file)) {
$str = $this->compiler->compileString($str);
return $finder->find($str);
}
$found = [];
$nowdocFinder = new BladeNowdocFinder();
foreach ($nowdocFinder->find($str) as $node) {
$str = $this->compiler->compileString($node->value);
array_push($found, ...$finder->find($str));
}
return array_merge($found, $finder->find($str));
}
/**
* Determine if the given file is likely a blade file.
*
* @param \Symfony\Component\Finder\SplFileInfo $file
* @return bool
*/
protected function isBladeFile(SplFileInfo $file): bool
{
return Str::endsWith($file->getFilename(), '.blade.php');
}
/**
* Resolve the first argument of a node.
*
* @param $node
* @return string|null
* @throws \CodingSocks\LostInTranslation\NonStringArgumentException
*/
public function resolveFirstArg($node): ?string
{
$resolver = new FirstArgumentResolver();
return $resolver->resolve($node);
}
}