From 6ec68014d5874b32b160f3fb4333b7114a35fc26 Mon Sep 17 00:00:00 2001 From: Freek Van der Herten Date: Mon, 22 Jul 2019 16:44:43 -0400 Subject: [PATCH] Clean up (#104) * cleanup * micro optimization * add typehint * improve readability * wip * wip * wip * wip * wip * Apply fixes from StyleCI (#103) --- .travis.yml | 4 +-- composer.json | 7 ++--- src/Downloader.php | 30 ---------------------- src/SslCertificate.php | 17 +++++++----- src/Url.php | 8 ++---- src/helpers.php | 8 +++--- tests/HelpersTest.php | 50 +++++++++++++++++++----------------- tests/SslCertificateTest.php | 48 ++++++++++++++++++---------------- 8 files changed, 75 insertions(+), 97 deletions(-) diff --git a/.travis.yml b/.travis.yml index d4da099..e83227c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,11 +1,9 @@ language: php php: - - 7.0 - - 7.1 - 7.2 - 7.3 - + - env: matrix: - COMPOSER_FLAGS="--prefer-lowest" diff --git a/composer.json b/composer.json index 3779231..9211a4a 100644 --- a/composer.json +++ b/composer.json @@ -16,14 +16,15 @@ } ], "require": { - "php": "^7.0", + "php": "^7.2", "ext-intl" : "*", + "ext-json": "*", "nesbot/carbon": "^1.15|^2.0", "spatie/macroable": "^1.0" }, "require-dev": { - "phpunit/phpunit": "^6.4", - "spatie/phpunit-snapshot-assertions": "^1.0" + "phpunit/phpunit": "^8.0", + "spatie/phpunit-snapshot-assertions": "^2.0" }, "autoload": { "psr-4": { diff --git a/src/Downloader.php b/src/Downloader.php index d3a49a6..03e954f 100644 --- a/src/Downloader.php +++ b/src/Downloader.php @@ -25,11 +25,6 @@ class Downloader /** @var bool */ protected $verifyPeerName = true; - /** - * @param int $port - * - * @return $this - */ public function usingPort(int $port) { $this->port = $port; @@ -37,11 +32,6 @@ public function usingPort(int $port) return $this; } - /** - * @param bool $sni - * - * @return $this - */ public function usingSni(bool $sni) { $this->enableSni = $sni; @@ -49,11 +39,6 @@ public function usingSni(bool $sni) return $this; } - /** - * @param bool $fullChain - * - * @return $this - */ public function withFullChain(bool $fullChain) { $this->capturePeerChain = $fullChain; @@ -61,11 +46,6 @@ public function withFullChain(bool $fullChain) return $this; } - /** - * @param bool $verifyPeer - * - * @return $this - */ public function withVerifyPeer(bool $verifyPeer) { $this->verifyPeer = $verifyPeer; @@ -73,11 +53,6 @@ public function withVerifyPeer(bool $verifyPeer) return $this; } - /** - * @param bool $verifyPeerName - * - * @return $this - */ public function withVerifyPeerName(bool $verifyPeerName) { $this->verifyPeerName = $verifyPeerName; @@ -85,11 +60,6 @@ public function withVerifyPeerName(bool $verifyPeerName) return $this; } - /** - * @param int $timeOutInSeconds - * - * @return $this - */ public function setTimeout(int $timeOutInSeconds) { $this->timeout = $timeOutInSeconds; diff --git a/src/SslCertificate.php b/src/SslCertificate.php index 33d4126..95c6099 100644 --- a/src/SslCertificate.php +++ b/src/SslCertificate.php @@ -25,9 +25,7 @@ public static function download(): Downloader public static function createForHostName(string $url, int $timeout = 30): self { - $sslCertificate = Downloader::downloadCertificateFromUrl($url, $timeout); - - return $sslCertificate; + return Downloader::downloadCertificateFromUrl($url, $timeout); } public function __construct( @@ -248,9 +246,16 @@ public function containsDomain(string $domain): bool return false; } - public function isPreCertificate() + public function isPreCertificate(): bool { - return array_key_exists('extensions', $this->rawCertificateFields) - && array_key_exists('ct_precert_poison', $this->rawCertificateFields['extensions']); + if (! array_key_exists('extensions', $this->rawCertificateFields)) { + return false; + } + + if (! array_key_exists('ct_precert_poison', $this->rawCertificateFields['extensions'])) { + return false; + } + + return true; } } diff --git a/src/Url.php b/src/Url.php index 8b0ae38..a3e585d 100644 --- a/src/Url.php +++ b/src/Url.php @@ -18,12 +18,8 @@ public function __construct(string $url) $url = "https://{$url}"; } - if (strlen($url) < 61 && function_exists('idn_to_ascii')) { - if (defined('INTL_IDNA_VARIANT_UTS46')) { - $url = idn_to_ascii($url, false, INTL_IDNA_VARIANT_UTS46); - } else { - $url = idn_to_ascii($url); - } + if (function_exists('idn_to_ascii') && strlen($url) < 61) { + $url = idn_to_ascii($url, false, INTL_IDNA_VARIANT_UTS46); } if (! filter_var($url, FILTER_VALIDATE_URL)) { diff --git a/src/helpers.php b/src/helpers.php index cb0d74f..7021bb2 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -16,7 +16,7 @@ function starts_with($haystack, $needles): bool /** * Determine if a given string ends with a given substring. * - * @param string $haystack + * @param string $haystack * @param string|array $needles * * @return bool @@ -35,8 +35,8 @@ function ends_with(string $haystack, $needles): bool /** * Returns the portion of string specified by the start and length parameters. * - * @param string $string - * @param int $start + * @param string $string + * @param int $start * @param int|null $length * * @return string @@ -61,7 +61,7 @@ function length(string $value): int /** * Determine if a given string contains a given substring. * - * @param string $haystack + * @param string $haystack * @param string|array $needles * * @return bool diff --git a/tests/HelpersTest.php b/tests/HelpersTest.php index 5020a9e..7c25431 100644 --- a/tests/HelpersTest.php +++ b/tests/HelpersTest.php @@ -3,32 +3,36 @@ namespace Spatie\SslCertificate\Test; use PHPUnit\Framework\TestCase; +use function Spatie\SslCertificate\length; +use function Spatie\SslCertificate\ends_with; +use function Spatie\SslCertificate\starts_with; +use function Spatie\SslCertificate\str_contains; class HelpersTest extends TestCase { /** @test */ public function it_can_determine_if_a_string_starts_with_a_given_string() { - $this->assertTrue(\Spatie\SslCertificate\starts_with('jason', 'jas')); - $this->assertTrue(\Spatie\SslCertificate\starts_with('jason', 'jason')); - $this->assertTrue(\Spatie\SslCertificate\starts_with('jason', ['jas'])); - $this->assertTrue(\Spatie\SslCertificate\starts_with('jason', ['day', 'jas'])); - $this->assertFalse(\Spatie\SslCertificate\starts_with('jason', 'day')); - $this->assertFalse(\Spatie\SslCertificate\starts_with('jason', ['day'])); - $this->assertFalse(\Spatie\SslCertificate\starts_with('jason', '')); + $this->assertTrue(starts_with('jason', 'jas')); + $this->assertTrue(starts_with('jason', 'jason')); + $this->assertTrue(starts_with('jason', ['jas'])); + $this->assertTrue(starts_with('jason', ['day', 'jas'])); + $this->assertFalse(starts_with('jason', 'day')); + $this->assertFalse(starts_with('jason', ['day'])); + $this->assertFalse(starts_with('jason', '')); } /** @test */ public function it_can_determine_if_a_string_end_with_a_given_string() { - $this->assertTrue(\Spatie\SslCertificate\ends_with('jason', 'on')); - $this->assertTrue(\Spatie\SslCertificate\ends_with('jason', 'jason')); - $this->assertTrue(\Spatie\SslCertificate\ends_with('jason', ['on'])); - $this->assertTrue(\Spatie\SslCertificate\ends_with('jason', ['no', 'on'])); - $this->assertFalse(\Spatie\SslCertificate\ends_with('jason', 'no')); - $this->assertFalse(\Spatie\SslCertificate\ends_with('jason', ['no'])); - $this->assertFalse(\Spatie\SslCertificate\ends_with('jason', '')); - $this->assertFalse(\Spatie\SslCertificate\ends_with('7', ' 7')); + $this->assertTrue(ends_with('jason', 'on')); + $this->assertTrue(ends_with('jason', 'jason')); + $this->assertTrue(ends_with('jason', ['on'])); + $this->assertTrue(ends_with('jason', ['no', 'on'])); + $this->assertFalse(ends_with('jason', 'no')); + $this->assertFalse(ends_with('jason', ['no'])); + $this->assertFalse(ends_with('jason', '')); + $this->assertFalse(ends_with('7', ' 7')); } /** @test */ @@ -50,18 +54,18 @@ public function it_can_create_substring_of_a_given_stirng() /** @test */ public function it_can_determine_the_lenght_of_a_string() { - $this->assertEquals(11, \Spatie\SslCertificate\length('foo bar baz')); + $this->assertEquals(11, length('foo bar baz')); } /** @test */ public function it_can_determine_if_a_string_str_contains_another_string() { - $this->assertTrue(\Spatie\SslCertificate\str_contains('taylor', 'ylo')); - $this->assertTrue(\Spatie\SslCertificate\str_contains('taylor', 'taylor')); - $this->assertTrue(\Spatie\SslCertificate\str_contains('taylor', ['ylo'])); - $this->assertTrue(\Spatie\SslCertificate\str_contains('taylor', ['xxx', 'ylo'])); - $this->assertFalse(\Spatie\SslCertificate\str_contains('taylor', 'xxx')); - $this->assertFalse(\Spatie\SslCertificate\str_contains('taylor', ['xxx'])); - $this->assertFalse(\Spatie\SslCertificate\str_contains('taylor', '')); + $this->assertTrue(str_contains('taylor', 'ylo')); + $this->assertTrue(str_contains('taylor', 'taylor')); + $this->assertTrue(str_contains('taylor', ['ylo'])); + $this->assertTrue(str_contains('taylor', ['xxx', 'ylo'])); + $this->assertFalse(str_contains('taylor', 'xxx')); + $this->assertFalse(str_contains('taylor', ['xxx'])); + $this->assertFalse(str_contains('taylor', '')); } } diff --git a/tests/SslCertificateTest.php b/tests/SslCertificateTest.php index 78e1986..39853d6 100644 --- a/tests/SslCertificateTest.php +++ b/tests/SslCertificateTest.php @@ -15,7 +15,7 @@ class SslCertificateTest extends TestCase /** @var SslCertificate */ protected $certificate; - public function setUp() + public function setUp(): void { parent::setUp(); @@ -138,29 +138,17 @@ public function it_can_determine_if_the_certificate_is_valid_until_a_date() public function it_can_determine_if_the_certificate_is_valid_for_a_certain_domain() { $this->assertTrue($this->certificate->isValid('spatie.be')); - $this->assertTrue($this->certificate->isValid('www.spatie.be')); - $this->assertFalse($this->certificate->isValid('another.spatie.be')); - $this->assertFalse($this->certificate->isValid('www.another.spatie.be')); - $this->assertFalse($this->certificate->isValid('another.www.another.spatie.be')); - $this->assertTrue($this->certificate->isValid('otherdomain.com')); - $this->assertTrue($this->certificate->isValid('www.otherdomain.com')); - $this->assertTrue($this->certificate->isValid('another.otherdomain.com')); - $this->assertFalse($this->certificate->isValid('www.another.otherdomain.com')); - $this->assertFalse($this->certificate->isValid('another.www.another.otherdomain.com')); - $this->assertFalse($this->certificate->isValid('facebook.com')); - $this->assertFalse($this->certificate->isValid('spatie.be.facebook.com')); - $this->assertFalse($this->certificate->isValid('www.spatie.be.facebook.com')); } @@ -192,7 +180,10 @@ public function it_can_convert_the_certificate_to_json() /** @test */ public function it_can_convert_the_certificate_to_a_string() { - $this->assertEquals($this->certificate->getRawCertificateFieldsJson(), (string) $this->certificate); + $this->assertEquals( + $this->certificate->getRawCertificateFieldsJson(), + (string) $this->certificate + ); } /** @test */ @@ -259,7 +250,10 @@ public function it_can_be_encoded_as_json() /** @test */ public function does_not_notify_on_wrong_domains() { - $rawCertificateFields = json_decode(file_get_contents(__DIR__.'/stubs/certificateWithRandomWildcardDomains.json'), true); + $rawCertificateFields = json_decode( + file_get_contents(__DIR__.'/stubs/certificateWithRandomWildcardDomains.json'), + true + ); $this->certificate = new SslCertificate($rawCertificateFields); @@ -269,7 +263,10 @@ public function does_not_notify_on_wrong_domains() /** @test */ public function it_correctly_compares_uppercase_domain_names() { - $rawCertificateFields = json_decode(file_get_contents(__DIR__.'/stubs/certificateWithUppercaseDomains.json'), true); + $rawCertificateFields = json_decode( + file_get_contents(__DIR__.'/stubs/certificateWithUppercaseDomains.json'), + true + ); $this->certificate = new SslCertificate($rawCertificateFields); @@ -280,13 +277,20 @@ public function it_correctly_compares_uppercase_domain_names() /** @test */ public function it_correctly_identifies_pre_certificates() { - $rawCertificateFieldsNormalCertificate = json_decode(file_get_contents(__DIR__.'/stubs/spatieCertificateFields.json'), true); - $rawCertificateFieldsPreCertificate = json_decode(file_get_contents(__DIR__.'/stubs/preCertificate.json'), true); + $rawCertificateFieldsNormalCertificate = json_decode( + file_get_contents(__DIR__.'/stubs/spatieCertificateFields.json'), + true + ); + + $rawCertificateFieldsPreCertificate = json_decode( + file_get_contents(__DIR__.'/stubs/preCertificate.json'), + true + ); - $this->certificateNormal = new SslCertificate($rawCertificateFieldsNormalCertificate); - $this->certificatePreCertificate = new SslCertificate($rawCertificateFieldsPreCertificate); + $certificateNormal = new SslCertificate($rawCertificateFieldsNormalCertificate); + $certificatePreCertificate = new SslCertificate($rawCertificateFieldsPreCertificate); - $this->assertFalse($this->certificateNormal->isPreCertificate()); - $this->assertTrue($this->certificatePreCertificate->isPreCertificate()); + $this->assertFalse($certificateNormal->isPreCertificate()); + $this->assertTrue($certificatePreCertificate->isPreCertificate()); } }