From 51fd1d6b7e45d7a7c0c26cae3cab7811b871362e Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Tue, 13 Jun 2023 10:11:12 +1200 Subject: [PATCH] FIX Handle __TRAIT__ in i18nTextCollector --- src/i18n/TextCollection/i18nTextCollector.php | 10 +++--- tests/php/i18n/i18nTextCollectorTest.php | 33 +++++++++++++++++++ 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/i18n/TextCollection/i18nTextCollector.php b/src/i18n/TextCollection/i18nTextCollector.php index 558dadf63..212655e87 100644 --- a/src/i18n/TextCollection/i18nTextCollector.php +++ b/src/i18n/TextCollection/i18nTextCollector.php @@ -625,14 +625,14 @@ class i18nTextCollector } } - // 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; @@ -722,7 +722,7 @@ class i18nTextCollector } else { throw new LogicException("Invalid string escape: " . $text); } - } elseif ($id === T_CLASS_C) { + } elseif ($id === T_CLASS_C || $id === T_TRAIT_C) { // Evaluate __CLASS__ . '.KEY' and self::class concatenation $text = implode('\\', $currentClass); } else { diff --git a/tests/php/i18n/i18nTextCollectorTest.php b/tests/php/i18n/i18nTextCollectorTest.php index df33ced47..f79c534c3 100644 --- a/tests/php/i18n/i18nTextCollectorTest.php +++ b/tests/php/i18n/i18nTextCollectorTest.php @@ -407,6 +407,39 @@ PHP; ); } + public function testCollectFromTrait() + { + $c = i18nTextCollector::create(); + $mymodule = ModuleLoader::inst()->getManifest()->getModule('i18ntestmodule'); + $php = <<assertEquals( + [ + 'SilverStripe\\Framework\\Core\\MyTrait.NEWLINES' => "New Lines", + ], + $c->collectFromCode($php, null, $mymodule) + ); + } + public function testNewlinesInEntityValues() { $c = i18nTextCollector::create();