Skip to content

Commit da5bdb8

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

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

src/Data/QRData.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,9 @@ public function estimateTotalBitLength():int{
165165

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

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

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

tests/Data/DataInterfaceTestAbstract.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace chillerlan\QRCodeTest\Data;
1212

1313
use chillerlan\QRCode\Common\{EccLevel, MaskPattern, Mode, Version};
14+
use PHPUnit\Framework\ExpectationFailedException;
1415
use chillerlan\QRCode\Data\{QRCodeDataException, QRData, QRDataModeInterface, QRMatrix};
1516
use chillerlan\QRCode\QROptions;
1617
use chillerlan\QRCodeTest\QRMaxLengthTrait;
@@ -187,7 +188,13 @@ public function testGetMinimumVersion(Version $version, EccLevel $eccLevel, stri
187188

188189
$minimumVersionNumber = $this->QRData->getMinimumVersion()->getVersionNumber();
189190

190-
$this::assertSame($version->getVersionNumber(), $minimumVersionNumber);
191+
try{
192+
$this::assertSame($version->getVersionNumber(), $minimumVersionNumber);
193+
}
194+
catch(ExpectationFailedException $e){
195+
$this::assertSame(($version->getVersionNumber() + 1), $minimumVersionNumber, 'safety margin');
196+
}
197+
191198
// verify the encoded data
192199
$this::assertSame($this->dataMode::DATAMODE, $bitBuffer->read(4));
193200
$this::assertSame($str, $this->dataMode::decodeSegment($bitBuffer, $minimumVersionNumber));

tests/Data/QRDataTest.php

+2-1
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)