Skip to content

Commit e7316eb

Browse files
committed
Code cleanup and added UnknownTypeException
1 parent 4992416 commit e7316eb

File tree

6 files changed

+39
-25
lines changed

6 files changed

+39
-25
lines changed

src/BarcodeGenerator.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use Picqer\Barcode\Exceptions\InvalidCheckDigitException;
3535
use Picqer\Barcode\Exceptions\InvalidFormatException;
3636
use Picqer\Barcode\Exceptions\InvalidLengthException;
37+
use Picqer\Barcode\Exceptions\UnknownTypeException;
3738

3839
abstract class BarcodeGenerator
3940
{
@@ -200,12 +201,14 @@ protected function getBarcodeData($code, $type)
200201
break;
201202
}
202203
default: {
203-
$arrcode = false;
204+
throw new UnknownTypeException();
204205
break;
205206
}
206207
}
207208

208-
$arrcode = $this->convertBarcodeArrayToNewStyle($arrcode);
209+
if ( ! isset($arrcode['maxWidth'])) {
210+
$arrcode = $this->convertBarcodeArrayToNewStyle($arrcode);
211+
}
209212

210213
return $arrcode;
211214
}
@@ -517,8 +520,8 @@ protected function checksum_code39($code)
517520
'%'
518521
);
519522
$sum = 0;
520-
$clen = strlen($code);
521-
for ($i = 0; $i < $clen; ++$i) {
523+
$codelength = strlen($code);
524+
for ($i = 0; $i < $codelength; ++$i) {
522525
$k = array_keys($chars, $code{$i});
523526
$sum += $k[0];
524527
}
@@ -1429,7 +1432,7 @@ protected function get128ABsequence($code)
14291432
$sequence = array();
14301433
// get A sequences (if any)
14311434
$numseq = array();
1432-
preg_match_all('/([\0-\31])/', $code, $numseq, PREG_OFFSET_CAPTURE);
1435+
preg_match_all('/([\x00-\x1f])/', $code, $numseq, PREG_OFFSET_CAPTURE);
14331436
if (isset($numseq[1]) AND ! empty($numseq[1])) {
14341437
$end_offset = 0;
14351438
foreach ($numseq[1] as $val) {
@@ -2690,19 +2693,17 @@ protected function barcode_imb_pre($code)
26902693
* @param $number (string) number to convert specified as a string
26912694
* @return string hexadecimal representation
26922695
*/
2693-
public function dec_to_hex($number)
2696+
protected function dec_to_hex($number)
26942697
{
2695-
$hex = array();
26962698
if ($number == 0) {
26972699
return '00';
26982700
}
2701+
2702+
$hex = [];
2703+
26992704
while ($number > 0) {
2700-
if ($number == 0) {
2701-
array_push($hex, '0');
2702-
} else {
2703-
array_push($hex, strtoupper(dechex(bcmod($number, '16'))));
2704-
$number = bcdiv($number, '16', 0);
2705-
}
2705+
array_push($hex, strtoupper(dechex(bcmod($number, '16'))));
2706+
$number = bcdiv($number, '16', 0);
27062707
}
27072708
$hex = array_reverse($hex);
27082709

@@ -2716,7 +2717,7 @@ public function dec_to_hex($number)
27162717
* @param $hex (string) hexadecimal number to convert specified as a string
27172718
* @return string hexadecimal representation
27182719
*/
2719-
public function hex_to_dec($hex)
2720+
protected function hex_to_dec($hex)
27202721
{
27212722
$dec = 0;
27222723
$bitval = 1;

src/BarcodeGeneratorHTML.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@ class BarcodeGeneratorHTML extends BarcodeGenerator
1212
* @param string $type type of barcode
1313
* @param int $widthFactor Width of a single bar element in pixels.
1414
* @param int $totalHeight Height of a single bar element in pixels.
15-
* @param int $color Foreground color for bar elements (background is transparent).
15+
* @param int|string $color Foreground color for bar elements (background is transparent).
1616
* @return string HTML code.
1717
* @public
1818
*/
1919
public function getBarcode($code, $type, $widthFactor = 2, $totalHeight = 30, $color = 'black')
2020
{
21-
2221
$barcodeData = $this->getBarcodeData($code, $type);
2322

2423
$html = '<div style="font-size:0;position:relative;width:' . ($barcodeData['maxWidth'] * $widthFactor) . 'px;height:' . ($totalHeight) . 'px;">' . "\n";

src/BarcodeGeneratorJPG.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function getBarcode($code, $type, $widthFactor = 2, $totalHeight = 30, $c
3737
$jpg = new \Imagick();
3838
$jpg->newImage($width, $height, 'none', 'jpg');
3939
$imageMagickObject = new \imagickdraw();
40-
$imageMagickObject->setfillcolor($colorForeground);
40+
$imageMagickObject->setFillColor($colorForeground);
4141
} else {
4242
return false;
4343
}
@@ -50,7 +50,7 @@ public function getBarcode($code, $type, $widthFactor = 2, $totalHeight = 30, $c
5050
if ($bar['drawBar']) {
5151
$y = round(($bar['positionVertical'] * $totalHeight / $barcodeData['maxHeight']), 3);
5252
// draw a vertical bar
53-
if ($imagick) {
53+
if ($imagick && isset($imageMagickObject)) {
5454
$imageMagickObject->rectangle($positionHorizontal, $y, ($positionHorizontal + $bw), ($y + $bh));
5555
} else {
5656
imagefilledrectangle($jpg, $positionHorizontal, $y, ($positionHorizontal + $bw) - 1, ($y + $bh),
@@ -60,8 +60,8 @@ public function getBarcode($code, $type, $widthFactor = 2, $totalHeight = 30, $c
6060
$positionHorizontal += $bw;
6161
}
6262
ob_start();
63-
if ($imagick) {
64-
$jpg->drawimage($imageMagickObject);
63+
if ($imagick && isset($imageMagickObject)) {
64+
$jpg->drawImage($imageMagickObject);
6565
echo $jpg;
6666
} else {
6767
imagejpeg($jpg);

src/BarcodeGeneratorPNG.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function getBarcode($code, $type, $widthFactor = 2, $totalHeight = 30, $c
3737
$png = new \Imagick();
3838
$png->newImage($width, $height, 'none', 'png');
3939
$imageMagickObject = new \imagickdraw();
40-
$imageMagickObject->setfillcolor($colorForeground);
40+
$imageMagickObject->setFillColor($colorForeground);
4141
} else {
4242
return false;
4343
}
@@ -50,7 +50,7 @@ public function getBarcode($code, $type, $widthFactor = 2, $totalHeight = 30, $c
5050
if ($bar['drawBar']) {
5151
$y = round(($bar['positionVertical'] * $totalHeight / $barcodeData['maxHeight']), 3);
5252
// draw a vertical bar
53-
if ($imagick) {
53+
if ($imagick && isset($imageMagickObject)) {
5454
$imageMagickObject->rectangle($positionHorizontal, $y, ($positionHorizontal + $bw), ($y + $bh));
5555
} else {
5656
imagefilledrectangle($png, $positionHorizontal, $y, ($positionHorizontal + $bw) - 1, ($y + $bh),
@@ -60,8 +60,8 @@ public function getBarcode($code, $type, $widthFactor = 2, $totalHeight = 30, $c
6060
$positionHorizontal += $bw;
6161
}
6262
ob_start();
63-
if ($imagick) {
64-
$png->drawimage($imageMagickObject);
63+
if ($imagick && isset($imageMagickObject)) {
64+
$png->drawImage($imageMagickObject);
6565
echo $png;
6666
} else {
6767
imagepng($png);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
namespace Picqer\Barcode\Exceptions;
4+
5+
class UnknownTypeException extends BarcodeException {}

tests/BarcodeTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ public function ean13_generator_accepting_11_chars_and_generates_13th_check_digi
8787
$this->assertStringEqualsFile('tests/verified-files/0049000004632-ean13.svg', $generated);
8888
}
8989

90-
9190
/**
9291
* @test
9392
* @expectedException \Picqer\Barcode\Exceptions\InvalidCheckDigitException
@@ -97,4 +96,14 @@ public function ean13_generator_throws_exception_when_wrong_check_digit_is_given
9796
$generator = new Picqer\Barcode\BarcodeGeneratorSVG();
9897
$generator->getBarcode('0049000004633', $generator::TYPE_EAN_13);
9998
}
99+
100+
/**
101+
* @test
102+
* @expectedException \Picqer\Barcode\Exceptions\UnknownTypeException
103+
*/
104+
public function generator_throws_unknown_type_exceptions()
105+
{
106+
$generator = new Picqer\Barcode\BarcodeGeneratorSVG();
107+
$generator->getBarcode('0049000004633', 'vladimir');
108+
}
100109
}

0 commit comments

Comments
 (0)