FrameFiller::next()   B
last analyzed

Complexity

Conditions 11
Paths 25

Size

Total Lines 51
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 35
nc 25
nop 0
dl 0
loc 51
rs 7.3166
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Class FrameFiller
4
 * @author Tinymeng <[email protected]>
5
 * @date: 2019/9/26 18:23
6
 */
7
namespace tinymeng\code\Gateways\qrcode;
8
9
class FrameFiller
10
{
11
12
    public $width;
13
    public $frame;
14
    public $x;
15
    public $y;
16
    public $dir;
17
    public $bit;
18
19
    //----------------------------------------------------------------------
20
    public function __construct($width, &$frame)
21
    {
22
        $this->width = $width;
23
        $this->frame = $frame;
24
        $this->x = $width - 1;
25
        $this->y = $width - 1;
26
        $this->dir = -1;
27
        $this->bit = -1;
28
    }
29
30
    //----------------------------------------------------------------------
31
    public function setFrameAt($at, $val)
32
    {
33
        $this->frame[$at['y']][$at['x']] = chr($val);
34
    }
35
36
    //----------------------------------------------------------------------
37
    public function getFrameAt($at)
38
    {
39
        return ord($this->frame[$at['y']][$at['x']]);
40
    }
41
42
    //----------------------------------------------------------------------
43
    public function next()
44
    {
45
        do {
46
47
            if($this->bit == -1) {
48
                $this->bit = 0;
49
                return array('x'=>$this->x, 'y'=>$this->y);
50
            }
51
52
            $x = $this->x;
53
            $y = $this->y;
54
            $w = $this->width;
55
56
            if($this->bit == 0) {
57
                $x--;
58
                $this->bit++;
59
            } else {
60
                $x++;
61
                $y += $this->dir;
62
                $this->bit--;
63
            }
64
65
            if($this->dir < 0) {
66
                if($y < 0) {
67
                    $y = 0;
68
                    $x -= 2;
69
                    $this->dir = 1;
70
                    if($x == 6) {
71
                        $x--;
72
                        $y = 9;
73
                    }
74
                }
75
            } else {
76
                if($y == $w) {
77
                    $y = $w - 1;
78
                    $x -= 2;
79
                    $this->dir = -1;
80
                    if($x == 6) {
81
                        $x--;
82
                        $y -= 8;
83
                    }
84
                }
85
            }
86
            if($x < 0 || $y < 0) return null;
87
88
            $this->x = $x;
89
            $this->y = $y;
90
91
        } while(ord($this->frame[$y][$x]) & 0x80);
92
93
        return array('x'=>$x, 'y'=>$y);
94
    }
95
96
}