Skip to content
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: 1 addition & 1 deletion src/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

namespace Symfony\Bundle\MakerBundle;

use Symfony\Component\DependencyInjection\Container;
use Doctrine\Common\Inflector\Inflector;
use Symfony\Component\DependencyInjection\Container;

/**
* @author Javier Eguiluz <[email protected]>
Expand Down
22 changes: 22 additions & 0 deletions src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,35 @@ public static function validateClassName(string $className, string $errorMessage
{
// remove potential opening slash so we don't match on it
$pieces = explode('\\', ltrim($className, '\\'));
$shortClassName = Str::getShortClassName($className);

$reservedKeywords = ['__halt_compiler', 'abstract', 'and', 'array',
'as', 'break', 'callable', 'case', 'catch', 'class',
'clone', 'const', 'continue', 'declare', 'default', 'die', 'do',
'echo', 'else', 'elseif', 'empty', 'enddeclare', 'endfor',
'endforeach', 'endif', 'endswitch', 'endwhile', 'eval',
'exit', 'extends', 'final', 'for', 'foreach', 'function',
'global', 'goto', 'if', 'implements', 'include',
'include_once', 'instanceof', 'insteadof', 'interface', 'isset',
'list', 'namespace', 'new', 'or', 'print', 'private',
'protected', 'public', 'require', 'require_once', 'return',
'static', 'switch', 'throw', 'trait', 'try', 'unset',
'use', 'var', 'while', 'xor',
'int', 'float', 'bool', 'string', 'true', 'false', 'null', 'void',
'iterable', 'object', '__FILE__', '__LINE__', '__DIR__', '__FUNCTION__', '__CLASS__',
'__METHOD__', '__NAMESPACE__', '__TRAIT__',
];

foreach ($pieces as $piece) {
if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $piece)) {
$errorMessage = $errorMessage ?: sprintf('"%s" is not valid as a PHP class name (it must start with a letter or underscore, followed by any number of letters, numbers, or underscores)', $className);

throw new RuntimeCommandException($errorMessage);
}

if (\in_array(strtolower($shortClassName), $reservedKeywords, true)) {
throw new RuntimeCommandException(sprintf('"%s" is a reserved keyword and thus cannot be used as class name in PHP.', $shortClassName));
}
}

// return original class name
Expand Down
15 changes: 14 additions & 1 deletion tests/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,17 @@ public function testInvalidScale()

Validator::validateScale(31);
}
}

public function testValidateClassName()
{
$this->assertSame('\App\Service\Foo', Validator::validateClassName('\App\Service\Foo'));
$this->assertSame('Foo', Validator::validateClassName('Foo'));
}

public function testInvalidClassName()
{
$this->expectException(RuntimeCommandException::class);
$this->expectExceptionMessage('"Class" is a reserved keyword and thus cannot be used as class name in PHP.');
Validator::validateClassName('App\Entity\Class');
}
}