Extract email validation into a method
parent
e3b9dd10c2
commit
9aa0aba77c
|
@ -36,7 +36,7 @@ class InitCommand extends Command
|
||||||
public function parseAuthorString($author)
|
public function parseAuthorString($author)
|
||||||
{
|
{
|
||||||
if (preg_match('/^(?P<name>[- \.,\w\'’]+) <(?P<email>.+?)>$/u', $author, $match)) {
|
if (preg_match('/^(?P<name>[- \.,\w\'’]+) <(?P<email>.+?)>$/u', $author, $match)) {
|
||||||
if (!function_exists('filter_var') || version_compare(PHP_VERSION, '5.3.3', '<') || $match['email'] === filter_var($match['email'], FILTER_VALIDATE_EMAIL)) {
|
if ($this->isValidEmail($match['email'])) {
|
||||||
return array(
|
return array(
|
||||||
'name' => trim($match['name']),
|
'name' => trim($match['name']),
|
||||||
'email' => $match['email']
|
'email' => $match['email']
|
||||||
|
@ -487,4 +487,11 @@ EOT
|
||||||
|
|
||||||
file_put_contents($ignoreFile, $contents . $vendor. "\n");
|
file_put_contents($ignoreFile, $contents . $vendor. "\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function isValidEmail($email)
|
||||||
|
{
|
||||||
|
if (!function_exists('filter_var')) return true; // Bypass if we can't validate it
|
||||||
|
if (version_compare(PHP_VERSION, '5.3.3', '<')) return true; // ?
|
||||||
|
return false !== filter_var($email, FILTER_VALIDATE_EMAIL);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,10 +13,17 @@ class InitCommandTest extends TestCase
|
||||||
$command->parseAuthorString('John Smith <john@example.com>');
|
$command->parseAuthorString('John Smith <john@example.com>');
|
||||||
}
|
}
|
||||||
|
|
||||||
function testParseInvalidAuthorString()
|
function testParseEmptyAuthorString()
|
||||||
{
|
{
|
||||||
$command = new InitCommand;
|
$command = new InitCommand;
|
||||||
$this->setExpectedException('InvalidArgumentException');
|
$this->setExpectedException('InvalidArgumentException');
|
||||||
$command->parseAuthorString('');
|
$command->parseAuthorString('');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function testParseAuthorStringWithInvalidEmail()
|
||||||
|
{
|
||||||
|
$command = new InitCommand;
|
||||||
|
$this->setExpectedException('InvalidArgumentException');
|
||||||
|
$command->parseAuthorString('John Smith <john>');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue