Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ vendor/
composer.lock
.idea
.DS_Store
/.phpunit.result.cache
/.phpunit.cache
10 changes: 6 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
{
"name": "nmiles/phpqrcode",
"description": "PHP 7 QR Code library - update of original version by Dominik Dzienia",
"description": "PHP 8 QR Code library - update of original version by Dominik Dzienia",
"type": "library",
"license": "LGPL-3.0-or-later",
"keywords": [
"qr codes",
"qr"
],
"require": {
"php": ">=7.3",
"php": "^8.1",
"ext-gd": "*",
"ext-zlib": "*"
},
"require-dev": {
"phpunit/phpunit": "^7.1.4",
"squizlabs/php_codesniffer": "^3.5"
"phpunit/phpunit": "^12.3.6",
"squizlabs/php_codesniffer": "^3.5.4",
"symplify/easy-coding-standard": "^12.5",
"rector/rector": "^2.1"
},
"autoload": {
"psr-4": {
Expand Down
21 changes: 21 additions & 0 deletions ecs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

use Symplify\EasyCodingStandard\Config\ECSConfig;
use Symplify\EasyCodingStandard\ValueObject\Option;

return ECSConfig::configure()
->withSpacing(
Option::INDENTATION_SPACES,
PHP_EOL
)
->withPaths([
__DIR__ . '/src',
__DIR__ . '/test',
])
->withPreparedSets(
psr12: true, common: true, symplify: true
)
->withRules([
]);
28 changes: 12 additions & 16 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true">
<testsuites>
<testsuite name="phpqrcode Test Suite">
<directory>./test</directory>
</testsuite>
</testsuites>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
</phpunit>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true" cacheDirectory=".phpunit.cache">
<testsuites>
<testsuite name="phpqrcode Test Suite">
<directory>./test</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">./src</directory>
</include>
</source>
</phpunit>
17 changes: 17 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;

return RectorConfig::configure()
->withPaths([
__DIR__ . '/src',
__DIR__ . '/test',
])
->withPreparedSets(
deadCode: true,
codeQuality: true,
codingStyle: true,
)
->withPhpSets(php81: true);
71 changes: 24 additions & 47 deletions src/Bitstream.php
Original file line number Diff line number Diff line change
@@ -1,78 +1,61 @@
<?php

declare (strict_types=1);
declare(strict_types=1);

namespace PhpQrCode;

/**
* Class Bitstream
* @package PhpQrCode
*/
class Bitstream
{
/** @var array */
public $data = [];
public array $data = [];

public function size(): int
{
return count($this->data);
}


public function allocate(int $setLength): int
{
$this->data = array_fill(0, $setLength, 0);
return 0;
}


public static function newFromNum(int $bits, int $num): Bitstream
public static function newFromNum(int $bits, int $num): self
{
$bstream = new Bitstream();
$bstream = new self();
$bstream->allocate($bits);

$mask = 1 << ($bits - 1);
for ($i = 0; $i < $bits; $i++) {
if ($num & $mask) {
$bstream->data[$i] = 1;
} else {
$bstream->data[$i] = 0;
}
$mask = $mask >> 1;
$bstream->data[$i] = ($num & $mask) !== 0 ? 1 : 0;

$mask >>= 1;
}

return $bstream;
}


public static function newFromBytes(int $size, array $data): Bitstream
public static function newFromBytes(int $size, array $data): self
{
$bstream = new Bitstream();
$bstream = new self();
$bstream->allocate($size * 8);

$p = 0;

for ($i = 0; $i < $size; $i++) {
$mask = 0x80;
for ($j = 0; $j < 8; $j++) {
if ($data[$i] & $mask) {
$bstream->data[$p] = 1;
} else {
$bstream->data[$p] = 0;
}
$bstream->data[$p] = ($data[$i] & $mask) !== 0 ? 1 : 0;

$p++;
$mask = $mask >> 1;
$mask >>= 1;
}
}

return $bstream;
}

public function append(Bitstream $arg): int
public function append(self $arg): int
{
if (is_null($arg)) {
return -1;
}

if ($arg->size() == 0) {
return 0;
}
Expand All @@ -93,29 +76,21 @@ public function appendNum(int $bits, int $num): int
return 0;
}

$b = Bitstream::newFromNum($bits, $num);

if (is_null($b)) {
return -1;
}
$b = self::newFromNum($bits, $num);

$ret = $this->append($b);
unset($b);

return $ret;
}

public function appendBytes(int $size, array $data) :int
public function appendBytes(int $size, array $data): int
{
if ($size == 0) {
return 0;
}

$b = Bitstream::newFromBytes($size, $data);

if (is_null($b)) {
return -1;
}
$b = self::newFromBytes($size, $data);

$ret = $this->append($b);
unset($b);
Expand All @@ -131,28 +106,30 @@ public function toByte(): array
return [];
}

$data = array_fill(0, (int)(($size + 7) / 8), 0);
$bytes = (int)($size / 8);
$data = array_fill(0, (int) (($size + 7) / 8), 0);
$bytes = (int) ($size / 8);

$p = 0;

for ($i = 0; $i < $bytes; $i++) {
$v = 0;
for ($j = 0; $j < 8; $j++) {
$v = $v << 1;
$v <<= 1;
$v |= $this->data[$p];
$p++;
}

$data[$i] = $v;
}

if ($size & 7) {
if (($size & 7) !== 0) {
$v = 0;
for ($j = 0; $j < ($size & 7); $j++) {
$v = $v << 1;
$v <<= 1;
$v |= $this->data[$p];
$p++;
}

$data[$bytes] = $v;
}

Expand Down
Loading