Gettext icon indicating copy to clipboard operation
Gettext copied to clipboard

PHP Brackets in function parameter

Open kdabek opened this issue 6 years ago • 3 comments

If you use brackets in function parameter PHPFunctionsScanner will interpret this as a new function and the resulting parameter list is incorrect.

Example:

<?php

require_once 'vendor/autoload.php';

$quantity = 10;
$default = 5;

$string = <<<EOD
<?php
sprintf(_n("%d Comment", "%d Comments", ($quantity ?? $default), "domain"), ($quantity ?? $default));
EOD;

$scanner = new \Gettext\Utils\PhpFunctionsScanner($string);
$output = $scanner->getFunctions();

and output:

array(2) {
  [0]=>
  array(4) {
    [0]=>
    string(2) "_n"
    [1]=>
    int(2)
    [2]=>
    array(3) {
      [0]=>
      string(10) "%d Comment"
      [1]=>
      string(11) "%d Comments"
      [2]=>
      NULL
    }
    [3]=>
    NULL
  }
  [1]=>
  array(4) {
    [0]=>
    string(7) "sprintf"
    [1]=>
    int(2)
    [2]=>
    array(2) {
      [0]=>
      NULL
      [1]=>
      string(6) "domain"
    }
    [3]=>
    NULL
  }
}

as you can see "domain" is associated with sprintf function which is not true.

kdabek avatar Oct 10 '19 08:10 kdabek

I recommend to update to gettext/gettext v5, that use nikic/PHP-Parser to scan php code and its much more reliable.

oscarotero avatar Nov 29 '19 09:11 oscarotero

Would you accept PRs against v4 to fix this?

swissspidy avatar Jan 19 '22 10:01 swissspidy

Yes, sure.

oscarotero avatar Jan 19 '22 11:01 oscarotero