php-scoper icon indicating copy to clipboard operation
php-scoper copied to clipboard

Namespace being added to a numbered string

Open regularlabs opened this issue 1 year ago • 3 comments
trafficstars

Came across this line:

$body = RegEx::replace('...(some).*?(regex)...', '\\1\\2', $body);

Which got replaced with:

$body = \MyPhpScoperPrefix\RegEx::replace('...(some).*?(regex)...', '\MyPhpScoperPrefix\\1\\2', $body);

I worked around this issue by adding this to my scoper.inc.php:

    'exclude-namespaces' => [
        ...
        '1'
    ],

But it is of course a bug.

regularlabs avatar Mar 05 '24 07:03 regularlabs

Unfortunately it is expected to some degree. It is hard to detect what string can and cannot be scoped so there is bound to be cases of strings incorrectly scoped and others incorrectly not scoped. You will have to fix this via patchers.

This peculiar case however does not make much sense indeed, as 1\2 cannot be a class name.

theofidry avatar Mar 08 '24 20:03 theofidry

Exactly, as classnames/namespaces cannot start with a number. So probably a good idea to adjust the regex (I assume that is being used) to find the namespaces.

regularlabs avatar Mar 08 '24 20:03 regularlabs

Probably in XmlScoper, the regex parts [\p{L}_\d]+ should be replaced with [\p{L}_][\p{L}_\d]*. And [^\\\\]+ with [\p{L}][^\\\\]*.

regularlabs avatar Mar 10 '24 19:03 regularlabs

Seems to be working great. Thanks!

regularlabs avatar May 08 '24 17:05 regularlabs