forked from php-gettext/Gettext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeScanner.php
More file actions
170 lines (132 loc) · 4.29 KB
/
CodeScanner.php
File metadata and controls
170 lines (132 loc) · 4.29 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
declare(strict_types = 1);
namespace Gettext\Scanner;
use Exception;
use Gettext\Translation;
/**
* Base class with common functions to scan files with code and get gettext translations.
*/
abstract class CodeScanner extends Scanner
{
protected $ignoreInvalidFunctions = false;
protected $addReferences = true;
protected $commentsPrefixes = [];
protected $functions = [];
/**
* @param array $functions [fnName => handler]
*/
public function setFunctions(array $functions): self
{
$this->functions = $functions;
return $this;
}
/**
* @return array [fnName => handler]
*/
public function getFunctions(): array
{
return $this->functions;
}
public function ignoreInvalidFunctions($ignore = true): self
{
$this->ignoreInvalidFunctions = $ignore;
return $this;
}
public function addReferences($enabled = true): self
{
$this->addReferences = $enabled;
return $this;
}
public function extractCommentsStartingWith(string ...$prefixes): self
{
$this->commentsPrefixes = $prefixes;
return $this;
}
public function scanString(string $string, string $filename): void
{
$functionsScanner = $this->getFunctionsScanner();
$functions = $functionsScanner->scan($string, $filename);
foreach ($functions as $function) {
$this->handleFunction($function);
}
}
abstract public function getFunctionsScanner(): FunctionsScannerInterface;
protected function handleFunction(ParsedFunction $function)
{
$handler = $this->getFunctionHandler($function);
if (is_null($handler)) {
return;
}
$translation = call_user_func($handler, $function);
if ($translation && $this->addReferences) {
$translation->getReferences()->add($function->getFilename(), $function->getLine());
}
}
protected function getFunctionHandler(ParsedFunction $function): ?callable
{
$name = $function->getName();
$handler = $this->functions[$name] ?? null;
return is_null($handler) ? null : [$this, $handler];
}
protected function addComments(ParsedFunction $function, ?Translation $translation): ?Translation
{
if (empty($this->commentsPrefixes) || $translation === null) {
return $translation;
}
foreach ($function->getComments() as $comment) {
if ($this->checkComment($comment)) {
$translation->getExtractedComments()->add($comment);
}
}
return $translation;
}
protected function addFlags(ParsedFunction $function, ?Translation $translation): ?Translation
{
if ($translation === null) {
return $translation;
}
foreach ($function->getFlags() as $flag) {
$translation->getFlags()->add($flag);
}
return $translation;
}
protected function checkFunction(ParsedFunction $function, int $minLength): bool
{
if ($function->countArguments() < $minLength) {
if ($this->ignoreInvalidFunctions) {
return false;
}
throw new Exception(
sprintf(
'Invalid gettext function in %s:%d. At least %d arguments are required',
$function->getFilename(),
$function->getLine(),
$minLength
)
);
}
$arguments = array_slice($function->getArguments(), 0, $minLength);
if (in_array(null, $arguments, true)) {
if ($this->ignoreInvalidFunctions) {
return false;
}
throw new Exception(
sprintf(
'Invalid gettext function in %s:%d. Some required arguments are not valid',
$function->getFilename(),
$function->getLine()
)
);
}
return true;
}
protected function checkComment(string $comment): bool
{
foreach ($this->commentsPrefixes as $prefix) {
if ($prefix === '' || strpos($comment, $prefix) === 0) {
return true;
}
}
return false;
}
}