Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Do not scope class-like strings that cannot be class names #1027

Merged
merged 1 commit into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions specs/string-literal/var.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
$x = '\\Symfony\\Component\\Yaml\\Ya_1';
$x = 'Humbug\\Symfony\\Component\\Yaml\\Ya_1';
$x = '\\Humbug\\Symfony\\Component\\Yaml\\Ya_1';
$x = '1\2';

----
<?php
Expand All @@ -45,6 +46,7 @@
$x = 'Humbug\Symfony\Component\Yaml\Ya_1';
$x = 'Humbug\Symfony\Component\Yaml\Ya_1';
$x = 'Humbug\Symfony\Component\Yaml\Ya_1';
$x = '1\2';

PHP,

Expand Down
3 changes: 2 additions & 1 deletion src/PhpParser/NodeVisitor/StringScalarPrefixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ final class StringScalarPrefixer extends NodeVisitorAbstract
/^
(\\)? # leading backslash
(
[\p{L}_\d]+ # class-like name
[\p{L}_] # class-like name first character
[\p{L}_\d]* # class-like name
\\ # separator
)*
[\p{L}_\d]+ # class-like name
Expand Down
2 changes: 1 addition & 1 deletion src/Scoper/Symfony/XmlScoper.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
{
private const XML_EXTENSION_REGEX = '/\.xml$/i';
private const NAMESPACE_PATTERN = '/<prototype.*\snamespace="(?:(?<namespace>(?:[^\\\\]+(?<separator>\\\\(?:\\\\)?))))"/';
private const SINGLE_CLASS_PATTERN = '/(?:(?<singleClass>(?:[\p{L}_\d]+(?<singleSeparator>\\\\(?:\\\\)?))):)|(?<class>(?:[\p{L}_\d]+(?<separator>\\\\(?:\\\\)?)+)+[\p{L}_\d]+)/u';
private const SINGLE_CLASS_PATTERN = '/(?:(?<singleClass>(?:[\p{L}_][\p{L}_\d]*(?<singleSeparator>\\\\(?:\\\\)?))):)|(?<class>(?:[\p{L}_][\p{L}_\d]*(?<separator>\\\\(?:\\\\)?)+)+[\p{L}_\d]+)/u';

public function __construct(
private Scoper $decoratedScoper,
Expand Down
2 changes: 1 addition & 1 deletion src/Scoper/Symfony/YamlScoper.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
final readonly class YamlScoper implements Scoper
{
private const YAML_EXTENSION_REGEX = '/\.ya?ml$/i';
private const CLASS_PATTERN = '/(?:(?<singleClass>(?:[\p{L}_\d]+(?<singleSeparator>\\\\(?:\\\\)?))):)|(?<class>(?:[\p{L}_\d]+(?<separator>\\\\(?:\\\\)?)+)+[\p{L}_\d]+)/u';
private const CLASS_PATTERN = '/(?:(?<singleClass>(?:[\p{L}_][\p{L}_\d]*(?<singleSeparator>\\\\(?:\\\\)?))):)|(?<class>(?:[\p{L}_][\p{L}_\d]*(?<separator>\\\\(?:\\\\)?)+)+[\p{L}_\d]+)/u';

public function __construct(
private Scoper $decoratedScoper,
Expand Down
39 changes: 39 additions & 0 deletions tests/Scoper/Symfony/XmlScoperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,45 @@ public static function provideXmlFiles(): iterable
[],
];

yield 'class-like pattern' => [
<<<'XML'
<!-- config/services.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services
http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="App\Mail\PhpMailer" />
<service id="1\2" />

<service id="s1" class="App\Mail\PhpMailer" alias="App\Mail\PhpMailer" />
<service id="s2" class="1\2" alias="1\2" />
</services>
</container>
XML,
SymbolsConfiguration::create(),
<<<'XML'
<!-- config/services.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services
http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="Humbug\App\Mail\PhpMailer" />
<service id="1\2" />

<service id="s1" class="Humbug\App\Mail\PhpMailer" alias="Humbug\App\Mail\PhpMailer" />
<service id="s2" class="1\2" alias="1\2" />
</services>
</container>
XML,
[],
];

yield 'service with argument' => [
<<<'XML'
<!-- app/config/services.xml -->
Expand Down
10 changes: 10 additions & 0 deletions tests/Scoper/Symfony/YamlScoperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ public static function provideYamlFiles(): iterable
Symfony\Component\Console\Input\InputInterface:
alias: 'Symfony\Component\Console\Input\ArgvInput'
Symfony\Component\Console\Output\OutputInterface: '@Symfony\Component\Console\Output\ConsoleOutput'
1\2: ~
Service2:
class: 1\2
Service3:
alias: 1\2
YAML,
SymbolsConfiguration::create(),
<<<'YAML'
Expand All @@ -160,6 +165,11 @@ public static function provideYamlFiles(): iterable
Humbug\Symfony\Component\Console\Input\InputInterface:
alias: 'Humbug\Symfony\Component\Console\Input\ArgvInput'
Humbug\Symfony\Component\Console\Output\OutputInterface: '@Humbug\Symfony\Component\Console\Output\ConsoleOutput'
1\2: ~
Service2:
class: 1\2
Service3:
alias: 1\2
YAML,
[],
];
Expand Down
Loading