Skip to content

Commit 8be5d3e

Browse files
karyna-tandrewbess
authored andcommitted
Fixed tokenizer functionality in i18n
1 parent aadca37 commit 8be5d3e

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

setup/src/Magento/Setup/Module/I18n/Parser/Adapter/Php/Tokenizer.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@ public function isMatchingClass($className)
6868
$state = 1;
6969
$classString = '';
7070
while ($token = $this->getNextRealToken()) {
71-
if ($token->isNamespaceSeparator() && $state != 2) {
71+
if ($token->isFullQualifiedName()) {
72+
// In PHP 8.0, it can be already a full name e.g. \Magento\Framework\Phrase.
73+
$classString = $token->getValue();
74+
$state = 3;
75+
} elseif ($token->isNamespaceSeparator() && $state != 2) {
7276
$classString .= $token->getValue();
7377
$state = 2;
7478
} elseif ($token->isIdentifier() && $state != 3) {

setup/src/Magento/Setup/Module/I18n/Parser/Adapter/Php/Tokenizer/Token.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,16 @@ public function isNew()
8585
return $this->getName() == T_NEW;
8686
}
8787

88+
/**
89+
* Check if a token has a Qualified Name type, which was introduced in PHP 8.
90+
*
91+
* @return bool
92+
*/
93+
public function isFullQualifiedName(): bool
94+
{
95+
return $this->getName() === T_NAME_FULLY_QUALIFIED;
96+
}
97+
8898
/**
8999
* Whenever token is equal function
90100
*

setup/src/Magento/Setup/Test/Unit/Module/I18n/Parser/Adapter/Php/TokenizerTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,26 @@ public function testIsMatchingClass()
5858
$this->assertFalse($this->tokenizer->isMatchingClass($class)); // ;
5959
}
6060

61+
/**
62+
* Test getting next Real token for PHP > 8, where namespaced names are treated as single token.
63+
*
64+
* @requires PHP >= 8.0
65+
* @return void
66+
*/
67+
public function testGetNextRealTokenWhenNamespaceIsSingleToken(): void
68+
{
69+
$this->parseFile();
70+
$this->assertEquals('new', $this->tokenizer->getNextRealToken()->getValue());
71+
$this->assertEquals('\\Magento\\Framework\\Phrase', $this->tokenizer->getNextRealToken()->getValue());
72+
$this->assertEquals('(', $this->tokenizer->getNextRealToken()->getValue());
73+
$this->assertEquals('\'Testing\'', $this->tokenizer->getNextRealToken()->getValue());
74+
$this->assertEquals(')', $this->tokenizer->getNextRealToken()->getValue());
75+
$this->assertEquals(';', $this->tokenizer->getNextRealToken()->getValue());
76+
}
77+
6178
/**
6279
* @covers \Magento\Setup\Module\I18n\Parser\Adapter\Php\Tokenizer::getNextRealToken
80+
* @requires PHP < 8.0
6381
*/
6482
public function testGetNextRealToken()
6583
{
@@ -79,6 +97,7 @@ public function testGetNextRealToken()
7997

8098
/**
8199
* @covers \Magento\Setup\Module\I18n\Parser\Adapter\Php\Tokenizer::isEndOfLoop
100+
* @requires PHP < 8.0
82101
*/
83102
public function testIsEndOfLoop()
84103
{

0 commit comments

Comments
 (0)