Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 47 additions & 9 deletions src/Utils/PhpFunctionsScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,52 @@ public function __construct($code)
$this->tokens = token_get_all($code);
}

/**
* Decodes a T_CONSTANT_ENCAPSED_STRING string.
*
* @param string $value
*
* @return string
*/
public static function decodeString($value)
{
$result = '';
if ($value[0] === "'" || strpos($value, '$') === false) {
if (strpos($value, '\\') === false) {
$result = substr($value, 1, -1);
} else {
$result = eval("return $value;");
}
} else {
$value = substr($value, 1, -1);
while (($p = strpos($value, '\\')) !== false) {
if (!isset($value[$p + 1])) {
break;
}
if ($p > 0) {
$result .= substr($value, 0, $p);
}
$value = substr($value, $p + 1);
$p = strpos($value, '$');
if ($p === false) {
$result .= eval('return "\\'.$value.'";');
$value = '';
break;
}
if ($p === 0) {
$result .= '$';
$value = substr($value, 1);
} else {
$result .= eval('return "\\'.substr($value, 0, $p).'";');
$value = substr($value, $p);
}
}
$result .= $value;
}

return $result;
}

/**
* {@inheritdoc}
*/
Expand All @@ -39,15 +85,7 @@ public function getFunctions()

//add an argument to the current function
if (isset($bufferFunctions[0]) && ($value[0] === T_CONSTANT_ENCAPSED_STRING)) {
$val = $value[1];

if ($val[0] === '"') {
$val = str_replace('\\"', '"', $val);
} else {
$val = str_replace("\\'", "'", $val);
}

$bufferFunctions[0][2][] = substr($val, 1, -1);
$bufferFunctions[0][2][] = static::decodeString($value[1]);
continue;
}

Expand Down
32 changes: 32 additions & 0 deletions tests/DecodePhpStringTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

class DecodePhpStringTest extends PHPUnit_Framework_TestCase
{
public function decodeStringsProvider()
{
return array(
array('"test"', 'test'),
array("'test'", 'test'),
array("'DATE \a\\t TIME'", 'DATE \a\t TIME'),
array("'DATE \a\\t TIME$'", 'DATE \a\t TIME$'),
array("'DATE \a\\t TIME\$'", 'DATE \a\t TIME$'),
array("'DATE \a\\t TIME\$a'", 'DATE \a\t TIME$a'),
array('"FIELD\\tFIELD"', "FIELD\tFIELD"),
array('"$"', '$'),
array('"Hi $"', 'Hi $'),
array('"$ hi"', '$ hi'),
array('"Hi\t$name"', "Hi\t\$name"),
array('"Hi\\\\"', 'Hi\\'),
array('"{$obj->name}"', '{$obj->name}'),
array('"a\x20b $c"', 'a b $c'),
);
}

/**
* @dataProvider decodeStringsProvider
*/
public function testDecodeStrings($source, $decoded)
{
$this->assertSame($decoded, Gettext\Utils\PhpFunctionsScanner::decodeString($source));
}
}
10 changes: 10 additions & 0 deletions tests/PhpCodeExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,14 @@ public function testMultiline()
$this->assertInstanceOf('Gettext\\Translation', $translation);
$this->assertEquals($original, $translation->getOriginal());
}

public function testSpecialChars()
{
$translations = Gettext\Extractors\PhpCode::fromFile(__DIR__.'/files/special-chars.php');

$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);
}
}
7 changes: 7 additions & 0 deletions tests/files/special-chars.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<div>
<p><?php __('plain'); ?></p>
<p><?php __('DATE \a\t TIME'); ?></p>
<p><?php __("DATE \a\\t TIME"); ?></p>
<p><?php __("DATE \\a\\t TIME"); ?></p>
<p><?php __("FIELD\tFIELD"); ?></p>
</div>