Passed
Pull Request — master (#2)
by Akpé Aurelle Emmanuel Moïse
01:22
created

AVCIterator::prepareJson()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 26
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 20
nc 7
nop 1
dl 0
loc 26
rs 8.6666
c 0
b 0
f 0
1
<?php
2
namespace EZAMA{
3
4
5
    class AVCIterator implements \ArrayAccess, \Iterator, \Countable, \JsonSerializable
6
    {
7
        protected $container=array();
8
        protected $keys=array();
9
        protected $simple;
10
        public function __construct($keys, $container)
11
        {
12
            $this->container=$container;
13
            $this->keys=$keys;
14
            $this->simple=!is_array($container);
15
        }
16
        
17
        public function offsetSet($key, $value)
18
        {
19
            if ($this->isSimple()) {
20
                $this->keys[$key]=is_int($value)?$value:0;
21
            } else {
22
                $this->container[array_search($key, $this->keys, true)]=is_int($value)?$value:0;
23
            }
24
        }
25
        
26
        public function offsetGet($key)
27
        {
28
            if ($this->isSimple()) {
29
                return $this->keys[$key];
30
            } else {
31
                return $this->container[array_search($key, $this->keys, true)];
32
            }
33
        }
34
        
35
        public function offsetExists($key)
36
        {
37
            if ($this->isSimple()) {
38
                return isset($this->keys[$key]);
39
            } else {
40
                return false!==array_search($key, $this->keys, true);
41
            }
42
        }
43
        
44
        public function offsetUnset($key)
45
        {
46
            if ($this->isSimple()) {
47
                unset($this->keys[$key]);
48
            } else {
49
                unset($this->container[$k=array_search($key, $this->keys, true)], $this->keys[$k]);
50
            }
51
        }
52
        
53
        
54
        public function count()
55
        {
56
            return count($this->keys);
57
        }
58
        
59
        public function key()
60
        {
61
            if ($this->isSimple()) {
62
                return key($this->keys);
63
            } else {
64
                return $this->keys[key($this->keys)];
65
            }
66
        }
67
        
68
        public function current()
69
        {
70
            if ($this->isSimple()) {
71
                return current($this->keys);
72
            } else {
73
                return current($this->container);
74
            }
75
        }
76
        
77
        public function rewind()
78
        {
79
            if ($this->isSimple()) {
80
                reset($this->keys);
81
            } else {
82
                reset($this->keys);
83
                reset($this->container);
84
            }
85
        }
86
        
87
        public function next()
88
        {
89
            if ($this->isSimple()) {
90
                next($this->keys);
91
            } else {
92
                next($this->keys)&&next($this->container);
93
            }
94
        }
95
        
96
        public function valid()
97
        {
98
            $key=key($this->keys);
99
            return null!==$key&&false!==$key;
100
        }
101
        
102
        public function jsonSerialize()
103
        {
104
            if ($this->isSimple()) {
105
                return $this->keys;
106
            } else {
107
                $callback=function ($value, $count) {
108
                    return array($value, $count);
109
                };
110
                return array_map($callback, $this->keys, $this->container);
111
            }
112
        }
113
        
114
        public function isSimple()
115
        {
116
            return $this->simple;
117
        }
118
        
119
        private static function prepareJson($value)
0 ignored issues
show
Unused Code introduced by
The method prepareJson() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
120
        {
121
            switch (gettype($value)) {
122
                case "resource":
123
                case "resource(closed)":
124
                    throw new \InvalidArgumentError("Resource type detected while trying to prepare JsonSerialize ");
0 ignored issues
show
Bug introduced by
The type InvalidArgumentError was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
125
                    break;
126
                case "object":
127
                    if (is_object($value)) {
128
                        if (in_array('Serializable', class_implements(get_class($value)))) {
129
                            try {
130
                                $serialize=serialize($value);
131
                                return $serialize;
132
                            } catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The type EZAMA\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
133
                                throw new \InvalidArgumentError($e->getMessage());
134
                            }
135
                        }
136
                        return serialize($value);
137
                        break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
138
                    }
139
                    throw new \InvalidArgumentError("Invalid object type detected while trying to prepare JsonSerialize ");
140
                    break;
141
                    
142
                default:
143
                    return $value;
144
                    break;
145
                
146
            }
147
        }
148
    }
149
}
150