Skip to content

Commit

Permalink
Merge pull request #189 from picqer/phpstan
Browse files Browse the repository at this point in the history
Add static analysis with phpstan
  • Loading branch information
casperbakker committed Sep 16, 2023
2 parents 784de4b + b64906f commit 4cec189
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 20 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Static analysis (phpstan)

on:
push:
branches:
- main
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 --error-format=github --no-progress
16 changes: 11 additions & 5 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
name: phpunit
name: Unit tests (phpunit)

on: [push, pull_request]
on:
push:
branches:
- main
pull_request:

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
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
Expand All @@ -24,7 +30,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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 4 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
parameters:
paths:
- src
level: 4
4 changes: 2 additions & 2 deletions src/BarcodeGeneratorPNG.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/Types/TypeCode11.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,6 @@ private function getCheckDigitK(string $code): string
}
$check %= 11;

return $check;
return (string)$check;
}
}
10 changes: 9 additions & 1 deletion src/Types/TypeCode128.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -164,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');
Expand Down Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion src/Types/TypeCode32.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,6 @@ protected function checksum_code32(string $code): string
}
}

return (string)$s % 10;
return (string)($s % 10);
}
}
8 changes: 4 additions & 4 deletions src/Types/TypeIntelligentMailBarcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Types/TypeInterleaved25Checksum.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,6 @@ protected function getChecksum(string $code): string
$r = (10 - $r);
}

return $r;
return (string)$r;
}
}
5 changes: 5 additions & 0 deletions src/Types/TypePharmacodeTwoCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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));
Expand Down
8 changes: 4 additions & 4 deletions src/Types/TypeUpcExtension2.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 4cec189

Please sign in to comment.