Skip to content

Docblock containing the word "class" causes wrong class loading in Sylius entity parsing #1011

@Olivier127

Description

@Olivier127

Sylius version(s) affected

2.0.6

Description

When a PHP class contains a docblock header where the word class appears before the actual class keyword (in the comment), Sylius mistakenly tries to load a non-existent class.
It seems that the parser incorrectly interprets the first occurrence of the word class found in the file, even if it's inside a comment block, leading to unexpected behavior.

This issue occurs during entity metadata reading or any process relying on class scanning, and results in an attempt to autoload a class named after the word immediately following class in the comment block.

How to reproduce

Create a simple entity, e.g., MyClass.php:

<?php

namespace App\Entity;

/**
 * This docblock for this class explains what to do.
 */
class MyClass
{
}

Observe that Syliust tries to load a class named explains (taken from the comment text), causing an error like:

Class "explains" does not exist

Possible Solution

You could fix this issue by improving the class detection in
ResourceBundle/src/Component/src/Reflection/ClassReflection.php (around line 49).

Currently, the code uses:

if (!preg_match('/class +([^{ ]+)/', $fileContent, $matches)) {
    // no class found
    continue;
}

However, this regex may match the word class inside a comment block (/** ... */), causing incorrect behavior.

👉 I suggest improving the parsing logic by checking that the match is not located within a comment block.

may be this kind of code :

if (preg_match('/class +([^{ ]+)/', $fileContent, $matches)) {
    // Add extra validation to ensure this is not inside a comment
    if (preg_match('/^\s*[\*\/]/m', $matches[0])) {
        // It's inside a comment, ignore
        continue;
    }

    // Otherwise proceed
}

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions