Completed
Push — master ( 02b122...2a8558 )
by James Ekow Abaka
02:43
created

Variable::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.0123

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 4
nop 2
dl 0
loc 13
ccs 8
cts 9
cp 0.8889
crap 3.0123
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace ntentan\honam\template_engines\php;
4
5
class Variable implements \ArrayAccess, \Iterator, \Countable {
6
7
    private $keys;
8
    private $position;
9
    private $data;
10
    private $iteratable = false;
11
12 25
    public static function initialize($data) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
13 25
        $type = gettype($data);
14
        switch ($type) {
15 25
            case 'object':
16 25
            case 'string':
17 24
                return new Variable($data);
18
19 23
            case 'array':
20 23
                return new Variable($data, array_keys($data));
21
22 21
            case 'NULL':
23 20
            case 'boolean':
24 1
            case 'integer':
25
            case 'double':
26 21
                return $data;
27
28
            default:
29
                throw new \ntentan\honam\ViewException("Cannot handle the $type type in templates");
30
        }
31
    }
32
33 25
    public function __construct($data, $keys = array()) {
34 25
        if ($data instanceof Variable) {
35 17
            $this->data = $data->getData();
36 17
            $this->keys = $data->getKeys();
37
        } else {
38 25
            $this->data = $data;
39 25
            $this->keys = $keys;
40
        }
41
42 25
        if ($this->data instanceof \Iterator) {
43
            $this->iteratable = true;
44
        }
45 25
    }
46
47 24
    public function __toString() {
48 24
        return Janitor::cleanHtml($this->data);
0 ignored issues
show
Documentation introduced by
$this->data is of type array<integer|string,*>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
49
    }
50
51 11
    public function u() {
52 11
        return $this->unescape();
53
    }
54
55 4
    public function count() {
56 4
        return count($this->data);
57
    }
58
59 19
    public function unescape() {
60 19
        return $this->data;
61
    }
62
63 23
    public function rewind() {
64 23
        if ($this->iteratable) {
65
            $this->data->rewind();
0 ignored issues
show
Bug introduced by
The method rewind cannot be called on $this->data (of type array<integer|string,*>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
66
        } else {
67 23
            $this->position = 0;
68
        }
69 23
    }
70
71 23
    public function valid() {
72 23
        if ($this->iteratable) {
73
            return $this->data->valid();
0 ignored issues
show
Bug introduced by
The method valid cannot be called on $this->data (of type array<integer|string,*>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
74
        } else {
75 23
            return @isset($this->data[$this->keys[$this->position]]);
76
        }
77
    }
78
79 22
    public function current() {
80 22
        if ($this->iteratable) {
81
            return $this->data->current();
0 ignored issues
show
Bug introduced by
The method current cannot be called on $this->data (of type array<integer|string,*>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
82
        } else {
83 22
            return Variable::initialize($this->data[$this->keys[$this->position]]);
84
        }
85
    }
86
87 17
    public function key() {
88 17
        if ($this->iteratable) {
89
            return $this->data->key();
0 ignored issues
show
Bug introduced by
The method key cannot be called on $this->data (of type array<integer|string,*>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
90
        } else {
91 17
            return $this->keys[$this->position];
92
        }
93
    }
94
95 22
    public function next() {
96 22
        if ($this->iteratable) {
97
            $this->data->next();
0 ignored issues
show
Bug introduced by
The method next cannot be called on $this->data (of type array<integer|string,*>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
98
        } else {
99 22
            $this->position++;
100
        }
101 22
    }
102
103 5
    public function offsetExists($offset) {
104 5
        return isset($this->data[$offset]);
105
    }
106
107 9
    public function offsetGet($offset) {
108 9
        if (isset($this->data[$offset])) {
109 9
            return Variable::initialize($this->data[$offset]);
110
        } else {
111
            return null;
112
        }
113
    }
114
115 1
    public function offsetSet($offset, $value) {
116 1
        if (is_null($offset)) {
117 1
            $this->data[] = $value;
118
        } else {
119 1
            $this->data[$offset] = $value;
120
        }
121 1
    }
122
123
    public function offsetUnset($offset) {
124
        unset($this->data[$offset]);
125
    }
126
127 12
    public function __call($method, $arguments) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
128 12
        return Variable::initialize(call_user_func_array([$this->data, $method], $arguments));
129
    }
130
131
    public function __get($name) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
132
        return Variable::initialize($this->data->$name);
133
    }
134
    
135
    public function __debugInfo() {
136
        return is_array($this->data) ? $this->data : [$this->data];
137
    }
138
    
139 17
    public function getData() {
140 17
        return $this->data;
141
    }
142
143 17
    public function getKeys() {
144 17
        return $this->keys;
145
    }
146
}
147