Skip to content

Commit b33d6f7

Browse files
committed
Implement additional hint to fix tests
This fixes #112
1 parent f70a53b commit b33d6f7

12 files changed

+100
-84
lines changed

lib/Common/DefaultGridSampler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function sampleGrid_(
8181
PerspectiveTransform $transform
8282
): BitMatrix {
8383
if ($dimensionX <= 0 || $dimensionY <= 0) {
84-
throw NotFoundException::getNotFoundInstance("X or Y dimensions smaller than zero");
84+
throw new NotFoundException("X or Y dimensions smaller than zero");
8585
}
8686
$bits = new BitMatrix($dimensionX, $dimensionY);
8787
$points = fill_array(0, 2 * $dimensionX, 0.0);
@@ -111,7 +111,7 @@ public function sampleGrid_(
111111
// This results in an ugly runtime exception despite our clever checks above -- can't have
112112
// that. We could check each point's coordinates but that feels duplicative. We settle for
113113
// catching and wrapping ArrayIndexOutOfBoundsException.
114-
throw NotFoundException::getNotFoundInstance("ArrayIndexOutOfBoundsException");
114+
throw new NotFoundException("ArrayIndexOutOfBoundsException");
115115
}
116116
}
117117

lib/Common/Detector/MonochromeRectangleDetector.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ private function findCornerFromCenter(
177177
}
178178
if ($range == null) {
179179
if ($lastRange == null) {
180-
throw NotFoundException::getNotFoundInstance("No corner from center found");
180+
throw new NotFoundException("No corner from center found");
181181
}
182182
// lastRange was found
183183
if ($deltaX == 0) {
@@ -207,7 +207,7 @@ private function findCornerFromCenter(
207207
}
208208
$lastRange = $range;
209209
}
210-
throw NotFoundException::getNotFoundInstance("No corner from center found");
210+
throw new NotFoundException("No corner from center found");
211211
}
212212

213213

lib/Common/GlobalHistogramBinarizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ private static function estimateBlackPoint(array $buckets): int
145145
// If there is too little contrast in the image to pick a meaningful black point, throw rather
146146
// than waste time trying to decode the image, and risk false positives.
147147
if ($secondPeak - $firstPeak <= $numBuckets / 16) {
148-
throw NotFoundException::getNotFoundInstance("too little contrast in the image to pick a meaningful black point");
148+
throw new NotFoundException("too little contrast in the image to pick a meaningful black point");
149149
}
150150

151151
// Find a valley between them that is low and closer to the white peak.

lib/Common/GridSampler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ protected static function checkAndNudgePoints(
9696
$x = (int)$points[$offset];
9797
$y = (int)$points[$offset + 1];
9898
if ($x < -1 || $x > $width || $y < -1 || $y > $height) {
99-
throw NotFoundException::getNotFoundInstance("Endpoint lies outside the image boundaries");
99+
throw new NotFoundException("Endpoint ($x, $y) lies outside the image boundaries ($width, $height)");
100100
}
101101
$nudged = false;
102102
if ($x == -1) {
@@ -120,7 +120,7 @@ protected static function checkAndNudgePoints(
120120
$x = (int)$points[$offset];
121121
$y = (int)$points[$offset + 1];
122122
if ($x < -1 || $x > $width || $y < -1 || $y > $height) {
123-
throw NotFoundException::getNotFoundInstance("Endpoint lies outside the image boundaries");
123+
throw new NotFoundException("Endpoint ($x, $y) lies outside the image boundaries ($width, $height)");
124124
}
125125
$nudged = false;
126126
if ($x == -1) {

lib/IMagickLuminanceSource.php

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,32 @@ public function rotateCounterClockwise45(): void
5656
throw new \RuntimeException("This LuminanceSource does not support rotateCounterClockwise45");
5757
}
5858

59+
/**
60+
* TODO: move to some utility class or something
61+
* Converts shorthand memory notation value to bytes
62+
* From http://php.net/manual/en/function.ini-get.php
63+
*
64+
* @param int $val Memory size shorthand notation string
65+
*/
66+
protected static function kmgStringToBytes(string $val)
67+
{
68+
$val = trim($val);
69+
$last = strtolower($val[strlen($val) - 1]);
70+
$val = substr($val, 0, -1);
71+
switch ($last) {
72+
// The 'G' modifier is available since PHP 5.1.0
73+
case 'g':
74+
$val *= 1024;
75+
// no break
76+
case 'm':
77+
$val *= 1024;
78+
// no break
79+
case 'k':
80+
$val *= 1024;
81+
}
82+
return $val;
83+
}
84+
5985
public function _IMagickLuminanceSource(\Imagick $image, $width, $height): void
6086
{
6187
parent::__construct($width, $height);
@@ -66,38 +92,13 @@ public function _IMagickLuminanceSource(\Imagick $image, $width, $height): void
6692
$this->top = 0;
6793
$this->image = $image;
6894

69-
/**
70-
* Converts shorthand memory notation value to bytes
71-
* From http://php.net/manual/en/function.ini-get.php
72-
*
73-
* @param int $val Memory size shorthand notation string
74-
*/
75-
function kmgStringToBytes(string $val)
76-
{
77-
$val = trim($val);
78-
$last = strtolower($val[strlen($val) - 1]);
79-
$val = substr($val, 0, -1);
80-
switch ($last) {
81-
// The 'G' modifier is available since PHP 5.1.0
82-
case 'g':
83-
$val *= 1024;
84-
// no break
85-
case 'm':
86-
$val *= 1024;
87-
// no break
88-
case 'k':
89-
$val *= 1024;
90-
}
91-
return $val;
92-
}
93-
9495
// In order to measure pure decoding speed, we convert the entire image to a greyscale array
9596
// up front, which is the same as the Y channel of the YUVLuminanceSource in the real app.
9697
$this->luminances = [];
9798

9899
$image->setImageColorspace(\Imagick::COLORSPACE_GRAY);
99100
// Check that we actually have enough space to do it
100-
if ($width * $height * 16 * 3 > kmgStringToBytes(ini_get('memory_limit'))) {
101+
if ($width * $height * 16 * 3 > $this->kmgStringToBytes(ini_get('memory_limit'))) {
101102
throw new \RuntimeException("PHP Memory Limit does not allow pixel export.");
102103
}
103104
$pixels = $image->exportImagePixels(1, 1, $width, $height, "RGB", \Imagick::PIXEL_CHAR);

lib/Qrcode/Decoder/BitMatrixParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ public function readVersion()
221221

222222
return $theParsedVersion;
223223
}
224-
throw FormatException::getFormatInstance();
224+
throw FormatException::getFormatInstance("both version information locations cannot be parsed as the valid encoding of version information");
225225
}
226226

227227
/**

lib/Qrcode/Decoder/DecodedBitStreamParser.php

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
namespace Zxing\Qrcode\Decoder;
1919

20+
use ValueError;
2021
use Zxing\Common\BitSource;
2122
use Zxing\Common\CharacterSetECI;
2223
use Zxing\Common\DecoderResult;
@@ -53,7 +54,7 @@ public static function decode(
5354
array|null $hints
5455
): \Zxing\Common\DecoderResult {
5556
$bits = new BitSource($bytes);
56-
$result = '';//new StringBuilder(50);
57+
$result = ''; //new StringBuilder(50);
5758
$byteSegments = [];
5859
$symbolSequence = -1;
5960
$parityData = -1;
@@ -76,7 +77,7 @@ public static function decode(
7677
$fc1InEffect = true;
7778
} elseif ($mode == Mode::$STRUCTURED_APPEND) {
7879
if ($bits->available() < 16) {
79-
throw FormatException::getFormatInstance();
80+
throw FormatException::getFormatInstance("Bits available < 16");
8081
}
8182
// sequence number and parity is added later to the result metadata
8283
// Read next 8 bits (symbol sequence #) and 8 bits (parity data), then continue
@@ -87,7 +88,7 @@ public static function decode(
8788
$value = self::parseECIValue($bits);
8889
$currentCharacterSetECI = CharacterSetECI::getCharacterSetECIByValue($value);
8990
if ($currentCharacterSetECI == null) {
90-
throw FormatException::getFormatInstance();
91+
throw FormatException::getFormatInstance("Current character set ECI is null");
9192
}
9293
} else {
9394
// First handle Hanzi mode which does not start with character count
@@ -111,22 +112,22 @@ public static function decode(
111112
} elseif ($mode == Mode::$KANJI) {
112113
self::decodeKanjiSegment($bits, $result, $count);
113114
} else {
114-
throw FormatException::getFormatInstance();
115+
throw FormatException::getFormatInstance("Unknown mode $mode to decode");
115116
}
116117
}
117118
}
118119
}
119120
} while ($mode != Mode::$TERMINATOR);
120-
} catch (\InvalidArgumentException) {
121+
} catch (\InvalidArgumentException $e) {
121122
// from readBits() calls
122-
throw FormatException::getFormatInstance();
123+
throw FormatException::getFormatInstance("Invalid argument exception when formatting: " . $e->getMessage());
123124
}
124125

125126
return new DecoderResult(
126127
$bytes,
127128
$result,
128129
empty($byteSegments) ? null : $byteSegments,
129-
$ecLevel == null ? null : 'L',//ErrorCorrectionLevel::toString($ecLevel),
130+
$ecLevel == null ? null : 'L', //ErrorCorrectionLevel::toString($ecLevel),
130131
$symbolSequence,
131132
$parityData
132133
);
@@ -151,7 +152,7 @@ private static function parseECIValue(BitSource $bits): int
151152

152153
return (($firstByte & 0x1F) << 16) | $secondThirdBytes;
153154
}
154-
throw FormatException::getFormatInstance();
155+
throw FormatException::getFormatInstance("ECI Value parsing failed.");
155156
}
156157

157158
/**
@@ -166,7 +167,7 @@ private static function decodeHanziSegment(
166167
): void {
167168
// Don't crash trying to read more bits than we have available.
168169
if ($count * 13 > $bits->available()) {
169-
throw FormatException::getFormatInstance();
170+
throw FormatException::getFormatInstance("Trying to read more bits than we have available");
170171
}
171172

172173
// Each character will require 2 bytes. Read the characters as 2-byte pairs
@@ -184,8 +185,8 @@ private static function decodeHanziSegment(
184185
// In the 0xB0A1 to 0xFAFE range
185186
$assembledTwoBytes += 0x0A6A1;
186187
}
187-
$buffer[$offset] = (($assembledTwoBytes >> 8) & 0xFF);//(byte)
188-
$buffer[$offset + 1] = ($assembledTwoBytes & 0xFF);//(byte)
188+
$buffer[$offset] = (($assembledTwoBytes >> 8) & 0xFF); //(byte)
189+
$buffer[$offset + 1] = ($assembledTwoBytes & 0xFF); //(byte)
189190
$offset += 2;
190191
$count--;
191192
}
@@ -201,11 +202,11 @@ private static function decodeNumericSegment(
201202
while ($count >= 3) {
202203
// Each 10 bits encodes three digits
203204
if ($bits->available() < 10) {
204-
throw FormatException::getFormatInstance();
205+
throw FormatException::getFormatInstance("Not enough bits available");
205206
}
206207
$threeDigitsBits = $bits->readBits(10);
207208
if ($threeDigitsBits >= 1000) {
208-
throw FormatException::getFormatInstance();
209+
throw FormatException::getFormatInstance("Too many three digit bits");
209210
}
210211
$result .= (self::toAlphaNumericChar($threeDigitsBits / 100));
211212
$result .= (self::toAlphaNumericChar(($threeDigitsBits / 10) % 10));
@@ -215,22 +216,22 @@ private static function decodeNumericSegment(
215216
if ($count == 2) {
216217
// Two digits left over to read, encoded in 7 bits
217218
if ($bits->available() < 7) {
218-
throw FormatException::getFormatInstance();
219+
throw FormatException::getFormatInstance("Two digits left over to read, encoded in 7 bits, but only " . $bits->available() . ' bits available');
219220
}
220221
$twoDigitsBits = $bits->readBits(7);
221222
if ($twoDigitsBits >= 100) {
222-
throw FormatException::getFormatInstance();
223+
throw FormatException::getFormatInstance("Too many bits: $twoDigitsBits expected < 100");
223224
}
224225
$result .= (self::toAlphaNumericChar($twoDigitsBits / 10));
225226
$result .= (self::toAlphaNumericChar($twoDigitsBits % 10));
226227
} elseif ($count == 1) {
227228
// One digit left over to read
228229
if ($bits->available() < 4) {
229-
throw FormatException::getFormatInstance();
230+
throw FormatException::getFormatInstance("One digit left to read, but < 4 bits available");
230231
}
231232
$digitBits = $bits->readBits(4);
232233
if ($digitBits >= 10) {
233-
throw FormatException::getFormatInstance();
234+
throw FormatException::getFormatInstance("Too many bits: $digitBits expected < 10");
234235
}
235236
$result .= (self::toAlphaNumericChar($digitBits));
236237
}
@@ -242,7 +243,7 @@ private static function decodeNumericSegment(
242243
private static function toAlphaNumericChar(int|float $value)
243244
{
244245
if ($value >= count(self::$ALPHANUMERIC_CHARS)) {
245-
throw FormatException::getFormatInstance();
246+
throw FormatException::getFormatInstance("$value has too many alphanumeric chars");
246247
}
247248

248249
return self::$ALPHANUMERIC_CHARS[$value];
@@ -258,7 +259,7 @@ private static function decodeAlphanumericSegment(
258259
$start = strlen((string) $result);
259260
while ($count > 1) {
260261
if ($bits->available() < 11) {
261-
throw FormatException::getFormatInstance();
262+
throw FormatException::getFormatInstance("Not enough bits available to read two expected characters");
262263
}
263264
$nextTwoCharsBits = $bits->readBits(11);
264265
$result .= (self::toAlphaNumericChar($nextTwoCharsBits / 45));
@@ -268,7 +269,7 @@ private static function decodeAlphanumericSegment(
268269
if ($count == 1) {
269270
// special case: one character left
270271
if ($bits->available() < 6) {
271-
throw FormatException::getFormatInstance();
272+
throw FormatException::getFormatInstance("Not enough bits available to read one expected character");
272273
}
273274
$result .= self::toAlphaNumericChar($bits->readBits(6));
274275
}
@@ -279,7 +280,7 @@ private static function decodeAlphanumericSegment(
279280
if ($result[$i] == '%') {
280281
if ($i < strlen((string) $result) - 1 && $result[$i + 1] == '%') {
281282
// %% is rendered as %
282-
$result = substr_replace($result, '', $i + 1, 1);//deleteCharAt(i + 1);
283+
$result = substr_replace($result, '', $i + 1, 1); //deleteCharAt(i + 1);
283284
} else {
284285
// In alpha mode, % should be converted to FNC1 separator 0x1D
285286
$result[$i] = chr(0x1D);
@@ -299,12 +300,12 @@ private static function decodeByteSegment(
299300
): void {
300301
// Don't crash trying to read more bits than we have available.
301302
if (8 * $count > $bits->available()) {
302-
throw FormatException::getFormatInstance();
303+
throw FormatException::getFormatInstance("Trying to read more bits than we have available");
303304
}
304305

305306
$readBytes = fill_array(0, $count, 0);
306307
for ($i = 0; $i < $count; $i++) {
307-
$readBytes[$i] = $bits->readBits(8);//(byte)
308+
$readBytes[$i] = $bits->readBits(8); //(byte)
308309
}
309310
$text = implode(array_map('chr', $readBytes));
310311
$encoding = '';
@@ -315,12 +316,16 @@ private static function decodeByteSegment(
315316
// Shift_JIS -- without anything like an ECI designator to
316317
// give a hint.
317318

318-
$encoding = mb_detect_encoding($text, $hints);
319+
try {
320+
$encoding = mb_detect_encoding($text, $hints);
321+
} catch (ValueError $e) {
322+
$encoding = mb_detect_encoding($text, mb_detect_order(), false);
323+
}
319324
} else {
320325
$encoding = $currentCharacterSetECI->name();
321326
}
322-
// $result.= mb_convert_encoding($text ,$encoding);//(new String(readBytes, encoding));
323-
$result .= $text;//(new String(readBytes, encoding));
327+
$result .= mb_convert_encoding($text, $encoding); //(new String(readBytes, encoding));
328+
// $result .= $text; //(new String(readBytes, encoding));
324329

325330
$byteSegments = array_merge($byteSegments, $readBytes);
326331
}
@@ -332,7 +337,7 @@ private static function decodeKanjiSegment(
332337
): void {
333338
// Don't crash trying to read more bits than we have available.
334339
if ($count * 13 > $bits->available()) {
335-
throw FormatException::getFormatInstance();
340+
throw FormatException::getFormatInstance("Trying to read more bits than we have available");
336341
}
337342

338343
// Each character will require 2 bytes. Read the characters as 2-byte pairs
@@ -350,7 +355,7 @@ private static function decodeKanjiSegment(
350355
// In the 0xE040 to 0xEBBF range
351356
$assembledTwoBytes += 0x0C140;
352357
}
353-
$buffer[$offset] = ($assembledTwoBytes >> 8);//(byte)
358+
$buffer[$offset] = ($assembledTwoBytes >> 8); //(byte)
354359
$buffer[$offset + 1] = $assembledTwoBytes; //(byte)
355360
$offset += 2;
356361
$count--;

lib/Qrcode/Detector/AlignmentPatternFinder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public function find()
126126
return $this->possibleCenters[0];
127127
}
128128

129-
throw NotFoundException::getNotFoundInstance("Bottom right alignment pattern not found");
129+
throw new NotFoundException("Bottom right alignment pattern not found");
130130
}
131131

132132
/**

0 commit comments

Comments
 (0)