Canvas::getHighestCol()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 8
cp 0
rs 9.9
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 12
1
<?php
2
namespace Mathielen\ReportWriteEngine\Engine;
3
4
use Psr\Log\LoggerInterface;
5
use Psr\Log\NullLogger;
6
7
class Canvas extends \ArrayObject
8
{
9
10
    private $pointer = ['X' => 0, 'Y' => 0];
11
12
    /**
13
     * @var LoggerInterface
14
     */
15
    private $logger;
16
17 7
    public function __construct(LoggerInterface $logger = null, array $input = [])
18
    {
19 7
        parent::__construct($input);
20 7
        $this->logger = $logger ? $logger : new NullLogger();
21 7
    }
22
23 7
    public function getHeight()
24
    {
25 7
        return $this->count() === 0 ? 0 : $this->getHighestRow() - $this->getLowestRow() + 1;
26
    }
27
28 7
    public function getHighestRow()
29
    {
30 7
        $highestRow = $this->count() === 0 ? 0 : max(array_keys($this->getArrayCopy()));
31
32 7
        return $highestRow;
33
    }
34
35 7
    public function getLowestRow()
36
    {
37 7
        return $this->count() === 0 ? 0 : min(array_keys($this->getArrayCopy()));
38
    }
39
40
    public function getHighestCol()
41
    {
42
        $highestCol = 0;
43
        foreach ($this as $row) {
44
            foreach ($row as $c => $col) {
45
                $highestCol = max($highestCol, $c);
46
            }
47
        }
48
49
        return $highestCol;
50
    }
51
52
    public function setPointer($x, $y = null)
53
    {
54
        $this->pointer['X'] = is_null($x) ? $this->pointer['X'] : $x;
55
        $this->pointer['Y'] = is_null($y) ? $this->pointer['Y'] : $y;
56
57
        $this->logger->debug("Setting pointer to X:" . $this->pointer['X'] . ", Y:" . $this->pointer['Y']);
58
    }
59
60
    public function inc($direction = 'Y', $increment = 1)
61
    {
62
        $this->pointer[$direction] += $increment;
63
    }
64
65
    public function write(array $data)
66
    {
67
        $this->logger->debug("Writing array to canvas", ['array' => $data]);
68
69
        $y = $this->pointer['Y'];
70
71
        foreach ($data as $row) {
72
73
            foreach ($row as $x => $col) {
74
                $this->set($x + $this->pointer['X'], $y, $col);
0 ignored issues
show
Unused Code introduced by
The call to the method Mathielen\ReportWriteEngine\Engine\Canvas::set() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
75
            }
76
77
            $y++;
78
        }
79
80
        $this->sort();
81
    }
82
83 7
    private function set($x, $y, $data)
84
    {
85
        //$this->logger->debug("Write value to canvas [$x,$y] = " . (is_array($data) ? $data['value'] : $data));
86
87 7
        $this[$y][$x] = $data;
88 7
    }
89
90 7
    private function sort()
91
    {
92 7
        $this->ksort();
93 7
        foreach ($this as &$row) {
94 7
            ksort($row);
95 7
        }
96 7
    }
97
98 7
    public function insert(Canvas $canvas, $y, $x = 0, $h = 0, $w = 0)
99
    {
100 7
        $this->logger->debug("Inserting canvas at X:$x, Y:$y. Replacing W:$w, H:$h", ['canvas' => $canvas]);
101
102
        //remove height
103 7
        for ($i = $y; $i < $y + $h; $i++) {
104 3
            if (isset($this[$i])) {
105 3
                if ($x > 0) {
106 1
                    foreach ($this[$i] as $ci => $col) {
107 1
                        if ($ci > $x) {
108 1
                            unset($this[$i][$x]);
109 1
                        }
110 1
                    }
111 1
                } else {
112 2
                    unset($this[$i]);
113
                }
114 3
            }
115 3
        }
116
117
        //make space
118 7
        $insertHeight = $canvas->getHeight();
119 7
        foreach ($this->getArrayCopy() as $thisRowNum => $row) {
120 6
            if ($thisRowNum >= $y + $h && $x === 0) {
121 2
                $this->logger->debug("Shifting row $thisRowNum to " . ($thisRowNum + $insertHeight) . " due to >= Y:$y + H:$h = " . ($y + $h) . " and X === 0. Insertheight is: $insertHeight", ['row' => $row]);
122
123
                //unset($this[$thisRowNum]);
124 2
                $this[$thisRowNum + $insertHeight - $h] = $row;
125 2
            }
126 7
        }
127
128
        /*$startRowNum = $canvas->getLowestRow();
129
        $insertWidth = $canvas->getHighestCol() - $w;
130
        foreach ($this->getArrayCopy() as $thisRowNum => $row) {
131
            if ($thisRowNum == $startRowNum) {
132
                foreach ($row as $thisColNum => $col) {
133
                    if ($thisColNum > $x + $w && $col != '') {
134
                        $this[$thisRowNum][$thisColNum + $insertWidth] = $col;
135
                    }
136
                }
137
            }
138
        }*/
139
140 7
        $r = $c = 0;
141 7
        foreach ($canvas as $row) {
142 7
            foreach ($row as $c => $col) {
143 7
                $this->set($x + $c, $y + $r, $col);
0 ignored issues
show
Unused Code introduced by
The call to the method Mathielen\ReportWriteEngine\Engine\Canvas::set() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
144 7
            }
145
146 7
            $r++;
147 7
        }
148
149 7
        $this->sort();
150 7
    }
151
152
    public function __toString()
153
    {
154
        return json_encode($this->getArrayCopy(), JSON_PRETTY_PRINT);
155
    }
156
157
}
158