From 28ec4fa7b032586df098546dede9b60a19e3c4cc Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Wed, 16 Feb 2022 13:24:15 +0100 Subject: [PATCH] Fix handling of invalid emails --- src/Composer/Command/InitCommand.php | 11 ++++++++--- tests/Composer/Test/Command/InitCommandTest.php | 8 ++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) 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;