diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml deleted file mode 100644 index a8ff52a..0000000 --- a/.github/FUNDING.yml +++ /dev/null @@ -1 +0,0 @@ -github: casperbakker diff --git a/src/Barcode.php b/src/Barcode.php index dbb27f1..a39b382 100644 --- a/src/Barcode.php +++ b/src/Barcode.php @@ -14,7 +14,7 @@ public function __construct(string $barcode) $this->barcode = $barcode; } - public function addBar(BarcodeBar $bar) + public function addBar(BarcodeBar $bar): void { $this->bars[] = $bar; $this->width += $bar->getWidth(); diff --git a/src/BarcodeBar.php b/src/BarcodeBar.php index 61a415f..58a881f 100644 --- a/src/BarcodeBar.php +++ b/src/BarcodeBar.php @@ -2,7 +2,7 @@ namespace Picqer\Barcode; -class BarcodeBar +readonly class BarcodeBar { protected int $width; protected int $height; diff --git a/src/BarcodeGenerator.php b/src/BarcodeGenerator.php index 9dd7f76..8555b06 100644 --- a/src/BarcodeGenerator.php +++ b/src/BarcodeGenerator.php @@ -45,6 +45,7 @@ use Picqer\Barcode\Types\TypeEan13; use Picqer\Barcode\Types\TypeEan8; use Picqer\Barcode\Types\TypeIntelligentMailBarcode; +use Picqer\Barcode\Types\TypeInterface; use Picqer\Barcode\Types\TypeInterleaved25; use Picqer\Barcode\Types\TypeInterleaved25Checksum; use Picqer\Barcode\Types\TypeITF14; @@ -101,6 +102,9 @@ abstract class BarcodeGenerator const TYPE_PHARMA_CODE = 'PHARMA'; const TYPE_PHARMA_CODE_TWO_TRACKS = 'PHARMA2T'; + /** + * @throws UnknownTypeException + */ protected function getBarcodeData(string $code, string $type): Barcode { $barcodeDataBuilder = $this->createDataBuilderForType($type); @@ -108,7 +112,7 @@ protected function getBarcodeData(string $code, string $type): Barcode return $barcodeDataBuilder->getBarcode($code); } - protected function createDataBuilderForType(string $type) + protected function createDataBuilderForType(string $type): TypeInterface { switch (strtoupper($type)) { case self::TYPE_CODE_32: diff --git a/src/BarcodeGeneratorDynamicHTML.php b/src/BarcodeGeneratorDynamicHTML.php index 66845a2..c08a4ed 100644 --- a/src/BarcodeGeneratorDynamicHTML.php +++ b/src/BarcodeGeneratorDynamicHTML.php @@ -2,6 +2,8 @@ namespace Picqer\Barcode; +use Picqer\Barcode\Exceptions\UnknownTypeException; + class BarcodeGeneratorDynamicHTML extends BarcodeGenerator { /** @@ -12,6 +14,7 @@ class BarcodeGeneratorDynamicHTML extends BarcodeGenerator * @param BarcodeGenerator::TYPE_* $type (string) type of barcode * @param string $foregroundColor Foreground color for bar elements as '#333' or 'orange' for example (background is transparent). * @return string HTML code. + * @throws UnknownTypeException */ public function getBarcode(string $barcode, $type, string $foregroundColor = 'black'): string { diff --git a/src/BarcodeGeneratorHTML.php b/src/BarcodeGeneratorHTML.php index c38a723..bc30f25 100644 --- a/src/BarcodeGeneratorHTML.php +++ b/src/BarcodeGeneratorHTML.php @@ -2,6 +2,8 @@ namespace Picqer\Barcode; +use Picqer\Barcode\Exceptions\UnknownTypeException; + class BarcodeGeneratorHTML extends BarcodeGenerator { /** @@ -14,6 +16,7 @@ class BarcodeGeneratorHTML extends BarcodeGenerator * @param int $height Height of a single bar element in pixels. * @param string $foregroundColor Foreground color for bar elements as '#333' or 'orange' for example (background is transparent). * @return string HTML code. + * @throws UnknownTypeException */ public function getBarcode(string $barcode, $type, int $widthFactor = 2, int $height = 30, string $foregroundColor = 'black'): string { diff --git a/src/BarcodeGeneratorJPG.php b/src/BarcodeGeneratorJPG.php index ba89b14..2e27001 100644 --- a/src/BarcodeGeneratorJPG.php +++ b/src/BarcodeGeneratorJPG.php @@ -2,6 +2,8 @@ namespace Picqer\Barcode; +use Picqer\Barcode\Exceptions\UnknownTypeException; + class BarcodeGeneratorJPG extends BarcodeGenerator { protected ?bool $useImagick = null; @@ -15,6 +17,7 @@ class BarcodeGeneratorJPG extends BarcodeGenerator * @param int $height Height of a single bar element in pixels. * @param array $foregroundColor RGB (0-255) foreground color for bar elements (background is transparent). * @return string image data or false in case of error. + * @throws UnknownTypeException */ public function getBarcode(string $barcode, $type, int $widthFactor = 2, int $height = 30, array $foregroundColor = [0, 0, 0]): string { @@ -37,7 +40,7 @@ public function getBarcode(string $barcode, $type, int $widthFactor = 2, int $he /** * Force the use of Imagick image extension */ - public function useImagick() + public function useImagick(): void { $this->useImagick = true; } @@ -45,7 +48,7 @@ public function useImagick() /** * Force the use of the GD image library */ - public function useGd() + public function useGd(): void { $this->useImagick = false; } diff --git a/src/BarcodeGeneratorPNG.php b/src/BarcodeGeneratorPNG.php index f2c0b39..31edc56 100644 --- a/src/BarcodeGeneratorPNG.php +++ b/src/BarcodeGeneratorPNG.php @@ -2,6 +2,8 @@ namespace Picqer\Barcode; +use Picqer\Barcode\Exceptions\UnknownTypeException; + class BarcodeGeneratorPNG extends BarcodeGenerator { protected ?bool $useImagick = null; @@ -15,6 +17,7 @@ class BarcodeGeneratorPNG extends BarcodeGenerator * @param int $height Height of a single bar element in pixels. * @param array $foregroundColor RGB (0-255) foreground color for bar elements (background is transparent). * @return string image data or false in case of error. + * @throws UnknownTypeException */ public function getBarcode(string $barcode, $type, int $widthFactor = 2, int $height = 30, array $foregroundColor = [0, 0, 0]): string { @@ -37,7 +40,7 @@ public function getBarcode(string $barcode, $type, int $widthFactor = 2, int $he /** * Force the use of Imagick image extension */ - public function useImagick() + public function useImagick(): void { $this->useImagick = true; } @@ -45,7 +48,7 @@ public function useImagick() /** * Force the use of the GD image library */ - public function useGd() + public function useGd(): void { $this->useImagick = false; } diff --git a/src/BarcodeGeneratorSVG.php b/src/BarcodeGeneratorSVG.php index aab6a15..ef7d63f 100644 --- a/src/BarcodeGeneratorSVG.php +++ b/src/BarcodeGeneratorSVG.php @@ -2,6 +2,8 @@ namespace Picqer\Barcode; +use Picqer\Barcode\Exceptions\UnknownTypeException; + class BarcodeGeneratorSVG extends BarcodeGenerator { /** @@ -14,6 +16,7 @@ class BarcodeGeneratorSVG extends BarcodeGenerator * @param $foregroundColor (string) Foreground color (in SVG format) for bar elements (background is transparent). * @return string SVG code. * @public + * @throws UnknownTypeException */ public function getBarcode(string $barcode, $type, float $widthFactor = 2, float $height = 30, string $foregroundColor = 'black'): string { diff --git a/src/Types/TypeCodabar.php b/src/Types/TypeCodabar.php index 62c78f8..e782dc4 100644 --- a/src/Types/TypeCodabar.php +++ b/src/Types/TypeCodabar.php @@ -13,7 +13,7 @@ class TypeCodabar implements TypeInterface { - protected $conversionTable = [ + protected array $conversionTable = [ '0' => '11111221', '1' => '11112211', '2' => '11121121', diff --git a/src/Types/TypeCode11.php b/src/Types/TypeCode11.php index 1886364..ab469bd 100644 --- a/src/Types/TypeCode11.php +++ b/src/Types/TypeCode11.php @@ -13,7 +13,7 @@ class TypeCode11 implements TypeInterface { - protected $conversionTable = [ + protected array $conversionTable = [ '0' => '111121', '1' => '211121', '2' => '121121', diff --git a/src/Types/TypeCode128.php b/src/Types/TypeCode128.php index 3a6242d..ecc084e 100644 --- a/src/Types/TypeCode128.php +++ b/src/Types/TypeCode128.php @@ -18,9 +18,9 @@ class TypeCode128 implements TypeInterface { - protected $type = null; + protected ?string $type = null; - protected $conversionTable = [ + protected array $conversionTable = [ '212222', /* 00 */ '222122', /* 01 */ '222221', /* 02 */ @@ -383,7 +383,7 @@ public function getBarcode(string $code): Barcode * @return array sequence * @protected */ - protected function get128ABsequence($code) + protected function get128ABsequence($code): array { $len = strlen($code); $sequence = []; diff --git a/src/Types/TypeCode128A.php b/src/Types/TypeCode128A.php index ff2fe0c..78bb46e 100644 --- a/src/Types/TypeCode128A.php +++ b/src/Types/TypeCode128A.php @@ -12,5 +12,5 @@ class TypeCode128A extends TypeCode128 { - protected $type = 'A'; + protected ?string $type = 'A'; } diff --git a/src/Types/TypeCode128B.php b/src/Types/TypeCode128B.php index b462863..6dbc0d8 100644 --- a/src/Types/TypeCode128B.php +++ b/src/Types/TypeCode128B.php @@ -12,5 +12,5 @@ class TypeCode128B extends TypeCode128 { - protected $type = 'B'; + protected ?string $type = 'B'; } diff --git a/src/Types/TypeCode128C.php b/src/Types/TypeCode128C.php index f4d4460..82566a2 100644 --- a/src/Types/TypeCode128C.php +++ b/src/Types/TypeCode128C.php @@ -12,5 +12,5 @@ class TypeCode128C extends TypeCode128 { - protected $type = 'C'; + protected ?string $type = 'C'; } diff --git a/src/Types/TypeCode32.php b/src/Types/TypeCode32.php index 5c50395..54c9bde 100644 --- a/src/Types/TypeCode32.php +++ b/src/Types/TypeCode32.php @@ -13,7 +13,7 @@ */ class TypeCode32 extends TypeCode39 { - protected $conversionTable32 = [ + protected array $conversionTable32 = [ '0' => '0', '1' => '1', '2' => '2', diff --git a/src/Types/TypeCode39.php b/src/Types/TypeCode39.php index 53cbaf9..2be9cc3 100644 --- a/src/Types/TypeCode39.php +++ b/src/Types/TypeCode39.php @@ -14,10 +14,10 @@ class TypeCode39 implements TypeInterface { - protected $extended = false; - protected $checksum = false; + protected bool $extended = false; + protected bool $checksum = false; - protected $conversionTable = [ + protected array $conversionTable = [ '0' => '111331311', '1' => '311311113', '2' => '113311113', @@ -113,10 +113,11 @@ public function getBarcode(string $code): Barcode * Encode a string to be used for CODE 39 Extended mode. * * @param string $code code to represent. - * @return bool|string encoded string. + * @return string encoded string. * @protected + * @throws InvalidCharacterException */ - protected function encode_code39_ext($code) + protected function encode_code39_ext(string $code): string { $encode = [ chr(0) => '%U', diff --git a/src/Types/TypeCode39Checksum.php b/src/Types/TypeCode39Checksum.php index b129f28..7a47bb6 100644 --- a/src/Types/TypeCode39Checksum.php +++ b/src/Types/TypeCode39Checksum.php @@ -9,6 +9,6 @@ class TypeCode39Checksum extends TypeCode39 { - protected $extended = false; - protected $checksum = true; + protected bool $extended = false; + protected bool $checksum = true; } diff --git a/src/Types/TypeCode39Extended.php b/src/Types/TypeCode39Extended.php index d16cfd6..0d85043 100644 --- a/src/Types/TypeCode39Extended.php +++ b/src/Types/TypeCode39Extended.php @@ -9,6 +9,6 @@ class TypeCode39Extended extends TypeCode39 { - protected $extended = true; - protected $checksum = false; + protected bool $extended = true; + protected bool $checksum = false; } diff --git a/src/Types/TypeCode39ExtendedChecksum.php b/src/Types/TypeCode39ExtendedChecksum.php index 1d28a88..9e5292c 100644 --- a/src/Types/TypeCode39ExtendedChecksum.php +++ b/src/Types/TypeCode39ExtendedChecksum.php @@ -9,6 +9,6 @@ class TypeCode39ExtendedChecksum extends TypeCode39 { - protected $extended = true; - protected $checksum = true; + protected bool $extended = true; + protected bool $checksum = true; } diff --git a/src/Types/TypeCode93.php b/src/Types/TypeCode93.php index 0bd9317..f944cf7 100644 --- a/src/Types/TypeCode93.php +++ b/src/Types/TypeCode93.php @@ -15,7 +15,7 @@ class TypeCode93 implements TypeInterface { - protected $conversionTable = [ + protected array $conversionTable = [ 48 => '131112', // 0 49 => '111213', // 1 50 => '111312', // 2 @@ -246,7 +246,7 @@ public function getBarcode(string $code): Barcode * @return string checksum code. * @protected */ - protected function checksum_code93($code) + protected function checksum_code93(string $code): string { $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']; @@ -280,8 +280,6 @@ protected function checksum_code93($code) $check %= 47; $k = $chars[$check]; - $checksum = $c . $k; - - return $checksum; + return $c . $k; } } diff --git a/src/Types/TypeEanUpcBase.php b/src/Types/TypeEanUpcBase.php index fe8948a..fba0121 100644 --- a/src/Types/TypeEanUpcBase.php +++ b/src/Types/TypeEanUpcBase.php @@ -211,7 +211,7 @@ protected function calculateChecksumDigit(string $code): int // calculate check digit $sum_a = 0; for ($i = 1; $i < $this->length - 1; $i += 2) { - $sum_a += $code[$i]; + $sum_a += intval($code[$i]); } if ($this->length > 12) { $sum_a *= 3; diff --git a/src/Types/TypeInterleaved25Checksum.php b/src/Types/TypeInterleaved25Checksum.php index 5871e9b..10727e6 100644 --- a/src/Types/TypeInterleaved25Checksum.php +++ b/src/Types/TypeInterleaved25Checksum.php @@ -75,11 +75,11 @@ protected function getChecksum(string $code): string $len = strlen($code); $sum = 0; for ($i = 0; $i < $len; $i += 2) { - $sum += $code[$i]; + $sum += intval($code[$i]); } $sum *= 3; for ($i = 1; $i < $len; $i += 2) { - $sum += ($code[$i]); + $sum += intval($code[$i]); } $r = $sum % 10; if ($r > 0) { diff --git a/src/Types/TypeMsi.php b/src/Types/TypeMsi.php index d6e91cb..12f6ee6 100644 --- a/src/Types/TypeMsi.php +++ b/src/Types/TypeMsi.php @@ -13,5 +13,5 @@ class TypeMsi extends TypeMsiChecksum { - protected $checksum = false; + protected bool $checksum = false; } diff --git a/src/Types/TypeMsiChecksum.php b/src/Types/TypeMsiChecksum.php index c421391..3d06f07 100644 --- a/src/Types/TypeMsiChecksum.php +++ b/src/Types/TypeMsiChecksum.php @@ -17,7 +17,7 @@ class TypeMsiChecksum implements TypeInterface { - protected $checksum = true; + protected bool $checksum = true; public function getBarcode(string $code): Barcode { diff --git a/src/Types/TypePlanet.php b/src/Types/TypePlanet.php index 0752386..c3b7f5e 100644 --- a/src/Types/TypePlanet.php +++ b/src/Types/TypePlanet.php @@ -13,7 +13,7 @@ class TypePlanet extends TypePostnet { - protected $barlen = [ + protected array $barlen = [ 0 => [1, 1, 2, 2, 2], 1 => [2, 2, 2, 1, 1], 2 => [2, 2, 1, 2, 1], diff --git a/src/Types/TypePostnet.php b/src/Types/TypePostnet.php index 2d136b3..0d235d8 100644 --- a/src/Types/TypePostnet.php +++ b/src/Types/TypePostnet.php @@ -16,7 +16,7 @@ class TypePostnet implements TypeInterface { - protected $barlen = [ + protected array $barlen = [ 0 => [2, 2, 1, 1, 1], 1 => [1, 1, 1, 2, 2], 2 => [1, 1, 2, 1, 2], diff --git a/src/Types/TypeStandard2of5.php b/src/Types/TypeStandard2of5.php index 906445d..1445a3c 100644 --- a/src/Types/TypeStandard2of5.php +++ b/src/Types/TypeStandard2of5.php @@ -14,7 +14,7 @@ class TypeStandard2of5 implements TypeInterface { - protected $checksum = false; + protected bool $checksum = false; public function getBarcode(string $code): Barcode { @@ -50,25 +50,25 @@ public function getBarcode(string $code): Barcode * Checksum for standard 2 of 5 barcodes. * * @param $code (string) code to process. - * @return int checksum. + * @return string checksum. * @protected */ - protected function checksum_s25($code) + protected function checksum_s25(string $code): string { $len = strlen($code); $sum = 0; for ($i = 0; $i < $len; $i += 2) { - $sum += $code[$i]; + $sum += intval($code[$i]); } $sum *= 3; for ($i = 1; $i < $len; $i += 2) { - $sum += ($code[$i]); + $sum += intval($code[$i]); } $r = $sum % 10; if ($r > 0) { $r = (10 - $r); } - return $r; + return strval($r); } } diff --git a/src/Types/TypeStandard2of5Checksum.php b/src/Types/TypeStandard2of5Checksum.php index 25bc4a0..c1b6fc5 100644 --- a/src/Types/TypeStandard2of5Checksum.php +++ b/src/Types/TypeStandard2of5Checksum.php @@ -10,5 +10,5 @@ class TypeStandard2of5Checksum extends TypeStandard2of5 { - protected $checksum = true; + protected bool $checksum = true; } diff --git a/src/Types/TypeTelepen.php b/src/Types/TypeTelepen.php index 378e342..477d4b9 100644 --- a/src/Types/TypeTelepen.php +++ b/src/Types/TypeTelepen.php @@ -23,11 +23,12 @@ class TypeTelepen implements TypeInterface { private const TELEPEN_START_CHAR = '_'; private const TELEPEN_STOP_CHAR = 'z'; + private const TELEPEN_ALPHA = 'alpha'; private const TELEPEN_NUMERIC = 'numeric'; - private $telepen_lookup_table; - private $mode; + private array $telepen_lookup_table; + private string $mode; public function __construct($m = 'alpha') { @@ -59,7 +60,6 @@ public function getBarcode(string $code): Barcode protected function encode($code) : string { - $result = null; if ($this->mode == self::TELEPEN_ALPHA) { $result = $this->encodeAlpha($code); } else { @@ -160,7 +160,7 @@ private function encodeNumeric(string $code) : string * More information about Telepen symbology is available from * https://v4l237.n3cdn1.secureserver.net/wp-content/uploads/2022/05/Barcode-Symbology-information-and-History.pdf */ - private function createTelepenConversionTable() + private function createTelepenConversionTable(): void { $this->telepen_lookup_table = [ "1111111111111111", "1131313111", "33313111", "1111313131", diff --git a/src/Types/TypeUpcExtension2.php b/src/Types/TypeUpcExtension2.php index f0620e1..22ecbca 100644 --- a/src/Types/TypeUpcExtension2.php +++ b/src/Types/TypeUpcExtension2.php @@ -14,7 +14,7 @@ class TypeUpcExtension2 implements TypeInterface { - protected $length = 2; + protected int $length = 2; public function getBarcode(string $code): Barcode { diff --git a/src/Types/TypeUpcExtension5.php b/src/Types/TypeUpcExtension5.php index b1d3424..c9e3065 100644 --- a/src/Types/TypeUpcExtension5.php +++ b/src/Types/TypeUpcExtension5.php @@ -10,5 +10,5 @@ class TypeUpcExtension5 extends TypeUpcExtension2 { - protected $length = 5; + protected int $length = 5; }