ERROR: Unable to parse file PHP ENUMs

Template for a good bug report, formatted with Markdown:

  • versions used (SonarQube: Community Edition 9.1 (build 47736), Scanner: 4.6.2.2472, Plugin, and any relevant extension)
  • steps to reproduce: run sonar-scanner on app/code using PHP 8.1 with Enums.
ERROR: Unable to parse file [file:///var/www/cpt.local/html/app/Enums/Export/Orientation.php] at line 7
ERROR: Parse error at line 7 column 8:

 1: <?php
 2: 
 3: namespace App\Enums\Export;
 4: 
 5: use App\Enums\Base;
 6: 
 7: enum Orientation: int
           ^
 8: {
 9:     use Base;
10: 
11:     case LANDSCAPE = 1;
12:     case PORTRAIT = 2;
13: 
14:     public const DEFAULT = self::LANDSCAPE;
15: 
16:     /**
17:      * @throws \Exception

ERROR: Unable to parse file [file:///var/www/cpt.local/html/app/Enums/File/Type.php] at line 7
ERROR: Parse error at line 7 column 6:

 1: <?php
 2: 
 3: namespace App\Enums\File;
 4: 
 5: use App\Enums\Base;
 6: 
 7: enum Type: int
         ^
 8: {
 9:     use Base;
10: 
11:     case INCOMING = 1;
12:     case RESULTING = 2;
13:     case ERROR = 3;
14: 
15:     public const DEFAULT = self::INCOMING;
16: 
17:     public function getName(): string

ERROR: Unable to parse file [file:///var/www/cpt.local/html/app/Enums/Export/Type.php] at line 7
ERROR: Parse error at line 7 column 6:

 1: <?php
 2: 
 3: namespace App\Enums\Export;
 4: 
 5: use App\Enums\Base;
 6: 
 7: enum Type: int
         ^
 8: {
 9:     use Base;
10: 
11:     case ORDERS = 1;
12:     case ORDER_PRODUCTS = 2;
13: 
14:     public const DEFAULT = self::ORDERS;
15: 
16:     public function getName(): string
17:     {

ERROR: Unable to parse file [file:///var/www/cpt.local/html/app/Enums/Export/Format.php] at line 7
ERROR: Parse error at line 7 column 6:

 1: <?php
 2: 
 3: namespace App\Enums\Export;
 4: 
 5: use App\Enums\Base;
 6: 
 7: enum Format: int
         ^
 8: {
 9:     use Base;
10: 
11:     case CSV = 1;
12:     case ODS = 2;
13:     case PDF = 3;
14:     case XLSX = 4;
15: 
16:     public const DEFAULT = self::XLSX;
17: 


Hey there.

PHP 8.1 syntax will become supported in SonarQube v9.3, which should be released at the end of this month.

Did Sonar Team consider make some update to previous version of Sonar - eg.: 9.1/9.2 - to work with PHP 8.1? Or PHP 8.1 will start working from 9.3 Sonar version?

PHP 8.1 will be supported from SonarQube v9.3. New language analysis features are exclusively supported in the newest versions of SonarQube, not backported.

Right now I have SonarQube v9.4 and newest SonarScanner v4.7 and I still have same unsupported ENUMs in PHP 8.1.

Hello @LocalHeroPro,

Iā€™m not able to reproduce the problem with version 9.4 using the code snippet from your first post. Is it exactly this code that causes problems with the current version? Otherwise, can you provide us with another reproducer that constructs an enum that is not parsed properly?

Best,
Nils

  • Community Edition Version 9.4 (build 54424)
  • SonarScanner 4.7.0.2747, Java 11.0.14.1 Eclipse Adoptium (64-bit), Linux 5.4.0-109-generic amd64

In that snippet you have two examples of errors with ENUMs.

//
INFO: Sensor PHP sensor [php]
INFO: Starting PHP symbol indexer
INFO: 395 source files to be analyzed
INFO: 395/395 source files have been analyzed
INFO: Starting PHP rules
INFO: 395 source files to be analyzed
ERROR: Unable to parse file [file:///var/www/cpt.local/html/app/Enums/OrderStatus.php] at line 9
ERROR: Parse error at line 9 column 10:

 1: <?php
 2: 
 3: namespace App\Enums;
 4: 
 5: enum OrderStatus: int
 6: {
 7:     use Base;
 8: 
 9:     case NEW = 1;
             ^
10:     case PICKUP_IN_PERSON = 2;
11:     case FOR_DISPATCH = 3;
12:     case CANCELED = 4;
13:     case ENDED = 5;
14: 
15:     public function label(): string
16:     {
17:         return match ($this) {
18:             self::NEW => 'new',
19:             self::PICKUP_IN_PERSON => 'pickup in person',

ERROR: Unable to parse file [file:///var/www/cpt.local/html/app/Enums/TaskStatus.php] at line 7
ERROR: Parse error at line 7 column 10:

 1: <?php
 2: 
 3: namespace App\Enums;
 4: 
 5: enum TaskStatus: int
 6: {
 7:     case NEW = 1;
             ^
 8:     case IN_PROGRESS = 2;
 9:     case ENDED_WITH_ERRORS = 3;
10:     case ENDED_WITH_SUCCESS = 4;
11: 
12:     public const DEFAULT = self::NEW;
13: 
14:     public function label(): string
15:     {
16:         return match ($this) {
17:             self::NEW => 'new',

INFO: 395/395 source files have been analyzed
INFO: Importing /var/www/cpt.local/html/phpunit-execution-result.xml
INFO: Importing /var/www/cpt.local/html/phpunit-coverage-result.xml
INFO: Sensor PHP sensor [php] (done) | time=13201ms
INFO: Sensor Analyzer for "php.ini" files [php]
INFO: Sensor Analyzer for "php.ini" files [php] (done) | time=3ms
INFO: Sensor Text Sensor [text]

//
1 Like

This is a very helpful reproducer. The problem comes from the keyword NEW which leads to the parser error. I have created a ticket so that this error will be fixed as soon as possible. Thanks for your advice.

Best,

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.