chillerlan / php-qrcode

A PHP QR Code generator and reader with a user-friendly API.
https://smiley.codes/qrcode/
Apache License 2.0
2.01k stars 302 forks source link

[BUG] QRCode::render() might return unexpected results when data segments were added before the call #246

Closed codemasher closed 1 week ago

codemasher commented 9 months ago

Describe the bug or unexpected behaviour

Version 5 introduced full support for mixed mode QR Codes by adding several segments via the methods QRCode::addByteSegment() [...] and then call QRCode::render() without the $data parameter. However, it is possible to add several data segments and call the render method with data, which then adds another segment to the existing ones.

Steps to reproduce the behavior

Code sample (if applicable)

$qrcode = (new QRCode($options))
    ->addNumericSegment('1312')
    ->addAlphaNumSegment('ACAB')
;

$out = $qrcode->render('1312'); // rendered QR Code content: 1312ACAB1312

Expected behavior

The logical expectation is that the rendered QR Code would only contain the data given via the parameter or via the add*Segment() methods - this could be easily ensured by calling QRCode::clearSegments() after the $data check in line 79 in the code excerpt below.

https://github.com/chillerlan/php-qrcode/blob/87a833e797b88ea70047bf503cd1bbf67edb58a8/src/QRCode.php#L74-L92

However, I could see a legitimate reason for leaving the current behaivor as it is: adding an ECI designator before calling QRCode::render('<binary data>'), which is equivalent to calling QRCode::addEciSegment(<encoding>, '<binary data>') and then call the renderer without data.

$qrcode = (new QRCode($options))->addEciDesignator(ECICharset::WINDOWS_1251_CYRILLIC);

$out = $qrcode->render('<encoded binary data>'); // rendered QR Code content: <ECI header><encoded binary data>

I'm not exactly sure how to proceed with this (fix it or just document the behavior), so I'm leaving it here for a while to gather community opinions (maybe!?).

Environment:

codemasher commented 1 week ago

Won't fix - information has been added to the method`s doc block.