Matrix   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 25
lcom 1
cbo 0
dl 0
loc 109
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A setData() 0 3 1
A clearColors() 0 3 1
A clear() 0 4 1
A setPixel() 0 7 3
A getPixel() 0 8 3
F render() 0 56 14
A __toString() 0 3 1
1
<?php
2
3
/**
4
 * Pixeler
5
 *
6
 * UTF-8 Dot matrix renderer.
7
 *
8
 * @package pixeler
9
 * @author [email protected]
10
 * @version 1.0
11
 * @copyright Stefano Azzolini - 2014 - http://dreamnoctis.com
12
 */
13
14
namespace Pixeler;
15
16
class Matrix {
17
  protected $matrix = null,
18
            $colors = null,
19
            $width  = 0,
20
            $height = 0,
21
            $size   = 0,
22
            $csize  = 0;
23
24
  public function __construct($width,$height){
25
    $this->width    = 2 * ($w2=ceil($width/2));
0 ignored issues
show
Documentation Bug introduced by
The property $width was declared of type integer, but 2 * ($w2 = ceil($width / 2)) is of type double. Maybe add a type cast?

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.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
26
    $this->height   = 4 * ($h2=ceil($height/4));
0 ignored issues
show
Documentation Bug introduced by
The property $height was declared of type integer, but 4 * ($h2 = ceil($height / 4)) is of type double. Maybe add a type cast?

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.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
27
    $this->size     = $this->width * $this->height;
0 ignored issues
show
Documentation Bug introduced by
The property $size was declared of type integer, but $this->width * $this->height is of type double. Maybe add a type cast?

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.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
28
    $this->csize    = $w2 * $h2;
0 ignored issues
show
Documentation Bug introduced by
The property $csize was declared of type integer, but $w2 * $h2 is of type double. Maybe add a type cast?

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.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
29
    $this->matrix   = new \SplFixedArray($this->size);
30
    $this->colors   = new \SplFixedArray($this->csize);
31
  }
32
33
  public function setData(array $data) {
34
    $this->matrix   = \SplFixedArray::fromArray(array_slice($data,0,$this->size),false);
35
  }
36
  
37
  public function clearColors() {
38
    $this->colors   = new \SplFixedArray($this->csize);
39
  }
40
41
  public function clear() {
42
    $this->matrix   = new \SplFixedArray($this->size);
43
    $this->colors   = new \SplFixedArray($this->csize);
44
  }
45
46
  public function setPixel($x, $y, $value = true,$color = null){
47
    $y = (int)$y; $x = (int)$x;
48
    if ( $x < $this->width && $y < $this->height) {
49
      $this->matrix[$x + $y * $this->width] = !! $value;
50
      $this->colors[$x>>1 + ($y>>2) * $this->width] = $color;
51
    }
52
  }
53
54
  public function getPixel($x, $y){
55
    $y = (int)$y; $x = (int)$x;
56
    if ( $x < $this->width && $y < $this->height) {
57
      return [ $this->matrix[$x + $y * $this->width], $this->colors[$x + $y * $this->width] ];
58
    } else {
59
      return false;
60
    }
61
  }
62
63
  public function render(){
64
    $i  = 0;
0 ignored issues
show
Unused Code introduced by
$i is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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
120
  public function __toString(){
121
    return $this->render();
122
  }
123
124
}
125