Skip to content

Commit 29be56c

Browse files
Reject subdomain (#110)
1 parent f8d405b commit 29be56c

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

src/EmailChecker/EmailChecker.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,24 @@ public function isValid(string $email): bool
5151
return false;
5252
}
5353

54-
return !$this->adapter->isThrowawayDomain($domain);
54+
foreach ($this->allDomainSuffixes($domain) as $domainSuffix) {
55+
if ($this->adapter->isThrowawayDomain($domainSuffix)) {
56+
return false;
57+
}
58+
}
59+
60+
return true;
61+
}
62+
63+
/**
64+
* @return iterable<string>
65+
*/
66+
private function allDomainSuffixes(string $domain): iterable
67+
{
68+
$components = explode('.', $domain);
69+
70+
foreach ($components as $i => $_) {
71+
yield implode('.', array_slice($components, $i));
72+
}
5573
}
5674
}

tests/EmailChecker/Tests/EmailCheckerTest.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,39 +12,40 @@
1212
namespace EmailChecker\Tests;
1313

1414
use EmailChecker\Adapter\AdapterInterface;
15+
use EmailChecker\Adapter\ArrayAdapter;
1516
use EmailChecker\EmailChecker;
1617

1718
final class EmailCheckerTest extends TestCase
1819
{
1920
public function testEmailIsValid(): void
2021
{
21-
$adapter = $this->createMock(AdapterInterface::class);
22-
$adapter
23-
->method('isThrowawayDomain')
24-
->willReturn(false);
25-
22+
$adapter = new ArrayAdapter([]);
2623
$checker = new EmailChecker($adapter);
2724

2825
self::assertTrue($checker->isValid('foo@bar.org'));
2926
}
3027

3128
public function testEmailIsNotValid(): void
3229
{
33-
$adapter = $this->createMock(AdapterInterface::class);
34-
$adapter
35-
->method('isThrowawayDomain')
36-
->willReturn(true);
37-
30+
$adapter = new ArrayAdapter(['bar.org']);
3831
$checker = new EmailChecker($adapter);
3932

4033
self::assertFalse($checker->isValid('foo@bar.org'));
4134
}
4235

4336
public function testMalformattedEmail(): void
4437
{
45-
$adapter = $this->createMock(AdapterInterface::class);
38+
$adapter = new ArrayAdapter([]);
4639
$checker = new EmailChecker($adapter);
4740

4841
self::assertFalse($checker->isValid('foo[at]bar.org'));
4942
}
43+
44+
public function testSubDomain(): void
45+
{
46+
$adapter = new ArrayAdapter(['bar.org']);
47+
$checker = new EmailChecker($adapter);
48+
49+
self::assertFalse($checker->isValid('foo@baz.bar.org'));
50+
}
5051
}

0 commit comments

Comments
 (0)