Skip to content

Commit

Permalink
Fix init author support to make email optional, fixes #10538
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed Feb 16, 2022
1 parent 6ea5b84 commit f1ebc1d
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/Composer/Command/InitCommand.php
Expand Up @@ -350,14 +350,18 @@ function ($value) use ($name) {

$self = $this;
$author = $io->askAndValidate(
'Author [<comment>'.$author.'</comment>, n to skip]: ',
'Author ['.(is_string($author) ? '<comment>'.$author.'</comment>, ' : '') . 'n to skip]: ',
function ($value) use ($self, $author) {
if ($value === 'n' || $value === 'no') {
return;
}
$value = $value ?: $author;
$author = $self->parseAuthorString($value);

if ($author['email'] === null) {
return $author['name'];
}

return sprintf('%s <%s>', $author['name'], $author['email']);
},
null,
Expand Down Expand Up @@ -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<name>[- .,\p{L}\p{N}\p{Mn}\'’"()]+) <(?P<email>.+?)>$/u', $author, $match)) {
if ($this->isValidEmail($match['email'])) {
return array(
'name' => trim($match['name']),
'email' => $match['email'],
);
}
if (Preg::isMatch('/^(?P<name>[- .,\p{L}\p{N}\p{Mn}\'’"()]+)(?: <(?P<email>.+?)>)?$/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 <john@example.com>'
'Invalid author string. Must be in the formats: '.
'Jane Doe or John Smith <john@example.com>'
);
}

Expand Down Expand Up @@ -684,11 +686,11 @@ final protected function determineRequirements(InputInterface $input, OutputInte
/**
* @param string $author
*
* @return array<int, array{name: string, email: string}>
* @return array<int, array{name: string, email: string|null}>
*/
protected function formatAuthors($author)
{
return array($this->parseAuthorString($author));
return array(array_filter($this->parseAuthorString($author)));
}

/**
Expand Down

0 comments on commit f1ebc1d

Please sign in to comment.