1
0
Fork 0

Fix init author support to make email optional, fixes #10538

pull/10516/head
Jordi Boggiano 2022-02-16 13:08:54 +01:00
parent 6ea5b84bd9
commit f1ebc1d2b6
No known key found for this signature in database
GPG Key ID: 7BBD42C429EC80BC
1 changed files with 15 additions and 13 deletions

View File

@ -350,7 +350,7 @@ EOT
$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;
@ -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<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>'
);
}
@ -684,11 +686,11 @@ EOT
/**
* @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)));
}
/**