diff --git a/src/Token/TokenCollection.php b/src/Token/TokenCollection.php index b432436..68a1606 100644 --- a/src/Token/TokenCollection.php +++ b/src/Token/TokenCollection.php @@ -58,9 +58,12 @@ public function addToken(Token $token): self public function forSimpleTokenParser(): array { $data = []; + $normalized = []; /** @var Token $token */ foreach ($this as $token) { + $data[$token->getName()] = $token->getParserValue(); + // Replace everything that's not allowed from the beginning of a string (PHP // variables cannot start with numbers for example) $tokenName = preg_replace_callback( @@ -84,7 +87,15 @@ static function (array $matches) { (string) $tokenName, ); - $data[$tokenName] = $token->getParserValue(); + if ($tokenName !== $token->getName()) { + $normalized[$tokenName] = $token->getParserValue(); + } + } + + foreach ($normalized as $tokenName => $value) { + if (!isset($data[$tokenName])) { + $data[$tokenName] = $value; + } } return $data; diff --git a/tests/Token/TokenCollectionTest.php b/tests/Token/TokenCollectionTest.php index 6dd27aa..71f5dba 100644 --- a/tests/Token/TokenCollectionTest.php +++ b/tests/Token/TokenCollectionTest.php @@ -103,12 +103,16 @@ public function testInvalidParserTokenNamesAreNormalized(): void { $tokenCollection = new TokenCollection(); $tokenCollection->add(Token::fromValue('invalid-because-of-dashes', 'foobar')); + $tokenCollection->add(Token::fromValue('invalid_because_of_dashes', 'foobar2')); $tokenCollection->add(Token::fromValue('123invalid_because_of_numbers_at_the_start', 'foobar')); $tokenCollection->add(Token::fromValue('1234-', 'foobar')); $this->assertSame( [ - 'invalid_because_of_dashes' => 'foobar', + 'invalid-because-of-dashes' => 'foobar', + 'invalid_because_of_dashes' => 'foobar2', + '123invalid_because_of_numbers_at_the_start' => 'foobar', + '1234-' => 'foobar', '___invalid_because_of_numbers_at_the_start' => 'foobar', '_____' => 'foobar', ], @@ -121,6 +125,7 @@ public function testInvalidParserTokenNamesAreNormalized(): void $this->assertSame( [ 'invalid-because-of-dashes' => 'foobar', + 'invalid_because_of_dashes' => 'foobar2', '123invalid_because_of_numbers_at_the_start' => 'foobar', '1234-' => 'foobar', ],