Skip to content

Commit b90bb2b

Browse files
committed
Migrate acmephp/ssl to use typehints
1 parent c5042fa commit b90bb2b

26 files changed

+116
-362
lines changed

Certificate.php

+5-18
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,7 @@ class Certificate
2727
/** @var Certificate */
2828
private $issuerCertificate;
2929

30-
/**
31-
* @param string $certificatePEM
32-
* @param Certificate|null $issuerCertificate
33-
*/
34-
public function __construct($certificatePEM, self $issuerCertificate = null)
30+
public function __construct(string $certificatePEM, self $issuerCertificate = null)
3531
{
3632
Assert::stringNotEmpty($certificatePEM, __CLASS__.'::$certificatePEM should not be an empty string. Got %s');
3733

@@ -42,7 +38,7 @@ public function __construct($certificatePEM, self $issuerCertificate = null)
4238
/**
4339
* @return Certificate[]
4440
*/
45-
public function getIssuerChain()
41+
public function getIssuerChain(): array
4642
{
4743
$chain = [];
4844
$issuerCertificate = $this->getIssuerCertificate();
@@ -55,18 +51,12 @@ public function getIssuerChain()
5551
return $chain;
5652
}
5753

58-
/**
59-
* @return string
60-
*/
61-
public function getPEM()
54+
public function getPEM(): string
6255
{
6356
return $this->certificatePEM;
6457
}
6558

66-
/**
67-
* @return Certificate|null
68-
*/
69-
public function getIssuerCertificate()
59+
public function getIssuerCertificate(): ?Certificate
7060
{
7161
return $this->issuerCertificate;
7262
}
@@ -83,10 +73,7 @@ public function getPublicKeyResource()
8373
return $resource;
8474
}
8575

86-
/**
87-
* @return PublicKey
88-
*/
89-
public function getPublicKey()
76+
public function getPublicKey(): PublicKey
9077
{
9178
return new PublicKey(openssl_pkey_get_details($this->getPublicKeyResource())['key']);
9279
}

CertificateRequest.php

+2-8
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,12 @@ public function __construct(DistinguishedName $distinguishedName, KeyPair $keyPa
3030
$this->keyPair = $keyPair;
3131
}
3232

33-
/**
34-
* @return DistinguishedName
35-
*/
36-
public function getDistinguishedName()
33+
public function getDistinguishedName(): DistinguishedName
3734
{
3835
return $this->distinguishedName;
3936
}
4037

41-
/**
42-
* @return KeyPair
43-
*/
44-
public function getKeyPair()
38+
public function getKeyPair(): KeyPair
4539
{
4640
return $this->keyPair;
4741
}

CertificateResponse.php

+4-12
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,18 @@ class CertificateResponse
2424
/** @var Certificate */
2525
private $certificate;
2626

27-
public function __construct(
28-
CertificateRequest $certificateRequest,
29-
Certificate $certificate
30-
) {
27+
public function __construct(CertificateRequest $certificateRequest, Certificate $certificate)
28+
{
3129
$this->certificateRequest = $certificateRequest;
3230
$this->certificate = $certificate;
3331
}
3432

35-
/**
36-
* @return CertificateRequest
37-
*/
38-
public function getCertificateRequest()
33+
public function getCertificateRequest(): CertificateRequest
3934
{
4035
return $this->certificateRequest;
4136
}
4237

43-
/**
44-
* @return Certificate
45-
*/
46-
public function getCertificate()
38+
public function getCertificate(): Certificate
4739
{
4840
return $this->certificate;
4941
}

DistinguishedName.php

+15-54
Original file line numberDiff line numberDiff line change
@@ -44,32 +44,17 @@ class DistinguishedName
4444
/** @var array */
4545
private $subjectAlternativeNames;
4646

47-
/**
48-
* @param string $commonName
49-
* @param string $countryName
50-
* @param string $stateOrProvinceName
51-
* @param string $localityName
52-
* @param string $organizationName
53-
* @param string $organizationalUnitName
54-
* @param string $emailAddress
55-
*/
5647
public function __construct(
57-
$commonName,
58-
$countryName = null,
59-
$stateOrProvinceName = null,
60-
$localityName = null,
61-
$organizationName = null,
62-
$organizationalUnitName = null,
63-
$emailAddress = null,
48+
string $commonName,
49+
string $countryName = null,
50+
string $stateOrProvinceName = null,
51+
string $localityName = null,
52+
string $organizationName = null,
53+
string $organizationalUnitName = null,
54+
string $emailAddress = null,
6455
array $subjectAlternativeNames = []
6556
) {
6657
Assert::stringNotEmpty($commonName, __CLASS__.'::$commonName expected a non empty string. Got: %s');
67-
Assert::nullOrStringNotEmpty($countryName, __CLASS__.'::$countryName expected a string. Got: %s');
68-
Assert::nullOrStringNotEmpty($stateOrProvinceName, __CLASS__.'::$stateOrProvinceName expected a string. Got: %s');
69-
Assert::nullOrStringNotEmpty($localityName, __CLASS__.'::$localityName expected a string. Got: %s');
70-
Assert::nullOrStringNotEmpty($organizationName, __CLASS__.'::$organizationName expected a string. Got: %s');
71-
Assert::nullOrStringNotEmpty($organizationalUnitName, __CLASS__.'::$organizationalUnitName expected a string. Got: %s');
72-
Assert::nullOrStringNotEmpty($emailAddress, __CLASS__.'::$emailAddress expected a string. Got: %s');
7358
Assert::allStringNotEmpty(
7459
$subjectAlternativeNames,
7560
__CLASS__.'::$subjectAlternativeNames expected an array of non empty string. Got: %s'
@@ -85,66 +70,42 @@ public function __construct(
8570
$this->subjectAlternativeNames = array_diff(array_unique($subjectAlternativeNames), [$commonName]);
8671
}
8772

88-
/**
89-
* @return string
90-
*/
91-
public function getCommonName()
73+
public function getCommonName(): string
9274
{
9375
return $this->commonName;
9476
}
9577

96-
/**
97-
* @return string
98-
*/
99-
public function getCountryName()
78+
public function getCountryName(): ?string
10079
{
10180
return $this->countryName;
10281
}
10382

104-
/**
105-
* @return string
106-
*/
107-
public function getStateOrProvinceName()
83+
public function getStateOrProvinceName(): ?string
10884
{
10985
return $this->stateOrProvinceName;
11086
}
11187

112-
/**
113-
* @return string
114-
*/
115-
public function getLocalityName()
88+
public function getLocalityName(): ?string
11689
{
11790
return $this->localityName;
11891
}
11992

120-
/**
121-
* @return string
122-
*/
123-
public function getOrganizationName()
93+
public function getOrganizationName(): ?string
12494
{
12595
return $this->organizationName;
12696
}
12797

128-
/**
129-
* @return string
130-
*/
131-
public function getOrganizationalUnitName()
98+
public function getOrganizationalUnitName(): ?string
13299
{
133100
return $this->organizationalUnitName;
134101
}
135102

136-
/**
137-
* @return string
138-
*/
139-
public function getEmailAddress()
103+
public function getEmailAddress(): ?string
140104
{
141105
return $this->emailAddress;
142106
}
143107

144-
/**
145-
* @return array
146-
*/
147-
public function getSubjectAlternativeNames()
108+
public function getSubjectAlternativeNames(): array
148109
{
149110
return $this->subjectAlternativeNames;
150111
}

Generator/ChainPrivateKeyGenerator.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace AcmePhp\Ssl\Generator;
1313

14+
use AcmePhp\Ssl\PrivateKey;
15+
1416
/**
1517
* Generate random RSA private key using OpenSSL.
1618
*
@@ -24,12 +26,12 @@ class ChainPrivateKeyGenerator implements PrivateKeyGeneratorInterface
2426
/**
2527
* @param PrivateKeyGeneratorInterface[] $generators
2628
*/
27-
public function __construct($generators)
29+
public function __construct(iterable $generators)
2830
{
2931
$this->generators = $generators;
3032
}
3133

32-
public function generatePrivateKey(KeyOption $keyOption)
34+
public function generatePrivateKey(KeyOption $keyOption): PrivateKey
3335
{
3436
foreach ($this->generators as $generator) {
3537
if ($generator->supportsKeyOption($keyOption)) {
@@ -40,7 +42,7 @@ public function generatePrivateKey(KeyOption $keyOption)
4042
throw new \LogicException(sprintf('Unable to find a generator for a key option of type %s', \get_class($keyOption)));
4143
}
4244

43-
public function supportsKeyOption(KeyOption $keyOption)
45+
public function supportsKeyOption(KeyOption $keyOption): bool
4446
{
4547
foreach ($this->generators as $generator) {
4648
if ($generator->supportsKeyOption($keyOption)) {

Generator/DhKey/DhKeyGenerator.php

+10-14
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use AcmePhp\Ssl\Generator\KeyOption;
1515
use AcmePhp\Ssl\Generator\OpensslPrivateKeyGeneratorTrait;
1616
use AcmePhp\Ssl\Generator\PrivateKeyGeneratorInterface;
17+
use AcmePhp\Ssl\PrivateKey;
1718
use Webmozart\Assert\Assert;
1819

1920
/**
@@ -25,25 +26,20 @@ class DhKeyGenerator implements PrivateKeyGeneratorInterface
2526
{
2627
use OpensslPrivateKeyGeneratorTrait;
2728

28-
/**
29-
* @param DhKeyOption|KeyOption $keyOption
30-
*/
31-
public function generatePrivateKey(KeyOption $keyOption)
29+
public function generatePrivateKey(KeyOption $keyOption): PrivateKey
3230
{
3331
Assert::isInstanceOf($keyOption, DhKeyOption::class);
3432

35-
return $this->generatePrivateKeyFromOpensslOptions(
36-
[
37-
'private_key_type' => OPENSSL_KEYTYPE_DH,
38-
'dh' => [
39-
'p' => $keyOption->getPrime(),
40-
'g' => $keyOption->getGenerator(),
41-
],
42-
]
43-
);
33+
return $this->generatePrivateKeyFromOpensslOptions([
34+
'private_key_type' => OPENSSL_KEYTYPE_DH,
35+
'dh' => [
36+
'p' => $keyOption->getPrime(),
37+
'g' => $keyOption->getGenerator(),
38+
],
39+
]);
4440
}
4541

46-
public function supportsKeyOption(KeyOption $keyOption)
42+
public function supportsKeyOption(KeyOption $keyOption): bool
4743
{
4844
return $keyOption instanceof DhKeyOption;
4945
}

Generator/DhKey/DhKeyOption.php

+4-9
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class DhKeyOption implements KeyOption
1717
{
1818
/** @var string */
1919
private $generator;
20+
2021
/** @var string */
2122
private $prime;
2223

@@ -26,24 +27,18 @@ class DhKeyOption implements KeyOption
2627
*
2728
* @see https://tools.ietf.org/html/rfc3526 how to choose a prime and generator numbers
2829
*/
29-
public function __construct($prime, $generator = '02')
30+
public function __construct(string $prime, string $generator = '02')
3031
{
3132
$this->generator = pack('H*', $generator);
3233
$this->prime = pack('H*', $prime);
3334
}
3435

35-
/**
36-
* @return string
37-
*/
38-
public function getGenerator()
36+
public function getGenerator(): string
3937
{
4038
return $this->generator;
4139
}
4240

43-
/**
44-
* @return string
45-
*/
46-
public function getPrime()
41+
public function getPrime(): string
4742
{
4843
return $this->prime;
4944
}

Generator/DsaKey/DsaKeyGenerator.php

+7-11
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use AcmePhp\Ssl\Generator\KeyOption;
1515
use AcmePhp\Ssl\Generator\OpensslPrivateKeyGeneratorTrait;
1616
use AcmePhp\Ssl\Generator\PrivateKeyGeneratorInterface;
17+
use AcmePhp\Ssl\PrivateKey;
1718
use Webmozart\Assert\Assert;
1819

1920
/**
@@ -25,22 +26,17 @@ class DsaKeyGenerator implements PrivateKeyGeneratorInterface
2526
{
2627
use OpensslPrivateKeyGeneratorTrait;
2728

28-
/**
29-
* @param DsaKeyOption|KeyOption $keyOption
30-
*/
31-
public function generatePrivateKey(KeyOption $keyOption)
29+
public function generatePrivateKey(KeyOption $keyOption): PrivateKey
3230
{
3331
Assert::isInstanceOf($keyOption, DsaKeyOption::class);
3432

35-
return $this->generatePrivateKeyFromOpensslOptions(
36-
[
37-
'private_key_type' => OPENSSL_KEYTYPE_DSA,
38-
'private_key_bits' => $keyOption->getBits(),
39-
]
40-
);
33+
return $this->generatePrivateKeyFromOpensslOptions([
34+
'private_key_type' => OPENSSL_KEYTYPE_DSA,
35+
'private_key_bits' => $keyOption->getBits(),
36+
]);
4137
}
4238

43-
public function supportsKeyOption(KeyOption $keyOption)
39+
public function supportsKeyOption(KeyOption $keyOption): bool
4440
{
4541
return $keyOption instanceof DsaKeyOption;
4642
}

Generator/DsaKey/DsaKeyOption.php

+2-8
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,18 @@
1212
namespace AcmePhp\Ssl\Generator\DsaKey;
1313

1414
use AcmePhp\Ssl\Generator\KeyOption;
15-
use Webmozart\Assert\Assert;
1615

1716
class DsaKeyOption implements KeyOption
1817
{
1918
/** @var int */
2019
private $bits;
2120

22-
public function __construct($bits = 2048)
21+
public function __construct(int $bits = 2048)
2322
{
24-
Assert::integer($bits);
25-
2623
$this->bits = $bits;
2724
}
2825

29-
/**
30-
* @return int
31-
*/
32-
public function getBits()
26+
public function getBits(): int
3327
{
3428
return $this->bits;
3529
}

0 commit comments

Comments
 (0)