From 1f65da38fc54483690bd20908aadcbdcf66c482e Mon Sep 17 00:00:00 2001 From: Jeroen van den Enden Date: Mon, 2 Oct 2023 22:25:20 +0000 Subject: [PATCH] Modernize code base --- composer.json | 2 +- src/Bacon/ErrorCorrectionLevelConverter.php | 27 ++++------ src/Builder/Builder.php | 9 ++-- src/Builder/BuilderInterface.php | 12 ++--- src/Color/Color.php | 8 +-- src/Encoding/Encoding.php | 2 +- src/Encoding/EncodingInterface.php | 2 +- src/ErrorCorrectionLevel.php | 13 +++++ .../ErrorCorrectionLevelHigh.php | 9 ---- .../ErrorCorrectionLevelInterface.php | 9 ---- .../ErrorCorrectionLevelLow.php | 9 ---- .../ErrorCorrectionLevelMedium.php | 9 ---- .../ErrorCorrectionLevelQuartile.php | 9 ---- src/ImageData/LabelImageData.php | 6 +-- src/ImageData/LogoImageData.php | 12 ++--- src/Label/Alignment/LabelAlignmentCenter.php | 9 ---- .../Alignment/LabelAlignmentInterface.php | 9 ---- src/Label/Alignment/LabelAlignmentLeft.php | 9 ---- src/Label/Alignment/LabelAlignmentRight.php | 9 ---- src/Label/Font/Font.php | 4 +- src/Label/Label.php | 23 +++----- src/Label/LabelAlignment.php | 12 +++++ src/Label/LabelInterface.php | 3 +- src/Label/Margin/Margin.php | 8 +-- src/Matrix/Matrix.php | 54 ++++++++++--------- src/QrCode.php | 33 ++++-------- src/QrCodeInterface.php | 6 +-- src/RoundBlockSizeMode.php | 13 +++++ .../RoundBlockSizeModeEnlarge.php | 9 ---- .../RoundBlockSizeModeInterface.php | 9 ---- .../RoundBlockSizeModeMargin.php | 9 ---- .../RoundBlockSizeModeNone.php | 9 ---- .../RoundBlockSizeModeShrink.php | 9 ---- src/Writer/AbstractGdWriter.php | 11 ++-- src/Writer/Result/AbstractResult.php | 2 +- src/Writer/Result/ConsoleResult.php | 2 +- src/Writer/Result/DebugResult.php | 8 +-- src/Writer/Result/EpsResult.php | 2 +- src/Writer/Result/GdResult.php | 2 +- src/Writer/Result/PdfResult.php | 2 +- src/Writer/Result/PngResult.php | 10 ++-- src/Writer/Result/SvgResult.php | 4 +- src/Writer/Result/WebPResult.php | 10 ++-- tests/BuilderTest.php | 22 ++++---- tests/QrCodeTest.php | 25 ++++----- 45 files changed, 176 insertions(+), 299 deletions(-) create mode 100644 src/ErrorCorrectionLevel.php delete mode 100644 src/ErrorCorrectionLevel/ErrorCorrectionLevelHigh.php delete mode 100644 src/ErrorCorrectionLevel/ErrorCorrectionLevelInterface.php delete mode 100644 src/ErrorCorrectionLevel/ErrorCorrectionLevelLow.php delete mode 100644 src/ErrorCorrectionLevel/ErrorCorrectionLevelMedium.php delete mode 100644 src/ErrorCorrectionLevel/ErrorCorrectionLevelQuartile.php delete mode 100644 src/Label/Alignment/LabelAlignmentCenter.php delete mode 100644 src/Label/Alignment/LabelAlignmentInterface.php delete mode 100644 src/Label/Alignment/LabelAlignmentLeft.php delete mode 100644 src/Label/Alignment/LabelAlignmentRight.php create mode 100644 src/Label/LabelAlignment.php create mode 100644 src/RoundBlockSizeMode.php delete mode 100644 src/RoundBlockSizeMode/RoundBlockSizeModeEnlarge.php delete mode 100644 src/RoundBlockSizeMode/RoundBlockSizeModeInterface.php delete mode 100644 src/RoundBlockSizeMode/RoundBlockSizeModeMargin.php delete mode 100644 src/RoundBlockSizeMode/RoundBlockSizeModeNone.php delete mode 100644 src/RoundBlockSizeMode/RoundBlockSizeModeShrink.php diff --git a/composer.json b/composer.json index 31166417..abc1b561 100644 --- a/composer.json +++ b/composer.json @@ -51,7 +51,7 @@ }, "extra": { "branch-alias": { - "dev-master": "4.x-dev" + "dev-master": "5.x-dev" } } } diff --git a/src/Bacon/ErrorCorrectionLevelConverter.php b/src/Bacon/ErrorCorrectionLevelConverter.php index 90f23e0d..ea1ec105 100644 --- a/src/Bacon/ErrorCorrectionLevelConverter.php +++ b/src/Bacon/ErrorCorrectionLevelConverter.php @@ -4,27 +4,18 @@ namespace Endroid\QrCode\Bacon; -use BaconQrCode\Common\ErrorCorrectionLevel; -use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelHigh; -use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelInterface; -use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelLow; -use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelMedium; -use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelQuartile; +use BaconQrCode\Common\ErrorCorrectionLevel as BaconErrorCorrectionLevel; +use Endroid\QrCode\ErrorCorrectionLevel; final class ErrorCorrectionLevelConverter { - public static function convertToBaconErrorCorrectionLevel(ErrorCorrectionLevelInterface $errorCorrectionLevel): ErrorCorrectionLevel + public static function convertToBaconErrorCorrectionLevel(ErrorCorrectionLevel $errorCorrectionLevel): BaconErrorCorrectionLevel { - if ($errorCorrectionLevel instanceof ErrorCorrectionLevelLow) { - return ErrorCorrectionLevel::valueOf('L'); - } elseif ($errorCorrectionLevel instanceof ErrorCorrectionLevelMedium) { - return ErrorCorrectionLevel::valueOf('M'); - } elseif ($errorCorrectionLevel instanceof ErrorCorrectionLevelQuartile) { - return ErrorCorrectionLevel::valueOf('Q'); - } elseif ($errorCorrectionLevel instanceof ErrorCorrectionLevelHigh) { - return ErrorCorrectionLevel::valueOf('H'); - } - - throw new \Exception('Error correction level could not be converted'); + return match ($errorCorrectionLevel) { + ErrorCorrectionLevel::Low => BaconErrorCorrectionLevel::valueOf('L'), + ErrorCorrectionLevel::Medium => BaconErrorCorrectionLevel::valueOf('M'), + ErrorCorrectionLevel::Quartile => BaconErrorCorrectionLevel::valueOf('Q'), + ErrorCorrectionLevel::High => BaconErrorCorrectionLevel::valueOf('H') + }; } } diff --git a/src/Builder/Builder.php b/src/Builder/Builder.php index 1aa39a70..634888d2 100644 --- a/src/Builder/Builder.php +++ b/src/Builder/Builder.php @@ -6,16 +6,19 @@ use Endroid\QrCode\Color\ColorInterface; use Endroid\QrCode\Encoding\EncodingInterface; +use Endroid\QrCode\ErrorCorrectionLevel; use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelInterface; use Endroid\QrCode\Exception\ValidationException; use Endroid\QrCode\Label\Alignment\LabelAlignmentInterface; use Endroid\QrCode\Label\Font\FontInterface; use Endroid\QrCode\Label\Label; +use Endroid\QrCode\Label\LabelAlignment; use Endroid\QrCode\Label\LabelInterface; use Endroid\QrCode\Label\Margin\MarginInterface; use Endroid\QrCode\Logo\Logo; use Endroid\QrCode\Logo\LogoInterface; use Endroid\QrCode\QrCode; +use Endroid\QrCode\RoundBlockSizeMode; use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeInterface; use Endroid\QrCode\Writer\PngWriter; use Endroid\QrCode\Writer\Result\ResultInterface; @@ -100,7 +103,7 @@ public function encoding(EncodingInterface $encoding): BuilderInterface return $this; } - public function errorCorrectionLevel(ErrorCorrectionLevelInterface $errorCorrectionLevel): BuilderInterface + public function errorCorrectionLevel(ErrorCorrectionLevel $errorCorrectionLevel): BuilderInterface { $this->options['errorCorrectionLevel'] = $errorCorrectionLevel; @@ -121,7 +124,7 @@ public function margin(int $margin): BuilderInterface return $this; } - public function roundBlockSizeMode(RoundBlockSizeModeInterface $roundBlockSizeMode): BuilderInterface + public function roundBlockSizeMode(RoundBlockSizeMode $roundBlockSizeMode): BuilderInterface { $this->options['roundBlockSizeMode'] = $roundBlockSizeMode; @@ -184,7 +187,7 @@ public function labelFont(FontInterface $labelFont): BuilderInterface return $this; } - public function labelAlignment(LabelAlignmentInterface $labelAlignment): BuilderInterface + public function labelAlignment(LabelAlignment $labelAlignment): BuilderInterface { $this->options['labelAlignment'] = $labelAlignment; diff --git a/src/Builder/BuilderInterface.php b/src/Builder/BuilderInterface.php index c173e5a7..ece4a90f 100644 --- a/src/Builder/BuilderInterface.php +++ b/src/Builder/BuilderInterface.php @@ -6,11 +6,11 @@ use Endroid\QrCode\Color\ColorInterface; use Endroid\QrCode\Encoding\EncodingInterface; -use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelInterface; -use Endroid\QrCode\Label\Alignment\LabelAlignmentInterface; +use Endroid\QrCode\ErrorCorrectionLevel; use Endroid\QrCode\Label\Font\FontInterface; +use Endroid\QrCode\Label\LabelAlignment; use Endroid\QrCode\Label\Margin\MarginInterface; -use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeInterface; +use Endroid\QrCode\RoundBlockSizeMode; use Endroid\QrCode\Writer\Result\ResultInterface; use Endroid\QrCode\Writer\WriterInterface; @@ -27,13 +27,13 @@ public function data(string $data): BuilderInterface; public function encoding(EncodingInterface $encoding): BuilderInterface; - public function errorCorrectionLevel(ErrorCorrectionLevelInterface $errorCorrectionLevel): BuilderInterface; + public function errorCorrectionLevel(ErrorCorrectionLevel $errorCorrectionLevel): BuilderInterface; public function size(int $size): BuilderInterface; public function margin(int $margin): BuilderInterface; - public function roundBlockSizeMode(RoundBlockSizeModeInterface $roundBlockSizeMode): BuilderInterface; + public function roundBlockSizeMode(RoundBlockSizeMode $roundBlockSizeMode): BuilderInterface; public function foregroundColor(ColorInterface $foregroundColor): BuilderInterface; @@ -51,7 +51,7 @@ public function labelText(string $labelText): BuilderInterface; public function labelFont(FontInterface $labelFont): BuilderInterface; - public function labelAlignment(LabelAlignmentInterface $labelAlignment): BuilderInterface; + public function labelAlignment(LabelAlignment $labelAlignment): BuilderInterface; public function labelMargin(MarginInterface $labelMargin): BuilderInterface; diff --git a/src/Color/Color.php b/src/Color/Color.php index 2fb716d7..529cf718 100644 --- a/src/Color/Color.php +++ b/src/Color/Color.php @@ -7,10 +7,10 @@ final class Color implements ColorInterface { public function __construct( - private int $red, - private int $green, - private int $blue, - private int $alpha = 0 + private readonly int $red, + private readonly int $green, + private readonly int $blue, + private readonly int $alpha = 0 ) { } diff --git a/src/Encoding/Encoding.php b/src/Encoding/Encoding.php index 54e6375d..65b64c16 100644 --- a/src/Encoding/Encoding.php +++ b/src/Encoding/Encoding.php @@ -7,7 +7,7 @@ final class Encoding implements EncodingInterface { public function __construct( - private string $value + private readonly string $value ) { if (!in_array($value, mb_list_encodings())) { throw new \Exception(sprintf('Invalid encoding "%s"', $value)); diff --git a/src/Encoding/EncodingInterface.php b/src/Encoding/EncodingInterface.php index f57001b3..17748a4b 100644 --- a/src/Encoding/EncodingInterface.php +++ b/src/Encoding/EncodingInterface.php @@ -4,7 +4,7 @@ namespace Endroid\QrCode\Encoding; -interface EncodingInterface +interface EncodingInterface extends \Stringable { public function __toString(): string; } diff --git a/src/ErrorCorrectionLevel.php b/src/ErrorCorrectionLevel.php new file mode 100644 index 00000000..2dbe4db0 --- /dev/null +++ b/src/ErrorCorrectionLevel.php @@ -0,0 +1,13 @@ +assertValidPath($path); } diff --git a/src/Label/Label.php b/src/Label/Label.php index 633936bf..4e3f890a 100644 --- a/src/Label/Label.php +++ b/src/Label/Label.php @@ -6,8 +6,6 @@ use Endroid\QrCode\Color\Color; use Endroid\QrCode\Color\ColorInterface; -use Endroid\QrCode\Label\Alignment\LabelAlignmentCenter; -use Endroid\QrCode\Label\Alignment\LabelAlignmentInterface; use Endroid\QrCode\Label\Font\Font; use Endroid\QrCode\Label\Font\FontInterface; use Endroid\QrCode\Label\Margin\Margin; @@ -15,22 +13,13 @@ final class Label implements LabelInterface { - private FontInterface $font; - private LabelAlignmentInterface $alignment; - private MarginInterface $margin; - private ColorInterface $textColor; - public function __construct( private string $text, - FontInterface $font = null, - LabelAlignmentInterface $alignment = null, - MarginInterface $margin = null, - ColorInterface $textColor = null + private FontInterface $font = new Font(__DIR__.'/../../assets/noto_sans.otf', 16), + private LabelAlignment $alignment = LabelAlignment::Center, + private MarginInterface $margin = new Margin(0, 10, 10, 10), + private ColorInterface $textColor = new Color(0, 0, 0) ) { - $this->font = $font ?? new Font(__DIR__.'/../../assets/noto_sans.otf', 16); - $this->alignment = $alignment ?? new LabelAlignmentCenter(); - $this->margin = $margin ?? new Margin(0, 10, 10, 10); - $this->textColor = $textColor ?? new Color(0, 0, 0); } public static function create(string $text): self @@ -62,12 +51,12 @@ public function setFont(FontInterface $font): self return $this; } - public function getAlignment(): LabelAlignmentInterface + public function getAlignment(): LabelAlignment { return $this->alignment; } - public function setAlignment(LabelAlignmentInterface $alignment): self + public function setAlignment(LabelAlignment $alignment): self { $this->alignment = $alignment; diff --git a/src/Label/LabelAlignment.php b/src/Label/LabelAlignment.php new file mode 100644 index 00000000..b609fe45 --- /dev/null +++ b/src/Label/LabelAlignment.php @@ -0,0 +1,12 @@ +> $blockValues */ public function __construct( private array $blockValues, int $size, int $margin, - RoundBlockSizeModeInterface $roundBlockSizeMode + RoundBlockSizeMode $roundBlockSizeMode ) { - $this->blockSize = $size / $this->getBlockCount(); - $this->innerSize = $size; - $this->outerSize = $size + 2 * $margin; + $blockSize = $size / $this->getBlockCount(); + $innerSize = $size; + $outerSize = $size + 2 * $margin; - if ($roundBlockSizeMode instanceof RoundBlockSizeModeEnlarge) { - $this->blockSize = intval(ceil($this->blockSize)); - $this->innerSize = intval($this->blockSize * $this->getBlockCount()); - $this->outerSize = $this->innerSize + 2 * $margin; - } elseif ($roundBlockSizeMode instanceof RoundBlockSizeModeShrink) { - $this->blockSize = intval(floor($this->blockSize)); - $this->innerSize = intval($this->blockSize * $this->getBlockCount()); - $this->outerSize = $this->innerSize + 2 * $margin; - } elseif ($roundBlockSizeMode instanceof RoundBlockSizeModeMargin) { - $this->blockSize = intval(floor($this->blockSize)); - $this->innerSize = intval($this->blockSize * $this->getBlockCount()); + switch ($roundBlockSizeMode) { + case RoundBlockSizeMode::Enlarge: + $blockSize = intval(ceil($blockSize)); + $innerSize = intval($blockSize * $this->getBlockCount()); + $outerSize = $innerSize + 2 * $margin; + break; + case RoundBlockSizeMode::Shrink: + $blockSize = intval(floor($blockSize)); + $innerSize = intval($blockSize * $this->getBlockCount()); + $outerSize = $innerSize + 2 * $margin; + break; + case RoundBlockSizeMode::Margin: + $blockSize = intval(floor($blockSize)); + $innerSize = intval($blockSize * $this->getBlockCount()); + break; } - if ($this->blockSize < 1) { + if ($blockSize < 1) { throw new \Exception('Too much data: increase image dimensions or lower error correction level'); } + $this->blockSize = $blockSize; + $this->innerSize = $innerSize; + $this->outerSize = $outerSize; $this->marginLeft = intval(($this->outerSize - $this->innerSize) / 2); $this->marginRight = $this->outerSize - $this->innerSize - $this->marginLeft; } diff --git a/src/QrCode.php b/src/QrCode.php index d6a56525..b7808271 100644 --- a/src/QrCode.php +++ b/src/QrCode.php @@ -8,34 +8,19 @@ use Endroid\QrCode\Color\ColorInterface; use Endroid\QrCode\Encoding\Encoding; use Endroid\QrCode\Encoding\EncodingInterface; -use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelInterface; -use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelLow; -use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeInterface; -use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin; final class QrCode implements QrCodeInterface { - private EncodingInterface $encoding; - private ErrorCorrectionLevelInterface $errorCorrectionLevel; - private RoundBlockSizeModeInterface $roundBlockSizeMode; - private ColorInterface $foregroundColor; - private ColorInterface $backgroundColor; - public function __construct( private string $data, - EncodingInterface $encoding = null, - ErrorCorrectionLevelInterface $errorCorrectionLevel = null, + private EncodingInterface $encoding = new Encoding('UTF-8'), + private ErrorCorrectionLevel $errorCorrectionLevel = ErrorCorrectionLevel::Low, private int $size = 300, private int $margin = 10, - RoundBlockSizeModeInterface $roundBlockSizeMode = null, - ColorInterface $foregroundColor = null, - ColorInterface $backgroundColor = null + private RoundBlockSizeMode $roundBlockSizeMode = RoundBlockSizeMode::Margin, + private ColorInterface $foregroundColor = new Color(0, 0, 0), + private ColorInterface $backgroundColor = new Color(255, 255, 255) ) { - $this->encoding = $encoding ?? new Encoding('UTF-8'); - $this->errorCorrectionLevel = $errorCorrectionLevel ?? new ErrorCorrectionLevelLow(); - $this->roundBlockSizeMode = $roundBlockSizeMode ?? new RoundBlockSizeModeMargin(); - $this->foregroundColor = $foregroundColor ?? new Color(0, 0, 0); - $this->backgroundColor = $backgroundColor ?? new Color(255, 255, 255); } public static function create(string $data): self @@ -67,12 +52,12 @@ public function setEncoding(Encoding $encoding): self return $this; } - public function getErrorCorrectionLevel(): ErrorCorrectionLevelInterface + public function getErrorCorrectionLevel(): ErrorCorrectionLevel { return $this->errorCorrectionLevel; } - public function setErrorCorrectionLevel(ErrorCorrectionLevelInterface $errorCorrectionLevel): self + public function setErrorCorrectionLevel(ErrorCorrectionLevel $errorCorrectionLevel): self { $this->errorCorrectionLevel = $errorCorrectionLevel; @@ -103,12 +88,12 @@ public function setMargin(int $margin): self return $this; } - public function getRoundBlockSizeMode(): RoundBlockSizeModeInterface + public function getRoundBlockSizeMode(): RoundBlockSizeMode { return $this->roundBlockSizeMode; } - public function setRoundBlockSizeMode(RoundBlockSizeModeInterface $roundBlockSizeMode): self + public function setRoundBlockSizeMode(RoundBlockSizeMode $roundBlockSizeMode): self { $this->roundBlockSizeMode = $roundBlockSizeMode; diff --git a/src/QrCodeInterface.php b/src/QrCodeInterface.php index 2c969845..1a757262 100644 --- a/src/QrCodeInterface.php +++ b/src/QrCodeInterface.php @@ -6,8 +6,6 @@ use Endroid\QrCode\Color\ColorInterface; use Endroid\QrCode\Encoding\EncodingInterface; -use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelInterface; -use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeInterface; interface QrCodeInterface { @@ -15,13 +13,13 @@ public function getData(): string; public function getEncoding(): EncodingInterface; - public function getErrorCorrectionLevel(): ErrorCorrectionLevelInterface; + public function getErrorCorrectionLevel(): ErrorCorrectionLevel; public function getSize(): int; public function getMargin(): int; - public function getRoundBlockSizeMode(): RoundBlockSizeModeInterface; + public function getRoundBlockSizeMode(): RoundBlockSizeMode; public function getForegroundColor(): ColorInterface; diff --git a/src/RoundBlockSizeMode.php b/src/RoundBlockSizeMode.php new file mode 100644 index 00000000..29fce0ee --- /dev/null +++ b/src/RoundBlockSizeMode.php @@ -0,0 +1,13 @@ +create($qrCode); - $baseBlockSize = $qrCode->getRoundBlockSizeMode() instanceof RoundBlockSizeModeNone ? 10 : intval($matrix->getBlockSize()); + $baseBlockSize = RoundBlockSizeMode::None === $qrCode->getRoundBlockSizeMode() ? 10 : intval($matrix->getBlockSize()); $baseImage = imagecreatetruecolor($matrix->getBlockCount() * $baseBlockSize, $matrix->getBlockCount() * $baseBlockSize); if (!$baseImage) { @@ -178,9 +177,9 @@ private function addLabel(LabelInterface $label, GdResult $result): GdResult $x = intval(imagesx($targetImage) / 2 - $labelImageData->getWidth() / 2); $y = imagesy($targetImage) - $label->getMargin()->getBottom(); - if ($label->getAlignment() instanceof LabelAlignmentLeft) { + if (LabelAlignment::Left === $label->getAlignment()) { $x = $label->getMargin()->getLeft(); - } elseif ($label->getAlignment() instanceof LabelAlignmentRight) { + } elseif (LabelAlignment::Right === $label->getAlignment()) { $x = imagesx($targetImage) - $labelImageData->getWidth() - $label->getMargin()->getRight(); } diff --git a/src/Writer/Result/AbstractResult.php b/src/Writer/Result/AbstractResult.php index fc8f30bc..e3eaa7d6 100644 --- a/src/Writer/Result/AbstractResult.php +++ b/src/Writer/Result/AbstractResult.php @@ -9,7 +9,7 @@ abstract class AbstractResult implements ResultInterface { public function __construct( - private MatrixInterface $matrix + private readonly MatrixInterface $matrix ) { } diff --git a/src/Writer/Result/ConsoleResult.php b/src/Writer/Result/ConsoleResult.php index 1da89841..e148259c 100644 --- a/src/Writer/Result/ConsoleResult.php +++ b/src/Writer/Result/ConsoleResult.php @@ -7,7 +7,7 @@ use Endroid\QrCode\Color\ColorInterface; use Endroid\QrCode\Matrix\MatrixInterface; -class ConsoleResult extends AbstractResult +final class ConsoleResult extends AbstractResult { private const TWO_BLOCKS = [ 0 => ' ', diff --git a/src/Writer/Result/DebugResult.php b/src/Writer/Result/DebugResult.php index 58827170..195fe919 100644 --- a/src/Writer/Result/DebugResult.php +++ b/src/Writer/Result/DebugResult.php @@ -15,11 +15,11 @@ final class DebugResult extends AbstractResult public function __construct( MatrixInterface $matrix, - private QrCodeInterface $qrCode, - private LogoInterface|null $logo = null, - private LabelInterface|null $label = null, + private readonly QrCodeInterface $qrCode, + private readonly LogoInterface|null $logo = null, + private readonly LabelInterface|null $label = null, /** @var array $options */ - private array $options = [] + private readonly array $options = [] ) { parent::__construct($matrix); } diff --git a/src/Writer/Result/EpsResult.php b/src/Writer/Result/EpsResult.php index 361551ff..16eff109 100644 --- a/src/Writer/Result/EpsResult.php +++ b/src/Writer/Result/EpsResult.php @@ -11,7 +11,7 @@ final class EpsResult extends AbstractResult public function __construct( MatrixInterface $matrix, /** @var array $lines */ - private array $lines + private readonly array $lines ) { parent::__construct($matrix); } diff --git a/src/Writer/Result/GdResult.php b/src/Writer/Result/GdResult.php index 237884a2..20c3ec8e 100644 --- a/src/Writer/Result/GdResult.php +++ b/src/Writer/Result/GdResult.php @@ -10,7 +10,7 @@ class GdResult extends AbstractResult { public function __construct( MatrixInterface $matrix, - protected \GdImage $image + protected readonly \GdImage $image ) { parent::__construct($matrix); } diff --git a/src/Writer/Result/PdfResult.php b/src/Writer/Result/PdfResult.php index 2b74a2a0..5bd55ad1 100644 --- a/src/Writer/Result/PdfResult.php +++ b/src/Writer/Result/PdfResult.php @@ -10,7 +10,7 @@ final class PdfResult extends AbstractResult { public function __construct( MatrixInterface $matrix, - private \FPDF $fpdf + private readonly \FPDF $fpdf ) { parent::__construct($matrix); } diff --git a/src/Writer/Result/PngResult.php b/src/Writer/Result/PngResult.php index 73aa5bac..5dcf31c6 100644 --- a/src/Writer/Result/PngResult.php +++ b/src/Writer/Result/PngResult.php @@ -8,12 +8,12 @@ final class PngResult extends GdResult { - private int $quality; - - public function __construct(MatrixInterface $matrix, \GdImage $image, int $quality = -1) - { + public function __construct( + MatrixInterface $matrix, + \GdImage $image, + private readonly int $quality = -1 + ) { parent::__construct($matrix, $image); - $this->quality = $quality; } public function getString(): string diff --git a/src/Writer/Result/SvgResult.php b/src/Writer/Result/SvgResult.php index 31a637f9..873d118c 100644 --- a/src/Writer/Result/SvgResult.php +++ b/src/Writer/Result/SvgResult.php @@ -10,8 +10,8 @@ final class SvgResult extends AbstractResult { public function __construct( MatrixInterface $matrix, - private \SimpleXMLElement $xml, - private bool $excludeXmlDeclaration = false + private readonly \SimpleXMLElement $xml, + private readonly bool $excludeXmlDeclaration = false ) { parent::__construct($matrix); } diff --git a/src/Writer/Result/WebPResult.php b/src/Writer/Result/WebPResult.php index b5141d19..d44c74f4 100644 --- a/src/Writer/Result/WebPResult.php +++ b/src/Writer/Result/WebPResult.php @@ -8,12 +8,12 @@ final class WebPResult extends GdResult { - private int $quality; - - public function __construct(MatrixInterface $matrix, \GdImage $image, int $quality = -1) - { + public function __construct( + MatrixInterface $matrix, + \GdImage $image, + private readonly int $quality = -1 + ) { parent::__construct($matrix, $image); - $this->quality = $quality; } public function getString(): string diff --git a/tests/BuilderTest.php b/tests/BuilderTest.php index 89c399ec..6ef6b2be 100644 --- a/tests/BuilderTest.php +++ b/tests/BuilderTest.php @@ -7,11 +7,11 @@ use Endroid\QrCode\Builder\Builder; use Endroid\QrCode\Color\Color; use Endroid\QrCode\Encoding\Encoding; -use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelHigh; -use Endroid\QrCode\Label\Alignment\LabelAlignmentCenter; +use Endroid\QrCode\ErrorCorrectionLevel; use Endroid\QrCode\Label\Font\NotoSans; +use Endroid\QrCode\Label\LabelAlignment; use Endroid\QrCode\QrCode; -use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin; +use Endroid\QrCode\RoundBlockSizeMode; use Endroid\QrCode\Writer\PngWriter; use Endroid\QrCode\Writer\Result\PngResult; use PHPUnit\Framework\TestCase; @@ -28,14 +28,14 @@ public function testBuilder(): void ->writerOptions([]) ->data('Custom QR code contents') ->encoding(new Encoding('UTF-8')) - ->errorCorrectionLevel(new ErrorCorrectionLevelHigh()) + ->errorCorrectionLevel(ErrorCorrectionLevel::High) ->size(300) ->margin(10) - ->roundBlockSizeMode(new RoundBlockSizeModeMargin()) + ->roundBlockSizeMode(RoundBlockSizeMode::Margin) ->logoPath(__DIR__.'/assets/symfony.png') ->labelText('This is the label') ->labelFont(new NotoSans(20)) - ->labelAlignment(new LabelAlignmentCenter()) + ->labelAlignment(LabelAlignment::Center) ->build() ; @@ -55,13 +55,13 @@ public function testGithubIssue365(): void $result = Builder::create() ->data($data) ->encoding(new Encoding('UTF-8')) - ->errorCorrectionLevel(new ErrorCorrectionLevelHigh()) + ->errorCorrectionLevel(ErrorCorrectionLevel::High) ->size($size) ->margin(10) - ->roundBlockSizeMode(new RoundBlockSizeModeMargin()) + ->roundBlockSizeMode(RoundBlockSizeMode::Margin) ->labelText($label) ->labelFont(new NotoSans(20)) - ->labelAlignment(new LabelAlignmentCenter()) + ->labelAlignment(LabelAlignment::Center) ->build(); $this->assertInstanceOf(PngResult::class, $result); @@ -70,10 +70,10 @@ public function testGithubIssue365(): void $writer = new PngWriter(); $qrCode = QrCode::create('https://xxxxxxx.xxxx/xxxxxxx') ->setEncoding(new Encoding('UTF-8')) - ->setErrorCorrectionLevel(new ErrorCorrectionLevelHigh()) + ->setErrorCorrectionLevel(ErrorCorrectionLevel::High) ->setSize(1200) ->setMargin(40) - ->setRoundBlockSizeMode(new RoundBlockSizeModeMargin()) + ->setRoundBlockSizeMode(RoundBlockSizeMode::Margin) ->setForegroundColor(new Color(0, 0, 0)) ->setBackgroundColor(new Color(255, 255, 255, 127)); diff --git a/tests/QrCodeTest.php b/tests/QrCodeTest.php index d2cd786e..a62b8eb0 100644 --- a/tests/QrCodeTest.php +++ b/tests/QrCodeTest.php @@ -8,15 +8,12 @@ use Endroid\QrCode\Builder\Builder; use Endroid\QrCode\Color\Color; use Endroid\QrCode\Encoding\Encoding; -use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelLow; +use Endroid\QrCode\ErrorCorrectionLevel; use Endroid\QrCode\Label\Label; use Endroid\QrCode\Logo\Logo; use Endroid\QrCode\Matrix\MatrixInterface; use Endroid\QrCode\QrCode; -use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeEnlarge; -use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeInterface; -use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin; -use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeShrink; +use Endroid\QrCode\RoundBlockSizeMode; use Endroid\QrCode\Writer\BinaryWriter; use Endroid\QrCode\Writer\ConsoleWriter; use Endroid\QrCode\Writer\DebugWriter; @@ -50,10 +47,10 @@ public function testQrCode(WriterInterface $writer, string $resultClass, string { $qrCode = QrCode::create('Data') ->setEncoding(new Encoding('UTF-8')) - ->setErrorCorrectionLevel(new ErrorCorrectionLevelLow()) + ->setErrorCorrectionLevel(ErrorCorrectionLevel::Low) ->setSize(300) ->setMargin(10) - ->setRoundBlockSizeMode(new RoundBlockSizeModeMargin()) + ->setRoundBlockSizeMode(RoundBlockSizeMode::Margin) ->setForegroundColor(new Color(0, 0, 0)) ->setBackgroundColor(new Color(255, 255, 255)); @@ -112,7 +109,7 @@ public function testSetSize(): void * * @dataProvider roundedSizeProvider */ - public function testSetSizeRounded(int $size, int $margin, RoundBlockSizeModeInterface $roundBlockSizeMode, int $expectedSize): void + public function testSetSizeRounded(int $size, int $margin, RoundBlockSizeMode $roundBlockSizeMode, int $expectedSize): void { $imageData = Builder::create() ->data('QR Code contents with some length to have some data') @@ -129,12 +126,12 @@ public function testSetSizeRounded(int $size, int $margin, RoundBlockSizeModeInt public static function roundedSizeProvider(): iterable { - yield [400, 0, new RoundBlockSizeModeEnlarge(), 406]; - yield [400, 5, new RoundBlockSizeModeEnlarge(), 416]; - yield [400, 0, new RoundBlockSizeModeMargin(), 400]; - yield [400, 5, new RoundBlockSizeModeMargin(), 410]; - yield [400, 0, new RoundBlockSizeModeShrink(), 377]; - yield [400, 5, new RoundBlockSizeModeShrink(), 387]; + yield [400, 0, RoundBlockSizeMode::Enlarge, 406]; + yield [400, 5, RoundBlockSizeMode::Enlarge, 416]; + yield [400, 0, RoundBlockSizeMode::Margin, 400]; + yield [400, 5, RoundBlockSizeMode::Margin, 410]; + yield [400, 0, RoundBlockSizeMode::Shrink, 377]; + yield [400, 5, RoundBlockSizeMode::Shrink, 387]; } /**