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))); } /**