Skip to content

Commit 460f3ee

Browse files
committed
More typehints
1 parent ec4502f commit 460f3ee

32 files changed

+70
-53
lines changed

.github/FUNDING.yml

-1
This file was deleted.

src/Barcode.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function __construct(string $barcode)
1414
$this->barcode = $barcode;
1515
}
1616

17-
public function addBar(BarcodeBar $bar)
17+
public function addBar(BarcodeBar $bar): void
1818
{
1919
$this->bars[] = $bar;
2020
$this->width += $bar->getWidth();

src/BarcodeBar.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Picqer\Barcode;
44

5-
class BarcodeBar
5+
readonly class BarcodeBar
66
{
77
protected int $width;
88
protected int $height;

src/BarcodeGenerator.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
use Picqer\Barcode\Types\TypeEan13;
4646
use Picqer\Barcode\Types\TypeEan8;
4747
use Picqer\Barcode\Types\TypeIntelligentMailBarcode;
48+
use Picqer\Barcode\Types\TypeInterface;
4849
use Picqer\Barcode\Types\TypeInterleaved25;
4950
use Picqer\Barcode\Types\TypeInterleaved25Checksum;
5051
use Picqer\Barcode\Types\TypeITF14;
@@ -101,14 +102,17 @@ abstract class BarcodeGenerator
101102
const TYPE_PHARMA_CODE = 'PHARMA';
102103
const TYPE_PHARMA_CODE_TWO_TRACKS = 'PHARMA2T';
103104

105+
/**
106+
* @throws UnknownTypeException
107+
*/
104108
protected function getBarcodeData(string $code, string $type): Barcode
105109
{
106110
$barcodeDataBuilder = $this->createDataBuilderForType($type);
107111

108112
return $barcodeDataBuilder->getBarcode($code);
109113
}
110114

111-
protected function createDataBuilderForType(string $type)
115+
protected function createDataBuilderForType(string $type): TypeInterface
112116
{
113117
switch (strtoupper($type)) {
114118
case self::TYPE_CODE_32:

src/BarcodeGeneratorDynamicHTML.php

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Picqer\Barcode;
44

5+
use Picqer\Barcode\Exceptions\UnknownTypeException;
6+
57
class BarcodeGeneratorDynamicHTML extends BarcodeGenerator
68
{
79
/**
@@ -12,6 +14,7 @@ class BarcodeGeneratorDynamicHTML extends BarcodeGenerator
1214
* @param BarcodeGenerator::TYPE_* $type (string) type of barcode
1315
* @param string $foregroundColor Foreground color for bar elements as '#333' or 'orange' for example (background is transparent).
1416
* @return string HTML code.
17+
* @throws UnknownTypeException
1518
*/
1619
public function getBarcode(string $barcode, $type, string $foregroundColor = 'black'): string
1720
{

src/BarcodeGeneratorHTML.php

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Picqer\Barcode;
44

5+
use Picqer\Barcode\Exceptions\UnknownTypeException;
6+
57
class BarcodeGeneratorHTML extends BarcodeGenerator
68
{
79
/**
@@ -14,6 +16,7 @@ class BarcodeGeneratorHTML extends BarcodeGenerator
1416
* @param int $height Height of a single bar element in pixels.
1517
* @param string $foregroundColor Foreground color for bar elements as '#333' or 'orange' for example (background is transparent).
1618
* @return string HTML code.
19+
* @throws UnknownTypeException
1720
*/
1821
public function getBarcode(string $barcode, $type, int $widthFactor = 2, int $height = 30, string $foregroundColor = 'black'): string
1922
{

src/BarcodeGeneratorJPG.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Picqer\Barcode;
44

5+
use Picqer\Barcode\Exceptions\UnknownTypeException;
6+
57
class BarcodeGeneratorJPG extends BarcodeGenerator
68
{
79
protected ?bool $useImagick = null;
@@ -15,6 +17,7 @@ class BarcodeGeneratorJPG extends BarcodeGenerator
1517
* @param int $height Height of a single bar element in pixels.
1618
* @param array $foregroundColor RGB (0-255) foreground color for bar elements (background is transparent).
1719
* @return string image data or false in case of error.
20+
* @throws UnknownTypeException
1821
*/
1922
public function getBarcode(string $barcode, $type, int $widthFactor = 2, int $height = 30, array $foregroundColor = [0, 0, 0]): string
2023
{
@@ -37,15 +40,15 @@ public function getBarcode(string $barcode, $type, int $widthFactor = 2, int $he
3740
/**
3841
* Force the use of Imagick image extension
3942
*/
40-
public function useImagick()
43+
public function useImagick(): void
4144
{
4245
$this->useImagick = true;
4346
}
4447

4548
/**
4649
* Force the use of the GD image library
4750
*/
48-
public function useGd()
51+
public function useGd(): void
4952
{
5053
$this->useImagick = false;
5154
}

src/BarcodeGeneratorPNG.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Picqer\Barcode;
44

5+
use Picqer\Barcode\Exceptions\UnknownTypeException;
6+
57
class BarcodeGeneratorPNG extends BarcodeGenerator
68
{
79
protected ?bool $useImagick = null;
@@ -15,6 +17,7 @@ class BarcodeGeneratorPNG extends BarcodeGenerator
1517
* @param int $height Height of a single bar element in pixels.
1618
* @param array $foregroundColor RGB (0-255) foreground color for bar elements (background is transparent).
1719
* @return string image data or false in case of error.
20+
* @throws UnknownTypeException
1821
*/
1922
public function getBarcode(string $barcode, $type, int $widthFactor = 2, int $height = 30, array $foregroundColor = [0, 0, 0]): string
2023
{
@@ -37,15 +40,15 @@ public function getBarcode(string $barcode, $type, int $widthFactor = 2, int $he
3740
/**
3841
* Force the use of Imagick image extension
3942
*/
40-
public function useImagick()
43+
public function useImagick(): void
4144
{
4245
$this->useImagick = true;
4346
}
4447

4548
/**
4649
* Force the use of the GD image library
4750
*/
48-
public function useGd()
51+
public function useGd(): void
4952
{
5053
$this->useImagick = false;
5154
}

src/BarcodeGeneratorSVG.php

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Picqer\Barcode;
44

5+
use Picqer\Barcode\Exceptions\UnknownTypeException;
6+
57
class BarcodeGeneratorSVG extends BarcodeGenerator
68
{
79
/**
@@ -14,6 +16,7 @@ class BarcodeGeneratorSVG extends BarcodeGenerator
1416
* @param $foregroundColor (string) Foreground color (in SVG format) for bar elements (background is transparent).
1517
* @return string SVG code.
1618
* @public
19+
* @throws UnknownTypeException
1720
*/
1821
public function getBarcode(string $barcode, $type, float $widthFactor = 2, float $height = 30, string $foregroundColor = 'black'): string
1922
{

src/Types/TypeCodabar.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
class TypeCodabar implements TypeInterface
1515
{
16-
protected $conversionTable = [
16+
protected array $conversionTable = [
1717
'0' => '11111221',
1818
'1' => '11112211',
1919
'2' => '11121121',

src/Types/TypeCode11.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
class TypeCode11 implements TypeInterface
1515
{
16-
protected $conversionTable = [
16+
protected array $conversionTable = [
1717
'0' => '111121',
1818
'1' => '211121',
1919
'2' => '121121',

src/Types/TypeCode128.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818

1919
class TypeCode128 implements TypeInterface
2020
{
21-
protected $type = null;
21+
protected ?string $type = null;
2222

23-
protected $conversionTable = [
23+
protected array $conversionTable = [
2424
'212222', /* 00 */
2525
'222122', /* 01 */
2626
'222221', /* 02 */
@@ -383,7 +383,7 @@ public function getBarcode(string $code): Barcode
383383
* @return array sequence
384384
* @protected
385385
*/
386-
protected function get128ABsequence($code)
386+
protected function get128ABsequence($code): array
387387
{
388388
$len = strlen($code);
389389
$sequence = [];

src/Types/TypeCode128A.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212

1313
class TypeCode128A extends TypeCode128
1414
{
15-
protected $type = 'A';
15+
protected ?string $type = 'A';
1616
}

src/Types/TypeCode128B.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212

1313
class TypeCode128B extends TypeCode128
1414
{
15-
protected $type = 'B';
15+
protected ?string $type = 'B';
1616
}

src/Types/TypeCode128C.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212

1313
class TypeCode128C extends TypeCode128
1414
{
15-
protected $type = 'C';
15+
protected ?string $type = 'C';
1616
}

src/Types/TypeCode32.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*/
1414
class TypeCode32 extends TypeCode39
1515
{
16-
protected $conversionTable32 = [
16+
protected array $conversionTable32 = [
1717
'0' => '0',
1818
'1' => '1',
1919
'2' => '2',

src/Types/TypeCode39.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414

1515
class TypeCode39 implements TypeInterface
1616
{
17-
protected $extended = false;
18-
protected $checksum = false;
17+
protected bool $extended = false;
18+
protected bool $checksum = false;
1919

20-
protected $conversionTable = [
20+
protected array $conversionTable = [
2121
'0' => '111331311',
2222
'1' => '311311113',
2323
'2' => '113311113',
@@ -113,10 +113,11 @@ public function getBarcode(string $code): Barcode
113113
* Encode a string to be used for CODE 39 Extended mode.
114114
*
115115
* @param string $code code to represent.
116-
* @return bool|string encoded string.
116+
* @return string encoded string.
117117
* @protected
118+
* @throws InvalidCharacterException
118119
*/
119-
protected function encode_code39_ext($code)
120+
protected function encode_code39_ext(string $code): string
120121
{
121122
$encode = [
122123
chr(0) => '%U',

src/Types/TypeCode39Checksum.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99

1010
class TypeCode39Checksum extends TypeCode39
1111
{
12-
protected $extended = false;
13-
protected $checksum = true;
12+
protected bool $extended = false;
13+
protected bool $checksum = true;
1414
}

src/Types/TypeCode39Extended.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99

1010
class TypeCode39Extended extends TypeCode39
1111
{
12-
protected $extended = true;
13-
protected $checksum = false;
12+
protected bool $extended = true;
13+
protected bool $checksum = false;
1414
}

src/Types/TypeCode39ExtendedChecksum.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99

1010
class TypeCode39ExtendedChecksum extends TypeCode39
1111
{
12-
protected $extended = true;
13-
protected $checksum = true;
12+
protected bool $extended = true;
13+
protected bool $checksum = true;
1414
}

src/Types/TypeCode93.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
class TypeCode93 implements TypeInterface
1717
{
18-
protected $conversionTable = [
18+
protected array $conversionTable = [
1919
48 => '131112', // 0
2020
49 => '111213', // 1
2121
50 => '111312', // 2
@@ -246,7 +246,7 @@ public function getBarcode(string $code): Barcode
246246
* @return string checksum code.
247247
* @protected
248248
*/
249-
protected function checksum_code93($code)
249+
protected function checksum_code93(string $code): string
250250
{
251251
$chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '-', '.', ' ', '$', '/', '+', '%', 'a', 'b', 'c', 'd'];
252252

@@ -280,8 +280,6 @@ protected function checksum_code93($code)
280280
$check %= 47;
281281
$k = $chars[$check];
282282

283-
$checksum = $c . $k;
284-
285-
return $checksum;
283+
return $c . $k;
286284
}
287285
}

src/Types/TypeEanUpcBase.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ protected function calculateChecksumDigit(string $code): int
211211
// calculate check digit
212212
$sum_a = 0;
213213
for ($i = 1; $i < $this->length - 1; $i += 2) {
214-
$sum_a += $code[$i];
214+
$sum_a += intval($code[$i]);
215215
}
216216
if ($this->length > 12) {
217217
$sum_a *= 3;

src/Types/TypeInterleaved25Checksum.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@ protected function getChecksum(string $code): string
7575
$len = strlen($code);
7676
$sum = 0;
7777
for ($i = 0; $i < $len; $i += 2) {
78-
$sum += $code[$i];
78+
$sum += intval($code[$i]);
7979
}
8080
$sum *= 3;
8181
for ($i = 1; $i < $len; $i += 2) {
82-
$sum += ($code[$i]);
82+
$sum += intval($code[$i]);
8383
}
8484
$r = $sum % 10;
8585
if ($r > 0) {

src/Types/TypeMsi.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@
1313

1414
class TypeMsi extends TypeMsiChecksum
1515
{
16-
protected $checksum = false;
16+
protected bool $checksum = false;
1717
}

src/Types/TypeMsiChecksum.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
class TypeMsiChecksum implements TypeInterface
1919
{
20-
protected $checksum = true;
20+
protected bool $checksum = true;
2121

2222
public function getBarcode(string $code): Barcode
2323
{

src/Types/TypePlanet.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
class TypePlanet extends TypePostnet
1515
{
16-
protected $barlen = [
16+
protected array $barlen = [
1717
0 => [1, 1, 2, 2, 2],
1818
1 => [2, 2, 2, 1, 1],
1919
2 => [2, 2, 1, 2, 1],

src/Types/TypePostnet.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
class TypePostnet implements TypeInterface
1818
{
19-
protected $barlen = [
19+
protected array $barlen = [
2020
0 => [2, 2, 1, 1, 1],
2121
1 => [1, 1, 1, 2, 2],
2222
2 => [1, 1, 2, 1, 2],

0 commit comments

Comments
 (0)