diff --git a/src/Utils/ParsedFunction.php b/src/Utils/ParsedFunction.php new file mode 100644 index 00000000..1f6fd3c0 --- /dev/null +++ b/src/Utils/ParsedFunction.php @@ -0,0 +1,127 @@ +name = $name; + $this->line = $line; + $this->arguments = array(); + $this->argumentIndex = -1; + $this->argumentStopped = false; + } + + /** + * Stop extracting strings from the current argument (because we found something that's not a string). + */ + public function stopArgument() + { + if ($this->argumentIndex === -1) { + $this->argumentIndex = 0; + } + $this->argumentStopped = true; + } + + /** + * Go to the next argument because we a comma was found. + */ + public function nextArgument() + { + if ($this->argumentIndex === -1) { + // This should neve occur, but let's stay safe - During test/development an Exception should be thrown. + $this->argumentIndex = 1; + } else { + ++$this->argumentIndex; + } + $this->argumentStopped = false; + } + + /** + * Add a string to the current argument. + * + * @param string $chunk + */ + public function addArgumentChunk($chunk) + { + if ($this->argumentStopped === false) { + if ($this->argumentIndex === -1) { + $this->argumentIndex = 0; + } + if (isset($this->arguments[$this->argumentIndex])) { + $this->arguments[$this->argumentIndex] .= $chunk; + } else { + $this->arguments[$this->argumentIndex] = $chunk; + } + } + } + + /** + * A closing parenthesis was found: return the final data. + * + * @return array{ + * + * @var string The function name. + * @var int The line where the function starts. + * @var string[] the strings extracted from the function arguments. + * } + */ + public function close() + { + $arguments = array(); + for ($i = 0; $i <= $this->argumentIndex; ++$i) { + $arguments[$i] = isset($this->arguments[$i]) ? $this->arguments[$i] : ''; + } + + return array( + $this->name, + $this->line, + $arguments, + ); + } +} diff --git a/src/Utils/PhpFunctionsScanner.php b/src/Utils/PhpFunctionsScanner.php index fb28bdda..b487535c 100644 --- a/src/Utils/PhpFunctionsScanner.php +++ b/src/Utils/PhpFunctionsScanner.php @@ -2,6 +2,8 @@ namespace Gettext\Utils; +use Gettext\Extractors\PhpCode; + class PhpFunctionsScanner extends FunctionsScanner { protected $tokens; @@ -23,32 +25,70 @@ public function getFunctions() { $count = count($this->tokens); $bufferFunctions = array(); + /* @var ParsedFunction[] $bufferFunctions */ $functions = array(); + /* @var ParsedFunction[] $functions */ for ($k = 0; $k < $count; ++$k) { $value = $this->tokens[$k]; - //close the current function if (is_string($value)) { - if ($value === ')' && isset($bufferFunctions[0])) { - $functions[] = array_shift($bufferFunctions); - } - - continue; + $s = $value; + } else { + $s = token_name($value[0]).' >'.$value[1].'<'; } - //add an argument to the current function - if (isset($bufferFunctions[0]) && ($value[0] === T_CONSTANT_ENCAPSED_STRING)) { - $bufferFunctions[0][2][] = \Gettext\Extractors\PhpCode::convertString($value[1]); + if (is_string($value)) { + if (isset($bufferFunctions[0])) { + switch ($value) { + case ',': + $bufferFunctions[0]->nextArgument(); + break; + case ')': + $functions[] = array_shift($bufferFunctions)->close(); + break; + case '.': + break; + default: + $bufferFunctions[0]->stopArgument(); + break; + } + } continue; } - //new function found - if (($value[0] === T_STRING) && is_string($this->tokens[$k + 1]) && ($this->tokens[$k + 1] === '(')) { - array_unshift($bufferFunctions, array($value[1], $value[2], array())); - ++$k; - - continue; + switch ($value[0]) { + case T_CONSTANT_ENCAPSED_STRING: + //add an argument to the current function + if (isset($bufferFunctions[0])) { + $bufferFunctions[0]->addArgumentChunk(PhpCode::convertString($value[1])); + } + break; + case T_STRING: + if (isset($bufferFunctions[0])) { + $bufferFunctions[0]->stopArgument(); + } + //new function found + for ($j = $k + 1; $j < $count; ++$j) { + $nextToken = $this->tokens[$j]; + if (is_array($nextToken) && ($nextToken[0] === T_COMMENT || $nextToken[0] === T_WHITESPACE)) { + continue; + } + if ($nextToken === '(') { + array_unshift($bufferFunctions, new ParsedFunction($value[1], $value[2])); + $k = $j; + } + break; + } + break; + case T_WHITESPACE: + case T_COMMENT: + break; + default: + if (isset($bufferFunctions[0])) { + $bufferFunctions[0]->stopArgument(); + } + break; } } diff --git a/tests/PhpCodeExtractorTest.php b/tests/PhpCodeExtractorTest.php index 3c9c12a3..3a8a7dfa 100644 --- a/tests/PhpCodeExtractorTest.php +++ b/tests/PhpCodeExtractorTest.php @@ -68,6 +68,9 @@ public function testSpecialChars() $this->assertInstanceOf('Gettext\\Translation', $translations->find(null, 'plain')); $this->assertInstanceOf('Gettext\\Translation', $translations->find(null, 'DATE \\a\\t TIME')); $this->assertInstanceOf('Gettext\\Translation', $translations->find(null, "FIELD\tFIELD")); - $this->assertCount(3, $translations); + $this->assertFalse($translations->find(null, "text ")); + $this->assertInstanceOf('Gettext\\Translation', $translations->find(null, "text concatenated with 'comments'")); + $this->assertInstanceOf('Gettext\\Translation', $translations->find(null, "Stop at the variable")); + $this->assertCount(5, $translations); } } diff --git a/tests/files/special-chars.php b/tests/files/special-chars.php index 5dd008ca..ba0e06e8 100644 --- a/tests/files/special-chars.php +++ b/tests/files/special-chars.php @@ -1,7 +1,15 @@
-

+

+

+

+