QRrawcode::getCode()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 4
nop 0
dl 0
loc 21
rs 9.7666
c 0
b 0
f 0
1
<?php
2
/**
3
 * Class QRrawcode
4
 * @author Tinymeng <[email protected]>
5
 * @date: 2019/9/26 18:24
6
 */
7
namespace tinymeng\code\Gateways\qrcode;
8
use Exception;
9
10
class QRrawcode
11
{
12
13
    public $version;
14
    public $datacode = array();
15
    public $ecccode = array();
16
    public $blocks;
17
    public $rsblocks = array(); //of RSblock
18
    public $count;
19
    public $dataLength;
20
    public $eccLength;
21
    public $b1;
22
23
    //----------------------------------------------------------------------
24
    public function __construct(QRinput $input)
25
    {
26
        $spec = array(0,0,0,0,0);
27
28
        $this->datacode = $input->getByteStream();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $this->datacode is correct as $input->getByteStream() targeting tinymeng\code\Gateways\q...Rinput::getByteStream() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
29
        if(is_null($this->datacode)) {
0 ignored issues
show
introduced by
The condition is_null($this->datacode) is always true.
Loading history...
30
            throw new Exception('null imput string');
31
        }
32
33
        QRspec::getEccSpec($input->getVersion(), $input->getErrorCorrectionLevel(), $spec);
34
35
        $this->version = $input->getVersion();
36
        $this->b1 = QRspec::rsBlockNum1($spec);
37
        $this->dataLength = QRspec::rsDataLength($spec);
38
        $this->eccLength = QRspec::rsEccLength($spec);
39
        $this->ecccode = array_fill(0, $this->eccLength, 0);
40
        $this->blocks = QRspec::rsBlockNum($spec);
41
42
        $ret = $this->init($spec);
43
        if($ret < 0) {
44
            throw new Exception('block alloc error');
45
            return null;
0 ignored issues
show
Unused Code introduced by
return null is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
46
        }
47
48
        $this->count = 0;
49
    }
50
51
    //----------------------------------------------------------------------
52
    public function init(array $spec)
53
    {
54
        $dl = QRspec::rsDataCodes1($spec);
55
        $el = QRspec::rsEccCodes1($spec);
56
        $rs = QRrs::init_rs(8, 0x11d, 0, 1, $el, 255 - $dl - $el);
57
58
59
        $blockNo = 0;
60
        $dataPos = 0;
61
        $eccPos = 0;
62
        for($i=0; $i<QRspec::rsBlockNum1($spec); $i++) {
63
            $ecc = array_slice($this->ecccode,$eccPos);
64
            $this->rsblocks[$blockNo] = new QRrsblock($dl, array_slice($this->datacode, $dataPos), $el,  $ecc, $rs);
0 ignored issues
show
Bug introduced by
It seems like $rs can also be of type null; however, parameter $rs of tinymeng\code\Gateways\q...Rrsblock::__construct() does only seem to accept tinymeng\code\Gateways\qrcode\QRrsItem, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

64
            $this->rsblocks[$blockNo] = new QRrsblock($dl, array_slice($this->datacode, $dataPos), $el,  $ecc, /** @scrutinizer ignore-type */ $rs);
Loading history...
65
            $this->ecccode = array_merge(array_slice($this->ecccode,0, $eccPos), $ecc);
66
67
            $dataPos += $dl;
68
            $eccPos += $el;
69
            $blockNo++;
70
        }
71
72
        if(QRspec::rsBlockNum2($spec) == 0)
73
            return 0;
74
75
        $dl = QRspec::rsDataCodes2($spec);
76
        $el = QRspec::rsEccCodes2($spec);
77
        $rs = QRrs::init_rs(8, 0x11d, 0, 1, $el, 255 - $dl - $el);
78
79
        if($rs == NULL) return -1;
80
81
        for($i=0; $i<QRspec::rsBlockNum2($spec); $i++) {
82
            $ecc = array_slice($this->ecccode,$eccPos);
83
            $this->rsblocks[$blockNo] = new QRrsblock($dl, array_slice($this->datacode, $dataPos), $el, $ecc, $rs);
84
            $this->ecccode = array_merge(array_slice($this->ecccode,0, $eccPos), $ecc);
85
86
            $dataPos += $dl;
87
            $eccPos += $el;
88
            $blockNo++;
89
        }
90
91
        return 0;
92
    }
93
94
    //----------------------------------------------------------------------
95
    public function getCode()
96
    {
97
        $ret;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ret seems to be never defined.
Loading history...
98
99
        if($this->count < $this->dataLength) {
100
            $row = $this->count % $this->blocks;
101
            $col = $this->count / $this->blocks;
102
            if($col >= $this->rsblocks[0]->dataLength) {
103
                $row += $this->b1;
104
            }
105
            $ret = $this->rsblocks[$row]->data[$col];
106
        } else if($this->count < $this->dataLength + $this->eccLength) {
107
            $row = ($this->count - $this->dataLength) % $this->blocks;
108
            $col = ($this->count - $this->dataLength) / $this->blocks;
109
            $ret = $this->rsblocks[$row]->ecc[$col];
110
        } else {
111
            return 0;
112
        }
113
        $this->count++;
114
115
        return $ret;
116
    }
117
118
}