-
-
Notifications
You must be signed in to change notification settings - Fork 162
Description
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