diff --git a/src/Composer/Command/InitCommand.php b/src/Composer/Command/InitCommand.php index a5cf0fee4a68..c792d12f7a3f 100644 --- a/src/Composer/Command/InitCommand.php +++ b/src/Composer/Command/InitCommand.php @@ -350,7 +350,7 @@ function ($value) use ($name) { $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 @@ function ($value) use ($self, $author) { $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 @@ function ($value) use ($autoload) { /** * @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 @@ final protected function determineRequirements(InputInterface $input, OutputInte /** * @param string $author * - * @return array + * @return array */ protected function formatAuthors($author) { - return array($this->parseAuthorString($author)); + return array(array_filter($this->parseAuthorString($author))); } /**