Completed
Branch dev (374206)
by James Ekow Abaka
06:04
created

Variable::initialize()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 8.4444
c 0
b 0
f 0
cc 8
nc 8
nop 1
1
<?php
2
3
namespace ntentan\honam\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
    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
        $type = gettype($data);
14
        switch ($type) {
15
            case 'object':
16
            case 'string':
17
                return new Variable($data);
18
19
            case 'array':
20
                return new Variable($data, array_keys($data));
21
22
            case 'NULL':
23
            case 'boolean':
24
            case 'integer':
25
            case 'double':
26
                return $data;
27
28
            default:
29
                throw new \ntentan\honam\ViewException("Cannot handle the $type type in templates");
30
        }
31
    }
32
33
    public function __construct($data, $keys = array()) {
34
        if ($data instanceof Variable) {
35
            $this->data = $data->getData();
36
            $this->keys = $data->getKeys();
37
        } else {
38
            $this->data = $data;
39
            $this->keys = $keys;
40
        }
41
42
        if ($this->data instanceof \Iterator) {
43
            $this->iteratable = true;
44
        }
45
    }
46
47
    public function __toString() {
48
        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
    public function u() {
52
        return $this->unescape();
53
    }
54
55
    public function count() {
56
        return count($this->data);
57
    }
58
59
    public function unescape() {
60
        return $this->data;
61
    }
62
63
    public function rewind() {
64
        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
            $this->position = 0;
68
        }
69
    }
70
71
    public function valid() {
72
        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
            return @isset($this->data[$this->keys[$this->position]]);
76
        }
77
    }
78
79
    public function current() {
80
        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
            return Variable::initialize($this->data[$this->keys[$this->position]]);
84
        }
85
    }
86
87
    public function key() {
88
        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
            return $this->keys[$this->position];
92
        }
93
    }
94
95
    public function next() {
96
        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
            $this->position++;
100
        }
101
    }
102
103
    public function offsetExists($offset) {
104
        return isset($this->data[$offset]);
105
    }
106
107
    public function offsetGet($offset) {
108
        if (isset($this->data[$offset])) {
109
            return Variable::initialize($this->data[$offset]);
110
        } else {
111
            return null;
112
        }
113
    }
114
115
    public function offsetSet($offset, $value) {
116
        if (is_null($offset)) {
117
            $this->data[] = $value;
118
        } else {
119
            $this->data[$offset] = $value;
120
        }
121
    }
122
123
    public function offsetUnset($offset) {
124
        unset($this->data[$offset]);
125
    }
126
127
    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
        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
    public function getData() {
140
        return $this->data;
141
    }
142
143
    public function getKeys() {
144
        return $this->keys;
145
    }
146
}
147