From 76a15ec324523c141f30b706db5c52c055900f65 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Sat, 16 Sep 2023 09:54:08 +0200 Subject: [PATCH 1/7] Fix phpstan errors level 1 --- src/BarcodeGeneratorPNG.php | 4 ++-- src/Types/TypeCode128.php | 8 ++++++++ src/Types/TypePharmacodeTwoCode.php | 5 +++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/BarcodeGeneratorPNG.php b/src/BarcodeGeneratorPNG.php index 05ac413..150e1e4 100644 --- a/src/BarcodeGeneratorPNG.php +++ b/src/BarcodeGeneratorPNG.php @@ -76,7 +76,7 @@ public function getBarcode(string $barcode, $type, int $widthFactor = 2, int $he $barHeight = round(($bar->getHeight() * $height / $barcodeData->getHeight()), 3); // draw a vertical bar - if ($this->useImagick && isset($imagickBarsShape)) { + if ($this->useImagick) { $imagickBarsShape->rectangle($positionHorizontal, $y, ($positionHorizontal + $barWidth - 1), ($y + $barHeight)); } else { imagefilledrectangle($image, $positionHorizontal, $y, ($positionHorizontal + $barWidth - 1), ($y + $barHeight), $gdForegroundColor); @@ -85,7 +85,7 @@ public function getBarcode(string $barcode, $type, int $widthFactor = 2, int $he $positionHorizontal += $barWidth; } - if ($this->useImagick && isset($imagickBarsShape)) { + if ($this->useImagick) { $image = $this->createImagickImageObject($width, $height); $image->drawImage($imagickBarsShape); return $image->getImageBlob(); diff --git a/src/Types/TypeCode128.php b/src/Types/TypeCode128.php index 0f4c5a5..27d3ea6 100644 --- a/src/Types/TypeCode128.php +++ b/src/Types/TypeCode128.php @@ -4,6 +4,7 @@ use Picqer\Barcode\Barcode; use Picqer\Barcode\BarcodeBar; +use Picqer\Barcode\Exceptions\BarcodeException; use Picqer\Barcode\Exceptions\InvalidCharacterException; use Picqer\Barcode\Exceptions\InvalidLengthException; @@ -331,11 +332,18 @@ public function getBarcodeData(string $code): Barcode $code_data[] = intval($chrnum); } break; + + default: + throw new InvalidCharacterException('Do not support different mode then A, B or C.'); } } } // calculate check character + if (! isset($startid)) { + throw new BarcodeException('Could not determine start char for barcode.'); + } + $sum = $startid; foreach ($code_data as $key => $val) { $sum += ($val * ($key + 1)); diff --git a/src/Types/TypePharmacodeTwoCode.php b/src/Types/TypePharmacodeTwoCode.php index 7c1959a..3d7c2fa 100644 --- a/src/Types/TypePharmacodeTwoCode.php +++ b/src/Types/TypePharmacodeTwoCode.php @@ -9,6 +9,8 @@ use Picqer\Barcode\Barcode; use Picqer\Barcode\BarcodeBar; +use Picqer\Barcode\Exceptions\BarcodeException; +use Picqer\Barcode\Exceptions\InvalidCharacterException; use Picqer\Barcode\Exceptions\InvalidLengthException; class TypePharmacodeTwoCode implements TypeInterface @@ -62,6 +64,9 @@ public function getBarcodeData(string $code): Barcode $p = 0; $h = 2; break; + + default: + throw new InvalidCharacterException('Could not find bar for char.'); } $barcode->addBar(new BarcodeBar(1, $h, 1, $p)); From 326792a2b8a4bcef7fa972a63e0fc85796eff348 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Sat, 16 Sep 2023 10:32:58 +0200 Subject: [PATCH 2/7] Run static analysis in Github Actions --- .github/workflows/phpstan.yml | 25 +++++++++++++++++++++++++ .github/workflows/phpunit.yml | 10 ++++++---- composer.json | 3 ++- 3 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 .github/workflows/phpstan.yml diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml new file mode 100644 index 0000000..831b110 --- /dev/null +++ b/.github/workflows/phpstan.yml @@ -0,0 +1,25 @@ +name: Static analysis (phpstan) + +on: [push, pull_request] + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: 8.2 + extensions: mbstring, gd, bcmath, imagick + + - name: Install dependencies + run: composer install --prefer-dist --no-progress --no-interaction + + - name: Run analysis + run: vendor/bin/phpstan analyse src --level 1 diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index 785311c..2c3d4e3 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -1,10 +1,9 @@ -name: phpunit +name: Unit tests (phpunit) on: [push, pull_request] jobs: build: - runs-on: ubuntu-latest strategy: @@ -12,7 +11,10 @@ jobs: php-versions: ['7.3', '7.4', '8.0', '8.1', '8.2'] steps: - - uses: actions/checkout@v4 + - name: Checkout code + uses: actions/checkout@v4 + with: + fetch-depth: 0 - name: Setup PHP uses: shivammathur/setup-php@v2 @@ -24,7 +26,7 @@ jobs: run: composer validate - name: Install dependencies - run: composer install --prefer-dist --no-progress + run: composer install --prefer-dist --no-progress --no-interaction - name: Run test suite run: composer run-script test diff --git a/composer.json b/composer.json index 180639e..3328695 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,8 @@ "ext-mbstring": "*" }, "require-dev": { - "phpunit/phpunit": "^9.5" + "phpunit/phpunit": "^9.5", + "phpstan/phpstan": "^1.10" }, "suggest": { "ext-bcmath": "Barcode IMB (Intelligent Mail Barcode) needs bcmath extension", From 3d6b3400cbc25fe12bed65407c7fa211ffb05add Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Sat, 16 Sep 2023 10:45:08 +0200 Subject: [PATCH 3/7] Phpstan fixed to level 2 --- .github/workflows/phpstan.yml | 2 +- src/Types/TypeIntelligentMailBarcode.php | 8 ++++---- src/Types/TypeUpcExtension2.php | 8 ++++---- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml index 831b110..db0f34d 100644 --- a/.github/workflows/phpstan.yml +++ b/.github/workflows/phpstan.yml @@ -22,4 +22,4 @@ jobs: run: composer install --prefer-dist --no-progress --no-interaction - name: Run analysis - run: vendor/bin/phpstan analyse src --level 1 + run: vendor/bin/phpstan analyse src --level 2 diff --git a/src/Types/TypeIntelligentMailBarcode.php b/src/Types/TypeIntelligentMailBarcode.php index 8c4f8c4..e574cf7 100644 --- a/src/Types/TypeIntelligentMailBarcode.php +++ b/src/Types/TypeIntelligentMailBarcode.php @@ -378,11 +378,11 @@ public function getBarcodeData(string $code): Barcode // convert codewords to characters $characters = []; $bitmask = 512; - foreach ($codewords as $k => $val) { + foreach ($codewords as $val) { if ($val <= 1286) { - $chrcode = $table5of13[$val]; + $chrcode = (int)$table5of13[$val]; } else { - $chrcode = $table2of13[($val - 1287)]; + $chrcode = (int)$table2of13[($val - 1287)]; } if (($fcs & $bitmask) > 0) { // bitwise invert @@ -517,7 +517,7 @@ protected function hex_to_dec($hex) * @return array requested table * @protected */ - protected function imb_tables($n, $size) + protected function imb_tables(int $n, int $size): array { $table = []; $lli = 0; // LUT lower index diff --git a/src/Types/TypeUpcExtension2.php b/src/Types/TypeUpcExtension2.php index 98ee3c6..cebbfa8 100644 --- a/src/Types/TypeUpcExtension2.php +++ b/src/Types/TypeUpcExtension2.php @@ -20,20 +20,20 @@ public function getBarcodeData(string $code): Barcode { $len = $this->length; - //Padding + // Padding $code = str_pad($code, $len, '0', STR_PAD_LEFT); - // calculate check digit + // Calculate check digit if ($len == 2) { $r = $code % 4; } elseif ($len == 5) { - $r = (3 * ($code[0] + $code[2] + $code[4])) + (9 * ($code[1] + $code[3])); + $r = (3 * intval($code[0] . $code[2] . $code[4])) + (9 * intval($code[1] . $code[3])); $r %= 10; } else { throw new InvalidCheckDigitException(); } - //Convert digits to bars + // Convert digits to bars $codes = [ 'A' => [ // left odd parity '0' => '0001101', From da543987d6a083ffa03366e005eb9c90579c92c8 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Sat, 16 Sep 2023 10:46:25 +0200 Subject: [PATCH 4/7] Phpstan level 3 --- src/Types/TypeCode11.php | 2 +- src/Types/TypeCode32.php | 2 +- src/Types/TypeInterleaved25Checksum.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Types/TypeCode11.php b/src/Types/TypeCode11.php index 7352e99..2857cb3 100644 --- a/src/Types/TypeCode11.php +++ b/src/Types/TypeCode11.php @@ -106,6 +106,6 @@ private function getCheckDigitK(string $code): string } $check %= 11; - return $check; + return (string)$check; } } diff --git a/src/Types/TypeCode32.php b/src/Types/TypeCode32.php index e5df479..6ca6816 100644 --- a/src/Types/TypeCode32.php +++ b/src/Types/TypeCode32.php @@ -109,6 +109,6 @@ protected function checksum_code32(string $code): string } } - return (string)$s % 10; + return (string)($s % 10); } } diff --git a/src/Types/TypeInterleaved25Checksum.php b/src/Types/TypeInterleaved25Checksum.php index 1af0439..6192126 100644 --- a/src/Types/TypeInterleaved25Checksum.php +++ b/src/Types/TypeInterleaved25Checksum.php @@ -86,6 +86,6 @@ protected function getChecksum(string $code): string $r = (10 - $r); } - return $r; + return (string)$r; } } From c72e5c801c1b8ae1f284b804fd5111e6da4ec373 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Sat, 16 Sep 2023 10:51:06 +0200 Subject: [PATCH 5/7] Phpstan level 4 --- .github/workflows/phpstan.yml | 2 +- src/Types/TypeCode128.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml index db0f34d..70afc49 100644 --- a/.github/workflows/phpstan.yml +++ b/.github/workflows/phpstan.yml @@ -22,4 +22,4 @@ jobs: run: composer install --prefer-dist --no-progress --no-interaction - name: Run analysis - run: vendor/bin/phpstan analyse src --level 2 + run: vendor/bin/phpstan analyse src --level 4 diff --git a/src/Types/TypeCode128.php b/src/Types/TypeCode128.php index 27d3ea6..38fc618 100644 --- a/src/Types/TypeCode128.php +++ b/src/Types/TypeCode128.php @@ -165,7 +165,7 @@ public function getBarcodeData(string $code): Barcode $char_id = ord($char); if (($char_id >= 241) AND ($char_id <= 244)) { $code_data[] = $fnc_a[$char_id]; - } elseif (($char_id >= 0) AND ($char_id <= 95)) { + } elseif ($char_id <= 95) { $code_data[] = strpos($keys_a, $char); } else { throw new InvalidCharacterException('Char ' . $char . ' is unsupported'); From 02365e8e279c1f5414c769dcfc5dcaf65966d8f9 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Sat, 16 Sep 2023 10:56:21 +0200 Subject: [PATCH 6/7] Tweak phpstan running --- .github/workflows/phpstan.yml | 2 +- phpstan.neon.dist | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 phpstan.neon.dist diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml index 70afc49..d2e5686 100644 --- a/.github/workflows/phpstan.yml +++ b/.github/workflows/phpstan.yml @@ -22,4 +22,4 @@ jobs: run: composer install --prefer-dist --no-progress --no-interaction - name: Run analysis - run: vendor/bin/phpstan analyse src --level 4 + run: vendor/bin/phpstan --error-format=github --no-progress diff --git a/phpstan.neon.dist b/phpstan.neon.dist new file mode 100644 index 0000000..0405354 --- /dev/null +++ b/phpstan.neon.dist @@ -0,0 +1,4 @@ +parameters: + paths: + - src + level: 4 From b64906f233792c031aabe10512eb34d05f824e9d Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Sat, 16 Sep 2023 10:57:37 +0200 Subject: [PATCH 7/7] No double runs on PR's --- .github/workflows/phpstan.yml | 6 +++++- .github/workflows/phpunit.yml | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml index d2e5686..7528e81 100644 --- a/.github/workflows/phpstan.yml +++ b/.github/workflows/phpstan.yml @@ -1,6 +1,10 @@ name: Static analysis (phpstan) -on: [push, pull_request] +on: + push: + branches: + - main + pull_request: jobs: build: diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index 2c3d4e3..cfd73af 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -1,6 +1,10 @@ name: Unit tests (phpunit) -on: [push, pull_request] +on: + push: + branches: + - main + pull_request: jobs: build: