Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX Anonymous function use statements in text collector #11455

Merged
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
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] ?? '';
Comment on lines +627 to +628
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't find any documentation about this but I'll assume it's correct


// 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