Skip to content

Commit

Permalink
Clean up (#104)
Browse files Browse the repository at this point in the history
* cleanup

* micro optimization

* add typehint

* improve readability

* wip

* wip

* wip

* wip

* wip

* Apply fixes from StyleCI (#103)
  • Loading branch information
freekmurze authored Jul 22, 2019
1 parent a5ddbe9 commit 6ec6801
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 97 deletions.
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
language: php

php:
- 7.0
- 7.1
- 7.2
- 7.3

-
env:
matrix:
- COMPOSER_FLAGS="--prefer-lowest"
Expand Down
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
30 changes: 0 additions & 30 deletions src/Downloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,71 +25,41 @@ class Downloader
/** @var bool */
protected $verifyPeerName = true;

/**
* @param int $port
*
* @return $this
*/
public function usingPort(int $port)
{
$this->port = $port;

return $this;
}

/**
* @param bool $sni
*
* @return $this
*/
public function usingSni(bool $sni)
{
$this->enableSni = $sni;

return $this;
}

/**
* @param bool $fullChain
*
* @return $this
*/
public function withFullChain(bool $fullChain)
{
$this->capturePeerChain = $fullChain;

return $this;
}

/**
* @param bool $verifyPeer
*
* @return $this
*/
public function withVerifyPeer(bool $verifyPeer)
{
$this->verifyPeer = $verifyPeer;

return $this;
}

/**
* @param bool $verifyPeerName
*
* @return $this
*/
public function withVerifyPeerName(bool $verifyPeerName)
{
$this->verifyPeerName = $verifyPeerName;

return $this;
}

/**
* @param int $timeOutInSeconds
*
* @return $this
*/
public function setTimeout(int $timeOutInSeconds)
{
$this->timeout = $timeOutInSeconds;
Expand Down
17 changes: 11 additions & 6 deletions src/SslCertificate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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;
}
}
8 changes: 2 additions & 6 deletions src/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
8 changes: 4 additions & 4 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
50 changes: 27 additions & 23 deletions tests/HelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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', ''));
}
}
48 changes: 26 additions & 22 deletions tests/SslCertificateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class SslCertificateTest extends TestCase
/** @var SslCertificate */
protected $certificate;

public function setUp()
public function setUp(): void
{
parent::setUp();

Expand Down Expand Up @@ -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'));
}

Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand All @@ -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());
}
}

0 comments on commit 6ec6801

Please sign in to comment.