Skip to content

Commit 63b49ce

Browse files
authored
Merge pull request #108 from xHeaven/master
code improvements, readability enhancements, type safety, modern syntax adaptation
2 parents 098dce6 + e554a9b commit 63b49ce

24 files changed

+192
-206
lines changed

lib/Providers/Qr/BaconQrCodeProvider.php

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use BaconQrCode\Renderer\Image\ImagickImageBackEnd;
1111
use BaconQrCode\Renderer\Image\SvgImageBackEnd;
1212
use BaconQrCode\Renderer\ImageRenderer;
13-
1413
use BaconQrCode\Renderer\RendererStyle\EyeFill;
1514
use BaconQrCode\Renderer\RendererStyle\Fill;
1615
use BaconQrCode\Renderer\RendererStyle\RendererStyle;
@@ -23,10 +22,10 @@ class BaconQrCodeProvider implements IQRCodeProvider
2322
* Ensure we using the latest Bacon QR Code and specify default options
2423
*/
2524
public function __construct(
26-
private int $borderWidth = 4,
25+
private readonly int $borderWidth = 4,
2726
private string|array $backgroundColour = '#ffffff',
2827
private string|array $foregroundColour = '#000000',
29-
private string $format = 'png',
28+
private string $format = 'png',
3029
) {
3130
$this->backgroundColour = $this->handleColour($this->backgroundColour);
3231
$this->foregroundColour = $this->handleColour($this->foregroundColour);
@@ -54,20 +53,15 @@ public function getMimeType(): string
5453

5554
public function getQRCodeImage(string $qrText, int $size): string
5655
{
57-
switch ($this->format) {
58-
case 'svg':
59-
$backend = new SvgImageBackEnd();
60-
break;
61-
case 'eps':
62-
$backend = new EpsImageBackEnd();
63-
break;
64-
default:
65-
$backend = new ImagickImageBackEnd($this->format);
66-
}
56+
$backend = match ($this->format) {
57+
'svg' => new SvgImageBackEnd(),
58+
'eps' => new EpsImageBackEnd(),
59+
default => new ImagickImageBackEnd($this->format),
60+
};
6761

6862
$output = $this->getQRCodeByBackend($qrText, $size, $backend);
6963

70-
if ($this->format == 'svg') {
64+
if ($this->format === 'svg') {
7165
$svg = explode("\n", $output);
7266
return $svg[1];
7367
}
@@ -84,7 +78,7 @@ private function getQRCodeByBackend($qrText, $size, ImageBackEndInterface $backe
8478
$rendererStyleArgs = array($size, $this->borderWidth);
8579

8680
if (is_array($this->foregroundColour) && is_array($this->backgroundColour)) {
87-
$rendererStyleArgs = array_merge($rendererStyleArgs, array(
81+
$rendererStyleArgs = array(...$rendererStyleArgs, ...array(
8882
null,
8983
null,
9084
Fill::withForegroundColor(
@@ -112,7 +106,7 @@ private function getQRCodeByBackend($qrText, $size, ImageBackEndInterface $backe
112106
private function handleColour(array|string $colour): array|string
113107
{
114108
if (is_string($colour) && $colour[0] == '#') {
115-
$hexToRGB = function ($input) {
109+
$hexToRGB = static function ($input) {
116110
// ensure input no longer has a # for more predictable division
117111
// PHP 8.1 does not like implicitly casting a float to an int
118112
$input = trim($input, '#');
@@ -126,7 +120,7 @@ private function handleColour(array|string $colour): array|string
126120

127121
// cope with three character hex reference
128122
if (strlen($input) == 3) {
129-
array_walk($split, function (&$character) {
123+
array_walk($split, static function (&$character) {
130124
$character = str_repeat($character, 2);
131125
});
132126
}

lib/Providers/Qr/EndroidQrCodeProvider.php

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,19 @@ public function getMimeType(): string
4141
return 'image/png';
4242
}
4343

44-
public function getQRCodeImage(string $qrtext, int $size): string
44+
public function getQRCodeImage(string $qrText, int $size): string
4545
{
4646
if (!$this->endroid4) {
47-
return $this->qrCodeInstance($qrtext, $size)->writeString();
47+
return $this->qrCodeInstance($qrText, $size)->writeString();
4848
}
4949

5050
$writer = new PngWriter();
51-
return $writer->write($this->qrCodeInstance($qrtext, $size))->getString();
51+
return $writer->write($this->qrCodeInstance($qrText, $size))->getString();
5252
}
5353

54-
protected function qrCodeInstance(string $qrtext, int $size): QrCode
54+
protected function qrCodeInstance(string $qrText, int $size): QrCode
5555
{
56-
$qrCode = new QrCode($qrtext);
56+
$qrCode = new QrCode($qrText);
5757
$qrCode->setSize($size);
5858

5959
$qrCode->setErrorCorrectionLevel($this->errorcorrectionlevel);
@@ -64,7 +64,7 @@ protected function qrCodeInstance(string $qrtext, int $size): QrCode
6464
return $qrCode;
6565
}
6666

67-
private function handleColor(string $color): Color
67+
private function handleColor(string $color): Color|array
6868
{
6969
$split = str_split($color, 2);
7070
$r = hexdec($split[0]);
@@ -74,18 +74,13 @@ private function handleColor(string $color): Color
7474
return $this->endroid4 ? new Color($r, $g, $b, 0) : array('r' => $r, 'g' => $g, 'b' => $b, 'a' => 0);
7575
}
7676

77-
private function handleErrorCorrectionLevel(string $level): ErrorCorrectionLevelInterface
77+
private function handleErrorCorrectionLevel(string $level): ErrorCorrectionLevelInterface|ErrorCorrectionLevel
7878
{
79-
switch ($level) {
80-
case 'L':
81-
return $this->endroid4 ? new ErrorCorrectionLevelLow() : ErrorCorrectionLevel::LOW();
82-
case 'M':
83-
return $this->endroid4 ? new ErrorCorrectionLevelMedium() : ErrorCorrectionLevel::MEDIUM();
84-
case 'Q':
85-
return $this->endroid4 ? new ErrorCorrectionLevelQuartile() : ErrorCorrectionLevel::QUARTILE();
86-
case 'H':
87-
default:
88-
return $this->endroid4 ? new ErrorCorrectionLevelHigh() : ErrorCorrectionLevel::HIGH();
89-
}
79+
return match ($level) {
80+
'L' => $this->endroid4 ? new ErrorCorrectionLevelLow() : ErrorCorrectionLevel::LOW(),
81+
'M' => $this->endroid4 ? new ErrorCorrectionLevelMedium() : ErrorCorrectionLevel::MEDIUM(),
82+
'Q' => $this->endroid4 ? new ErrorCorrectionLevelQuartile() : ErrorCorrectionLevel::QUARTILE(),
83+
default => $this->endroid4 ? new ErrorCorrectionLevelHigh() : ErrorCorrectionLevel::HIGH(),
84+
};
9085
}
9186
}

lib/Providers/Qr/EndroidQrCodeWithLogoProvider.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ public function setLogo($path, $size = null)
2525
$this->logoSize = (array)$size;
2626
}
2727

28-
public function getQRCodeImage(string $qrtext, int $size): string
28+
public function getQRCodeImage(string $qrText, int $size): string
2929
{
3030
if (!$this->endroid4) {
31-
return $this->qrCodeInstance($qrtext, $size)->writeString();
31+
return $this->qrCodeInstance($qrText, $size)->writeString();
3232
}
3333

3434
$logo = null;
@@ -42,12 +42,12 @@ public function getQRCodeImage(string $qrtext, int $size): string
4242
}
4343
}
4444
$writer = new PngWriter();
45-
return $writer->write($this->qrCodeInstance($qrtext, $size), $logo)->getString();
45+
return $writer->write($this->qrCodeInstance($qrText, $size), $logo)->getString();
4646
}
4747

48-
protected function qrCodeInstance(string $qrtext, int $size): QrCode
48+
protected function qrCodeInstance(string $qrText, int $size): QrCode
4949
{
50-
$qrCode = parent::qrCodeInstance($qrtext, $size);
50+
$qrCode = parent::qrCodeInstance($qrText, $size);
5151

5252
if (!$this->endroid4 && $this->logoPath) {
5353
$qrCode->setLogoPath($this->logoPath);

lib/Providers/Qr/GoogleChartsQrCodeProvider.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,21 @@ public function getMimeType(): string
1616
return 'image/png';
1717
}
1818

19-
public function getQRCodeImage(string $qrtext, int $size): string
19+
public function getQRCodeImage(string $qrText, int $size): string
2020
{
21-
return $this->getContent($this->getUrl($qrtext, $size));
21+
return $this->getContent($this->getUrl($qrText, $size));
2222
}
2323

24-
public function getUrl(string $qrtext, int $size): string
24+
public function getUrl(string $qrText, int $size): string
2525
{
26-
return 'https://chart.googleapis.com/chart'
27-
. '?chs=' . $size . 'x' . $size
28-
. '&chld=' . urlencode(strtoupper($this->errorcorrectionlevel) . '|' . $this->margin)
29-
. '&cht=' . 'qr'
30-
. '&choe=' . $this->encoding
31-
. '&chl=' . rawurlencode($qrtext);
26+
$queryParameters = array(
27+
'chs' => $size . 'x' . $size,
28+
'chld' => strtoupper($this->errorcorrectionlevel) . '|' . $this->margin,
29+
'cht' => 'qr',
30+
'choe' => $this->encoding,
31+
'chl' => $qrText,
32+
);
33+
34+
return 'https://chart.googleapis.com/chart?' . http_build_query($queryParameters);
3235
}
3336
}

lib/Providers/Qr/HandlesDataUri.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
trait HandlesDataUri
1111
{
1212
/**
13-
* @return array<string, string>
13+
* @return array<string, string>|null
1414
*/
1515
private function DecodeDataUri(string $datauri): ?array
1616
{

lib/Providers/Qr/IQRCodeProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ interface IQRCodeProvider
99
/**
1010
* Generate and return the QR code to embed in a web page
1111
*
12-
* @param string $qrtext the value to encode in the QR code
12+
* @param string $qrText the value to encode in the QR code
1313
* @param int $size the desired size of the QR code
1414
*
1515
* @return string file contents of the QR code
1616
*/
17-
public function getQRCodeImage(string $qrtext, int $size): string;
17+
public function getQRCodeImage(string $qrText, int $size): string;
1818

1919
/**
2020
* Returns the appropriate mime type for the QR code

lib/Providers/Qr/ImageChartsQRCodeProvider.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,20 @@ public function getMimeType(): string
1818
return 'image/png';
1919
}
2020

21-
public function getQRCodeImage(string $qrtext, int $size): string
21+
public function getQRCodeImage(string $qrText, int $size): string
2222
{
23-
return $this->getContent($this->getUrl($qrtext, $size));
23+
return $this->getContent($this->getUrl($qrText, $size));
2424
}
2525

26-
public function getUrl(string $qrtext, int $size): string
26+
public function getUrl(string $qrText, int $size): string
2727
{
28-
return 'https://image-charts.com/chart?cht=qr'
29-
. '&chs=' . ceil($size / 2) . 'x' . ceil($size / 2)
30-
. '&chld=' . $this->errorcorrectionlevel . '|' . $this->margin
31-
. '&chl=' . rawurlencode($qrtext);
28+
$queryParameters = array(
29+
'cht' => 'qr',
30+
'chs' => ceil($size / 2) . 'x' . ceil($size / 2),
31+
'chld' => $this->errorcorrectionlevel . '|' . $this->margin,
32+
'chl' => $qrText,
33+
);
34+
35+
return 'https://image-charts.com/chart?' . http_build_query($queryParameters);
3236
}
3337
}

lib/Providers/Qr/QRServerProvider.php

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,25 @@ public function getMimeType(): string
3131
throw new QRException(sprintf('Unknown MIME-type: %s', $this->format));
3232
}
3333

34-
public function getQRCodeImage(string $qrtext, int $size): string
34+
public function getQRCodeImage(string $qrText, int $size): string
3535
{
36-
return $this->getContent($this->getUrl($qrtext, $size));
36+
return $this->getContent($this->getUrl($qrText, $size));
3737
}
3838

39-
public function getUrl(string $qrtext, int $size): string
39+
public function getUrl(string $qrText, int $size): string
4040
{
41-
return 'https://api.qrserver.com/v1/create-qr-code/'
42-
. '?size=' . $size . 'x' . $size
43-
. '&ecc=' . strtoupper($this->errorcorrectionlevel)
44-
. '&margin=' . $this->margin
45-
. '&qzone=' . $this->qzone
46-
. '&bgcolor=' . $this->decodeColor($this->bgcolor)
47-
. '&color=' . $this->decodeColor($this->color)
48-
. '&format=' . strtolower($this->format)
49-
. '&data=' . rawurlencode($qrtext);
41+
$queryParameters = array(
42+
'size' => $size . 'x' . $size,
43+
'ecc' => strtoupper($this->errorcorrectionlevel),
44+
'margin' => $this->margin,
45+
'qzone' => $this->qzone,
46+
'bgcolor' => $this->decodeColor($this->bgcolor),
47+
'color' => $this->decodeColor($this->color),
48+
'format' => strtolower($this->format),
49+
'data' => $qrText,
50+
);
51+
52+
return 'https://api.qrserver.com/v1/create-qr-code/?' . http_build_query($queryParameters);
5053
}
5154

5255
private function decodeColor(string $value): string

lib/Providers/Qr/QRicketProvider.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,22 @@ public function getMimeType(): string
2727
throw new QRException(sprintf('Unknown MIME-type: %s', $this->format));
2828
}
2929

30-
public function getQRCodeImage(string $qrtext, int $size): string
30+
public function getQRCodeImage(string $qrText, int $size): string
3131
{
32-
return $this->getContent($this->getUrl($qrtext, $size));
32+
return $this->getContent($this->getUrl($qrText, $size));
3333
}
3434

35-
public function getUrl(string $qrtext, int $size): string
35+
public function getUrl(string $qrText, int $size): string
3636
{
37-
return 'http://qrickit.com/api/qr'
38-
. '?qrsize=' . (string) $size
39-
. '&e=' . strtolower($this->errorcorrectionlevel)
40-
. '&bgdcolor=' . $this->bgcolor
41-
. '&fgdcolor=' . $this->color
42-
. '&t=' . strtolower($this->format)
43-
. '&d=' . rawurlencode($qrtext);
37+
$queryParameters = array(
38+
'qrsize' => $size,
39+
'e' => strtolower($this->errorcorrectionlevel),
40+
'bgdcolor' => $this->bgcolor,
41+
'fgdcolor' => $this->color,
42+
't' => strtolower($this->format),
43+
'd' => $qrText,
44+
);
45+
46+
return 'http://qrickit.com/api/qr?' . http_build_query($queryParameters);
4447
}
4548
}

lib/Providers/Rng/HashRNGProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
class HashRNGProvider implements IRNGProvider
1010
{
11-
public function __construct(private string $algorithm = 'sha256')
11+
public function __construct(private readonly string $algorithm = 'sha256')
1212
{
1313
$algos = array_values(hash_algos());
1414
if (!in_array($this->algorithm, $algos, true)) {

0 commit comments

Comments
 (0)