Passed
Push — master ( 550b9e...103607 )
by Sébastien
09:49 queued 07:35
created

AbstractJava::offsetExists()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.7085

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 4
nop 2
dl 0
loc 14
ccs 4
cts 7
cp 0.5714
crap 3.7085
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * soluble-japha / PHPJavaBridge driver client.
6
 *
7
 * Refactored version of phpjababridge's Java.inc file compatible
8
 * with php java bridge 6.2
9
 *
10
 *
11
 * @credits   http://php-java-bridge.sourceforge.net/pjb/
12
 *
13
 * @see      http://github.com/belgattitude/soluble-japha
14
 *
15
 * @author Jost Boekemeier
16
 * @author Vanvelthem Sébastien (refactoring and fixes from original implementation)
17
 * @license   MIT
18
 *
19
 * The MIT License (MIT)
20
 * Copyright (c) 2014-2017 Jost Boekemeier
21
 * Permission is hereby granted, free of charge, to any person obtaining a copy
22
 * of this software and associated documentation files (the "Software"), to deal
23
 * in the Software without restriction, including without limitation the rights
24
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
25
 * copies of the Software, and to permit persons to whom the Software is
26
 * furnished to do so, subject to the following conditions:
27
 *
28
 * The above copyright notice and this permission notice shall be included in
29
 * all copies or substantial portions of the Software.
30
 *
31
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
34
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
36
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
37
 * THE SOFTWARE.
38
 *
39
 * @method string getName()
40
 * @method string forName()
41
 */
42
43
namespace Soluble\Japha\Bridge\Driver\Pjb62;
44
45
use Soluble\Japha\Interfaces;
46
47
abstract class AbstractJava implements \IteratorAggregate, \ArrayAccess, JavaType, Interfaces\JavaObject
48
{
49
    /**
50
     * @var Client|string|null
51
     */
52
    public $__client;
53
54
    /**
55
     * @var JavaType&JavaProxy
56
     */
57
    public $__delegate;
58
    protected $__serialID;
59
    public $__factory;
60
61
    /**
62
     * @var int
63
     */
64
    public $__java;
65
    /**
66
     * @var string|null
67
     */
68
    public $__signature;
69
    public $__cancelProxyCreationTag;
70
71
    protected function __createDelegate(): void
72
    {
73
        $proxy = $this->__factory->create($this->__java, $this->__signature);
74
        $this->__delegate = $proxy;
75
        $this->__java = $proxy->__java;
76
        $this->__signature = $proxy->__signature;
77
    }
78
79
    /**
80
     * @param string $type
81
     *
82
     * @return mixed
83
     */
84
    public function __cast(string $type)
85
    {
86
        if (!isset($this->__delegate)) {
87
            $this->__createDelegate();
88
        }
89
90
        return $this->__delegate->__cast($type);
91
    }
92
93
    /**
94
     * @return array
95
     */
96 1
    public function __sleep()
97
    {
98 1
        if (!isset($this->__delegate)) {
99
            $this->__createDelegate();
100
        }
101 1
        $this->__delegate->__sleep();
102
103 1
        return ['__delegate'];
104
    }
105
106 1
    public function __wakeup()
107
    {
108 1
        if (!isset($this->__delegate)) {
109
            $this->__createDelegate();
110
        }
111 1
        $this->__delegate->__wakeup();
112 1
        $this->__java = $this->__delegate->get__java();
113 1
        $this->__client = $this->__delegate->get__signature();
114 1
    }
115
116
    /**
117
     * Delegate the magic method __get() to the java object
118
     * to access the Java object properties (and not the PHP
119
     * remote proxied object).
120
     *
121
     * @throws \Exception Depending on ThrowExceptionProxy
122
     *
123
     * @param string $key
124
     *
125
     * @return mixed
126
     */
127 2
    public function __get(string $key)
128
    {
129 2
        if (!isset($this->__delegate)) {
130
            $this->__createDelegate();
131
        }
132
133 2
        return $this->__delegate->__get($key);
134
    }
135
136
    /**
137
     * Delegate the magic method __set() to the java object
138
     * to access the Java object properties (and not the PHP
139
     * remote proxied object).
140
     *
141
     * @throws \Exception Depending on ThrowExceptionProxy
142
     *
143
     * @param string $key
144
     * @param mixed  $val
145
     */
146 1
    public function __set(string $key, $val): void
147
    {
148 1
        if (!isset($this->__delegate)) {
149
            $this->__createDelegate();
150
        }
151 1
        $this->__delegate->__set($key, $val);
152
    }
153
154
    /**
155
     * Delegate the magic method __cal() to the java object
156
     * to access the Java object method (and not the PHP
157
     * remote proxied object).
158
     *
159
     * @throws \Exception Depending on ThrowExceptionProxy
160
     *
161
     * @param string $method
162
     * @param array  $args
163
     *
164
     * @return mixed
165
     */
166 73
    public function __call(string $method, array $args)
167
    {
168 73
        if (!isset($this->__delegate)) {
169
            $this->__createDelegate();
170
        }
171
172 73
        return $this->__delegate->__call($method, $args);
173
    }
174
175
    /**
176
     * @param mixed|null ...$args arguments
177
     *
178
     * @return ObjectIterator
179
     */
180 8
    public function getIterator(...$args)
181
    {
182 8
        if (!isset($this->__delegate)) {
183
            $this->__createDelegate();
184
        }
185
186 8
        if (empty($args)) {
187 8
            return $this->__delegate->getIterator();
0 ignored issues
show
Bug introduced by
The method getIterator() does not exist on Soluble\Japha\Bridge\Driver\Pjb62\JavaType. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

187
            return $this->__delegate->/** @scrutinizer ignore-call */ getIterator();
Loading history...
188
        }
189
190 1
        return $this->__call('getIterator', $args);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->__call('getIterator', $args) also could return the type Soluble\Japha\Bridge\Driver\Pjb62\JavaType which is incompatible with the documented return type Soluble\Japha\Bridge\Driver\Pjb62\ObjectIterator.
Loading history...
191
    }
192
193
    /**
194
     * @param string|int $idx
195
     * @param mixed|null ...$args
196
     *
197
     * @return bool
198
     */
199 2
    public function offsetExists($idx, ...$args): bool
200
    {
201 2
        if (!isset($this->__delegate)) {
202
            $this->__createDelegate();
203
        }
204 2
        if (empty($args)) {
205 2
            return $this->__delegate->offsetExists($idx);
0 ignored issues
show
Bug introduced by
The method offsetExists() does not exist on Soluble\Japha\Bridge\Driver\Pjb62\JavaType. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

205
            return $this->__delegate->/** @scrutinizer ignore-call */ offsetExists($idx);
Loading history...
206
        }
207
208
        // In case we supplied more arguments than what ArrayAccess
209
        // suggest, let's try for a java method called offsetExists
210
        // with all the provided parameters
211
        array_unshift($args, $idx); // Will add idx at the beginning of args params
212
        return $this->__call('offsetExists', $args);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->__call('offsetExists', $args) could return the type Soluble\Japha\Bridge\Driver\Pjb62\JavaType which is incompatible with the type-hinted return boolean. Consider adding an additional type-check to rule them out.
Loading history...
213
    }
214
215
    /**
216
     * @param string|int $idx
217
     * @param mixed|null ...$args additional arguments
218
     *
219
     * @return mixed
220
     */
221 4
    public function offsetGet($idx, ...$args)
222
    {
223 4
        if (!isset($this->__delegate)) {
224
            $this->__createDelegate();
225
        }
226 4
        if (empty($args)) {
227 4
            return $this->__delegate->offsetGet($idx);
0 ignored issues
show
Bug introduced by
The method offsetGet() does not exist on Soluble\Japha\Bridge\Driver\Pjb62\JavaType. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

227
            return $this->__delegate->/** @scrutinizer ignore-call */ offsetGet($idx);
Loading history...
228
        }
229 1
        array_unshift($args, $idx);
230
231 1
        return $this->__call('offsetGet', $args);
232
    }
233
234
    /**
235
     * @param string|int $idx
236
     * @param mixed      $val
237
     * @param mixed|null ...$args additional arguments
238
     *
239
     * @return mixed
240
     */
241 3
    public function offsetSet($idx, $val, ...$args)
242
    {
243 3
        if (!isset($this->__delegate)) {
244
            $this->__createDelegate();
245
        }
246 3
        if (empty($args)) {
247 3
            return $this->__delegate->offsetSet($idx, $val);
0 ignored issues
show
Bug introduced by
The method offsetSet() does not exist on Soluble\Japha\Bridge\Driver\Pjb62\JavaType. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

247
            return $this->__delegate->/** @scrutinizer ignore-call */ offsetSet($idx, $val);
Loading history...
248
        }
249
250 1
        array_unshift($args, $idx, $val);
251
252 1
        return $this->__call('offsetSet', $args);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->__call('offsetSet', $args) also could return the type Soluble\Japha\Bridge\Driver\Pjb62\JavaType which is incompatible with the return type mandated by ArrayAccess::offsetSet() of void.
Loading history...
253
    }
254
255
    /**
256
     * @param mixed      $idx
257
     * @param mixed|null ...$args additional arguments
258
     *
259
     * @return mixed|void
260
     */
261 1
    public function offsetUnset($idx, ...$args)
262
    {
263 1
        if (!isset($this->__delegate)) {
264
            $this->__createDelegate();
265
        }
266 1
        if (empty($args)) {
267 1
            return $this->__delegate->offsetUnset($idx);
0 ignored issues
show
Bug introduced by
The method offsetUnset() does not exist on Soluble\Japha\Bridge\Driver\Pjb62\JavaType. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

267
            return $this->__delegate->/** @scrutinizer ignore-call */ offsetUnset($idx);
Loading history...
268
        }
269 1
        array_unshift($args, $idx);
270
271 1
        return $this->__call('offsetUnset', $args);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->__call('offsetUnset', $args) also could return the type Soluble\Japha\Bridge\Driver\Pjb62\JavaType which is incompatible with the return type mandated by ArrayAccess::offsetUnset() of void.
Loading history...
272
    }
273
274
    /**
275
     * @return int
276
     */
277 57
    public function get__java(): int
278
    {
279 57
        return $this->__java;
280
    }
281
282
    /**
283
     * Return java object id.
284
     *
285
     * @return int
286
     */
287 3
    public function __getJavaInternalObjectId(): int
288
    {
289 3
        return $this->__java;
290
    }
291
292
    /**
293
     * @return string
294
     */
295 35
    public function get__signature(): ?string
296
    {
297 35
        return $this->__signature;
298
    }
299
300
    /**
301
     * The PHP magic method __toString() cannot be applied
302
     * on the PHP object but has to be delegated to the Java one.
303
     *
304
     * @return string
305
     */
306 62
    public function __toString(): string
307
    {
308 62
        if (!isset($this->__delegate)) {
309
            $this->__createDelegate();
310
        }
311
312 62
        return $this->__delegate->__toString();
313
    }
314
315
    /**
316
     * Returns the runtime class of this Object.
317
     * The returned Class object is the object that is locked by static synchronized methods of the represented class.
318
     *
319
     * @return Interfaces\JavaObject Java('java.lang.Object')
320
     */
321 1
    public function getClass(): Interfaces\JavaObject
322
    {
323 1
        return $this->__delegate->getClass();
0 ignored issues
show
Bug introduced by
The method getClass() does not exist on Soluble\Japha\Bridge\Driver\Pjb62\JavaType. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

323
        return $this->__delegate->/** @scrutinizer ignore-call */ getClass();
Loading history...
324
    }
325
}
326