Skip to content

Commit df49b3f

Browse files
committed
🔧 estimated bit length was correct, add a safety margin instead
1 parent ccc7376 commit df49b3f

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

src/Data/QRData.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,9 @@ public function estimateTotalBitLength():int{
164164

165165
// it seems that in some cases the estimated total length is not 100% accurate,
166166
// so we substract 4 bits from the total when not in mixed mode
167-
# if(count($this->dataSegments) <= 1){
168-
# $length -= 4;
169-
# }
167+
if(count($this->dataSegments) <= 1){
168+
$length -= 4;
169+
}
170170

171171
// we've got a match!
172172
// or let's see if there's a higher version number available
@@ -194,7 +194,7 @@ public function getMinimumVersion():Version{
194194

195195
// guess the version number within the given range
196196
for($version = $this->options->versionMin; $version <= $this->options->versionMax; $version++){
197-
if($total <= $this->maxBitsForEcc[$version]){
197+
if($total <= ($this->maxBitsForEcc[$version] - 4)){
198198
return new Version($version);
199199
}
200200
}

tests/Data/DataInterfaceTestAbstract.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use chillerlan\QRCodeTest\QRMaxLengthTrait;
1717
use Exception, Generator;
1818
use PHPUnit\Framework\Attributes\DataProvider;
19+
use PHPUnit\Framework\ExpectationFailedException;
1920
use PHPUnit\Framework\TestCase;
2021
use function array_map, hex2bin, mb_strlen, mb_substr, sprintf, str_repeat, strlen, substr;
2122

@@ -178,7 +179,13 @@ public function testGetMinimumVersion(Version $version, EccLevel $eccLevel, stri
178179

179180
$minimumVersionNumber = $this->QRData->getMinimumVersion()->getVersionNumber();
180181

181-
$this::assertSame($version->getVersionNumber(), $minimumVersionNumber);
182+
try{
183+
$this::assertSame($version->getVersionNumber(), $minimumVersionNumber);
184+
}
185+
catch(ExpectationFailedException $e){
186+
$this::assertSame(($version->getVersionNumber() + 1), $minimumVersionNumber, 'safety margin');
187+
}
188+
182189
// verify the encoded data
183190
$this::assertSame($this->dataMode::DATAMODE, $bitBuffer->read(4));
184191
$this::assertSame($str, $this->dataMode::decodeSegment($bitBuffer, $minimumVersionNumber));

tests/Data/QRDataTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ public function testEstimateTotalBitLength():void{
8686

8787
$qrData = new QRData($options, [new Byte($str)]);
8888

89-
$this::assertSame(980, $qrData->estimateTotalBitLength());
89+
$this::assertSame(976, $qrData->estimateTotalBitLength());
90+
$this::assertSame(11, $qrData->getMinimumVersion()->getVersionNumber()); // version adjusted to 11
9091
}
9192

9293
}

0 commit comments

Comments
 (0)