scramble icon indicating copy to clipboard operation
scramble copied to clipboard

`use` on comment break the parser

Open Eighke opened this issue 2 years ago • 1 comments

Hello there,

Small bug I have at the moment: if any comment is set above the namespace and contain the word use (or namespace I suppose) it will break the parser. For example:

/*
 * you may not use this file except in compliance with the License.
 */

namespace App\Http\Controllers;

use ...

class JsonApiController extends Controller {}

Will return

<?php
use this file except in compliance with the License.
 */

namespace App\Http\Controllers;
use ...

Instead of

<?php
namespace App\Http\Controllers;
use ...

The problem is that when using 3rd party, we don't have any control over the comments. And some add licence inside every files. And licence text has a high change to contain use.


\Dedoc\Scramble\Infer\Reflector\ClassReflector::getNameContext

$re = '/(namespace|use) ([.\s\S]*?);/m';

Will keep the comment one.

I guess the comment should be removed before, but I'm not sure if this method is used for anything else.

$re = '/\/\*(.*?)\*\//s';
$code = preg_replace($re, '', $code);

Should do the trick else.

Eighke avatar Aug 23 '23 11:08 Eighke

@Eighke what a great catch! Thanks, will fix.

romalytvynenko avatar Aug 24 '23 07:08 romalytvynenko