Skip to content

Commit 4f8d70b

Browse files
authored
fix: Do not scope class-like strings that cannot be class names (#1027)
Closes #951.
1 parent b532a5b commit 4f8d70b

File tree

6 files changed

+55
-3
lines changed

6 files changed

+55
-3
lines changed

specs/string-literal/var.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
$x = '\\Symfony\\Component\\Yaml\\Ya_1';
3232
$x = 'Humbug\\Symfony\\Component\\Yaml\\Ya_1';
3333
$x = '\\Humbug\\Symfony\\Component\\Yaml\\Ya_1';
34+
$x = '1\2';
3435
3536
----
3637
<?php
@@ -45,6 +46,7 @@
4546
$x = 'Humbug\Symfony\Component\Yaml\Ya_1';
4647
$x = 'Humbug\Symfony\Component\Yaml\Ya_1';
4748
$x = 'Humbug\Symfony\Component\Yaml\Ya_1';
49+
$x = '1\2';
4850

4951
PHP,
5052

src/PhpParser/NodeVisitor/StringScalarPrefixer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ final class StringScalarPrefixer extends NodeVisitorAbstract
102102
/^
103103
(\\)? # leading backslash
104104
(
105-
[\p{L}_\d]+ # class-like name
105+
[\p{L}_] # class-like name first character
106+
[\p{L}_\d]* # class-like name
106107
\\ # separator
107108
)*
108109
[\p{L}_\d]+ # class-like name

src/Scoper/Symfony/XmlScoper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
{
3636
private const XML_EXTENSION_REGEX = '/\.xml$/i';
3737
private const NAMESPACE_PATTERN = '/<prototype.*\snamespace="(?:(?<namespace>(?:[^\\\\]+(?<separator>\\\\(?:\\\\)?))))"/';
38-
private const SINGLE_CLASS_PATTERN = '/(?:(?<singleClass>(?:[\p{L}_\d]+(?<singleSeparator>\\\\(?:\\\\)?))):)|(?<class>(?:[\p{L}_\d]+(?<separator>\\\\(?:\\\\)?)+)+[\p{L}_\d]+)/u';
38+
private const SINGLE_CLASS_PATTERN = '/(?:(?<singleClass>(?:[\p{L}_][\p{L}_\d]*(?<singleSeparator>\\\\(?:\\\\)?))):)|(?<class>(?:[\p{L}_][\p{L}_\d]*(?<separator>\\\\(?:\\\\)?)+)+[\p{L}_\d]+)/u';
3939

4040
public function __construct(
4141
private Scoper $decoratedScoper,

src/Scoper/Symfony/YamlScoper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
final readonly class YamlScoper implements Scoper
3535
{
3636
private const YAML_EXTENSION_REGEX = '/\.ya?ml$/i';
37-
private const CLASS_PATTERN = '/(?:(?<singleClass>(?:[\p{L}_\d]+(?<singleSeparator>\\\\(?:\\\\)?))):)|(?<class>(?:[\p{L}_\d]+(?<separator>\\\\(?:\\\\)?)+)+[\p{L}_\d]+)/u';
37+
private const CLASS_PATTERN = '/(?:(?<singleClass>(?:[\p{L}_][\p{L}_\d]*(?<singleSeparator>\\\\(?:\\\\)?))):)|(?<class>(?:[\p{L}_][\p{L}_\d]*(?<separator>\\\\(?:\\\\)?)+)+[\p{L}_\d]+)/u';
3838

3939
public function __construct(
4040
private Scoper $decoratedScoper,

tests/Scoper/Symfony/XmlScoperTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,45 @@ public static function provideXmlFiles(): iterable
387387
[],
388388
];
389389

390+
yield 'class-like pattern' => [
391+
<<<'XML'
392+
<!-- config/services.xml -->
393+
<?xml version="1.0" encoding="UTF-8" ?>
394+
<container xmlns="http://symfony.com/schema/dic/services"
395+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
396+
xsi:schemaLocation="http://symfony.com/schema/dic/services
397+
http://symfony.com/schema/dic/services/services-1.0.xsd">
398+
399+
<services>
400+
<service id="App\Mail\PhpMailer" />
401+
<service id="1\2" />
402+
403+
<service id="s1" class="App\Mail\PhpMailer" alias="App\Mail\PhpMailer" />
404+
<service id="s2" class="1\2" alias="1\2" />
405+
</services>
406+
</container>
407+
XML,
408+
SymbolsConfiguration::create(),
409+
<<<'XML'
410+
<!-- config/services.xml -->
411+
<?xml version="1.0" encoding="UTF-8" ?>
412+
<container xmlns="http://symfony.com/schema/dic/services"
413+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
414+
xsi:schemaLocation="http://symfony.com/schema/dic/services
415+
http://symfony.com/schema/dic/services/services-1.0.xsd">
416+
417+
<services>
418+
<service id="Humbug\App\Mail\PhpMailer" />
419+
<service id="1\2" />
420+
421+
<service id="s1" class="Humbug\App\Mail\PhpMailer" alias="Humbug\App\Mail\PhpMailer" />
422+
<service id="s2" class="1\2" alias="1\2" />
423+
</services>
424+
</container>
425+
XML,
426+
[],
427+
];
428+
390429
yield 'service with argument' => [
391430
<<<'XML'
392431
<!-- app/config/services.xml -->

tests/Scoper/Symfony/YamlScoperTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,11 @@ public static function provideYamlFiles(): iterable
152152
Symfony\Component\Console\Input\InputInterface:
153153
alias: 'Symfony\Component\Console\Input\ArgvInput'
154154
Symfony\Component\Console\Output\OutputInterface: '@Symfony\Component\Console\Output\ConsoleOutput'
155+
1\2: ~
156+
Service2:
157+
class: 1\2
158+
Service3:
159+
alias: 1\2
155160
YAML,
156161
SymbolsConfiguration::create(),
157162
<<<'YAML'
@@ -160,6 +165,11 @@ public static function provideYamlFiles(): iterable
160165
Humbug\Symfony\Component\Console\Input\InputInterface:
161166
alias: 'Humbug\Symfony\Component\Console\Input\ArgvInput'
162167
Humbug\Symfony\Component\Console\Output\OutputInterface: '@Humbug\Symfony\Component\Console\Output\ConsoleOutput'
168+
1\2: ~
169+
Service2:
170+
class: 1\2
171+
Service3:
172+
alias: 1\2
163173
YAML,
164174
[],
165175
];

0 commit comments

Comments
 (0)