List keyword is allowed in namespaces since php 8.0, but it causes parse error

Must-share information (formatted with Markdown):

  • which versions are you using?
    • Community Edition, Version 9.6.1 (build 59531)
    • SonarScanner 4.7.0.2747
  • what are you trying to achieve

Analyze PHP 8.x source code with List in namespace name.

Since PHP 8.0 List is not a reserved word in namespaces anymore, so the following code is valid and passes:

# src/List/SomeClass.php
<?php
declare(strict_types=1);

namespace Kata\List;

final class SomeClass {
    public function hello(): string
    {
        return 'Hello';
    }
}

# tests/List/SomeClassTest.php

<?php
declare(strict_types=1);

namespace Kata\Tests\List;

use Kata\List\SomeClass;
use PHPUnit\Framework\TestCase;

final class SomeClassTest extends TestCase
{
    /**
     * @test
     */
    public function hello(): void
    {
        $some = new SomeClass();
        self::assertSame('Hello', $some->hello());
    }
}

Also see Online PHP editor | output for QBqOF for example.

SonarScanner tells me:

ERROR: Unable to parse file [file:///builds/gSZtMLGf/0/oroessner/sonar-php-list-issue/src/List/SomeClass.php] at line 4
ERROR: Parse error at line 4 column 16:
 1: <?php
 2: declare(strict_types=1);
 3: 
 4: namespace Kata\List;
                   ^
 5: 
 6: final class SomeClass {
 7:     public function hello(): string
 8:     {
 9:         return 'Hello';
10:     }
11: }
12: 
INFO: 2 source files to be analyzed
ERROR: Unable to parse file [file:///builds/gSZtMLGf/0/oroessner/sonar-php-list-issue/tests/List/SomeClassTest.php] at line 4
ERROR: Parse error at line 4 column 22:
 1: <?php
 2: declare(strict_types=1);
 3: 
 4: namespace Kata\Tests\List;
                         ^
 5: 
 6: use Kata\List\SomeClass;
 7: use PHPUnit\Framework\TestCase;
 8: 
 9: final class SomeClassTest extends TestCase {
10:     /**
11:      * @test
12:      */
13:     public function hello(): void {
14:         
INFO: 2/2 source files have been analyzed

Could you please look into it?

Hey there.

This bug is tracked at SONARPHP-1085.

I’ve linked your Community Thread to help build some traction.

1 Like