Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"phpunit/phpunit": "^9.5",
"symfony/validator": "^5.4 || ^6.4 || ^7.0",
"phpstan/phpstan": "^2.1",
"phpstan/phpstan-phpunit": "^2.0"
"phpstan/phpstan-phpunit": "^2.0",
"phpstan/phpstan-strict-rules": "^2.0"
},
"suggest": {
"symfony/validator": "Add validation constraints",
Expand Down
1 change: 1 addition & 0 deletions phpstan.dist.neon
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
includes:
- vendor/phpstan/phpstan-phpunit/extension.neon
- vendor/phpstan/phpstan-phpunit/rules.neon
- vendor/phpstan/phpstan-strict-rules/rules.neon
- vendor/phpstan/phpstan/conf/bleedingEdge.neon

parameters:
Expand Down
46 changes: 46 additions & 0 deletions src/EmailChecker/Adapter/AggregatorAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/*
* This file is part of the EmailChecker package.
*
* (c) Matthieu Moquet <matthieu@moquet.net>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace EmailChecker\Adapter;

/**
* Combine data from other adapters.
*
* @author Matthieu Moquet <matthieu@moquet.net>
*/
class AggregatorAdapter implements AdapterInterface
{
/**
* @var AdapterInterface[]
*/
protected $adapters;

/**
* Build aggregator adapter with a list of adapters (order matters).
*
* @param AdapterInterface[] $adapters List of AdapterInterface objects
*/
public function __construct(array $adapters)
{
$this->adapters = $adapters;
}

public function isThrowawayDomain($domain)
{
foreach ($this->adapters as $adapter) {
if ($adapter->isThrowawayDomain($domain)) {
return true;
}
}

return false;
}
}
31 changes: 9 additions & 22 deletions src/EmailChecker/Adapter/AgregatorAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,19 @@
* Combine data from other adapters.
*
* @author Matthieu Moquet <matthieu@moquet.net>
*
* @deprecated Use \EmailChecker\Adapter\AggregatorAdapter instead.
*/
class AgregatorAdapter implements AdapterInterface
class AgregatorAdapter extends AggregatorAdapter
{
/**
* @var AdapterInterface[]
*/
protected $adapters;

/**
* Build aggregator adapter with a list of adapters (order matters).
*
* @param AdapterInterface[] $adapters List of AdapterInterface objects
*/
public function __construct(array $adapters)
{
$this->adapters = $adapters;
}

public function isThrowawayDomain($domain)
{
foreach ($this->adapters as $adapter) {
if ($adapter->isThrowawayDomain($domain)) {
return true;
}
}
parent::__construct($adapters);

return false;
trigger_error(\sprintf(
'Since mattketmo/email-checker 2.5.0: Class "%s" is deprecated, use "%s" instead.',
self::class,
AggregatorAdapter::class,
), \E_USER_DEPRECATED);
}
}
2 changes: 1 addition & 1 deletion src/EmailChecker/Adapter/ArrayAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ public function __construct(array $domains)

public function isThrowawayDomain($domain)
{
return in_array($domain, $this->domains);
return in_array($domain, $this->domains, true);
}
}
2 changes: 1 addition & 1 deletion src/EmailChecker/Adapter/BuiltInAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class BuiltInAdapter implements AdapterInterface

public function isThrowawayDomain($domain)
{
return in_array($domain, $this->getDomains());
return in_array($domain, $this->getDomains(), true);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/EmailChecker/Adapter/FileAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ public function __construct($filename)

public function isThrowawayDomain($domain)
{
return in_array($domain, $this->domains);
return in_array($domain, $this->domains, true);
}
}
2 changes: 1 addition & 1 deletion src/EmailChecker/Adapter/GaufretteAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ public function __construct(File $file)

public function isThrowawayDomain($domain)
{
return in_array($domain, $this->domains);
return in_array($domain, $this->domains, true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class NotThrowawayEmailValidator extends ConstraintValidator

public function __construct(?EmailChecker $emailChecker = null)
{
$this->emailChecker = $emailChecker ?: new EmailChecker();
$this->emailChecker = $emailChecker ?? new EmailChecker();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/EmailChecker/EmailChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function isValid($email)
}

try {
list($local, $domain) = Utilities::parseEmailAddress($email);
[$local, $domain] = Utilities::parseEmailAddress($email);
} catch (InvalidEmailException $e) {
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions src/EmailChecker/Utilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static function parseEmailAddress($email)
{
$pattern = sprintf('/^(?<local>%s)@(?<domain>%s)$/iD', self::EMAIL_REGEX_LOCAL, self::EMAIL_REGEX_DOMAIN);

if (!preg_match($pattern, $email, $parts)) {
if (1 !== preg_match($pattern, $email, $parts)) {
throw new InvalidEmailException(sprintf('"%s" is not a valid email', $email));
}

Expand All @@ -60,7 +60,7 @@ public static function parseLines($content)
// Remove empty lines and comments
return array_filter(
$lines,
static fn (string $line): bool => 0 !== strlen($line) && '#' !== $line[0],
static fn (string $line): bool => $line !== '' && '#' !== $line[0],
);
}
}
66 changes: 66 additions & 0 deletions tests/EmailChecker/Tests/Adpater/AggregatorAdapterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

/*
* This file is part of the EmailChecker package.
*
* (c) Matthieu Moquet <matthieu@moquet.net>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace EmailChecker\Tests\Adpater;

use EmailChecker\Adapter\AdapterInterface;
use EmailChecker\Adapter\AggregatorAdapter;
use EmailChecker\Tests\TestCase;

final class AggregatorAdapterTest extends TestCase
{
public function testAllValid(): void
{
$adapter1 = $this->getAdapterMock(false);
$adapter2 = $this->getAdapterMock(false);

$adapter = new AggregatorAdapter([$adapter1, $adapter2]);

self::assertFalse($adapter->isThrowawayDomain('example.org'));
}

public function testFirstInvalid(): void
{
$adapter1 = $this->getAdapterMock(true);
$adapter2 = $this->getAdapterMock(false);

$adapter = new AggregatorAdapter([$adapter1, $adapter2]);

self::assertTrue($adapter->isThrowawayDomain('example.org'));
}

public function testSecondInvalid(): void
{
$adapter1 = $this->getAdapterMock(false);
$adapter2 = $this->getAdapterMock(true);

$adapter = new AggregatorAdapter([$adapter1, $adapter2]);

self::assertTrue($adapter->isThrowawayDomain('example.org'));
}

/**
* Build a mock of adapter interface.
*
* @param bool $isThrowawayDomain The value returned by the isThrowawayDomain method
*
* @return AdapterInterface The mock adapter
*/
private function getAdapterMock(bool $isThrowawayDomain): AdapterInterface
{
$adapter = $this->createStub(AdapterInterface::class);
$adapter
->method('isThrowawayDomain')
->willReturn($isThrowawayDomain);

return $adapter;
}
}
66 changes: 0 additions & 66 deletions tests/EmailChecker/Tests/Adpater/AgregatorAdapterTest.php

This file was deleted.

4 changes: 2 additions & 2 deletions tests/EmailChecker/Tests/Adpater/ArrayAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ protected function setUp(): void
*/
public function testThrowawayDomains(string $domain): void
{
$this->assertTrue($this->adapter->isThrowawayDomain($domain));
self::assertTrue($this->adapter->isThrowawayDomain($domain));
}

/**
* @dataProvider notThrowawayDomains
*/
public function testNotThrowawayDomains(string $domain): void
{
$this->assertFalse($this->adapter->isThrowawayDomain($domain));
self::assertFalse($this->adapter->isThrowawayDomain($domain));
}

public static function throwawayDomains(): iterable
Expand Down
2 changes: 1 addition & 1 deletion tests/EmailChecker/Tests/Adpater/BuiltInAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected function setUp(): void
*/
public function testThrowawayDomains(string $domain): void
{
$this->assertTrue($this->adapter->isThrowawayDomain($domain));
self::assertTrue($this->adapter->isThrowawayDomain($domain));
}

public static function throwawayDomains(): iterable
Expand Down
4 changes: 2 additions & 2 deletions tests/EmailChecker/Tests/Adpater/FileAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ protected function setUp(): void
*/
public function testThrowawayDomains(string $domain): void
{
$this->assertTrue($this->adapter->isThrowawayDomain($domain));
self::assertTrue($this->adapter->isThrowawayDomain($domain));
}

/**
* @dataProvider notThrowawayDomains
*/
public function testNotThrowawayDomains(string $domain): void
{
$this->assertFalse($this->adapter->isThrowawayDomain($domain));
self::assertFalse($this->adapter->isThrowawayDomain($domain));
}

public static function throwawayDomains(): iterable
Expand Down
4 changes: 2 additions & 2 deletions tests/EmailChecker/Tests/Adpater/GaufretteAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ protected function setUp(): void
*/
public function testThrowawayDomains(string $domain): void
{
$this->assertTrue($this->adapter->isThrowawayDomain($domain));
self::assertTrue($this->adapter->isThrowawayDomain($domain));
}

/**
* @dataProvider notThrowawayDomains
*/
public function testNotThrowawayDomains(string $domain): void
{
$this->assertFalse($this->adapter->isThrowawayDomain($domain));
self::assertFalse($this->adapter->isThrowawayDomain($domain));
}

public static function throwawayDomains(): iterable
Expand Down
Loading