Conditions | 14 |
Paths | 770 |
Total Lines | 56 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
63 | public function render(){ |
||
64 | $i = 0; |
||
65 | $w = $this->width; |
||
66 | $w2 = $this->width >> 1; |
||
67 | $h = $this->height; |
||
68 | $m = $this->matrix; |
||
69 | $c = $this->colors; |
||
70 | $ESC = chr(27); |
||
71 | ob_start(); |
||
72 | for ($y = 0, $cy = 0; $y < $h; $y += 4, $cy++){ |
||
73 | $cx = 0; $cy0 = $cy * $w2; |
||
74 | $y0 = $y * $w; $y1 = ($y + 1) * $w; $y2 = ($y + 2) * $w; $y3 = ($y + 3) * $w; |
||
75 | for ($x = 0; $x < $w; $x += 2, $cx++){ |
||
76 | $cell = 0; |
||
77 | $x1 = $x + 1; |
||
78 | |||
79 | foreach([ |
||
80 | 0x01 => $x1 + $y3, |
||
81 | 0x02 => $x + $y3, |
||
82 | 0x04 => $x1 + $y2, |
||
83 | 0x08 => $x + $y2, |
||
84 | 0x10 => $x1 + $y1, |
||
85 | 0x20 => $x + $y1, |
||
86 | 0x40 => $x1 + $y0, |
||
87 | 0x80 => $x + $y0, |
||
88 | ] as $bit => $ofs) { |
||
89 | if (!empty($m[$ofs])) $cell |= $bit; |
||
90 | } |
||
91 | |||
92 | $dots_r = 0x2800; |
||
93 | |||
94 | if ($cell & 0x80) $dots_r |= 0x01; |
||
95 | if ($cell & 0x40) $dots_r |= 0x08; |
||
96 | if ($cell & 0x20) $dots_r |= 0x02; |
||
97 | if ($cell & 0x10) $dots_r |= 0x10; |
||
98 | if ($cell & 0x08) $dots_r |= 0x04; |
||
99 | if ($cell & 0x04) $dots_r |= 0x20; |
||
100 | if ($cell & 0x02) $dots_r |= 0x40; |
||
101 | if ($cell & 0x01) $dots_r |= 0x80; |
||
102 | |||
103 | $dots_r_64 = $dots_r % 64; |
||
104 | $dots_r_4096 = $dots_r % 4096; |
||
105 | |||
106 | // Print UTF-8 character and color |
||
107 | echo |
||
108 | $ESC.'[' . ($c[$cy0+$cx]?'38;5;'.$c[$cy0+$cx]:39).'m' |
||
109 | . chr(224 + (($dots_r - $dots_r_4096) >> 12 )) |
||
110 | . chr(128 + (($dots_r_4096 - $dots_r_64) >> 6 )) |
||
111 | . chr(128 + $dots_r_64); |
||
112 | } |
||
113 | echo $ESC."[0m\n"; |
||
114 | } |
||
115 | $buffer = ob_get_contents(); |
||
116 | ob_end_clean(); |
||
117 | return $buffer; |
||
118 | } |
||
119 | |||
125 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.