Skip to content

Commit 3c42ccb

Browse files
committed
🚿 unify exceptions to QRCodeException
1 parent f1b05e4 commit 3c42ccb

File tree

5 files changed

+30
-26
lines changed

5 files changed

+30
-26
lines changed

src/Common/BitBuffer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace chillerlan\QRCode\Common;
1212

13-
use InvalidArgumentException;
13+
use chillerlan\QRCode\QRCodeException;
1414
use function count, floor, min;
1515

1616
/**
@@ -108,12 +108,12 @@ public function available():int{
108108
* @param int $numBits number of bits to read
109109
*
110110
* @return int representing the bits read. The bits will appear as the least-significant bits of the int
111-
* @throws InvalidArgumentException if numBits isn't in [1,32] or more than is available
111+
* @throws \chillerlan\QRCode\QRCodeException if numBits isn't in [1,32] or more than is available
112112
*/
113113
public function read(int $numBits):int{
114114

115115
if($numBits < 1 || $numBits > 32 || $numBits > $this->available()){
116-
throw new InvalidArgumentException('invalid $numBits: '.$numBits);
116+
throw new QRCodeException('invalid $numBits: '.$numBits);
117117
}
118118

119119
$result = 0;

src/Common/ECICharset.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace chillerlan\QRCode\Common;
1212

13-
use InvalidArgumentException;
13+
use chillerlan\QRCode\QRCodeException;
1414
use function array_key_exists;
1515

1616
/**
@@ -93,12 +93,12 @@ final class ECICharset{
9393
private int $charsetID;
9494

9595
/**
96-
*
96+
* @throws \chillerlan\QRCode\QRCodeException
9797
*/
9898
public function __construct(int $charsetID){
9999

100100
if(!array_key_exists($charsetID, self::MB_ENCODINGS)){
101-
throw new InvalidArgumentException('invalid charset id: '.$charsetID);
101+
throw new QRCodeException('invalid charset id: '.$charsetID);
102102
}
103103

104104
$this->charsetID = $charsetID;

src/Common/GF256.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace chillerlan\QRCode\Common;
1313

14-
use InvalidArgumentException;
14+
use chillerlan\QRCode\QRCodeException;
1515

1616
use function array_fill;
1717

@@ -84,11 +84,12 @@ public static function addOrSubtract(int $a, int $b):int{
8484

8585
/**
8686
* @return GenericGFPoly the monomial representing coefficient * x^degree
87+
* @throws \chillerlan\QRCode\QRCodeException
8788
*/
8889
public static function buildMonomial(int $degree, int $coefficient):GenericGFPoly{
8990

9091
if($degree < 0){
91-
throw new InvalidArgumentException();
92+
throw new QRCodeException('degree < 0');
9293
}
9394

9495
$coefficients = array_fill(0, $degree + 1, 0);
@@ -114,23 +115,25 @@ public static function exp(int $a):int{
114115

115116
/**
116117
* @return int base 2 log of a in GF(size)
118+
* @throws \chillerlan\QRCode\QRCodeException
117119
*/
118120
public static function log(int $a):int{
119121

120122
if($a < 1){
121-
throw new InvalidArgumentException();
123+
throw new QRCodeException('$a < 1');
122124
}
123125

124126
return self::logTable[$a];
125127
}
126128

127129
/**
128130
* @return int multiplicative inverse of a
131+
* @throws \chillerlan\QRCode\QRCodeException
129132
*/
130133
public static function inverse(int $a):int{
131134

132135
if($a === 0){
133-
throw new InvalidArgumentException();
136+
throw new QRCodeException('$a === 0');
134137
}
135138

136139
return self::expTable[256 - self::logTable[$a] - 1];

src/Common/GenericGFPoly.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace chillerlan\QRCode\Common;
1313

14-
use InvalidArgumentException;
14+
use chillerlan\QRCode\QRCodeException;
1515

1616
use function array_fill, array_slice, array_splice, count;
1717

@@ -33,18 +33,18 @@ final class GenericGFPoly{
3333
* from most significant (highest-power term) coefficient to least significant
3434
* @param int|null $degree
3535
*
36-
* @throws \InvalidArgumentException if argument is null or empty, or if leading coefficient is 0 and this is not a
37-
* constant polynomial (that is, it is not the monomial "0")
36+
* @throws \chillerlan\QRCode\QRCodeException if argument is null or empty, or if leading coefficient is 0 and this
37+
* is not a constant polynomial (that is, it is not the monomial "0")
3838
*/
3939
public function __construct(array $coefficients, int $degree = null){
4040
$degree ??= 0;
4141

4242
if(empty($coefficients)){
43-
throw new InvalidArgumentException('arg $coefficients is empty');
43+
throw new QRCodeException('arg $coefficients is empty');
4444
}
4545

4646
if($degree < 0){
47-
throw new InvalidArgumentException('negative degree');
47+
throw new QRCodeException('negative degree');
4848
}
4949

5050
$coefficientsLength = count($coefficients);
@@ -138,11 +138,12 @@ public function multiply(GenericGFPoly $other):self{
138138

139139
/**
140140
* @return \chillerlan\QRCode\Common\GenericGFPoly[] [quotient, remainder]
141+
* @throws \chillerlan\QRCode\QRCodeException
141142
*/
142143
public function divide(GenericGFPoly $other):array{
143144

144145
if($other->isZero()){
145-
throw new InvalidArgumentException('Division by 0');
146+
throw new QRCodeException('Division by 0');
146147
}
147148

148149
$quotient = new self([0]);
@@ -185,12 +186,12 @@ public function multiplyInt(int $scalar):self{
185186
}
186187

187188
/**
188-
*
189+
* @throws \chillerlan\QRCode\QRCodeException
189190
*/
190191
public function multiplyByMonomial(int $degree, int $coefficient):self{
191192

192193
if($degree < 0){
193-
throw new InvalidArgumentException();
194+
throw new QRCodeException('degree < 0');
194195
}
195196

196197
if($coefficient === 0){

src/Common/ReedSolomonDecoder.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace chillerlan\QRCode\Common;
1313

14-
use RuntimeException;
14+
use chillerlan\QRCode\QRCodeException;
1515
use function array_fill, array_reverse, count;
1616

1717
/**
@@ -85,7 +85,7 @@ private function correctErrors(array $codewordBytes, int $numDataCodewords):arra
8585
* @param int $numEccCodewords number of error-correction codewords available
8686
*
8787
* @return int[]
88-
* @throws \RuntimeException if decoding fails for any reason
88+
* @throws \chillerlan\QRCode\QRCodeException if decoding fails for any reason
8989
*/
9090
private function decodeWords(array $received, int $numEccCodewords):array{
9191
$poly = new GenericGFPoly($received);
@@ -119,7 +119,7 @@ private function decodeWords(array $received, int $numEccCodewords):array{
119119
$position = $receivedCount - 1 - GF256::log($errorLocations[$i]);
120120

121121
if($position < 0){
122-
throw new RuntimeException('Bad error location');
122+
throw new QRCodeException('Bad error location');
123123
}
124124

125125
$received[$position] ^= $errorMagnitudes[$i];
@@ -130,7 +130,7 @@ private function decodeWords(array $received, int $numEccCodewords):array{
130130

131131
/**
132132
* @return \chillerlan\QRCode\Common\GenericGFPoly[] [sigma, omega]
133-
* @throws \RuntimeException
133+
* @throws \chillerlan\QRCode\QRCodeException
134134
*/
135135
private function runEuclideanAlgorithm(GenericGFPoly $a, GenericGFPoly $b, int $R):array{
136136
// Assume a's degree is >= b's
@@ -158,14 +158,14 @@ private function runEuclideanAlgorithm(GenericGFPoly $a, GenericGFPoly $b, int $
158158
$t = $q->multiply($tLast)->addOrSubtract($tLastLast);
159159

160160
if($r->getDegree() >= $rLast->getDegree()){
161-
throw new RuntimeException('Division algorithm failed to reduce polynomial?');
161+
throw new QRCodeException('Division algorithm failed to reduce polynomial?');
162162
}
163163
}
164164

165165
$sigmaTildeAtZero = $t->getCoefficient(0);
166166

167167
if($sigmaTildeAtZero === 0){
168-
throw new RuntimeException('sigmaTilde(0) was zero');
168+
throw new QRCodeException('sigmaTilde(0) was zero');
169169
}
170170

171171
$inverse = GF256::inverse($sigmaTildeAtZero);
@@ -174,7 +174,7 @@ private function runEuclideanAlgorithm(GenericGFPoly $a, GenericGFPoly $b, int $
174174
}
175175

176176
/**
177-
* @throws \RuntimeException
177+
* @throws \chillerlan\QRCode\QRCodeException
178178
*/
179179
private function findErrorLocations(GenericGFPoly $errorLocator):array{
180180
// This is a direct application of Chien's search
@@ -195,7 +195,7 @@ private function findErrorLocations(GenericGFPoly $errorLocator):array{
195195
}
196196

197197
if($e !== $numErrors){
198-
throw new RuntimeException('Error locator degree does not match number of roots');
198+
throw new QRCodeException('Error locator degree does not match number of roots');
199199
}
200200

201201
return $result;

0 commit comments

Comments
 (0)