Skip to content

Commit 3e53ee3

Browse files
authored
Merge pull request #16 from petrknap/refactor
BC: removed public constants from coders, removed interfaces for abstract classes, rearranged exceptions
2 parents e184ef7 + ffa7b99 commit 3e53ee3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+315
-354
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ See the examples below for more information, or check out [`Encoder`](./src/Enco
77
namespace PetrKnap\Binary;
88

99
$data = base64_decode('hmlpFnFwbchsoQARSibVpfbWVfuwAHLbGxjFl9eC8fiGaWkWcXBtyGyhABFKJtWl9tZV+7AActsbGMWX14Lx+A==');
10-
$encoded = Binary::encode($data)->checksum()->zlib()->base64(urlSafe: true)->getData();
11-
$decoded = Binary::decode($encoded)->base64()->zlib()->checksum()->getData();
10+
$encoded = Binary::encode($data)->checksum()->zlib()->base64(urlSafe: true)->data;
11+
$decoded = Binary::decode($encoded)->base64()->zlib()->checksum()->data;
1212

1313
printf('Data was coded into `%s` %s.', $encoded, $decoded === $data ? 'successfully' : 'unsuccessfully');
1414
```

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"require": {
4242
"php": ">=8.1",
4343
"petrknap/optional": "^3.1",
44-
"petrknap/shorts": "^2.0"
44+
"petrknap/shorts": "^3.0"
4545
},
4646
"require-dev": {
4747
"ext-igbinary": "*",
@@ -63,7 +63,7 @@
6363
"phpcs --colors --standard=PSR12 --exclude=Generic.Files.LineLength src tests",
6464
"phpstan analyse --level max src --ansi --no-interaction",
6565
"phpstan analyse --level 5 tests --ansi --no-interaction",
66-
"phpinsights analyse src --ansi --no-interaction"
66+
"phpinsights analyse src tests --ansi --no-interaction --format=github-action | sed -e \"s#::error file=$PWD/#::notice file=#g\""
6767
],
6868
"check-requirements": [
6969
"composer update \"petrknap/*\"",

src/Binary.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66

77
final class Binary
88
{
9-
public static function encode(string $data): EncoderInterface
9+
public static function encode(string $data): Encoder
1010
{
1111
return new Encoder($data);
1212
}
1313

14-
public static function decode(string $data): DecoderInterface
14+
public static function decode(string $data): Decoder
1515
{
1616
return new Decoder($data);
1717
}

src/Byter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use PetrKnap\Shorts\HasRequirements;
88
use RuntimeException;
99

10-
class Byter
10+
final class Byter
1111
{
1212
use HasRequirements;
1313

@@ -28,15 +28,15 @@ functions: [
2828
*
2929
* @return array<string> bites of specified sizes; and remains, if any
3030
*
31-
* @throws Exception\CouldNotBiteData
31+
* @throws Exception\ByterCouldNotBiteData
3232
*/
3333
public function bite(string $data, int $size1, int ...$sizeN): array
3434
{
3535
$remains = $data;
3636
$bites = [];
3737
foreach ([$size1, ...$sizeN] as $size) {
3838
if (abs($size) > $this->size($remains)) {
39-
throw new Exception\CouldNotBiteData(__METHOD__, $data, new RuntimeException(
39+
throw new Exception\ByterCouldNotBiteData(__METHOD__, $data, new RuntimeException(
4040
'Remains are smaller than bite',
4141
));
4242
}

src/Coder.php

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,54 @@
44

55
namespace PetrKnap\Binary;
66

7-
use PetrKnap\Shorts\Exception;
8-
97
/**
10-
* @internal please use subclass
11-
*
12-
* @phpstan-consistent-constructor override {@see self::create()} if not
13-
*
14-
* @implements CoderInterface<Exception\CouldNotProcessData>
8+
* @internal shared logic
159
*/
16-
abstract class Coder implements CoderInterface
10+
abstract class Coder
1711
{
18-
public function __construct(
19-
protected readonly string $data = '',
12+
final public function __construct(
13+
public readonly string $data = '',
2014
) {
2115
}
2216

23-
public function withData(string $data): static
17+
final public function withData(string $data): static
2418
{
25-
return static::create($this, $data);
19+
return new static($data);
2620
}
2721

28-
public function getData(): string
22+
/**
23+
* @deprecated use readonly property $data
24+
*/
25+
final public function getData(): string
2926
{
3027
return $this->data;
3128
}
3229

33-
protected static function create(self $coder, string $data): static
34-
{
35-
return new static($data);
36-
}
30+
/**
31+
* @see Coder\Base64
32+
*
33+
* @throws Coder\Exception\CoderException
34+
*/
35+
abstract public function base64(): static;
36+
37+
/**
38+
* @see Coder\Checksum
39+
*
40+
* @throws Coder\Exception\CoderException
41+
*/
42+
abstract public function checksum(string|null $algorithm = null): static;
43+
44+
/**
45+
* @see Coder\Hex
46+
*
47+
* @throws Coder\Exception\CoderException
48+
*/
49+
abstract public function hex(): static;
50+
51+
/**
52+
* @see Coder\zlib
53+
*
54+
* @throws Coder\Exception\CoderException
55+
*/
56+
abstract public function zlib(): static;
3757
}

src/Coder/Base64.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,16 @@
1212
*/
1313
final class Base64 extends Coder
1414
{
15-
public const URL_SAFE = false;
16-
1715
private const URL_REPLACE_TABLE = [
1816
['+', '/', '='],
1917
['-', '_', ''],
2018
];
2119

2220
private bool $urlSafe;
2321

24-
public function encode(string $decoded, ?bool $urlSafe = null): string
22+
public function encode(string $decoded, bool|null $urlSafe = null): string
2523
{
26-
$this->urlSafe = $urlSafe ?? self::URL_SAFE;
24+
$this->urlSafe = $urlSafe ?? false;
2725
return parent::encode($decoded);
2826
}
2927

@@ -42,7 +40,7 @@ protected function doDecode(string $encoded): string
4240
str_replace(self::URL_REPLACE_TABLE[1], self::URL_REPLACE_TABLE[0], $encoded),
4341
strict: true,
4442
))->orElseThrow(
45-
static fn () => new Exception\CouldNotDecodeData(__METHOD__, $encoded),
43+
static fn () => new Exception\CoderCouldNotDecodeData(__METHOD__, $encoded),
4644
);
4745
}
4846
}

src/Coder/Checksum.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
*/
1414
final class Checksum extends Coder
1515
{
16-
public const ALGORITHM = 'crc32';
16+
/**
17+
* @internal public for testing purposes only
18+
*/
19+
public const DEFAULT_ALGORITHM = 'crc32';
1720

1821
private string $algorithm;
1922
private readonly Byter $byter;
@@ -23,15 +26,15 @@ public function __construct()
2326
$this->byter = new Byter();
2427
}
2528

26-
public function encode(string $decoded, ?string $algorithm = null): string
29+
public function encode(string $decoded, string|null $algorithm = null): string
2730
{
28-
$this->algorithm = $algorithm ?? self::ALGORITHM;
31+
$this->algorithm = $algorithm ?? self::DEFAULT_ALGORITHM;
2932
return parent::encode($decoded);
3033
}
3134

32-
public function decode(string $encoded, ?string $algorithm = null): string
35+
public function decode(string $encoded, string|null $algorithm = null): string
3336
{
34-
$this->algorithm = $algorithm ?? self::ALGORITHM;
37+
$this->algorithm = $algorithm ?? self::DEFAULT_ALGORITHM;
3538
return parent::decode($encoded);
3639
}
3740

@@ -46,7 +49,7 @@ protected function doDecode(string $encoded): string
4649
$checksumLength = $this->byter->size(hash($this->algorithm, '', binary: true));
4750
[,$decoded] = $this->byter->bite($encoded, -$checksumLength);
4851
if ($this->doEncode($decoded) !== $encoded) {
49-
throw new Exception\CouldNotDecodeData(__METHOD__, $encoded);
52+
throw new Exception\CoderCouldNotDecodeData(__METHOD__, $encoded);
5053
}
5154
return $decoded;
5255
}

src/Coder/Coder.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,30 @@
66

77
use Throwable;
88

9+
/**
10+
* @internal shared logic
11+
*/
912
abstract class Coder implements CoderInterface
1013
{
1114
public function encode(string $decoded): string
1215
{
1316
try {
1417
return $this->doEncode($decoded);
18+
} catch (Exception\CoderCouldNotEncodeData $exception) {
19+
throw $exception;
1520
} catch (Throwable $reason) {
16-
if ($reason instanceof Exception\CouldNotEncodeData) {
17-
throw $reason;
18-
}
19-
throw new Exception\CouldNotEncodeData(__METHOD__, $decoded, $reason);
21+
throw new Exception\CoderCouldNotEncodeData(__METHOD__, $decoded, $reason);
2022
}
2123
}
2224

2325
public function decode(string $encoded): string
2426
{
2527
try {
2628
return $this->doDecode($encoded);
29+
} catch (Exception\CoderCouldNotDecodeData $exception) {
30+
throw $exception;
2731
} catch (Throwable $reason) {
28-
if ($reason instanceof Exception\CouldNotDecodeData) {
29-
throw $reason;
30-
}
31-
throw new Exception\CouldNotDecodeData(__METHOD__, $encoded, $reason);
32+
throw new Exception\CoderCouldNotDecodeData(__METHOD__, $encoded, $reason);
3233
}
3334
}
3435

src/Coder/CoderInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
interface CoderInterface
88
{
99
/**
10-
* @throws Exception\CouldNotEncodeData
10+
* @throws Exception\CoderCouldNotEncodeData
1111
*/
1212
public function encode(string $decoded): string;
1313

1414
/**
15-
* @throws Exception\CouldNotDecodeData
15+
* @throws Exception\CoderCouldNotDecodeData
1616
*/
1717
public function decode(string $encoded): string;
1818
}

src/Coder/Exception/CouldNotDecodeData.php renamed to src/Coder/Exception/CoderCouldNotDecodeData.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
/**
1010
* @extends CouldNotProcessData<string>
1111
*/
12-
final class CouldNotDecodeData extends CouldNotProcessData implements CoderException
12+
final class CoderCouldNotDecodeData extends CouldNotProcessData implements CoderException
1313
{
1414
}

0 commit comments

Comments
 (0)