Skip to content

Commit

Permalink
FIX Anonymous function use statements in text collector
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Nov 5, 2024
1 parent fe39a2a commit 9116d8a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/i18n/TextCollection/i18nTextCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,7 @@ public function collectFromCode($content, $fileName, Module $module)
$entities = [];

$tokens = token_get_all("<?php\n" . $content);
$lines = explode("\n", str_replace("\r\n", "\n", $content));
$inTransFn = false;
$inConcat = false;
$inNamespace = false;
Expand All @@ -622,10 +623,14 @@ public function collectFromCode($content, $fileName, Module $module)
$previousToken = $thisToken;
$thisToken = $token;
if (is_array($token)) {
list($id, $text) = $token;
list($id, $text, $lineNo) = $token;
// minus 2 is used so the the line we get corresponds with what number token_get_all() returned
$line = $lines[$lineNo - 2] ?? '';

// Collect use statements so we can get fully qualified class names
if ($id === T_USE) {
// Note that T_USE will match both use statements and anonymous functions with the "use" keyword
// e.g. $func = function () use ($var) { ... };
if ($id === T_USE && !preg_match('#use *\(#', $line)) {
$inUse = true;
$currentUse = [];
continue;
Expand Down
52 changes: 52 additions & 0 deletions tests/php/i18n/i18nTextCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,58 @@ public function testCollectFromEntityProvidersInCustomObject()
);
}

public function testCollectFromAnonymousMethod()
{
$c = i18nTextCollector::create();
$mymodule = ModuleLoader::inst()->getManifest()->getModule('i18ntestmodule');
$php = <<<PHP
<?php
namespace SilverStripe\Framework\Core;
class MyClass
{
public function getNewLines(\$class) {
Deprecation::withSuppressedNotice(function () {
\$this->someMethod(_t(
MyClass::class . '.SOMETHING_A',
'something a',
));
});
}
public function getNewLinesWithSomething(\$class, \$something) {
Deprecation::withSuppressedNotice(function () use (\$something) {
\$this->someMethod(_t(
MyClass::class . '.SOMETHING_B',
'something {something}',
['something' => \$something]
));
});
}
public function getNewLinesWithSomething(\$class, \$something) {
Deprecation::withSuppressedNotice(function (
\$myparam
) use (\$something) {
\$this->someMethod(_t(
MyClass::class . '.SOMETHING_C',
'something {something} {myparam}',
['something' => \$something, 'myparam' => \$myparam]
));
});
}
}
PHP;
$this->assertEquals(
[
'SilverStripe\\Framework\\Core\\MyClass.SOMETHING_A' => "something a",
'SilverStripe\\Framework\\Core\\MyClass.SOMETHING_B' => "something {something}",
'SilverStripe\\Framework\\Core\\MyClass.SOMETHING_C' => "something {something} {myparam}",
],
$c->collectFromCode($php, null, $mymodule)
);
}

public function testCollectFromEntityProvidersInWebRoot()
{
// Collect from i18nProviderClass
Expand Down

0 comments on commit 9116d8a

Please sign in to comment.