Skip to content

Commit 1f65da3

Browse files
committed
Modernize code base
1 parent 0db25b5 commit 1f65da3

Some content is hidden

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

45 files changed

+176
-299
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
},
5252
"extra": {
5353
"branch-alias": {
54-
"dev-master": "4.x-dev"
54+
"dev-master": "5.x-dev"
5555
}
5656
}
5757
}

src/Bacon/ErrorCorrectionLevelConverter.php

+9-18
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,18 @@
44

55
namespace Endroid\QrCode\Bacon;
66

7-
use BaconQrCode\Common\ErrorCorrectionLevel;
8-
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelHigh;
9-
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelInterface;
10-
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelLow;
11-
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelMedium;
12-
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelQuartile;
7+
use BaconQrCode\Common\ErrorCorrectionLevel as BaconErrorCorrectionLevel;
8+
use Endroid\QrCode\ErrorCorrectionLevel;
139

1410
final class ErrorCorrectionLevelConverter
1511
{
16-
public static function convertToBaconErrorCorrectionLevel(ErrorCorrectionLevelInterface $errorCorrectionLevel): ErrorCorrectionLevel
12+
public static function convertToBaconErrorCorrectionLevel(ErrorCorrectionLevel $errorCorrectionLevel): BaconErrorCorrectionLevel
1713
{
18-
if ($errorCorrectionLevel instanceof ErrorCorrectionLevelLow) {
19-
return ErrorCorrectionLevel::valueOf('L');
20-
} elseif ($errorCorrectionLevel instanceof ErrorCorrectionLevelMedium) {
21-
return ErrorCorrectionLevel::valueOf('M');
22-
} elseif ($errorCorrectionLevel instanceof ErrorCorrectionLevelQuartile) {
23-
return ErrorCorrectionLevel::valueOf('Q');
24-
} elseif ($errorCorrectionLevel instanceof ErrorCorrectionLevelHigh) {
25-
return ErrorCorrectionLevel::valueOf('H');
26-
}
27-
28-
throw new \Exception('Error correction level could not be converted');
14+
return match ($errorCorrectionLevel) {
15+
ErrorCorrectionLevel::Low => BaconErrorCorrectionLevel::valueOf('L'),
16+
ErrorCorrectionLevel::Medium => BaconErrorCorrectionLevel::valueOf('M'),
17+
ErrorCorrectionLevel::Quartile => BaconErrorCorrectionLevel::valueOf('Q'),
18+
ErrorCorrectionLevel::High => BaconErrorCorrectionLevel::valueOf('H')
19+
};
2920
}
3021
}

src/Builder/Builder.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,19 @@
66

77
use Endroid\QrCode\Color\ColorInterface;
88
use Endroid\QrCode\Encoding\EncodingInterface;
9+
use Endroid\QrCode\ErrorCorrectionLevel;
910
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelInterface;
1011
use Endroid\QrCode\Exception\ValidationException;
1112
use Endroid\QrCode\Label\Alignment\LabelAlignmentInterface;
1213
use Endroid\QrCode\Label\Font\FontInterface;
1314
use Endroid\QrCode\Label\Label;
15+
use Endroid\QrCode\Label\LabelAlignment;
1416
use Endroid\QrCode\Label\LabelInterface;
1517
use Endroid\QrCode\Label\Margin\MarginInterface;
1618
use Endroid\QrCode\Logo\Logo;
1719
use Endroid\QrCode\Logo\LogoInterface;
1820
use Endroid\QrCode\QrCode;
21+
use Endroid\QrCode\RoundBlockSizeMode;
1922
use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeInterface;
2023
use Endroid\QrCode\Writer\PngWriter;
2124
use Endroid\QrCode\Writer\Result\ResultInterface;
@@ -100,7 +103,7 @@ public function encoding(EncodingInterface $encoding): BuilderInterface
100103
return $this;
101104
}
102105

103-
public function errorCorrectionLevel(ErrorCorrectionLevelInterface $errorCorrectionLevel): BuilderInterface
106+
public function errorCorrectionLevel(ErrorCorrectionLevel $errorCorrectionLevel): BuilderInterface
104107
{
105108
$this->options['errorCorrectionLevel'] = $errorCorrectionLevel;
106109

@@ -121,7 +124,7 @@ public function margin(int $margin): BuilderInterface
121124
return $this;
122125
}
123126

124-
public function roundBlockSizeMode(RoundBlockSizeModeInterface $roundBlockSizeMode): BuilderInterface
127+
public function roundBlockSizeMode(RoundBlockSizeMode $roundBlockSizeMode): BuilderInterface
125128
{
126129
$this->options['roundBlockSizeMode'] = $roundBlockSizeMode;
127130

@@ -184,7 +187,7 @@ public function labelFont(FontInterface $labelFont): BuilderInterface
184187
return $this;
185188
}
186189

187-
public function labelAlignment(LabelAlignmentInterface $labelAlignment): BuilderInterface
190+
public function labelAlignment(LabelAlignment $labelAlignment): BuilderInterface
188191
{
189192
$this->options['labelAlignment'] = $labelAlignment;
190193

src/Builder/BuilderInterface.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66

77
use Endroid\QrCode\Color\ColorInterface;
88
use Endroid\QrCode\Encoding\EncodingInterface;
9-
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelInterface;
10-
use Endroid\QrCode\Label\Alignment\LabelAlignmentInterface;
9+
use Endroid\QrCode\ErrorCorrectionLevel;
1110
use Endroid\QrCode\Label\Font\FontInterface;
11+
use Endroid\QrCode\Label\LabelAlignment;
1212
use Endroid\QrCode\Label\Margin\MarginInterface;
13-
use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeInterface;
13+
use Endroid\QrCode\RoundBlockSizeMode;
1414
use Endroid\QrCode\Writer\Result\ResultInterface;
1515
use Endroid\QrCode\Writer\WriterInterface;
1616

@@ -27,13 +27,13 @@ public function data(string $data): BuilderInterface;
2727

2828
public function encoding(EncodingInterface $encoding): BuilderInterface;
2929

30-
public function errorCorrectionLevel(ErrorCorrectionLevelInterface $errorCorrectionLevel): BuilderInterface;
30+
public function errorCorrectionLevel(ErrorCorrectionLevel $errorCorrectionLevel): BuilderInterface;
3131

3232
public function size(int $size): BuilderInterface;
3333

3434
public function margin(int $margin): BuilderInterface;
3535

36-
public function roundBlockSizeMode(RoundBlockSizeModeInterface $roundBlockSizeMode): BuilderInterface;
36+
public function roundBlockSizeMode(RoundBlockSizeMode $roundBlockSizeMode): BuilderInterface;
3737

3838
public function foregroundColor(ColorInterface $foregroundColor): BuilderInterface;
3939

@@ -51,7 +51,7 @@ public function labelText(string $labelText): BuilderInterface;
5151

5252
public function labelFont(FontInterface $labelFont): BuilderInterface;
5353

54-
public function labelAlignment(LabelAlignmentInterface $labelAlignment): BuilderInterface;
54+
public function labelAlignment(LabelAlignment $labelAlignment): BuilderInterface;
5555

5656
public function labelMargin(MarginInterface $labelMargin): BuilderInterface;
5757

src/Color/Color.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
final class Color implements ColorInterface
88
{
99
public function __construct(
10-
private int $red,
11-
private int $green,
12-
private int $blue,
13-
private int $alpha = 0
10+
private readonly int $red,
11+
private readonly int $green,
12+
private readonly int $blue,
13+
private readonly int $alpha = 0
1414
) {
1515
}
1616

src/Encoding/Encoding.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
final class Encoding implements EncodingInterface
88
{
99
public function __construct(
10-
private string $value
10+
private readonly string $value
1111
) {
1212
if (!in_array($value, mb_list_encodings())) {
1313
throw new \Exception(sprintf('Invalid encoding "%s"', $value));

src/Encoding/EncodingInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Endroid\QrCode\Encoding;
66

7-
interface EncodingInterface
7+
interface EncodingInterface extends \Stringable
88
{
99
public function __toString(): string;
1010
}

src/ErrorCorrectionLevel.php

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Endroid\QrCode;
6+
7+
enum ErrorCorrectionLevel: string
8+
{
9+
case High = 'high';
10+
case Low = 'low';
11+
case Medium = 'medium';
12+
case Quartile = 'quartile';
13+
}

src/ErrorCorrectionLevel/ErrorCorrectionLevelHigh.php

-9
This file was deleted.

src/ErrorCorrectionLevel/ErrorCorrectionLevelInterface.php

-9
This file was deleted.

src/ErrorCorrectionLevel/ErrorCorrectionLevelLow.php

-9
This file was deleted.

src/ErrorCorrectionLevel/ErrorCorrectionLevelMedium.php

-9
This file was deleted.

src/ErrorCorrectionLevel/ErrorCorrectionLevelQuartile.php

-9
This file was deleted.

src/ImageData/LabelImageData.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66

77
use Endroid\QrCode\Label\LabelInterface;
88

9-
class LabelImageData
9+
final class LabelImageData
1010
{
1111
private function __construct(
12-
private int $width,
13-
private int $height
12+
private readonly int $width,
13+
private readonly int $height
1414
) {
1515
}
1616

src/ImageData/LogoImageData.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66

77
use Endroid\QrCode\Logo\LogoInterface;
88

9-
class LogoImageData
9+
final class LogoImageData
1010
{
1111
private function __construct(
12-
private string $data,
12+
private readonly string $data,
1313
private \GdImage|null $image,
14-
private string $mimeType,
15-
private int $width,
16-
private int $height,
17-
private bool $punchoutBackground
14+
private readonly string $mimeType,
15+
private readonly int $width,
16+
private readonly int $height,
17+
private readonly bool $punchoutBackground
1818
) {
1919
}
2020

src/Label/Alignment/LabelAlignmentCenter.php

-9
This file was deleted.

src/Label/Alignment/LabelAlignmentInterface.php

-9
This file was deleted.

src/Label/Alignment/LabelAlignmentLeft.php

-9
This file was deleted.

src/Label/Alignment/LabelAlignmentRight.php

-9
This file was deleted.

src/Label/Font/Font.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
final class Font implements FontInterface
88
{
99
public function __construct(
10-
private string $path,
11-
private int $size = 16
10+
private readonly string $path,
11+
private readonly int $size = 16
1212
) {
1313
$this->assertValidPath($path);
1414
}

src/Label/Label.php

+6-17
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,20 @@
66

77
use Endroid\QrCode\Color\Color;
88
use Endroid\QrCode\Color\ColorInterface;
9-
use Endroid\QrCode\Label\Alignment\LabelAlignmentCenter;
10-
use Endroid\QrCode\Label\Alignment\LabelAlignmentInterface;
119
use Endroid\QrCode\Label\Font\Font;
1210
use Endroid\QrCode\Label\Font\FontInterface;
1311
use Endroid\QrCode\Label\Margin\Margin;
1412
use Endroid\QrCode\Label\Margin\MarginInterface;
1513

1614
final class Label implements LabelInterface
1715
{
18-
private FontInterface $font;
19-
private LabelAlignmentInterface $alignment;
20-
private MarginInterface $margin;
21-
private ColorInterface $textColor;
22-
2316
public function __construct(
2417
private string $text,
25-
FontInterface $font = null,
26-
LabelAlignmentInterface $alignment = null,
27-
MarginInterface $margin = null,
28-
ColorInterface $textColor = null
18+
private FontInterface $font = new Font(__DIR__.'/../../assets/noto_sans.otf', 16),
19+
private LabelAlignment $alignment = LabelAlignment::Center,
20+
private MarginInterface $margin = new Margin(0, 10, 10, 10),
21+
private ColorInterface $textColor = new Color(0, 0, 0)
2922
) {
30-
$this->font = $font ?? new Font(__DIR__.'/../../assets/noto_sans.otf', 16);
31-
$this->alignment = $alignment ?? new LabelAlignmentCenter();
32-
$this->margin = $margin ?? new Margin(0, 10, 10, 10);
33-
$this->textColor = $textColor ?? new Color(0, 0, 0);
3423
}
3524

3625
public static function create(string $text): self
@@ -62,12 +51,12 @@ public function setFont(FontInterface $font): self
6251
return $this;
6352
}
6453

65-
public function getAlignment(): LabelAlignmentInterface
54+
public function getAlignment(): LabelAlignment
6655
{
6756
return $this->alignment;
6857
}
6958

70-
public function setAlignment(LabelAlignmentInterface $alignment): self
59+
public function setAlignment(LabelAlignment $alignment): self
7160
{
7261
$this->alignment = $alignment;
7362

src/Label/LabelAlignment.php

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Endroid\QrCode\Label;
6+
7+
enum LabelAlignment: string
8+
{
9+
case Center = 'center';
10+
case Left = 'left';
11+
case Right = 'right';
12+
}

src/Label/LabelInterface.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace Endroid\QrCode\Label;
66

77
use Endroid\QrCode\Color\ColorInterface;
8-
use Endroid\QrCode\Label\Alignment\LabelAlignmentInterface;
98
use Endroid\QrCode\Label\Font\FontInterface;
109
use Endroid\QrCode\Label\Margin\MarginInterface;
1110

@@ -15,7 +14,7 @@ public function getText(): string;
1514

1615
public function getFont(): FontInterface;
1716

18-
public function getAlignment(): LabelAlignmentInterface;
17+
public function getAlignment(): LabelAlignment;
1918

2019
public function getMargin(): MarginInterface;
2120

0 commit comments

Comments
 (0)