Skip to content

Commit

Permalink
Fix i18nTextCollector produces corrupt output / namespaces when runni…
Browse files Browse the repository at this point in the history
…ng under PHP8.0 (#10228)

* FIX i18nTextCollector produces corrupt output / namespaces when running under PHP8.0
  • Loading branch information
bimthebam authored May 30, 2022
1 parent 8ae7f30 commit d37dded
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
9 changes: 1 addition & 8 deletions src/i18n/TextCollection/i18nTextCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -571,20 +571,13 @@ public function collectFromCode($content, $fileName, Module $module)
if (is_array($token)) {
list($id, $text) = $token;

// PHP 8 namespace tokens
if (\defined('T_NAME_QUALIFIED') && in_array($id, [T_NAME_FULLY_QUALIFIED, T_NAME_QUALIFIED])) {
$inNamespace = true;
$currentClass[] = $text;
continue;
}

// Check class
if ($id === T_NAMESPACE) {
$inNamespace = true;
$currentClass = [];
continue;
}
if ($inNamespace && $id === T_STRING) {
if ($inNamespace && ($id === T_STRING || (defined('T_NAME_QUALIFIED') && $id === T_NAME_QUALIFIED))) {
$currentClass[] = $text;
continue;
}
Expand Down
8 changes: 7 additions & 1 deletion tests/php/i18n/i18nTextCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,9 +300,14 @@ public function testCollectFromCodeNamespace()
<?php
namespace SilverStripe\Framework\Core;
use SilverStripe\ORM\DataObject;
class MyClass extends Base implements SomeService {
public function getNewLines(\$class) {
if (!is_subclass_of(\$class, DataObject::class) || !Object::has_extension(\$class, Versioned::class)) {
if (
!is_subclass_of(\$class, DataObject::class)
|| !Object::has_extension(\$class, \SilverStripe\Versioned\Versioned::class)
) {
return null;
}
return _t(
Expand Down Expand Up @@ -331,6 +336,7 @@ public function getMagicConstantStringFromSelf()
}
}
PHP;

$this->assertEquals(
[
'SilverStripe\\Framework\\Core\\MyClass.NEWLINES' => "New Lines",
Expand Down

0 comments on commit d37dded

Please sign in to comment.