Skip to content

Commit 94c8c0e

Browse files
author
Oscar Otero
committed
new option to ignore invalid functions by the scanner
1 parent 20059a7 commit 94c8c0e

2 files changed

Lines changed: 49 additions & 17 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
Previous releases are documented in [github releases](https://github.com/oscarotero/Gettext/releases)
99

10+
## [5.0.1] - Unreleased
11+
### Added
12+
- New function `CodeScanner::ignoreInvalidFunctions()` to ignore invalid functions instead throw an exception
13+
1014
## [5.0.0] - 2019-11-04
1115
### Added
1216
- New interfaces: `ScannerInterface` and `FunctionsScannerInterface`.
@@ -172,6 +176,7 @@ Previous releases are documented in [github releases](https://github.com/oscarot
172176
[#230]: https://github.com/oscarotero/Gettext/issues/230
173177
[#231]: https://github.com/oscarotero/Gettext/issues/231
174178

179+
[5.0.1]: https://github.com/oscarotero/Gettext/compare/v5.0.0...HEAD
175180
[5.0.0]: https://github.com/oscarotero/Gettext/compare/v4.8.0...v5.0.0
176181
[4.8.0]: https://github.com/oscarotero/Gettext/compare/v4.7.0...v4.8.0
177182
[4.7.0]: https://github.com/oscarotero/Gettext/compare/v4.6.3...v4.7.0

src/Scanner/CodeScanner.php

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
*/
1313
abstract class CodeScanner extends Scanner
1414
{
15+
protected $ignoreInvalidFunctions = false;
16+
1517
protected $functions = [
1618
'gettext' => 'gettext',
1719
'__' => 'gettext',
@@ -51,6 +53,13 @@ public function getFunctions(): array
5153
return $this->functions;
5254
}
5355

56+
public function ignoreInvalidFunctions($ignore = true): self
57+
{
58+
$this->ignoreInvalidFunctions = $ignore;
59+
60+
return $this;
61+
}
62+
5463
public function scanString(string $string, string $filename): void
5564
{
5665
$functionsScanner = $this->getFunctionsScanner();
@@ -81,71 +90,83 @@ protected function handleFunction(ParsedFunction $function)
8190

8291
protected function gettext(ParsedFunction $function): ?Translation
8392
{
84-
static::checkArguments($function, 1);
93+
if (!$this->checkFunction($function, 1)) {
94+
return null;
95+
}
8596
list($original) = $function->getArguments();
86-
8797
return $this->saveTranslation(null, null, $original);
8898
}
8999

90100
protected function ngettext(ParsedFunction $function): ?Translation
91101
{
92-
static::checkArguments($function, 2);
102+
if (!$this->checkFunction($function, 2)) {
103+
return null;
104+
}
93105
list($original, $plural) = $function->getArguments();
94-
95106
return $this->saveTranslation(null, null, $original, $plural);
96107
}
97108

98109
protected function pgettext(ParsedFunction $function): ?Translation
99110
{
100-
static::checkArguments($function, 2);
111+
if (!$this->checkFunction($function, 2)) {
112+
return null;
113+
}
101114
list($context, $original) = $function->getArguments();
102-
103115
return $this->saveTranslation(null, $context, $original);
104116
}
105117

106118
protected function dgettext(ParsedFunction $function): ?Translation
107119
{
108-
static::checkArguments($function, 2);
120+
if (!$this->checkFunction($function, 2)) {
121+
return null;
122+
}
109123
list($domain, $original) = $function->getArguments();
110-
111124
return $this->saveTranslation($domain, null, $original);
112125
}
113126

114127
protected function dpgettext(ParsedFunction $function): ?Translation
115128
{
116-
static::checkArguments($function, 3);
129+
if (!$this->checkFunction($function, 3)) {
130+
return null;
131+
}
117132
list($domain, $context, $original) = $function->getArguments();
118-
119133
return $this->saveTranslation($domain, $context, $original);
120134
}
121135

122136
protected function npgettext(ParsedFunction $function): ?Translation
123137
{
124-
static::checkArguments($function, 3);
138+
if (!$this->checkFunction($function, 3)) {
139+
return null;
140+
}
125141
list($context, $original, $plural) = $function->getArguments();
126-
127142
return $this->saveTranslation(null, $context, $original, $plural);
128143
}
129144

130145
protected function dngettext(ParsedFunction $function): ?Translation
131146
{
132-
static::checkArguments($function, 3);
147+
if (!$this->checkFunction($function, 3)) {
148+
return null;
149+
}
133150
list($domain, $original, $plural) = $function->getArguments();
134-
135151
return $this->saveTranslation($domain, null, $original, $plural);
136152
}
137153

138154
protected function dnpgettext(ParsedFunction $function): ?Translation
139155
{
140-
static::checkArguments($function, 4);
156+
if (!$this->checkFunction($function, 4)) {
157+
return null;
158+
}
141159
list($domain, $context, $original, $plural) = $function->getArguments();
142-
143160
return $this->saveTranslation($domain, $context, $original, $plural);
144161
}
145162

146-
protected static function checkArguments(ParsedFunction $function, int $minLength)
163+
protected function checkFunction(ParsedFunction $function, int $minLength): bool
147164
{
148165
if ($function->countArguments() < $minLength) {
166+
if ($this->ignoreInvalidFunctions) {
167+
return false;
168+
}
169+
149170
throw new Exception(
150171
sprintf(
151172
'Invalid gettext function in %s:%d. At least %d arguments are required',
@@ -159,6 +180,10 @@ protected static function checkArguments(ParsedFunction $function, int $minLengt
159180
$arguments = array_slice($function->getArguments(), 0, $minLength);
160181

161182
if (in_array(null, $arguments, true)) {
183+
if ($this->ignoreInvalidFunctions) {
184+
return false;
185+
}
186+
162187
throw new Exception(
163188
sprintf(
164189
'Invalid gettext function in %s:%d. Some required arguments are not valid',
@@ -167,5 +192,7 @@ protected static function checkArguments(ParsedFunction $function, int $minLengt
167192
)
168193
);
169194
}
195+
196+
return true;
170197
}
171198
}

0 commit comments

Comments
 (0)