Skip to content

Commit f8b5899

Browse files
committed
Use psalm to check and add more return types
1 parent 388aa46 commit f8b5899

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+623
-406
lines changed

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"require-dev": {
2727
"phpunit/phpunit": "^5.7 | ^7.5 | ^8.0 | ^9.0",
2828
"rector/rector": "^0.13.6",
29-
"symplify/easy-coding-standard": "^11.0"
29+
"symplify/easy-coding-standard": "^11.0",
30+
"vimeo/psalm": "^4.24"
3031
},
3132
"autoload": {
3233
"psr-4": {
@@ -39,7 +40,8 @@
3940
"scripts": {
4041
"check-cs": "./vendor/bin/ecs check",
4142
"fix-cs": "./vendor/bin/ecs check --fix",
42-
"tests": "./vendor/bin/phpunit"
43+
"tests": "./vendor/bin/phpunit",
44+
"static-tests": "./vendor/bin/psalm --php-version=8.1"
4345
},
4446
"autoload-dev": {
4547
"psr-4": {

lib/Binarizer.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ final public function getLuminanceSource()
5050
* and passed in with each call for performance. However it is legal to keep more than one row
5151
* at a time if needed.
5252
*
53-
* @param $y The row to fetch, which must be in [0, bitmap height)
54-
* @param An $row optional preallocated array. If null or too small, it will be ignored.
53+
* @param int $y The row to fetch, which must be in [0, bitmap height)
54+
* @param BitArray|null $row An optional preallocated array. If null or too small, it will be ignored.
5555
* If used, the Binarizer will call BitArray.clear(). Always use the returned object.
5656
*
57-
* @return array The array of bits for this row (true means black).
57+
* @return BitArray The array of bits for this row (true means black).
5858
* @throws NotFoundException if row can't be binarized
5959
*/
60-
abstract public function getBlackRow($y, $row);
60+
abstract public function getBlackRow(int $y, ?BitArray $row = null): BitArray;
6161

6262
/**
6363
* Converts a 2D array of luminance data to 1 bit data. As above, assume this method is expensive

lib/BinaryBitmap.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,14 @@ public function getHeight()
6060
* This method is intended for decoding 1D barcodes and may choose to apply sharpening.
6161
*
6262
* @param $y The row to fetch, which must be in [0, bitmap height)
63-
* @param An $row optional preallocated array. If null or too small, it will be ignored.
63+
* @param array|null $row An optional preallocated array. If null or too small, it will be ignored.
6464
* If used, the Binarizer will call BitArray.clear(). Always use the returned object.
6565
*
66-
* @return array The array of bits for this row (true means black).
66+
* @return Common\BitArray The array of bits for this row (true means black).
67+
*
6768
* @throws NotFoundException if row can't be binarized
6869
*/
69-
public function getBlackRow($y, $row)
70+
public function getBlackRow($y, $row): Common\BitArray
7071
{
7172
return $this->binarizer->getBlackRow($y, $row);
7273
}
@@ -98,7 +99,7 @@ public function crop($left, $top, $width, $height): \Zxing\BinaryBitmap
9899
}
99100

100101
/**
101-
* @return Whether this bitmap supports counter-clockwise rotation.
102+
* @return bool this Whether bitmap supports counter-clockwise rotation.
102103
*/
103104
public function isRotateSupported()
104105
{
@@ -131,7 +132,7 @@ public function rotateCounterClockwise45(): \Zxing\BinaryBitmap
131132
return new BinaryBitmap($this->binarizer->createBinarizer($newSource));
132133
}
133134

134-
public function toString()
135+
public function toString(): string
135136
{
136137
try {
137138
return $this->getBlackMatrix()->toString();

lib/ChecksumException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ final class ChecksumException extends ReaderException
2727
{
2828
private static ?\Zxing\ChecksumException $instance = null;
2929

30-
public static function getChecksumInstance($cause = null)
30+
public static function getChecksumInstance($cause = null): self
3131
{
3232
if (self::$isStackTrace) {
3333
return new ChecksumException($cause);

lib/Common/BitArray.php

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
final class BitArray
3434
{
3535
/**
36-
* @var mixed[]|mixed|int[]|null
36+
* @var mixed[]|int[]|null
3737
*/
3838
private $bits;
3939
/**
@@ -56,7 +56,10 @@ public function __construct($bits = [], $size = 0)
5656
}
5757
}
5858

59-
private static function makeArray($size)
59+
/**
60+
* @psalm-return array<empty, empty>
61+
*/
62+
private static function makeArray($size): array
6063
{
6164
return [];
6265
}
@@ -74,7 +77,7 @@ public function getSizeInBytes()
7477
/**
7578
* Sets bit i.
7679
*
77-
* @param bit $i to set
80+
* @param int $i bit to set
7881
*/
7982
public function set($i): void
8083
{
@@ -85,7 +88,7 @@ public function set($i): void
8588
/**
8689
* Flips bit i.
8790
*
88-
* @param bit $i to set
91+
* @param int $i bit to set
8992
*/
9093
public function flip($i): void
9194
{
@@ -94,9 +97,9 @@ public function flip($i): void
9497
}
9598

9699
/**
97-
* @param first $from bit to check
100+
* @param int $from first bit to check
98101
*
99-
* @return index of first bit that is set, starting from the given index, or size if none are set
102+
* @return int index of first bit that is set, starting from the given index, or size if none are set
100103
* at or beyond this given index
101104
* @see #getNextUnset(int)
102105
*/
@@ -121,9 +124,9 @@ public function getNextSet($from)
121124
}
122125

123126
/**
124-
* @param index $from to start looking for unset bit
127+
* @param int $from index to start looking for unset bit
125128
*
126-
* @return index of next unset bit, or {@code size} if none are unset until the end
129+
* @return int index of next unset bit, or {@code size} if none are unset until the end
127130
* @see #getNextSet(int)
128131
*/
129132
public function getNextUnset($from)
@@ -149,8 +152,8 @@ public function getNextUnset($from)
149152
/**
150153
* Sets a block of 32 bits, starting at bit i.
151154
*
152-
* @param first $i bit to set
153-
* @param the $newBits new value of the next 32 bits. Note again that the least-significant bit
155+
* @param int $i first bit to set
156+
* @param int $newBits the new value of the next 32 bits. Note again that the least-significant bit
154157
* corresponds to bit i, the next-least-significant to i+1, and so on.
155158
*/
156159
public function setBulk($i, $newBits): void
@@ -161,8 +164,10 @@ public function setBulk($i, $newBits): void
161164
/**
162165
* Sets a range of bits.
163166
*
164-
* @param start $start of range, inclusive.
165-
* @param end $end of range, exclusive
167+
* @param int $start start of range, inclusive.
168+
* @param int $end end of range, exclusive
169+
*
170+
* @return void
166171
*/
167172
public function setRange($start, $end)
168173
{
@@ -205,12 +210,13 @@ public function clear(): void
205210
/**
206211
* Efficient method to check if a range of bits is set, or not set.
207212
*
208-
* @param start $start of range, inclusive.
209-
* @param end $end of range, exclusive
210-
* @param if $value true, checks that bits in range are set, otherwise checks that they are not set
213+
* @param int $start start of range, inclusive.
214+
* @param int $end end of range, exclusive
215+
* @param bool $value if true, checks that bits in range are set, otherwise checks that they are not set
211216
*
212-
* @return true iff all bits are set or not set in range, according to value argument
213-
* @throws InvalidArgumentException if end is less than or equal to start
217+
* @return bool iff all bits are set or not set in range, according to value argument
218+
*
219+
* @throws \InvalidArgumentException if end is less than or equal to start
214220
*/
215221
public function isRange($start, $end, $value): bool
216222
{
@@ -251,10 +257,10 @@ public function isRange($start, $end, $value): bool
251257
* least-significant. For example, appending 6 bits from 0x000001E will append the bits
252258
* 0, 1, 1, 1, 1, 0 in that order.
253259
*
254-
* @param $value {@code int} containing bits to append
255-
* @param bits $numBits from value to append
260+
* @param int $value {@code int} containing bits to append
261+
* @param int $numBits bits from value to append
256262
*/
257-
public function appendBits($value, $numBits)
263+
public function appendBits($value, $numBits): void
258264
{
259265
if ($numBits < 0 || $numBits > 32) {
260266
throw new \InvalidArgumentException("Num bits must be between 0 and 32");
@@ -274,7 +280,7 @@ private function ensureCapacity($size): void
274280
}
275281
}
276282

277-
public function appendBit($bit): void
283+
public function appendBit(bool $bit): void
278284
{
279285
$this->ensureCapacity($this->size + 1);
280286
if ($bit) {
@@ -292,7 +298,7 @@ public function appendBitArray($other): void
292298
}
293299
}
294300

295-
public function _xor($other)
301+
public function _xor($other): void
296302
{
297303
if ((is_countable($this->bits) ? count($this->bits) : 0) !== (is_countable($other->bits) ? count($other->bits) : 0)) {
298304
throw new \InvalidArgumentException("Sizes don't match");
@@ -307,11 +313,11 @@ public function _xor($other)
307313

308314
/**
309315
*
310-
* @param first $bitOffset bit to start writing
316+
* @param int $bitOffset first bit to start writing
311317
* @param array $array to write into. Bytes are written most-significant byte first. This is the opposite
312318
* of the internal representation, which is exposed by {@link #getBitArray()}
313-
* @param position $offset in array to start writing
314-
* @param how $numBytes many bytes to write
319+
* @param int $offset position in array to start writing
320+
* @param int $numBytes how many bytes to write
315321
*/
316322
public function toBytes($bitOffset, array &$array, $offset, $numBytes): void
317323
{
@@ -329,21 +335,23 @@ public function toBytes($bitOffset, array &$array, $offset, $numBytes): void
329335

330336
/**
331337
* @param $i ; bit to get
338+
* @param float|int first $i
332339
*
333-
* @return true iff bit i is set
340+
* @return bool iff bit i is set
334341
*/
335-
public function get($i): bool
342+
public function get(int|float $i): bool
336343
{
337344
$key = (int)($i / 32);
338345

339346
return ($this->bits[$key] & (1 << ($i & 0x1F))) != 0;
340347
}
341348

342349
/**
343-
* @return array underlying array of ints. The first element holds the first 32 bits, and the least
344-
* significant bit is bit 0.
350+
* @return (int|mixed)[]|null underlying array of ints. The first element holds the first 32 bits, and the least significant bit is bit 0.
351+
*
352+
* @psalm-return array<int|mixed>|null
345353
*/
346-
public function getBitArray()
354+
public function getBitArray(): array|null
347355
{
348356
return $this->bits;
349357
}
@@ -405,7 +413,7 @@ public function hashCode()
405413
return 31 * $this->size + hashCode($this->bits);
406414
}
407415

408-
public function toString()
416+
public function toString(): string
409417
{
410418
$result = '';
411419
for ($i = 0; $i < $this->size; $i++) {

0 commit comments

Comments
 (0)