Skip to content

Commit

Permalink
More typehints
Browse files Browse the repository at this point in the history
  • Loading branch information
casperbakker committed Mar 31, 2024
1 parent ec4502f commit 460f3ee
Show file tree
Hide file tree
Showing 32 changed files with 70 additions and 53 deletions.
1 change: 0 additions & 1 deletion .github/FUNDING.yml

This file was deleted.

2 changes: 1 addition & 1 deletion src/Barcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/BarcodeBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Picqer\Barcode;

class BarcodeBar
readonly class BarcodeBar
{
protected int $width;
protected int $height;
Expand Down
6 changes: 5 additions & 1 deletion src/BarcodeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -101,14 +102,17 @@ 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);

return $barcodeDataBuilder->getBarcode($code);
}

protected function createDataBuilderForType(string $type)
protected function createDataBuilderForType(string $type): TypeInterface
{
switch (strtoupper($type)) {
case self::TYPE_CODE_32:
Expand Down
3 changes: 3 additions & 0 deletions src/BarcodeGeneratorDynamicHTML.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Picqer\Barcode;

use Picqer\Barcode\Exceptions\UnknownTypeException;

class BarcodeGeneratorDynamicHTML extends BarcodeGenerator
{
/**
Expand All @@ -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
{
Expand Down
3 changes: 3 additions & 0 deletions src/BarcodeGeneratorHTML.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Picqer\Barcode;

use Picqer\Barcode\Exceptions\UnknownTypeException;

class BarcodeGeneratorHTML extends BarcodeGenerator
{
/**
Expand All @@ -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
{
Expand Down
7 changes: 5 additions & 2 deletions src/BarcodeGeneratorJPG.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Picqer\Barcode;

use Picqer\Barcode\Exceptions\UnknownTypeException;

class BarcodeGeneratorJPG extends BarcodeGenerator
{
protected ?bool $useImagick = null;
Expand All @@ -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
{
Expand All @@ -37,15 +40,15 @@ 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;
}

/**
* Force the use of the GD image library
*/
public function useGd()
public function useGd(): void
{
$this->useImagick = false;
}
Expand Down
7 changes: 5 additions & 2 deletions src/BarcodeGeneratorPNG.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Picqer\Barcode;

use Picqer\Barcode\Exceptions\UnknownTypeException;

class BarcodeGeneratorPNG extends BarcodeGenerator
{
protected ?bool $useImagick = null;
Expand All @@ -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
{
Expand All @@ -37,15 +40,15 @@ 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;
}

/**
* Force the use of the GD image library
*/
public function useGd()
public function useGd(): void
{
$this->useImagick = false;
}
Expand Down
3 changes: 3 additions & 0 deletions src/BarcodeGeneratorSVG.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Picqer\Barcode;

use Picqer\Barcode\Exceptions\UnknownTypeException;

class BarcodeGeneratorSVG extends BarcodeGenerator
{
/**
Expand All @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion src/Types/TypeCodabar.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

class TypeCodabar implements TypeInterface
{
protected $conversionTable = [
protected array $conversionTable = [
'0' => '11111221',
'1' => '11112211',
'2' => '11121121',
Expand Down
2 changes: 1 addition & 1 deletion src/Types/TypeCode11.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

class TypeCode11 implements TypeInterface
{
protected $conversionTable = [
protected array $conversionTable = [
'0' => '111121',
'1' => '211121',
'2' => '121121',
Expand Down
6 changes: 3 additions & 3 deletions src/Types/TypeCode128.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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 = [];
Expand Down
2 changes: 1 addition & 1 deletion src/Types/TypeCode128A.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@

class TypeCode128A extends TypeCode128
{
protected $type = 'A';
protected ?string $type = 'A';
}
2 changes: 1 addition & 1 deletion src/Types/TypeCode128B.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@

class TypeCode128B extends TypeCode128
{
protected $type = 'B';
protected ?string $type = 'B';
}
2 changes: 1 addition & 1 deletion src/Types/TypeCode128C.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@

class TypeCode128C extends TypeCode128
{
protected $type = 'C';
protected ?string $type = 'C';
}
2 changes: 1 addition & 1 deletion src/Types/TypeCode32.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/
class TypeCode32 extends TypeCode39
{
protected $conversionTable32 = [
protected array $conversionTable32 = [
'0' => '0',
'1' => '1',
'2' => '2',
Expand Down
11 changes: 6 additions & 5 deletions src/Types/TypeCode39.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down
4 changes: 2 additions & 2 deletions src/Types/TypeCode39Checksum.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@

class TypeCode39Checksum extends TypeCode39
{
protected $extended = false;
protected $checksum = true;
protected bool $extended = false;
protected bool $checksum = true;
}
4 changes: 2 additions & 2 deletions src/Types/TypeCode39Extended.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@

class TypeCode39Extended extends TypeCode39
{
protected $extended = true;
protected $checksum = false;
protected bool $extended = true;
protected bool $checksum = false;
}
4 changes: 2 additions & 2 deletions src/Types/TypeCode39ExtendedChecksum.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@

class TypeCode39ExtendedChecksum extends TypeCode39
{
protected $extended = true;
protected $checksum = true;
protected bool $extended = true;
protected bool $checksum = true;
}
8 changes: 3 additions & 5 deletions src/Types/TypeCode93.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

class TypeCode93 implements TypeInterface
{
protected $conversionTable = [
protected array $conversionTable = [
48 => '131112', // 0
49 => '111213', // 1
50 => '111312', // 2
Expand Down Expand Up @@ -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'];

Expand Down Expand Up @@ -280,8 +280,6 @@ protected function checksum_code93($code)
$check %= 47;
$k = $chars[$check];

$checksum = $c . $k;

return $checksum;
return $c . $k;
}
}
2 changes: 1 addition & 1 deletion src/Types/TypeEanUpcBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/Types/TypeInterleaved25Checksum.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/Types/TypeMsi.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@

class TypeMsi extends TypeMsiChecksum
{
protected $checksum = false;
protected bool $checksum = false;
}
2 changes: 1 addition & 1 deletion src/Types/TypeMsiChecksum.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

class TypeMsiChecksum implements TypeInterface
{
protected $checksum = true;
protected bool $checksum = true;

public function getBarcode(string $code): Barcode
{
Expand Down
2 changes: 1 addition & 1 deletion src/Types/TypePlanet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
2 changes: 1 addition & 1 deletion src/Types/TypePostnet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
Loading

0 comments on commit 460f3ee

Please sign in to comment.