Skip to content

Commit 5a280c8

Browse files
committed
🚿 clean up some (int) casts
1 parent edbc995 commit 5a280c8

File tree

13 files changed

+89
-81
lines changed

13 files changed

+89
-81
lines changed

src/Common/MaskPattern.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use chillerlan\QRCode\Data\QRMatrix;
1515
use chillerlan\QRCode\QRCodeException;
1616
use Closure;
17-
use function abs, array_search, count, min;
17+
use function abs, array_search, count, intdiv, min;
1818

1919
/**
2020
* ISO/IEC 18004:2000 Section 8.8.1
@@ -102,10 +102,10 @@ public function getMask():Closure{
102102
self::PATTERN_001 => fn(int $x, int $y):bool => ($y % 2) === 0,
103103
self::PATTERN_010 => fn(int $x, int $y):bool => ($x % 3) === 0,
104104
self::PATTERN_011 => fn(int $x, int $y):bool => (($x + $y) % 3) === 0,
105-
self::PATTERN_100 => fn(int $x, int $y):bool => (((int)($y / 2) + (int)($x / 3)) % 2) === 0,
106-
self::PATTERN_101 => fn(int $x, int $y):bool => (($x * $y) % 6) === 0, // ((($x * $y) % 2) + (($x * $y) % 3)) === 0,
107-
self::PATTERN_110 => fn(int $x, int $y):bool => (($x * $y) % 6) < 3, // (((($x * $y) % 2) + (($x * $y) % 3)) % 2) === 0,
108-
self::PATTERN_111 => fn(int $x, int $y):bool => (($x + $y + (($x * $y) % 3)) % 2) === 0, // (((($x * $y) % 3) + (($x + $y) % 2)) % 2) === 0,
105+
self::PATTERN_100 => fn(int $x, int $y):bool => ((intdiv($y, 2) + intdiv($x, 3)) % 2) === 0,
106+
self::PATTERN_101 => fn(int $x, int $y):bool => (($x * $y) % 6) === 0,
107+
self::PATTERN_110 => fn(int $x, int $y):bool => (($x * $y) % 6) < 3,
108+
self::PATTERN_111 => fn(int $x, int $y):bool => (($x + $y + (($x * $y) % 3)) % 2) === 0,
109109
][$this->maskPattern];
110110
}
111111

@@ -310,7 +310,7 @@ public static function testRule4(array $matrix, int $height, int $width):int{
310310
}
311311
}
312312

313-
return ((int)(abs($darkCells * 2 - $totalCells) * 10 / $totalCells) * 10);
313+
return (intdiv((abs($darkCells * 2 - $totalCells) * 10), $totalCells) * 10);
314314
}
315315

316316
}

src/Data/AlphaNum.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
use chillerlan\QRCode\Common\{BitBuffer, Mode};
1414

15-
use function array_flip, ceil, str_split;
15+
use function array_flip, ceil, intdiv, str_split;
1616

1717
/**
1818
* Alphanumeric mode: 0 to 9, A to Z, space, $ % * + - . / :
@@ -118,7 +118,7 @@ public static function decodeSegment(BitBuffer $bitBuffer, int $versionNumber):s
118118
}
119119

120120
$nextTwoCharsBits = $bitBuffer->read(11);
121-
$result .= $toAlphaNumericChar((int)($nextTwoCharsBits / 45));
121+
$result .= $toAlphaNumericChar(intdiv($nextTwoCharsBits, 45));
122122
$result .= $toAlphaNumericChar($nextTwoCharsBits % 45);
123123
$length -= 2;
124124
}

src/Data/Hanzi.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use chillerlan\QRCode\Common\{BitBuffer, Mode};
1414

1515
use Throwable;
16-
use function chr, implode, is_string, mb_convert_encoding, mb_detect_encoding,
16+
use function chr, implode, intdiv, is_string, mb_convert_encoding, mb_detect_encoding,
1717
mb_detect_order, mb_internal_encoding, mb_strlen, ord, sprintf, strlen;
1818

1919
/**
@@ -188,7 +188,7 @@ public static function decodeSegment(BitBuffer $bitBuffer, int $versionNumber):s
188188
while($length > 0){
189189
// Each 13 bits encodes a 2-byte character
190190
$twoBytes = $bitBuffer->read(13);
191-
$assembledTwoBytes = ((((int)($twoBytes / 0x060)) << 8) | ($twoBytes % 0x060));
191+
$assembledTwoBytes = ((intdiv($twoBytes, 0x060) << 8) | ($twoBytes % 0x060));
192192

193193
$assembledTwoBytes += ($assembledTwoBytes < 0x00a00) // 0x003BF
194194
? 0x0a1a1 // In the 0xA1A1 to 0xAAFE range

src/Data/Kanji.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use chillerlan\QRCode\Common\{BitBuffer, Mode};
1414

1515
use Throwable;
16-
use function chr, implode, is_string, mb_convert_encoding, mb_detect_encoding,
16+
use function chr, implode, intdiv, is_string, mb_convert_encoding, mb_detect_encoding,
1717
mb_detect_order, mb_internal_encoding, mb_strlen, ord, sprintf, strlen;
1818

1919
/**
@@ -174,7 +174,7 @@ public static function decodeSegment(BitBuffer $bitBuffer, int $versionNumber):s
174174
while($length > 0){
175175
// Each 13 bits encodes a 2-byte character
176176
$twoBytes = $bitBuffer->read(13);
177-
$assembledTwoBytes = ((((int)($twoBytes / 0x0c0)) << 8) | ($twoBytes % 0x0c0));
177+
$assembledTwoBytes = ((intdiv($twoBytes, 0x0c0) << 8) | ($twoBytes % 0x0c0));
178178

179179
$assembledTwoBytes += ($assembledTwoBytes < 0x01f00)
180180
? 0x08140 // In the 0x8140 to 0x9FFC range

src/Data/Number.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
use chillerlan\QRCode\Common\{BitBuffer, Mode};
1414

15-
use function array_flip, ceil, str_split, substr, unpack;
15+
use function array_flip, ceil, intdiv, str_split, substr, unpack;
1616

1717
/**
1818
* Numeric mode: decimal digits 0 to 9
@@ -140,8 +140,8 @@ public static function decodeSegment(BitBuffer $bitBuffer, int $versionNumber):s
140140
throw new QRCodeDataException('error decoding numeric value');
141141
}
142142

143-
$result .= $toNumericChar((int)($threeDigitsBits / 100));
144-
$result .= $toNumericChar((int)($threeDigitsBits / 10) % 10);
143+
$result .= $toNumericChar(intdiv($threeDigitsBits, 100));
144+
$result .= $toNumericChar(intdiv($threeDigitsBits, 10) % 10);
145145
$result .= $toNumericChar($threeDigitsBits % 10);
146146

147147
$length -= 3;
@@ -159,7 +159,7 @@ public static function decodeSegment(BitBuffer $bitBuffer, int $versionNumber):s
159159
throw new QRCodeDataException('error decoding numeric value');
160160
}
161161

162-
$result .= $toNumericChar((int)($twoDigitsBits / 10));
162+
$result .= $toNumericChar(intdiv($twoDigitsBits, 10));
163163
$result .= $toNumericChar($twoDigitsBits % 10);
164164
}
165165
elseif($length === 1){

src/Data/QRMatrix.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
namespace chillerlan\QRCode\Data;
1212

1313
use chillerlan\QRCode\Common\{BitBuffer, EccLevel, MaskPattern, ReedSolomonEncoder, Version};
14-
use function array_fill, array_map, array_reverse, count, floor;
14+
use function array_fill, array_map, array_reverse, count, floor, intdiv;
1515

1616
/**
1717
* Holds an array representation of the final QR Code that contains numerical values for later output modifications;
@@ -485,7 +485,7 @@ public function setVersionNumber():self{
485485
if($bits !== null){
486486

487487
for($i = 0; $i < 18; $i++){
488-
$a = (int)($i / 3);
488+
$a = intdiv($i, 3);
489489
$b = (($i % 3) + ($this->moduleCount - 8 - 3));
490490
$v = (($bits >> $i) & 1) === 1;
491491

src/Decoder/Binarizer.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace chillerlan\QRCode\Decoder;
1313

1414
use chillerlan\QRCode\Data\QRMatrix;
15-
use function array_fill, count, max;
15+
use function array_fill, count, intdiv, max;
1616

1717
/**
1818
* This class implements a local thresholding algorithm, which while slower than the
@@ -167,13 +167,14 @@ private function getHistogramBlackMatrix(int $width, int $height):BitMatrix{
167167
// Quickly calculates the histogram by sampling four rows from the image. This proved to be
168168
// more robust on the blackbox tests than sampling a diagonal as we used to do.
169169
$buckets = array_fill(0, self::LUMINANCE_BUCKETS, 0);
170+
$right = intdiv(($width * 4), 5);
171+
$x = intdiv($width, 5);
170172

171173
for($y = 1; $y < 5; $y++){
172-
$row = (int)($height * $y / 5);
174+
$row = intdiv(($height * $y), 5);
173175
$localLuminances = $this->source->getRow($row);
174-
$right = (int)(($width * 4) / 5);
175176

176-
for($x = (int)($width / 5); $x < $right; $x++){
177+
for(; $x < $right; $x++){
177178
$pixel = ($localLuminances[$x] & 0xff);
178179
$buckets[($pixel >> self::LUMINANCE_SHIFT)]++;
179180
}
@@ -262,7 +263,7 @@ private function calculateBlackPoints(int $subWidth, int $subHeight, int $width,
262263
//
263264
// The default assumption is that the block is light/background. Since no estimate for
264265
// the level of dark pixels exists locally, use half the min for the block.
265-
$average = (int)($min / 2);
266+
$average = ($min / 2);
266267

267268
if($y > 0 && $x > 0){
268269
// Correct the "white background" assumption for blocks that have neighbors by comparing
@@ -272,7 +273,7 @@ private function calculateBlackPoints(int $subWidth, int $subHeight, int $width,
272273
// the boundaries is used for the interior.
273274

274275
// The (min < bp) is arbitrary but works better than other heuristics that were tried.
275-
$averageNeighborBlackPoint = (int)(
276+
$averageNeighborBlackPoint = (
276277
($blackPoints[($y - 1)][$x] + (2 * $blackPoints[$y][($x - 1)]) + $blackPoints[($y - 1)][($x - 1)]) / 4
277278
);
278279

src/Detector/Detector.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use chillerlan\QRCode\Decoder\{Binarizer, LuminanceSourceInterface};
1515
use chillerlan\QRCode\Common\Version;
1616
use chillerlan\QRCode\Decoder\BitMatrix;
17-
use function abs, is_nan, max, min, round;
17+
use function abs, intdiv, is_nan, max, min, round;
1818
use const NAN;
1919

2020
/**
@@ -42,7 +42,7 @@ public function detect():BitMatrix{
4242

4343
$moduleSize = $this->calculateModuleSize($topLeft, $topRight, $bottomLeft);
4444
$dimension = $this->computeDimension($topLeft, $topRight, $bottomLeft, $moduleSize);
45-
$provisionalVersion = new Version((int)(($dimension - 17) / 4));
45+
$provisionalVersion = new Version(intdiv(($dimension - 17), 4));
4646
$alignmentPattern = null;
4747

4848
// Anything above version 1 has an alignment pattern

src/Detector/FinderPatternFinder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
namespace chillerlan\QRCode\Detector;
1515

1616
use chillerlan\QRCode\Decoder\BitMatrix;
17-
use function abs, count, usort;
17+
use function abs, count, intdiv, usort;
1818
use const PHP_FLOAT_MAX;
1919

2020
/**
@@ -56,7 +56,7 @@ public function find():array{
5656
// Let's assume that the maximum version QR Code we support takes up 1/4 the height of the
5757
// image, and then account for the center being 3 modules in size. This gives the smallest
5858
// number of pixels the center could be, so skip this often.
59-
$iSkip = (int)((3 * $dimension) / (4 * self::MAX_MODULES));
59+
$iSkip = intdiv((3 * $dimension), (4 * self::MAX_MODULES));
6060

6161
if($iSkip < self::MIN_SKIP){
6262
$iSkip = self::MIN_SKIP;

src/Detector/GridSampler.php

Lines changed: 50 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use chillerlan\QRCode\Data\QRMatrix;
1515
use chillerlan\QRCode\Decoder\BitMatrix;
1616
use Throwable;
17-
use function array_fill, count, sprintf;
17+
use function array_fill, count, intdiv, sprintf;
1818

1919
/**
2020
* Implementations of this class can, given locations of finder patterns for a QR code in an
@@ -31,6 +31,8 @@
3131
*/
3232
final class GridSampler{
3333

34+
private array $points;
35+
3436
/**
3537
* Checks a set of points that have been transformed to sample points on an image against
3638
* the image's dimensions to see if the point are even within the image.
@@ -42,20 +44,18 @@ final class GridSampler{
4244
* For efficiency, the method will check points from either end of the line until one is found
4345
* to be within the image. Because the set of points are assumed to be linear, this is valid.
4446
*
45-
* @param \chillerlan\QRCode\Decoder\BitMatrix $matrix image into which the points should map
46-
* @param float[] $points actual points in x1,y1,...,xn,yn form
47+
* @param int $dimension matrix width/height
4748
*
4849
* @throws \chillerlan\QRCode\Detector\QRCodeDetectorException if an endpoint is lies outside the image boundaries
4950
*/
50-
private function checkAndNudgePoints(BitMatrix $matrix, array $points):void{
51-
$dimension = $matrix->getSize();
52-
$nudged = true;
53-
$max = count($points);
51+
private function checkAndNudgePoints(int $dimension):void{
52+
$nudged = true;
53+
$max = count($this->points);
5454

5555
// Check and nudge points from start until we see some that are OK:
5656
for($offset = 0; $offset < $max && $nudged; $offset += 2){
57-
$x = (int)$points[$offset];
58-
$y = (int)$points[($offset + 1)];
57+
$x = (int)$this->points[$offset];
58+
$y = (int)$this->points[($offset + 1)];
5959

6060
if($x < -1 || $x > $dimension || $y < -1 || $y > $dimension){
6161
throw new QRCodeDetectorException(sprintf('checkAndNudgePoints 1, x: %s, y: %s, d: %s', $x, $y, $dimension));
@@ -64,31 +64,31 @@ private function checkAndNudgePoints(BitMatrix $matrix, array $points):void{
6464
$nudged = false;
6565

6666
if($x === -1){
67-
$points[$offset] = 0.0;
68-
$nudged = true;
67+
$this->points[$offset] = 0.0;
68+
$nudged = true;
6969
}
7070
elseif($x === $dimension){
71-
$points[$offset] = ($dimension - 1);
72-
$nudged = true;
71+
$this->points[$offset] = ($dimension - 1);
72+
$nudged = true;
7373
}
7474

7575
if($y === -1){
76-
$points[($offset + 1)] = 0.0;
77-
$nudged = true;
76+
$this->points[($offset + 1)] = 0.0;
77+
$nudged = true;
7878
}
7979
elseif($y === $dimension){
80-
$points[($offset + 1)] = ($dimension - 1);
81-
$nudged = true;
80+
$this->points[($offset + 1)] = ($dimension - 1);
81+
$nudged = true;
8282
}
8383

8484
}
85+
8586
// Check and nudge points from end:
8687
$nudged = true;
87-
$offset = (count($points) - 2);
8888

89-
for(; $offset >= 0 && $nudged; $offset -= 2){
90-
$x = (int)$points[$offset];
91-
$y = (int)$points[($offset + 1)];
89+
for($offset = ($max - 2); $offset >= 0 && $nudged; $offset -= 2){
90+
$x = (int)$this->points[$offset];
91+
$y = (int)$this->points[($offset + 1)];
9292

9393
if($x < -1 || $x > $dimension || $y < -1 || $y > $dimension){
9494
throw new QRCodeDetectorException(sprintf('checkAndNudgePoints 2, x: %s, y: %s, d: %s', $x, $y, $dimension));
@@ -97,24 +97,25 @@ private function checkAndNudgePoints(BitMatrix $matrix, array $points):void{
9797
$nudged = false;
9898

9999
if($x === -1){
100-
$points[$offset] = 0.0;
101-
$nudged = true;
100+
$this->points[$offset] = 0.0;
101+
$nudged = true;
102102
}
103103
elseif($x === $dimension){
104-
$points[$offset] = ($dimension - 1);
105-
$nudged = true;
104+
$this->points[$offset] = ($dimension - 1);
105+
$nudged = true;
106106
}
107107

108108
if($y === -1){
109-
$points[($offset + 1)] = 0.0;
110-
$nudged = true;
109+
$this->points[($offset + 1)] = 0.0;
110+
$nudged = true;
111111
}
112112
elseif($y === $dimension){
113-
$points[($offset + 1)] = ($dimension - 1);
114-
$nudged = true;
113+
$this->points[($offset + 1)] = ($dimension - 1);
114+
$nudged = true;
115115
}
116116

117117
}
118+
118119
}
119120

120121
/**
@@ -133,41 +134,45 @@ public function sampleGrid(BitMatrix $matrix, int $dimension, PerspectiveTransfo
133134
throw new QRCodeDetectorException('invalid matrix size');
134135
}
135136

136-
$bits = new BitMatrix($dimension);
137-
$points = array_fill(0, (2 * $dimension), 0.0);
137+
$bits = new BitMatrix($dimension);
138+
$this->points = array_fill(0, (2 * $dimension), 0.0);
138139

139140
for($y = 0; $y < $dimension; $y++){
140-
$max = count($points);
141+
$max = count($this->points);
141142
$iValue = ($y + 0.5);
142143

143144
for($x = 0; $x < $max; $x += 2){
144-
$points[$x] = (($x / 2) + 0.5);
145-
$points[($x + 1)] = $iValue;
145+
$this->points[$x] = (($x / 2) + 0.5);
146+
$this->points[($x + 1)] = $iValue;
146147
}
147-
148-
$transform->transformPoints($points);
148+
// phpcs:ignore
149+
[$this->points, ] = $transform->transformPoints($this->points);
149150
// Quick check to see if points transformed to something inside the image;
150151
// sufficient to check the endpoints
151-
$this->checkAndNudgePoints($matrix, $points);
152+
$this->checkAndNudgePoints($matrix->getSize());
152153

153-
try{
154+
// no need to try/catch as QRMatrix::set() will silently discard out of bounds values
155+
# try{
154156
for($x = 0; $x < $max; $x += 2){
155157
// Black(-ish) pixel
156-
$bits->set(($x / 2), $y, $matrix->check((int)$points[$x], (int)$points[($x + 1)]), QRMatrix::M_DATA);
158+
$bits->set(
159+
intdiv($x, 2),
160+
$y,
161+
$matrix->check((int)$this->points[$x], (int)$this->points[($x + 1)]),
162+
QRMatrix::M_DATA
163+
);
157164
}
158-
}
159-
// @codeCoverageIgnoreStart
160-
catch(Throwable $aioobe){//ArrayIndexOutOfBoundsException
165+
# }
166+
# catch(Throwable $aioobe){//ArrayIndexOutOfBoundsException
161167
// This feels wrong, but, sometimes if the finder patterns are misidentified, the resulting
162168
// transform gets "twisted" such that it maps a straight line of points to a set of points
163169
// whose endpoints are in bounds, but others are not. There is probably some mathematical
164170
// way to detect this about the transformation that I don't know yet.
165171
// This results in an ugly runtime exception despite our clever checks above -- can't have
166172
// that. We could check each point's coordinates but that feels duplicative. We settle for
167173
// catching and wrapping ArrayIndexOutOfBoundsException.
168-
throw new QRCodeDetectorException('ArrayIndexOutOfBoundsException');
169-
}
170-
// @codeCoverageIgnoreEnd
174+
# throw new QRCodeDetectorException('ArrayIndexOutOfBoundsException');
175+
# }
171176

172177
}
173178

0 commit comments

Comments
 (0)