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 Handle __TRAIT__ in i18nTextCollector #10813

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
10 changes: 5 additions & 5 deletions src/i18n/TextCollection/i18nTextCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -625,14 +625,14 @@ public function collectFromCode($content, $fileName, Module $module)
}
}

// Check class
if ($id === T_CLASS) {
// Check class and trait
if ($id === T_CLASS || $id === T_TRAIT) {
// Skip if previous token was '::'. E.g. 'Object::class'
if (is_array($previousToken) && $previousToken[0] === T_DOUBLE_COLON) {
if ($inSelf) {
// Handle self::class by allowing logic further down
// for __CLASS__ to handle an array of class parts
$id = T_CLASS_C;
// for __CLASS__/__TRAIT__ to handle an array of class parts
$id = $id === T_TRAIT ? T_TRAIT_C : T_CLASS_C;
$inSelf = false;
} elseif ($potentialClassName) {
$id = T_CONSTANT_ENCAPSED_STRING;
Expand Down Expand Up @@ -722,7 +722,7 @@ function ($input) {
} else {
throw new LogicException("Invalid string escape: " . $text);
}
} elseif ($id === T_CLASS_C) {
} elseif ($id === T_CLASS_C || $id === T_TRAIT_C) {
Copy link
Member

Choose a reason for hiding this comment

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

Above we're setting $id to T_TRAIT if that's available, but it looks like we're only ever doing anything with it if it's set to T_TRAIT_C.... Should we just always set it to T_TRAIT above for consistency, so we make sure we always handle it here?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think so

$id gets get to T_TRAIT via

$tokens = token_get_all("<?php\n" . $content);
...
  foreach ($tokens as $token) {
    ...
    list($id, $text) = $token;

We then change it to T_TRAIT_C via
$id = $id === T_TRAIT ? T_TRAIT_C : T_CLASS_C;

For this PR I've simply adapted the existing code that handles __CLASS__ tokens inside a class block to handle __TRAIT__ inside a trait block.

Copy link
Member

Choose a reason for hiding this comment

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

Ahhh okay I misread that $id assigment line. No worries then.

// Evaluate __CLASS__ . '.KEY' and self::class concatenation
$text = implode('\\', $currentClass);
} else {
Expand Down
33 changes: 33 additions & 0 deletions tests/php/i18n/i18nTextCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,39 @@ public function pointlessFunction4()
);
}

public function testCollectFromTrait()
{
$c = i18nTextCollector::create();
$mymodule = ModuleLoader::inst()->getManifest()->getModule('i18ntestmodule');
$php = <<<PHP
<?php
namespace SilverStripe\Framework\Core;

use SilverStripe\ORM\DataObject;

class MyTrait extends Base implements SomeService {
public function getNewLines(\$class) {
if (
!is_subclass_of(\$class, DataObject::class)
|| !Object::has_extension(\$class, \SilverStripe\Versioned\Versioned::class)
) {
return null;
}
return _t(
__TRAIT__.'.NEWLINES',
'New Lines'
);
}
}
PHP;
$this->assertEquals(
[
'SilverStripe\\Framework\\Core\\MyTrait.NEWLINES' => "New Lines",
],
$c->collectFromCode($php, null, $mymodule)
);
}

public function testNewlinesInEntityValues()
{
$c = i18nTextCollector::create();
Expand Down