Ensure names are not empty

This commit is contained in:
Nikita Popov 2017-12-01 18:13:55 +01:00
parent 336a49b428
commit b507fa43da
2 changed files with 25 additions and 1 deletions

View File

@ -218,8 +218,16 @@ class Name extends NodeAbstract
*/
private static function prepareName($name) : array {
if (\is_string($name)) {
if ('' === $name) {
throw new \InvalidArgumentException('Name cannot be empty');
}
return explode('\\', $name);
} elseif (\is_array($name)) {
if (empty($name)) {
throw new \InvalidArgumentException('Name cannot be empty');
}
return $name;
} elseif ($name instanceof self) {
return $name->parts;

View File

@ -132,13 +132,29 @@ class NameTest extends TestCase
}
/**
* @expectedException \InvalidArgumentException
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Expected string, array of parts or Name instance
*/
public function testInvalidArg() {
Name::concat('foo', new \stdClass);
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Name cannot be empty
*/
public function testInvalidEmptyString() {
new Name('');
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Name cannot be empty
*/
public function testInvalidEmptyArray() {
new Name([]);
}
/** @dataProvider provideTestIsSpecialClassName */
public function testIsSpecialClassName($name, $expected) {
$name = new Name($name);