diff --git a/composer.json b/composer.json index 186e544..b19ca28 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/phpstan.dist.neon b/phpstan.dist.neon index 30a7c73..4590300 100644 --- a/phpstan.dist.neon +++ b/phpstan.dist.neon @@ -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: diff --git a/src/EmailChecker/Adapter/AggregatorAdapter.php b/src/EmailChecker/Adapter/AggregatorAdapter.php new file mode 100644 index 0000000..f54372b --- /dev/null +++ b/src/EmailChecker/Adapter/AggregatorAdapter.php @@ -0,0 +1,46 @@ + + * + * 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 + */ +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; + } +} diff --git a/src/EmailChecker/Adapter/AgregatorAdapter.php b/src/EmailChecker/Adapter/AgregatorAdapter.php index 0b893e8..aadd2c2 100644 --- a/src/EmailChecker/Adapter/AgregatorAdapter.php +++ b/src/EmailChecker/Adapter/AgregatorAdapter.php @@ -15,32 +15,19 @@ * Combine data from other adapters. * * @author Matthieu Moquet + * + * @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); } } diff --git a/src/EmailChecker/Adapter/ArrayAdapter.php b/src/EmailChecker/Adapter/ArrayAdapter.php index 56f7513..487d31c 100644 --- a/src/EmailChecker/Adapter/ArrayAdapter.php +++ b/src/EmailChecker/Adapter/ArrayAdapter.php @@ -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); } } diff --git a/src/EmailChecker/Adapter/BuiltInAdapter.php b/src/EmailChecker/Adapter/BuiltInAdapter.php index 0a598cd..66a0984 100644 --- a/src/EmailChecker/Adapter/BuiltInAdapter.php +++ b/src/EmailChecker/Adapter/BuiltInAdapter.php @@ -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); } /** diff --git a/src/EmailChecker/Adapter/FileAdapter.php b/src/EmailChecker/Adapter/FileAdapter.php index f1bbeac..922ad9c 100644 --- a/src/EmailChecker/Adapter/FileAdapter.php +++ b/src/EmailChecker/Adapter/FileAdapter.php @@ -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); } } diff --git a/src/EmailChecker/Adapter/GaufretteAdapter.php b/src/EmailChecker/Adapter/GaufretteAdapter.php index a7a4d21..cc1b330 100644 --- a/src/EmailChecker/Adapter/GaufretteAdapter.php +++ b/src/EmailChecker/Adapter/GaufretteAdapter.php @@ -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); } } diff --git a/src/EmailChecker/Constraints/NotThrowawayEmailValidator.php b/src/EmailChecker/Constraints/NotThrowawayEmailValidator.php index 50a7f2e..1ea2588 100644 --- a/src/EmailChecker/Constraints/NotThrowawayEmailValidator.php +++ b/src/EmailChecker/Constraints/NotThrowawayEmailValidator.php @@ -28,7 +28,7 @@ class NotThrowawayEmailValidator extends ConstraintValidator public function __construct(?EmailChecker $emailChecker = null) { - $this->emailChecker = $emailChecker ?: new EmailChecker(); + $this->emailChecker = $emailChecker ?? new EmailChecker(); } /** diff --git a/src/EmailChecker/EmailChecker.php b/src/EmailChecker/EmailChecker.php index 3acc4df..14e2ba3 100644 --- a/src/EmailChecker/EmailChecker.php +++ b/src/EmailChecker/EmailChecker.php @@ -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; } diff --git a/src/EmailChecker/Utilities.php b/src/EmailChecker/Utilities.php index 60d65a4..2eec987 100644 --- a/src/EmailChecker/Utilities.php +++ b/src/EmailChecker/Utilities.php @@ -34,7 +34,7 @@ public static function parseEmailAddress($email) { $pattern = sprintf('/^(?%s)@(?%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)); } @@ -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], ); } } diff --git a/tests/EmailChecker/Tests/Adpater/AggregatorAdapterTest.php b/tests/EmailChecker/Tests/Adpater/AggregatorAdapterTest.php new file mode 100644 index 0000000..950bdac --- /dev/null +++ b/tests/EmailChecker/Tests/Adpater/AggregatorAdapterTest.php @@ -0,0 +1,66 @@ + + * + * 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; + } +} diff --git a/tests/EmailChecker/Tests/Adpater/AgregatorAdapterTest.php b/tests/EmailChecker/Tests/Adpater/AgregatorAdapterTest.php deleted file mode 100644 index 60bdda1..0000000 --- a/tests/EmailChecker/Tests/Adpater/AgregatorAdapterTest.php +++ /dev/null @@ -1,66 +0,0 @@ - - * - * 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\AgregatorAdapter; -use EmailChecker\Tests\TestCase; - -final class AgregatorAdapterTest extends TestCase -{ - public function testAllValid(): void - { - $adapter1 = $this->getAdapterMock(false, 'once'); - $adapter2 = $this->getAdapterMock(false, 'once'); - - $adapter = new AgregatorAdapter([$adapter1, $adapter2]); - - $this->assertFalse($adapter->isThrowawayDomain('example.org')); - } - - public function testFirstInvalid(): void - { - $adapter1 = $this->getAdapterMock(true, 'once'); - $adapter2 = $this->getAdapterMock(false, 'never'); - - $adapter = new AgregatorAdapter([$adapter1, $adapter2]); - - $this->assertTrue($adapter->isThrowawayDomain('example.org')); - } - - public function testSecondInvalid(): void - { - $adapter1 = $this->getAdapterMock(false, 'once'); - $adapter2 = $this->getAdapterMock(true, 'once'); - - $adapter = new AgregatorAdapter([$adapter1, $adapter2]); - - $this->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 - */ - protected function getAdapterMock(bool $isThrowawayDomain, string $call = 'any'): AdapterInterface - { - $adapter = $this->createMock(AdapterInterface::class); - $adapter->expects($this->$call()) - ->method('isThrowawayDomain') - ->will($this->returnValue($isThrowawayDomain)); - - return $adapter; - } -} diff --git a/tests/EmailChecker/Tests/Adpater/ArrayAdapterTest.php b/tests/EmailChecker/Tests/Adpater/ArrayAdapterTest.php index 52908a3..6f3a867 100644 --- a/tests/EmailChecker/Tests/Adpater/ArrayAdapterTest.php +++ b/tests/EmailChecker/Tests/Adpater/ArrayAdapterTest.php @@ -35,7 +35,7 @@ protected function setUp(): void */ public function testThrowawayDomains(string $domain): void { - $this->assertTrue($this->adapter->isThrowawayDomain($domain)); + self::assertTrue($this->adapter->isThrowawayDomain($domain)); } /** @@ -43,7 +43,7 @@ public function testThrowawayDomains(string $domain): void */ public function testNotThrowawayDomains(string $domain): void { - $this->assertFalse($this->adapter->isThrowawayDomain($domain)); + self::assertFalse($this->adapter->isThrowawayDomain($domain)); } public static function throwawayDomains(): iterable diff --git a/tests/EmailChecker/Tests/Adpater/BuiltInAdapterTest.php b/tests/EmailChecker/Tests/Adpater/BuiltInAdapterTest.php index 6b9152c..e91e977 100644 --- a/tests/EmailChecker/Tests/Adpater/BuiltInAdapterTest.php +++ b/tests/EmailChecker/Tests/Adpater/BuiltInAdapterTest.php @@ -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 diff --git a/tests/EmailChecker/Tests/Adpater/FileAdapterTest.php b/tests/EmailChecker/Tests/Adpater/FileAdapterTest.php index fa683b3..c2edc75 100644 --- a/tests/EmailChecker/Tests/Adpater/FileAdapterTest.php +++ b/tests/EmailChecker/Tests/Adpater/FileAdapterTest.php @@ -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)); } /** @@ -38,7 +38,7 @@ public function testThrowawayDomains(string $domain): void */ public function testNotThrowawayDomains(string $domain): void { - $this->assertFalse($this->adapter->isThrowawayDomain($domain)); + self::assertFalse($this->adapter->isThrowawayDomain($domain)); } public static function throwawayDomains(): iterable diff --git a/tests/EmailChecker/Tests/Adpater/GaufretteAdapterTest.php b/tests/EmailChecker/Tests/Adpater/GaufretteAdapterTest.php index 13b30eb..40800f8 100644 --- a/tests/EmailChecker/Tests/Adpater/GaufretteAdapterTest.php +++ b/tests/EmailChecker/Tests/Adpater/GaufretteAdapterTest.php @@ -36,7 +36,7 @@ protected function setUp(): void */ public function testThrowawayDomains(string $domain): void { - $this->assertTrue($this->adapter->isThrowawayDomain($domain)); + self::assertTrue($this->adapter->isThrowawayDomain($domain)); } /** @@ -44,7 +44,7 @@ public function testThrowawayDomains(string $domain): void */ public function testNotThrowawayDomains(string $domain): void { - $this->assertFalse($this->adapter->isThrowawayDomain($domain)); + self::assertFalse($this->adapter->isThrowawayDomain($domain)); } public static function throwawayDomains(): iterable diff --git a/tests/EmailChecker/Tests/Constraint/NotThrowawayEmailValidatorTest.php b/tests/EmailChecker/Tests/Constraint/NotThrowawayEmailValidatorTest.php index 7c33250..ad9f962 100644 --- a/tests/EmailChecker/Tests/Constraint/NotThrowawayEmailValidatorTest.php +++ b/tests/EmailChecker/Tests/Constraint/NotThrowawayEmailValidatorTest.php @@ -35,7 +35,7 @@ protected function setUp(): void public function testNullIsValid(): void { - $this->context->expects($this->never()) + $this->context->expects(self::never()) ->method('addViolation'); $this->validator->validate(null, new NotThrowawayEmail()); @@ -43,7 +43,7 @@ public function testNullIsValid(): void public function testEmptyStringIsValid(): void { - $this->context->expects($this->never()) + $this->context->expects(self::never()) ->method('addViolation'); $this->validator->validate('', new NotThrowawayEmail()); @@ -61,7 +61,7 @@ public function testExpectsStringCompatibleType(): void */ public function testValidEmails(string $email): void { - $this->context->expects($this->never()) + $this->context->expects(self::never()) ->method('addViolation'); $this->validator->validate($email, new NotThrowawayEmail()); @@ -84,7 +84,7 @@ public function testInvalidEmails(string $email): void 'message' => 'myMessage', ]); - $this->context->expects($this->once()) + $this->context->expects(self::once()) ->method('addViolation') ->with('myMessage'); diff --git a/tests/EmailChecker/Tests/EmailCheckerTest.php b/tests/EmailChecker/Tests/EmailCheckerTest.php index f9b3ab1..78ce8e6 100644 --- a/tests/EmailChecker/Tests/EmailCheckerTest.php +++ b/tests/EmailChecker/Tests/EmailCheckerTest.php @@ -25,7 +25,7 @@ public function testEmailIsValid(): void $checker = new EmailChecker($adapter); - $this->assertTrue($checker->isValid('foo@bar.org')); + self::assertTrue($checker->isValid('foo@bar.org')); } public function testEmailIsNotValid(): void @@ -37,7 +37,7 @@ public function testEmailIsNotValid(): void $checker = new EmailChecker($adapter); - $this->assertFalse($checker->isValid('foo@bar.org')); + self::assertFalse($checker->isValid('foo@bar.org')); } public function testMalformattedEmail(): void @@ -45,6 +45,6 @@ public function testMalformattedEmail(): void $adapter = $this->createMock(AdapterInterface::class); $checker = new EmailChecker($adapter); - $this->assertFalse($checker->isValid('foo[at]bar.org')); + self::assertFalse($checker->isValid('foo[at]bar.org')); } } diff --git a/tests/EmailChecker/Tests/ThrowawayDomainsTest.php b/tests/EmailChecker/Tests/ThrowawayDomainsTest.php index 12ee8cc..12ec810 100644 --- a/tests/EmailChecker/Tests/ThrowawayDomainsTest.php +++ b/tests/EmailChecker/Tests/ThrowawayDomainsTest.php @@ -19,6 +19,6 @@ public function testIsInstanciable(): void { $domains = new ThrowawayDomains(); - $this->assertGreaterThan(0, count($domains)); + self::assertGreaterThan(0, count($domains)); } } diff --git a/tests/EmailChecker/Tests/UtilitiesTest.php b/tests/EmailChecker/Tests/UtilitiesTest.php index 331d5be..cf98b8b 100644 --- a/tests/EmailChecker/Tests/UtilitiesTest.php +++ b/tests/EmailChecker/Tests/UtilitiesTest.php @@ -21,9 +21,9 @@ final class UtilitiesTest extends TestCase */ public function testParseValidEmail(string $email, string $exceptedLocal, string $exceptedDomain): void { - list($local, $domain) = Utilities::parseEmailAddress($email); + [$local, $domain] = Utilities::parseEmailAddress($email); - $this->assertEquals([$exceptedLocal, $exceptedDomain], [$local, $domain]); + self::assertEquals([$exceptedLocal, $exceptedDomain], [$local, $domain]); } /** @@ -33,7 +33,7 @@ public function testParseInvalidEmail(string $email): void { try { Utilities::parseEmailAddress($email); - $this->fail(sprintf('"%s" should not be a valid email', $email)); + self::fail(sprintf('"%s" should not be a valid email', $email)); } catch (InvalidEmailException $e) { return; } @@ -65,7 +65,7 @@ public function testParseLines(): void Actual: %s MSG; - $this->assertEmpty($diffs, sprintf($message, print_r($expectedLines, true), print_r($parsedContent, true))); + self::assertEmpty($diffs, sprintf($message, print_r($expectedLines, true), print_r($parsedContent, true))); } public static function validEmails(): iterable