🚿 unify exceptions to QRCodeException

This commit is contained in:
smiley
2022-05-10 14:30:30 +02:00
parent f1b05e420d
commit 3c42ccb259
5 changed files with 30 additions and 26 deletions
+3 -3
View File
@@ -10,7 +10,7 @@
namespace chillerlan\QRCode\Common;
use InvalidArgumentException;
use chillerlan\QRCode\QRCodeException;
use function count, floor, min;
/**
@@ -108,12 +108,12 @@ final class BitBuffer{
* @param int $numBits number of bits to read
*
* @return int representing the bits read. The bits will appear as the least-significant bits of the int
* @throws InvalidArgumentException if numBits isn't in [1,32] or more than is available
* @throws \chillerlan\QRCode\QRCodeException if numBits isn't in [1,32] or more than is available
*/
public function read(int $numBits):int{
if($numBits < 1 || $numBits > 32 || $numBits > $this->available()){
throw new InvalidArgumentException('invalid $numBits: '.$numBits);
throw new QRCodeException('invalid $numBits: '.$numBits);
}
$result = 0;
+3 -3
View File
@@ -10,7 +10,7 @@
namespace chillerlan\QRCode\Common;
use InvalidArgumentException;
use chillerlan\QRCode\QRCodeException;
use function array_key_exists;
/**
@@ -93,12 +93,12 @@ final class ECICharset{
private int $charsetID;
/**
*
* @throws \chillerlan\QRCode\QRCodeException
*/
public function __construct(int $charsetID){
if(!array_key_exists($charsetID, self::MB_ENCODINGS)){
throw new InvalidArgumentException('invalid charset id: '.$charsetID);
throw new QRCodeException('invalid charset id: '.$charsetID);
}
$this->charsetID = $charsetID;
+7 -4
View File
@@ -11,7 +11,7 @@
namespace chillerlan\QRCode\Common;
use InvalidArgumentException;
use chillerlan\QRCode\QRCodeException;
use function array_fill;
@@ -84,11 +84,12 @@ final class GF256{
/**
* @return GenericGFPoly the monomial representing coefficient * x^degree
* @throws \chillerlan\QRCode\QRCodeException
*/
public static function buildMonomial(int $degree, int $coefficient):GenericGFPoly{
if($degree < 0){
throw new InvalidArgumentException();
throw new QRCodeException('degree < 0');
}
$coefficients = array_fill(0, $degree + 1, 0);
@@ -114,11 +115,12 @@ final class GF256{
/**
* @return int base 2 log of a in GF(size)
* @throws \chillerlan\QRCode\QRCodeException
*/
public static function log(int $a):int{
if($a < 1){
throw new InvalidArgumentException();
throw new QRCodeException('$a < 1');
}
return self::logTable[$a];
@@ -126,11 +128,12 @@ final class GF256{
/**
* @return int multiplicative inverse of a
* @throws \chillerlan\QRCode\QRCodeException
*/
public static function inverse(int $a):int{
if($a === 0){
throw new InvalidArgumentException();
throw new QRCodeException('$a === 0');
}
return self::expTable[256 - self::logTable[$a] - 1];
+9 -8
View File
@@ -11,7 +11,7 @@
namespace chillerlan\QRCode\Common;
use InvalidArgumentException;
use chillerlan\QRCode\QRCodeException;
use function array_fill, array_slice, array_splice, count;
@@ -33,18 +33,18 @@ final class GenericGFPoly{
* from most significant (highest-power term) coefficient to least significant
* @param int|null $degree
*
* @throws \InvalidArgumentException if argument is null or empty, or if leading coefficient is 0 and this is not a
* constant polynomial (that is, it is not the monomial "0")
* @throws \chillerlan\QRCode\QRCodeException if argument is null or empty, or if leading coefficient is 0 and this
* is not a constant polynomial (that is, it is not the monomial "0")
*/
public function __construct(array $coefficients, int $degree = null){
$degree ??= 0;
if(empty($coefficients)){
throw new InvalidArgumentException('arg $coefficients is empty');
throw new QRCodeException('arg $coefficients is empty');
}
if($degree < 0){
throw new InvalidArgumentException('negative degree');
throw new QRCodeException('negative degree');
}
$coefficientsLength = count($coefficients);
@@ -138,11 +138,12 @@ final class GenericGFPoly{
/**
* @return \chillerlan\QRCode\Common\GenericGFPoly[] [quotient, remainder]
* @throws \chillerlan\QRCode\QRCodeException
*/
public function divide(GenericGFPoly $other):array{
if($other->isZero()){
throw new InvalidArgumentException('Division by 0');
throw new QRCodeException('Division by 0');
}
$quotient = new self([0]);
@@ -185,12 +186,12 @@ final class GenericGFPoly{
}
/**
*
* @throws \chillerlan\QRCode\QRCodeException
*/
public function multiplyByMonomial(int $degree, int $coefficient):self{
if($degree < 0){
throw new InvalidArgumentException();
throw new QRCodeException('degree < 0');
}
if($coefficient === 0){
+8 -8
View File
@@ -11,7 +11,7 @@
namespace chillerlan\QRCode\Common;
use RuntimeException;
use chillerlan\QRCode\QRCodeException;
use function array_fill, array_reverse, count;
/**
@@ -85,7 +85,7 @@ final class ReedSolomonDecoder{
* @param int $numEccCodewords number of error-correction codewords available
*
* @return int[]
* @throws \RuntimeException if decoding fails for any reason
* @throws \chillerlan\QRCode\QRCodeException if decoding fails for any reason
*/
private function decodeWords(array $received, int $numEccCodewords):array{
$poly = new GenericGFPoly($received);
@@ -119,7 +119,7 @@ final class ReedSolomonDecoder{
$position = $receivedCount - 1 - GF256::log($errorLocations[$i]);
if($position < 0){
throw new RuntimeException('Bad error location');
throw new QRCodeException('Bad error location');
}
$received[$position] ^= $errorMagnitudes[$i];
@@ -130,7 +130,7 @@ final class ReedSolomonDecoder{
/**
* @return \chillerlan\QRCode\Common\GenericGFPoly[] [sigma, omega]
* @throws \RuntimeException
* @throws \chillerlan\QRCode\QRCodeException
*/
private function runEuclideanAlgorithm(GenericGFPoly $a, GenericGFPoly $b, int $R):array{
// Assume a's degree is >= b's
@@ -158,14 +158,14 @@ final class ReedSolomonDecoder{
$t = $q->multiply($tLast)->addOrSubtract($tLastLast);
if($r->getDegree() >= $rLast->getDegree()){
throw new RuntimeException('Division algorithm failed to reduce polynomial?');
throw new QRCodeException('Division algorithm failed to reduce polynomial?');
}
}
$sigmaTildeAtZero = $t->getCoefficient(0);
if($sigmaTildeAtZero === 0){
throw new RuntimeException('sigmaTilde(0) was zero');
throw new QRCodeException('sigmaTilde(0) was zero');
}
$inverse = GF256::inverse($sigmaTildeAtZero);
@@ -174,7 +174,7 @@ final class ReedSolomonDecoder{
}
/**
* @throws \RuntimeException
* @throws \chillerlan\QRCode\QRCodeException
*/
private function findErrorLocations(GenericGFPoly $errorLocator):array{
// This is a direct application of Chien's search
@@ -195,7 +195,7 @@ final class ReedSolomonDecoder{
}
if($e !== $numErrors){
throw new RuntimeException('Error locator degree does not match number of roots');
throw new QRCodeException('Error locator degree does not match number of roots');
}
return $result;