From f1ebc1d2b606a3c8386a89d7d65b3e986ab9961b Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Wed, 16 Feb 2022 13:08:54 +0100 Subject: [PATCH] Fix init author support to make email optional, fixes #10538 --- src/Composer/Command/InitCommand.php | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/Composer/Command/InitCommand.php b/src/Composer/Command/InitCommand.php index a5cf0fee4..c792d12f7 100644 --- a/src/Composer/Command/InitCommand.php +++ b/src/Composer/Command/InitCommand.php @@ -350,7 +350,7 @@ EOT $self = $this; $author = $io->askAndValidate( - 'Author ['.$author.', n to skip]: ', + 'Author ['.(is_string($author) ? ''.$author.', ' : '') . 'n to skip]: ', function ($value) use ($self, $author) { if ($value === 'n' || $value === 'no') { return; @@ -358,6 +358,10 @@ EOT $value = $value ?: $author; $author = $self->parseAuthorString($value); + if ($author['email'] === null) { + return $author['name']; + } + return sprintf('%s <%s>', $author['name'], $author['email']); }, null, @@ -471,22 +475,20 @@ EOT /** * @private * @param string $author - * @return array{name: string, email: string} + * @return array{name: string, email: string|null} */ public function parseAuthorString($author) { - if (Preg::isMatch('/^(?P[- .,\p{L}\p{N}\p{Mn}\'’"()]+) <(?P.+?)>$/u', $author, $match)) { - if ($this->isValidEmail($match['email'])) { - return array( - 'name' => trim($match['name']), - 'email' => $match['email'], - ); - } + if (Preg::isMatch('/^(?P[- .,\p{L}\p{N}\p{Mn}\'’"()]+)(?: <(?P.+?)>)?$/u', $author, $match)) { + return array( + 'name' => trim($match['name']), + 'email' => (isset($match['email']) && '' !== $match['email'] && $this->isValidEmail($match['email'])) ? $match['email'] : null, + ); } throw new \InvalidArgumentException( - 'Invalid author string. Must be in the format: '. - 'John Smith ' + 'Invalid author string. Must be in the formats: '. + 'Jane Doe or John Smith ' ); } @@ -684,11 +686,11 @@ EOT /** * @param string $author * - * @return array + * @return array */ protected function formatAuthors($author) { - return array($this->parseAuthorString($author)); + return array(array_filter($this->parseAuthorString($author))); } /**