Skip to content

Commit d426be3

Browse files
committed
Bump requirements to PHP 8.0 & follow PSR-12 coding style
1 parent 0753735 commit d426be3

File tree

10 files changed

+171
-127
lines changed

10 files changed

+171
-127
lines changed

.scrutinizer.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ build:
1515
file: build/logs/clover.xml
1616
environment:
1717
php:
18-
version: 7.2
18+
version: 8.0
1919
filter:
2020
excluded_paths:
2121
- test/

.travis.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
language: php
22
php:
3-
- "7.2"
4-
- "7.3"
3+
- "8.0"
4+
- "8.1"
55
before_script:
66
- "composer install"
77
- "composer require php-coveralls/php-coveralls"

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ textual encodings of cryptographic structures _(PEM)_.
1010

1111
## Requirements
1212

13-
- PHP >=7.2
13+
- PHP >=8.0
1414

1515
## Installation
1616

composer.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818
}
1919
],
2020
"require": {
21-
"php": ">=7.2"
21+
"php": "^8.0"
2222
},
2323
"require-dev": {
24-
"phpunit/phpunit": "^8.1"
24+
"phpunit/phpunit": "^9.5"
2525
},
2626
"autoload": {
2727
"psr-4": {
2828
"Sop\\CryptoEncoding\\": "lib/CryptoEncoding/"
2929
}
3030
}
31-
}
31+
}

lib/CryptoEncoding/PEM.php

+43-29
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
<?php
22

3-
declare(strict_types = 1);
3+
declare(strict_types=1);
44

55
namespace Sop\CryptoEncoding;
66

7+
use RuntimeException;
8+
use UnexpectedValueException;
9+
10+
use function base_decode;
11+
use function base_encode;
12+
use function chunk_split;
13+
use function file_get_contents;
14+
use function is_readable;
15+
use function preg_match;
16+
use function trim;
17+
718
/**
819
* Implements PEM file encoding and decoding.
920
*
@@ -12,44 +23,45 @@
1223
class PEM
1324
{
1425
// well-known PEM types
15-
const TYPE_CERTIFICATE = 'CERTIFICATE';
16-
const TYPE_CRL = 'X509 CRL';
17-
const TYPE_CERTIFICATE_REQUEST = 'CERTIFICATE REQUEST';
18-
const TYPE_ATTRIBUTE_CERTIFICATE = 'ATTRIBUTE CERTIFICATE';
19-
const TYPE_PRIVATE_KEY = 'PRIVATE KEY';
20-
const TYPE_PUBLIC_KEY = 'PUBLIC KEY';
21-
const TYPE_ENCRYPTED_PRIVATE_KEY = 'ENCRYPTED PRIVATE KEY';
22-
const TYPE_RSA_PRIVATE_KEY = 'RSA PRIVATE KEY';
23-
const TYPE_RSA_PUBLIC_KEY = 'RSA PUBLIC KEY';
24-
const TYPE_EC_PRIVATE_KEY = 'EC PRIVATE KEY';
25-
const TYPE_PKCS7 = 'PKCS7';
26-
const TYPE_CMS = 'CMS';
26+
public const TYPE_CERTIFICATE = 'CERTIFICATE';
27+
public const TYPE_CRL = 'X509 CRL';
28+
public const TYPE_CERTIFICATE_REQUEST = 'CERTIFICATE REQUEST';
29+
public const TYPE_ATTRIBUTE_CERTIFICATE = 'ATTRIBUTE CERTIFICATE';
30+
public const TYPE_PRIVATE_KEY = 'PRIVATE KEY';
31+
public const TYPE_PUBLIC_KEY = 'PUBLIC KEY';
32+
public const TYPE_ENCRYPTED_PRIVATE_KEY = 'ENCRYPTED PRIVATE KEY';
33+
public const TYPE_RSA_PRIVATE_KEY = 'RSA PRIVATE KEY';
34+
public const TYPE_RSA_PUBLIC_KEY = 'RSA PUBLIC KEY';
35+
public const TYPE_EC_PRIVATE_KEY = 'EC PRIVATE KEY';
36+
public const TYPE_PKCS7 = 'PKCS7';
37+
public const TYPE_CMS = 'CMS';
2738

2839
/**
2940
* Regular expression to match PEM block.
3041
*
3142
* @var string
3243
*/
33-
const PEM_REGEX = '/' .
44+
public const PEM_REGEX =
45+
'/' .
3446
/* line start */ '(?:^|[\r\n])' .
3547
/* header */ '-----BEGIN (.+?)-----[\r\n]+' .
3648
/* payload */ '(.+?)' .
3749
/* trailer */ '[\r\n]+-----END \\1-----' .
38-
'/ms';
50+
'/ms';
3951

4052
/**
4153
* Content type.
4254
*
4355
* @var string
4456
*/
45-
protected $_type;
57+
protected $type;
4658

4759
/**
4860
* Payload.
4961
*
5062
* @var string
5163
*/
52-
protected $_data;
64+
protected $data;
5365

5466
/**
5567
* Constructor.
@@ -59,8 +71,8 @@ class PEM
5971
*/
6072
public function __construct(string $type, string $data)
6173
{
62-
$this->_type = $type;
63-
$this->_data = $data;
74+
$this->type = $type;
75+
$this->data = $data;
6476
}
6577

6678
/**
@@ -83,12 +95,12 @@ public function __toString(): string
8395
public static function fromString(string $str): self
8496
{
8597
if (!preg_match(self::PEM_REGEX, $str, $match)) {
86-
throw new \UnexpectedValueException('Not a PEM formatted string.');
98+
throw new UnexpectedValueException('Not a PEM formatted string.');
8799
}
88100
$payload = preg_replace('/\s+/', '', $match[2]);
89101
$data = base64_decode($payload, true);
90102
if (false === $data) {
91-
throw new \UnexpectedValueException('Failed to decode PEM data.');
103+
throw new UnexpectedValueException('Failed to decode PEM data.');
92104
}
93105
return new self($match[1], $data);
94106
}
@@ -104,9 +116,11 @@ public static function fromString(string $str): self
104116
*/
105117
public static function fromFile(string $filename): self
106118
{
107-
if (!is_readable($filename) ||
108-
false === ($str = file_get_contents($filename))) {
109-
throw new \RuntimeException("Failed to read {$filename}.");
119+
if (
120+
!is_readable($filename) ||
121+
false === ($str = file_get_contents($filename))
122+
) {
123+
throw new RuntimeException("Failed to read {$filename}.");
110124
}
111125
return self::fromString($str);
112126
}
@@ -118,7 +132,7 @@ public static function fromFile(string $filename): self
118132
*/
119133
public function type(): string
120134
{
121-
return $this->_type;
135+
return $this->type;
122136
}
123137

124138
/**
@@ -128,7 +142,7 @@ public function type(): string
128142
*/
129143
public function data(): string
130144
{
131-
return $this->_data;
145+
return $this->data;
132146
}
133147

134148
/**
@@ -138,8 +152,8 @@ public function data(): string
138152
*/
139153
public function string(): string
140154
{
141-
return "-----BEGIN {$this->_type}-----\n" .
142-
trim(chunk_split(base64_encode($this->_data), 64, "\n")) . "\n" .
143-
"-----END {$this->_type}-----";
155+
return "-----BEGIN {$this->type}-----\n" .
156+
trim(chunk_split(base64_encode($this->data), 64, "\n")) . "\n" .
157+
"-----END {$this->type}-----";
144158
}
145159
}

lib/CryptoEncoding/PEMBundle.php

+55-29
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,48 @@
11
<?php
22

3-
declare(strict_types = 1);
3+
declare(strict_types=1);
44

55
namespace Sop\CryptoEncoding;
66

7+
use ArrayIterator;
8+
use Countable;
9+
use IteratorAggregate;
10+
use LogicException;
11+
use RuntimeException;
12+
use UnexpectedValueException;
13+
14+
use function array_map;
15+
use function array_merge;
16+
use function base64_decode;
17+
use function count;
18+
use function implode;
19+
use function is_readable;
20+
use function file_get_contents;
21+
use function preg_match_all;
22+
use function preg_replace;
23+
724
/**
825
* Container for multiple PEM objects.
926
*
1027
* The order of PEMs shall be retained, eg. when read from a file.
1128
*/
12-
class PEMBundle implements \Countable, \IteratorAggregate
29+
class PEMBundle implements Countable, IteratorAggregate
1330
{
1431
/**
1532
* Array of PEM objects.
1633
*
17-
* @var PEM[]
34+
* @var \Sop\CryptoEncoding\PEM[]
1835
*/
19-
protected $_pems;
36+
protected $pems;
2037

2138
/**
2239
* Constructor.
2340
*
24-
* @param PEM ...$pems
41+
* @param \Sop\CryptoEncoding\PEM[] $pems
2542
*/
2643
public function __construct(PEM ...$pems)
2744
{
28-
$this->_pems = $pems;
45+
$this->pems = $pems;
2946
}
3047

3148
/**
@@ -48,18 +65,21 @@ public function __toString(): string
4865
public static function fromString(string $str): self
4966
{
5067
if (!preg_match_all(PEM::PEM_REGEX, $str, $matches, PREG_SET_ORDER)) {
51-
throw new \UnexpectedValueException('No PEM blocks.');
68+
throw new UnexpectedValueException('No PEM blocks.');
5269
}
70+
5371
$pems = array_map(
5472
function ($match) {
5573
$payload = preg_replace('/\s+/', '', $match[2]);
5674
$data = base64_decode($payload, true);
5775
if (false === $data) {
58-
throw new \UnexpectedValueException(
59-
'Failed to decode PEM data.');
76+
throw new UnexpectedValueException('Failed to decode PEM data.');
6077
}
6178
return new PEM($match[1], $data);
62-
}, $matches);
79+
},
80+
$matches
81+
);
82+
6383
return new self(...$pems);
6484
}
6585

@@ -74,9 +94,11 @@ function ($match) {
7494
*/
7595
public static function fromFile(string $filename): self
7696
{
77-
if (!is_readable($filename) ||
78-
false === ($str = file_get_contents($filename))) {
79-
throw new \RuntimeException("Failed to read {$filename}.");
97+
if (
98+
!is_readable($filename) ||
99+
false === ($str = file_get_contents($filename))
100+
) {
101+
throw new RuntimeException("Failed to read {$filename}.");
80102
}
81103
return self::fromString($str);
82104
}
@@ -91,48 +113,48 @@ public static function fromFile(string $filename): self
91113
public function withPEMs(PEM ...$pems): self
92114
{
93115
$obj = clone $this;
94-
$obj->_pems = array_merge($obj->_pems, $pems);
116+
$obj->pems = array_merge($obj->pems, $pems);
95117
return $obj;
96118
}
97119

98120
/**
99121
* Get all PEMs in a bundle.
100122
*
101-
* @return PEM[]
123+
* @return \Sop\CryptoEncoding\PEM[]
102124
*/
103125
public function all(): array
104126
{
105-
return $this->_pems;
127+
return $this->pems;
106128
}
107129

108130
/**
109131
* Get the first PEM in a bundle.
110132
*
111133
* @throws \LogicException If bundle contains no PEM objects
112134
*
113-
* @return PEM
135+
* @return \Sop\CryptoEncoding\PEM
114136
*/
115137
public function first(): PEM
116138
{
117-
if (!count($this->_pems)) {
118-
throw new \LogicException('No PEMs.');
139+
if (!count($this->pems)) {
140+
throw new LogicException('No PEMs.');
119141
}
120-
return $this->_pems[0];
142+
return $this->pems[0];
121143
}
122144

123145
/**
124146
* Get the last PEM in a bundle.
125147
*
126148
* @throws \LogicException If bundle contains no PEM objects
127149
*
128-
* @return PEM
150+
* @return \Sop\CryptoEncoding\PEM
129151
*/
130152
public function last(): PEM
131153
{
132-
if (!count($this->_pems)) {
133-
throw new \LogicException('No PEMs.');
154+
if (!count($this->pems)) {
155+
throw new LogicException('No PEMs.');
134156
}
135-
return $this->_pems[count($this->_pems) - 1];
157+
return $this->pems[count($this->pems) - 1];
136158
}
137159

138160
/**
@@ -142,7 +164,7 @@ public function last(): PEM
142164
*/
143165
public function count(): int
144166
{
145-
return count($this->_pems);
167+
return count($this->pems);
146168
}
147169

148170
/**
@@ -152,9 +174,9 @@ public function count(): int
152174
*
153175
* @return \ArrayIterator
154176
*/
155-
public function getIterator(): \ArrayIterator
177+
public function getIterator(): ArrayIterator
156178
{
157-
return new \ArrayIterator($this->_pems);
179+
return new ArrayIterator($this->pems);
158180
}
159181

160182
/**
@@ -164,10 +186,14 @@ public function getIterator(): \ArrayIterator
164186
*/
165187
public function string(): string
166188
{
167-
return implode("\n",
189+
return implode(
190+
"\n",
168191
array_map(
169192
function (PEM $pem) {
170193
return $pem->string();
171-
}, $this->_pems));
194+
},
195+
$this->pems
196+
)
197+
);
172198
}
173199
}

0 commit comments

Comments
 (0)