Skip to content

Commit 2d1ec8d

Browse files
committed
🐛 be more specific with hex colour handling
1 parent ae4da10 commit 2d1ec8d

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

lib/Providers/Qr/BaconQrCodeProvider.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,19 @@ private function handleColour($colour)
125125
{
126126
if (is_string($colour) && $colour[0] == '#') {
127127
$hexToRGB = function ($input) {
128+
// ensure input no longer has a # for more predictable division
129+
// PHP 8.1 does not like implicitly casting a float to an int
130+
$input = trim($input, '#');
131+
132+
if (strlen($input) != 3 && strlen($input) != 6) {
133+
throw new \RuntimeException('Colour should be a 3 or 6 character value after the #');
134+
}
135+
128136
// split the array into three chunks
129-
$split = str_split(trim($input, '#'), strlen($input) / 3);
137+
$split = str_split($input, strlen($input) / 3);
130138

131139
// cope with three character hex reference
132-
// three characters plus a # = 4
133-
if (strlen($input) == 4) {
140+
if (strlen($input) == 3) {
134141
array_walk($split, function (&$character) {
135142
$character = str_repeat($character, 2);
136143
});

testsDependency/BaconQRCodeTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,20 @@ public function testBadBackgroundColour()
3434

3535
new BaconQrCodeProvider(1, '#000', 'not-a-colour');
3636
}
37+
38+
public function testBadTextColourHexRef()
39+
{
40+
$this->expectException(\RuntimeException::class);
41+
42+
new BaconQrCodeProvider(1, '#AAAA', '#FFF');
43+
}
44+
45+
public function testBadBackgroundColourHexRef()
46+
{
47+
$this->expectException(\RuntimeException::class);
48+
49+
new BaconQrCodeProvider(1, '#000', '#AAAA');
50+
}
51+
52+
3753
}

0 commit comments

Comments
 (0)