-
-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #98 from mlocati/xgettext-edge-cases
Extraction of variables and concatenated strings
- Loading branch information
Showing
4 changed files
with
195 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
<?php | ||
|
||
namespace Gettext\Utils; | ||
|
||
/** | ||
* Function parsed by PhpFunctionsScanner. | ||
*/ | ||
class ParsedFunction | ||
{ | ||
/** | ||
* The function name. | ||
* | ||
* @var string | ||
*/ | ||
protected $name; | ||
|
||
/** | ||
* The line where the function starts. | ||
* | ||
* @var int | ||
*/ | ||
protected $line; | ||
|
||
/** | ||
* The strings extracted from the function arguments. | ||
* | ||
* @var string[] | ||
*/ | ||
protected $arguments; | ||
|
||
/** | ||
* The current index of the function (-1 if no arguments). | ||
* | ||
* @var int|null | ||
*/ | ||
protected $argumentIndex; | ||
|
||
/** | ||
* Shall we stop adding string chunks to the current argument? | ||
* | ||
* @var bool | ||
*/ | ||
protected $argumentStopped; | ||
|
||
/** | ||
* Initializes the instance. | ||
* | ||
* @param string $name The function name. | ||
* @param int $line The line where the function starts. | ||
*/ | ||
public function __construct($name, $line) | ||
{ | ||
$this->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, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,15 @@ | ||
<div> | ||
<p><?php __('plain'); ?></p> | ||
<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> | ||
<p><?php __( | ||
"text " | ||
// test | ||
.'concatenated'. | ||
/* test*/ " with 'comments'" | ||
); ?></p> | ||
<p><?php __($avoid['me']); ?> | ||
<p><?php __('Stop at the variable'.$var.'!'); ?> | ||
</div> |