Browse Source

:shower: sprintf'd some things

codemasher 6 years ago
parent
commit
ab7a11f225

+ 2 - 2
src/Data/AlphaNum.php

@@ -14,7 +14,7 @@ namespace chillerlan\QRCode\Data;
 
 use chillerlan\QRCode\QRCode;
 
-use function array_search, ord;
+use function array_search, ord, sprintf;
 
 /**
  * Alphanumeric mode: 0 to 9, A to Z, space, $ % * + - . / :
@@ -59,7 +59,7 @@ class AlphaNum extends QRDataAbstract{
 			return $i;
 		}
 
-		throw new QRCodeDataException('illegal char: "'.$chr.'" ['.ord($chr).']');
+		throw new QRCodeDataException(sprintf('illegal char: "%s" [%d]', $chr, ord($chr)));
 	}
 
 }

+ 3 - 3
src/Data/Kanji.php

@@ -14,7 +14,7 @@ namespace chillerlan\QRCode\Data;
 
 use chillerlan\QRCode\QRCode;
 
-use function mb_strlen, ord, strlen;
+use function mb_strlen, ord, sprintf, strlen;
 
 /**
  * Kanji mode: double-byte characters from the Shift JIS character set
@@ -57,7 +57,7 @@ class Kanji extends QRDataAbstract{
 				$c -= 0xC140;
 			}
 			else{
-				throw new QRCodeDataException('illegal char at '.($i + 1).' ['.$c.']');
+				throw new QRCodeDataException(sprintf('illegal char at %d [%d]', $i + 1, $c));
 			}
 
 			$this->bitBuffer->put((($c >> 8) & 0xff) * 0xC0 + ($c & 0xff), 13);
@@ -65,7 +65,7 @@ class Kanji extends QRDataAbstract{
 		}
 
 		if($i < $len){
-			throw new QRCodeDataException('illegal char at '.($i + 1));
+			throw new QRCodeDataException(sprintf('illegal char at %d', $i + 1));
 		}
 
 	}

+ 4 - 5
src/Data/Number.php

@@ -14,7 +14,7 @@ namespace chillerlan\QRCode\Data;
 
 use chillerlan\QRCode\QRCode;
 
-use function substr;
+use function ord, sprintf, substr;
 
 /**
  * Numeric mode: decimal digits 0 through 9
@@ -68,14 +68,13 @@ class Number extends QRDataAbstract{
 
 		$len = strlen($string);
 		for($i = 0; $i < $len; $i++){
-			$c = \ord($string[$i]);
+			$c = ord($string[$i]);
 
 			if(!in_array($string[$i], $this::NUMBER_CHAR_MAP, true)){
-				throw new QRCodeDataException('illegal char: "'.$string[$i].'" ['.$c.']');
+				throw new QRCodeDataException(sprintf('illegal char: "%s" [%d]', $string[$i], $c));
 			}
 
-			$c = $c - \ord('0');
-
+			$c   = $c - 48; // ord('0')
 			$num = $num * 10 + $c;
 		}
 

+ 5 - 5
src/Data/QRDataAbstract.php

@@ -16,7 +16,7 @@ use chillerlan\QRCode\{QRCode, QRCodeException};
 use chillerlan\QRCode\Helpers\{BitBuffer, Polynomial};
 use chillerlan\Settings\SettingsContainerInterface;
 
-use function array_fill, array_merge, count, max, range, strlen;
+use function array_fill, array_merge, count, max, mb_convert_encoding, mb_detect_encoding, range, sprintf, strlen;
 
 /**
  * Processes the binary data and maps it on a matrix which is then being returned
@@ -106,7 +106,7 @@ abstract class QRDataAbstract implements QRDataInterface{
 	public function setData(string $data):QRDataInterface{
 
 		if($this->datamode === QRCode::DATA_KANJI){
-			$data = \mb_convert_encoding($data, 'SJIS', \mb_detect_encoding($data));
+			$data = mb_convert_encoding($data, 'SJIS', mb_detect_encoding($data));
 		}
 
 		$this->strlen  = $this->getLength($data);
@@ -160,7 +160,7 @@ abstract class QRDataAbstract implements QRDataInterface{
 			 }
 		 }
 
-		throw new QRCodeDataException('invalid version number: '.$this->version);
+		throw new QRCodeDataException(sprintf('invalid version number: %d', $this->version));
 	}
 
 	/**
@@ -192,7 +192,7 @@ abstract class QRDataAbstract implements QRDataInterface{
 			}
 		}
 
-		throw new QRCodeDataException('data exceeds '.$maxlength.' characters');
+		throw new QRCodeDataException(sprintf('data exceeds %d characters', $maxlength));
 	}
 
 	/**
@@ -227,7 +227,7 @@ abstract class QRDataAbstract implements QRDataInterface{
 
 		// there was an error writing the BitBuffer data, which is... unlikely.
 		if($this->bitBuffer->length > $MAX_BITS){
-			throw new QRCodeException('code length overflow. ('.$this->bitBuffer->length.' > '.$MAX_BITS.'bit)'); // @codeCoverageIgnore
+			throw new QRCodeException(sprintf('code length overflow. (%d > %d bit)', $this->bitBuffer->length, $MAX_BITS)); // @codeCoverageIgnore
 		}
 
 		// end code.

+ 2 - 2
src/Helpers/Polynomial.php

@@ -14,7 +14,7 @@ namespace chillerlan\QRCode\Helpers;
 
 use chillerlan\QRCode\QRCodeException;
 
-use function array_fill, count;
+use function array_fill, count, sprintf;
 
 /**
  * @link http://www.thonky.com/qr-code-tutorial/error-correction-coding
@@ -158,7 +158,7 @@ class Polynomial{
 	public function glog(int $n):int{
 
 		if($n < 1){
-			throw new QRCodeException('log('.$n.')');
+			throw new QRCodeException(sprintf('log(%s)', $n));
 		}
 
 		return Polynomial::table[$n][1];

+ 2 - 2
src/Output/QRImage.php

@@ -17,7 +17,7 @@ use Exception;
 
 use function array_values, base64_encode, call_user_func, count, imagecolorallocate, imagecolortransparent,
 	imagecreatetruecolor, imagedestroy, imagefilledrectangle, imagegif, imagejpeg, imagepng, in_array,
-	is_array, ob_end_clean, ob_get_contents, ob_start, range;
+	is_array, ob_end_clean, ob_get_contents, ob_start, range, sprintf;
 
 /**
  * Converts the matrix into GD images, raw or base64 output
@@ -91,7 +91,7 @@ class QRImage extends QROutputAbstract{
 		$imageData = $this->dumpImage($file);
 
 		if((bool)$this->options->imageBase64){
-			$imageData = 'data:image/'.$this->options->outputType.';base64,'.base64_encode($imageData);
+			$imageData = sprintf('data:image/%s;base64,%s', $this->options->outputType, base64_encode($imageData));
 		}
 
 		return $imageData;

+ 2 - 2
src/Output/QRMarkup.php

@@ -116,7 +116,7 @@ class QRMarkup extends QROutputAbstract{
 
 					if($count > 0){
 						$len = $count;
-						$path .= 'M' .$start. ' ' .$y. ' h'.$len.' v1 h-'.$len.'Z ';
+						$path .= sprintf('M%s %s h%s v1 h-%sZ ', $start, $y, $len, $len);
 
 						// reset count
 						$count = 0;
@@ -128,7 +128,7 @@ class QRMarkup extends QROutputAbstract{
 			}
 
 			if(!empty($path)){
-				$svg .= '<path class="qr-'.$M_TYPE.' '.$this->options->cssClass.'" stroke="transparent" fill="'.$value.'" fill-opacity="'.$this->options->svgOpacity.'" d="'.$path.'" />';
+				$svg .= sprintf('<path class="qr-%s %s" stroke="transparent" fill="%s" fill-opacity="%s" d="%s" />', $M_TYPE, $this->options->cssClass, $value, $this->options->svgOpacity, $path);
 			}
 
 		}

+ 2 - 2
src/Output/QROutputAbstract.php

@@ -15,7 +15,7 @@ namespace chillerlan\QRCode\Output;
 use chillerlan\QRCode\{Data\QRMatrix, QRCode};
 use chillerlan\Settings\SettingsContainerInterface;
 
-use function call_user_func, dirname, file_put_contents, get_called_class, in_array, is_writable;
+use function call_user_func, dirname, file_put_contents, get_called_class, in_array, is_writable, sprintf;
 
 /**
  * common output abstract
@@ -103,7 +103,7 @@ abstract class QROutputAbstract implements QROutputInterface{
 	protected function saveToFile(string $data, string $file):bool{
 
 		if(!is_writable(dirname($file))){
-			throw new QRCodeOutputException('Could not write data to cache file: '.$file);
+			throw new QRCodeOutputException(sprintf('Could not write data to cache file: %s', $file));
 		}
 
 		return (bool)file_put_contents($file, $data);

+ 2 - 2
src/QROptionsTrait.php

@@ -12,7 +12,7 @@
 
 namespace chillerlan\QRCode;
 
-use function array_values, count, is_array, is_numeric, max, min;
+use function array_values, count, is_array, is_numeric, max, min, sprintf;
 
 trait QROptionsTrait{
 
@@ -284,7 +284,7 @@ trait QROptionsTrait{
 	protected function set_eccLevel(int $eccLevel):void{
 
 		if(!isset(QRCode::ECC_MODES[$eccLevel])){
-			throw new QRCodeException('Invalid error correct level: '.$eccLevel);
+			throw new QRCodeException(sprintf('Invalid error correct level: %s', $eccLevel));
 		}
 
 		$this->eccLevel = $eccLevel;