Skip to content

Commit

Permalink
String starting with \ is valid class name
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed May 14, 2021
1 parent e7ac788 commit 428baf5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Reflection/ClassNameHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ClassNameHelper
public static function isValidClassName(string $name): bool
{
// from https://stackoverflow.com/questions/3195614/validate-class-method-names-with-regex#comment104531582_12011255
return Strings::match($name, '/^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*(\\\\[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)*$/') !== null;
return Strings::match(ltrim($name, '\\'), '/^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*(\\\\[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)*$/') !== null;

This comment has been minimized.

Copy link
@mvorisek

mvorisek May 17, 2021

Contributor

should be ...^\\\\?[...

see https://3v4l.org/A9Qbp

This comment has been minimized.

Copy link
@ondrejmirtes

ondrejmirtes May 17, 2021

Author Member

Please send a PR

}

}
5 changes: 5 additions & 0 deletions tests/PHPStan/Rules/Classes/InstantiationRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,4 +342,9 @@ public function testBug3425(): void
]);
}

public function testBug5002(): void
{
$this->analyse([__DIR__ . '/data/bug-5002.php'], []);
}

}
18 changes: 18 additions & 0 deletions tests/PHPStan/Rules/Classes/data/bug-5002.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Test;

class Foo
{

}

function (): void {
$name = '\Test\Foo';
new $name();
};

function (): void {
$name = '\\Test\Foo';
new $name();
};

0 comments on commit 428baf5

Please sign in to comment.