diff --git a/src/Composer/Command/InitCommand.php b/src/Composer/Command/InitCommand.php index c792d12f7..11a19de26 100644 --- a/src/Composer/Command/InitCommand.php +++ b/src/Composer/Command/InitCommand.php @@ -479,10 +479,15 @@ EOT */ public function parseAuthorString($author) { - if (Preg::isMatch('/^(?P[- .,\p{L}\p{N}\p{Mn}\'’"()]+)(?: <(?P.+?)>)?$/u', $author, $match)) { + if (Preg::isMatch('/^(?P[- .,\p{L}\p{N}\p{Mn}\'’"()]+)(?:\s+<(?P.+?)>)?$/u', $author, $match)) { + $hasEmail = isset($match['email']) && '' !== $match['email']; + if ($hasEmail && !$this->isValidEmail($match['email'])) { + throw new \InvalidArgumentException('Invalid email "'.$match['email'].'"'); + } + return array( 'name' => trim($match['name']), - 'email' => (isset($match['email']) && '' !== $match['email'] && $this->isValidEmail($match['email'])) ? $match['email'] : null, + 'email' => $hasEmail ? $match['email'] : null, ); } @@ -690,7 +695,7 @@ EOT */ protected function formatAuthors($author) { - return array(array_filter($this->parseAuthorString($author))); + return array(array_filter($this->parseAuthorString($author), 'is_string')); } /** diff --git a/tests/Composer/Test/Command/InitCommandTest.php b/tests/Composer/Test/Command/InitCommandTest.php index 8006fe106..d9a4f57c4 100644 --- a/tests/Composer/Test/Command/InitCommandTest.php +++ b/tests/Composer/Test/Command/InitCommandTest.php @@ -25,6 +25,14 @@ class InitCommandTest extends TestCase $this->assertEquals('john@example.com', $author['email']); } + public function testParseValidAuthorStringWithoutEmail() + { + $command = new InitCommand; + $author = $command->parseAuthorString('John Smith'); + $this->assertEquals('John Smith', $author['name']); + $this->assertNull($author['email']); + } + public function testParseValidUtf8AuthorString() { $command = new InitCommand;