Skip to content

Commit

Permalink
Adjust unit tests and fix coding style errors
Browse files Browse the repository at this point in the history
  • Loading branch information
DASPRiD committed Oct 26, 2017
1 parent 3974928 commit 6c4fc3b
Show file tree
Hide file tree
Showing 31 changed files with 1,230 additions and 1,591 deletions.
4 changes: 2 additions & 2 deletions composer.json
Expand Up @@ -9,7 +9,7 @@
"dasprid/enum": "^1.0"
},
"suggest": {
"ext-gd": "to generate QR code images"
"ext-imagick": "to generate QR code images"
},
"authors": [
{
Expand All @@ -25,7 +25,7 @@
}
},
"require-dev": {
"phpunit/phpunit": "^4.8",
"phpunit/phpunit": "^6.4",
"squizlabs/php_codesniffer": "^3.1"
}
}
8 changes: 4 additions & 4 deletions src/Common/BitArray.php
Expand Up @@ -31,7 +31,7 @@ final class BitArray
public function __construct(int $size = 0)
{
$this->size = $size;
$this->bits = new SplFixedArray(($this->size + 31) >> 3);
$this->bits = SplFixedArray::fromArray(array_fill(0, ($this->size + 31) >> 3, 0));
}

/**
Expand Down Expand Up @@ -165,7 +165,7 @@ public function setRange(int $start, int $end) : void
$lastInt = $end >> 5;

for ($i = $firstInt; $i <= $lastInt; ++$i) {
$firstBit = $i > $firstInt ? 0 : $start & 0x1f;
$firstBit = $i > $firstInt ? 0 : $start & 0x1f;
$lastBit = $i < $lastInt ? 31 : $end & 0x1f;

if (0 === $firstBit && 31 === $lastBit) {
Expand Down Expand Up @@ -199,7 +199,7 @@ public function clear() : void
* @throws InvalidArgumentException if end is smaller than start
*/
public function isRange(int $start, int $end, int $value) : bool
public function isRange(int $start, int $end, bool $value) : bool
{
if ($end < $start) {
throw new InvalidArgumentException('End must be greater or equal to start');
Expand All @@ -215,7 +215,7 @@ public function isRange(int $start, int $end, int $value) : bool
$lastInt = $end >> 5;

for ($i = $firstInt; $i <= $lastInt; ++$i) {
$firstBit = $i > $firstInt ? 0 : $start & 0x1f;
$firstBit = $i > $firstInt ? 0 : $start & 0x1f;
$lastBit = $i < $lastInt ? 31 : $end & 0x1f;

if (0 === $firstBit && 31 === $lastBit) {
Expand Down
4 changes: 2 additions & 2 deletions src/Common/BitMatrix.php
Expand Up @@ -59,7 +59,7 @@ public function __construct(int $width, int $height = null)
$this->width = $width;
$this->height = $height;
$this->rowSize = ($width + 31) >> 5;
$this->bits = new SplFixedArray($this->rowSize * $height);
$this->bits = SplFixedArray::fromArray(array_fill(0, $this->rowSize * $height, 0));
}

/**
Expand All @@ -68,7 +68,7 @@ public function __construct(int $width, int $height = null)
public function get(int $x, int $y) : bool
{
$offset = $y * $this->rowSize + ($x >> 5);
return (BitUtils::unsignedRightShift($this->bits[$offset], ($x & 0x1f)) & 1) !== 0;
return 0 !== (BitUtils::unsignedRightShift($this->bits[$offset], ($x & 0x1f)) & 1);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Common/CharacterSetEci.php
Expand Up @@ -133,6 +133,7 @@ public static function getCharacterSetEciByValue(int $value) : ?self
public static function getCharacterSetEciByName(string $name) : ?self
{
$nameToEci = self::nameToEci();
$name = strtolower($name);

if (! array_key_exists($name, $nameToEci)) {
return null;
Expand Down Expand Up @@ -167,10 +168,10 @@ private static function nameToEci() : array
self::$nameToEci = [];

foreach (self::values() as $eci) {
self::$nameToEci[$eci->name()] = $eci;
self::$nameToEci[strtolower($eci->name())] = $eci;

foreach ($eci->otherEncodingNames as $name) {
self::$nameToEci[$name] = $eci;
self::$nameToEci[strtolower($name)] = $eci;
}
}

Expand Down
13 changes: 6 additions & 7 deletions src/Common/ReedSolomonCodec.php
Expand Up @@ -153,7 +153,9 @@ public function __construct(
$this->numRoots = $numRoots;

// Find prim-th root of 1, used in decoding
for ($iPrimitive = 1; ($iPrimitive % $primitive) !== 0; $iPrimitive += $this->blockSize);
for ($iPrimitive = 1; ($iPrimitive % $primitive) !== 0; $iPrimitive += $this->blockSize) {
}

$this->iPrimitive = intdiv($iPrimitive, $primitive);

$this->generatorPoly[0] = 1;
Expand Down Expand Up @@ -358,12 +360,9 @@ public function decode(SplFixedArray $data, SplFixedArray $erasures = null) : ?i
$reg = clone $lambda;
$reg[0] = 0;
$count = 0;
$i = 1;

for (
$i = 1, $k = $this->iPrimitive - 1;
$i <= $this->blockSize;
$i++, $k = $this->modNn($k + $this->iPrimitive)
) {
for ($k = $this->iPrimitive - 1; $i <= $this->blockSize; ++$i, $k = $this->modNn($k + $this->iPrimitive)) {
$q = 1;

for ($j = $degLambda; $j > 0; $j--) {
Expand Down Expand Up @@ -457,7 +456,7 @@ public function decode(SplFixedArray $data, SplFixedArray $erasures = null) : ?i
/**
* Computes $x % GF_SIZE, where GF_SIZE is 2**GF_BITS - 1, without a slow divide.
*/
protected function modNn(int $x) : int
private function modNn(int $x) : int
{
while ($x >= $this->blockSize) {
$x -= $this->blockSize;
Expand Down
4 changes: 2 additions & 2 deletions src/Encoder/Encoder.php
Expand Up @@ -141,7 +141,7 @@ private static function getAlphanumericCode(int $code) : int
*/
private static function chooseMode(string $content, string $encoding = null) : Mode
{
if (0 === strcasecmp($encoding, 'SHIFT-JIS')) {
if (null !== $encoding && 0 === strcasecmp($encoding, 'SHIFT-JIS')) {
return self::isOnlyDoubleByteKanji($content) ? Mode::KANJI() : Mode::BYTE();
}

Expand Down Expand Up @@ -214,7 +214,7 @@ private static function isOnlyDoubleByteKanji(string $content) : bool
/**
* Chooses the best mask pattern for a matrix.
*/
protected static function chooseMaskPattern(
private static function chooseMaskPattern(
BitArray $bits,
ErrorCorrectionLevel $ecLevel,
Version $version,
Expand Down
2 changes: 1 addition & 1 deletion src/Encoder/MaskUtil.php
Expand Up @@ -235,7 +235,7 @@ public static function getDataMaskBit(int $maskPattern, int $x, int $y) : bool
* We need this for doing this calculation in both vertical and horizontal
* orders respectively.
*/
protected static function applyMaskPenaltyRule1Internal(ByteMatrix $matrix, bool $isHorizontal) : int
private static function applyMaskPenaltyRule1Internal(ByteMatrix $matrix, bool $isHorizontal) : int
{
$penalty = 0;
$iLimit = $isHorizontal ? $matrix->getHeight() : $matrix->getWidth();
Expand Down
2 changes: 1 addition & 1 deletion src/Encoder/MatrixUtil.php
Expand Up @@ -491,7 +491,7 @@ private static function embedDataBits(BitArray $dataBits, int $maskPattern, Byte

// Skip masking if maskPattern is -1.
if (-1 !== $maskPattern && MaskUtil::getDataMaskBit($maskPattern, $xx, $y)) {
$bit = !$bit;
$bit = ! $bit;
}

$matrix->set($xx, $y, (int) $bit);
Expand Down
8 changes: 4 additions & 4 deletions src/Encoder/QrCode.php
Expand Up @@ -122,10 +122,10 @@ public static function isValidMaskPattern(int $maskPattern) : bool
public function __toString() : string
{
$result = "<<\n"
. " mode: " . $this->mode . "\n"
. " ecLevel: " . $this->errorCorrectionLevel . "\n"
. " version: " . $this->version . "\n"
. " maskPattern: " . $this->maskPattern . "\n";
. ' mode: ' . $this->mode . "\n"
. ' ecLevel: ' . $this->errorCorrectionLevel . "\n"
. ' version: ' . $this->version . "\n"
. ' maskPattern: ' . $this->maskPattern . "\n";

if ($this->matrix === null) {
$result .= " matrix: null\n";
Expand Down
30 changes: 0 additions & 30 deletions test/BaconQrCode/Common/BitUtilsTest.php

This file was deleted.

40 changes: 0 additions & 40 deletions test/BaconQrCode/Common/ErrorCorrectionLevelTest.php

This file was deleted.

104 changes: 0 additions & 104 deletions test/BaconQrCode/Common/FormatInformationTest.php

This file was deleted.

0 comments on commit 6c4fc3b

Please sign in to comment.