Skip to content

Commit 4cec189

Browse files
authored
Merge pull request #189 from picqer/phpstan
Add static analysis with phpstan
2 parents 784de4b + b64906f commit 4cec189

12 files changed

+73
-20
lines changed

.github/workflows/phpstan.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Static analysis (phpstan)
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Checkout code
15+
uses: actions/checkout@v4
16+
with:
17+
fetch-depth: 0
18+
19+
- name: Setup PHP
20+
uses: shivammathur/setup-php@v2
21+
with:
22+
php-version: 8.2
23+
extensions: mbstring, gd, bcmath, imagick
24+
25+
- name: Install dependencies
26+
run: composer install --prefer-dist --no-progress --no-interaction
27+
28+
- name: Run analysis
29+
run: vendor/bin/phpstan --error-format=github --no-progress

.github/workflows/phpunit.yml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
1-
name: phpunit
1+
name: Unit tests (phpunit)
22

3-
on: [push, pull_request]
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
48

59
jobs:
610
build:
7-
811
runs-on: ubuntu-latest
912

1013
strategy:
1114
matrix:
1215
php-versions: ['7.3', '7.4', '8.0', '8.1', '8.2']
1316

1417
steps:
15-
- uses: actions/checkout@v4
18+
- name: Checkout code
19+
uses: actions/checkout@v4
20+
with:
21+
fetch-depth: 0
1622

1723
- name: Setup PHP
1824
uses: shivammathur/setup-php@v2
@@ -24,7 +30,7 @@ jobs:
2430
run: composer validate
2531

2632
- name: Install dependencies
27-
run: composer install --prefer-dist --no-progress
33+
run: composer install --prefer-dist --no-progress --no-interaction
2834

2935
- name: Run test suite
3036
run: composer run-script test

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"ext-mbstring": "*"
2323
},
2424
"require-dev": {
25-
"phpunit/phpunit": "^9.5"
25+
"phpunit/phpunit": "^9.5",
26+
"phpstan/phpstan": "^1.10"
2627
},
2728
"suggest": {
2829
"ext-bcmath": "Barcode IMB (Intelligent Mail Barcode) needs bcmath extension",

phpstan.neon.dist

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
parameters:
2+
paths:
3+
- src
4+
level: 4

src/BarcodeGeneratorPNG.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function getBarcode(string $barcode, $type, int $widthFactor = 2, int $he
7676
$barHeight = round(($bar->getHeight() * $height / $barcodeData->getHeight()), 3);
7777

7878
// draw a vertical bar
79-
if ($this->useImagick && isset($imagickBarsShape)) {
79+
if ($this->useImagick) {
8080
$imagickBarsShape->rectangle($positionHorizontal, $y, ($positionHorizontal + $barWidth - 1), ($y + $barHeight));
8181
} else {
8282
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
8585
$positionHorizontal += $barWidth;
8686
}
8787

88-
if ($this->useImagick && isset($imagickBarsShape)) {
88+
if ($this->useImagick) {
8989
$image = $this->createImagickImageObject($width, $height);
9090
$image->drawImage($imagickBarsShape);
9191
return $image->getImageBlob();

src/Types/TypeCode11.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,6 @@ private function getCheckDigitK(string $code): string
106106
}
107107
$check %= 11;
108108

109-
return $check;
109+
return (string)$check;
110110
}
111111
}

src/Types/TypeCode128.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Picqer\Barcode\Barcode;
66
use Picqer\Barcode\BarcodeBar;
7+
use Picqer\Barcode\Exceptions\BarcodeException;
78
use Picqer\Barcode\Exceptions\InvalidCharacterException;
89
use Picqer\Barcode\Exceptions\InvalidLengthException;
910

@@ -164,7 +165,7 @@ public function getBarcodeData(string $code): Barcode
164165
$char_id = ord($char);
165166
if (($char_id >= 241) AND ($char_id <= 244)) {
166167
$code_data[] = $fnc_a[$char_id];
167-
} elseif (($char_id >= 0) AND ($char_id <= 95)) {
168+
} elseif ($char_id <= 95) {
168169
$code_data[] = strpos($keys_a, $char);
169170
} else {
170171
throw new InvalidCharacterException('Char ' . $char . ' is unsupported');
@@ -331,11 +332,18 @@ public function getBarcodeData(string $code): Barcode
331332
$code_data[] = intval($chrnum);
332333
}
333334
break;
335+
336+
default:
337+
throw new InvalidCharacterException('Do not support different mode then A, B or C.');
334338
}
335339
}
336340
}
337341

338342
// calculate check character
343+
if (! isset($startid)) {
344+
throw new BarcodeException('Could not determine start char for barcode.');
345+
}
346+
339347
$sum = $startid;
340348
foreach ($code_data as $key => $val) {
341349
$sum += ($val * ($key + 1));

src/Types/TypeCode32.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,6 @@ protected function checksum_code32(string $code): string
109109
}
110110
}
111111

112-
return (string)$s % 10;
112+
return (string)($s % 10);
113113
}
114114
}

src/Types/TypeIntelligentMailBarcode.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -378,11 +378,11 @@ public function getBarcodeData(string $code): Barcode
378378
// convert codewords to characters
379379
$characters = [];
380380
$bitmask = 512;
381-
foreach ($codewords as $k => $val) {
381+
foreach ($codewords as $val) {
382382
if ($val <= 1286) {
383-
$chrcode = $table5of13[$val];
383+
$chrcode = (int)$table5of13[$val];
384384
} else {
385-
$chrcode = $table2of13[($val - 1287)];
385+
$chrcode = (int)$table2of13[($val - 1287)];
386386
}
387387
if (($fcs & $bitmask) > 0) {
388388
// bitwise invert
@@ -517,7 +517,7 @@ protected function hex_to_dec($hex)
517517
* @return array requested table
518518
* @protected
519519
*/
520-
protected function imb_tables($n, $size)
520+
protected function imb_tables(int $n, int $size): array
521521
{
522522
$table = [];
523523
$lli = 0; // LUT lower index

src/Types/TypeInterleaved25Checksum.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,6 @@ protected function getChecksum(string $code): string
8686
$r = (10 - $r);
8787
}
8888

89-
return $r;
89+
return (string)$r;
9090
}
9191
}

0 commit comments

Comments
 (0)