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

Java::__destruct()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 6.4222

Importance

Changes 0
Metric Value
cc 5
eloc 13
nc 4
nop 0
dl 0
loc 16
ccs 8
cts 13
cp 0.6153
crap 6.4222
rs 9.5222
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * soluble-japha / PHPJavaBridge driver client.
7
 *
8
 * Refactored version of phpjababridge's Java.inc file compatible
9
 * with php java bridge 6.2
10
 *
11
 *
12
 * @credits   http://php-java-bridge.sourceforge.net/pjb/
13
 *
14
 * @see      http://github.com/belgattitude/soluble-japha
15
 *
16
 * @author Jost Boekemeier
17
 * @author Vanvelthem Sébastien (refactoring and fixes from original implementation)
18
 * @license   MIT
19
 *
20
 * The MIT License (MIT)
21
 * Copyright (c) 2014-2017 Jost Boekemeier
22
 * Permission is hereby granted, free of charge, to any person obtaining a copy
23
 * of this software and associated documentation files (the "Software"), to deal
24
 * in the Software without restriction, including without limitation the rights
25
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
26
 * copies of the Software, and to permit persons to whom the Software is
27
 * furnished to do so, subject to the following conditions:
28
 *
29
 * The above copyright notice and this permission notice shall be included in
30
 * all copies or substantial portions of the Software.
31
 *
32
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
38
 * THE SOFTWARE.
39
 *
40
 * @method string getBufferContents()
41
 */
42
43
namespace Soluble\Japha\Bridge\Driver\Pjb62;
44
45
class Java extends AbstractJava
46
{
47
    /**
48
     * @var string
49
     */
50
    protected $__internal_encoding;
51
52
    /**
53
     * Java constructor.
54
     *
55
     * @param string     $name    Java FQCN or an array with JavaFQCN followed by params
56
     * @param mixed|null ...$args variadic parameters
57
     */
58 67
    public function __construct(string $name, ...$args)
59
    {
60 67
        $this->__client = PjbProxyClient::getInstance()->getClient();
61 66
        $this->__internal_encoding = $this->__client->getParam(Client::PARAM_JAVA_INTERNAL_ENCODING);
62
63 66
        $client = $this->__client;
64
65 66
        $sig = "&{$this->__signature}@{$name}";
66 66
        $len = count($args);
67 66
        $args2 = [];
68 66
        for ($i = 0; $i < $len; ++$i) {
69 53
            $val = $args[$i];
70 53
            switch (gettype($val)) {
71 53
                case 'boolean':
72 1
                    $args2[] = $val;
73 1
                    $sig .= '@b';
74 1
                    break;
75 53
                case 'integer':
76 10
                    $args2[] = $val;
77 10
                    $sig .= '@i';
78 10
                    break;
79 50
                case 'double':
80 2
                    $args2[] = $val;
81 2
                    $sig .= '@d';
82 2
                    break;
83 49
                case 'string':
84 37
                    $args2[] = htmlspecialchars($val, ENT_COMPAT, $this->__internal_encoding);
85 37
                    $sig .= '@s';
86 37
                    break;
87 20
                case 'array':
88 14
                    $sig = '~INVALID';
89 14
                    break;
90 7
                case 'object':
91 5
                    if ($val instanceof JavaType) {
92 4
                        $args2[] = $val->get__java();
93 4
                        $sig .= "@o{$val->get__signature()}";
94
                    } else {
95 1
                        $sig = '~INVALID';
96
                    }
97 5
                    break;
98 2
                case 'resource':
99 1
                    $args2[] = $val;
100 1
                    $sig .= '@r';
101 1
                    break;
102 1
                case 'NULL':
103 1
                    $args2[] = $val;
104 1
                    $sig .= '@N';
105 1
                    break;
106
                case 'unknown type':
107
                    $args2[] = $val;
108
                    $sig .= '@u';
109
                    break;
110
                default:
111
                    throw new Exception\IllegalArgumentException($val);
112
            }
113
        }
114
115 66
        if (array_key_exists($sig, $client->methodCache)) {
116
            $cacheEntry = &$client->methodCache[$sig];
117
            $client->sendBuffer .= $client->preparedToSendBuffer;
118
            //if (strlen($client->sendBuffer) >= JAVA_SEND_SIZE) {
119
            if (strlen($client->sendBuffer) >= $this->__client->java_send_size) {
120
                if ($client->protocol->handler->write($client->sendBuffer) <= 0) {
121
                    throw new Exception\IllegalStateException('Connection out of sync,check backend log for details.');
122
                }
123
                $client->sendBuffer = null;
124
            }
125
            $client->preparedToSendBuffer = vsprintf($cacheEntry->fmt, $args2);
126
            $this->__java = ++$client->asyncCtx;
127
128
            $this->__factory = $cacheEntry->factory;
129
            $this->__signature = $cacheEntry->signature;
130
            $this->__cancelProxyCreationTag = ++$client->cancelProxyCreationTag;
131
        } else {
132 66
            $client->currentCacheKey = $sig;
133 66
            $this->__delegate = $client->createObject($name, $args);
134 62
            $delegate = $this->__delegate;
135
136 62
            $this->__java = $delegate->get__java();
137 62
            $this->__signature = $delegate->get__signature();
138
        }
139 62
    }
140
141 100
    public function __destruct()
142
    {
143 100
        if (!isset($this->__client)) {
144 1
            return;
145
        }
146 100
        $client = $this->__client;
147 100
        $preparedToSendBuffer = &$client->preparedToSendBuffer;
148 100
        if ($preparedToSendBuffer &&
149 100
                $client->cancelProxyCreationTag == $this->__cancelProxyCreationTag) {
150
            $preparedToSendBuffer[6] = '3';
151
            $client->sendBuffer .= $preparedToSendBuffer;
152
            $preparedToSendBuffer = null;
153
            --$client->asyncCtx;
154
        } else {
155 100
            if (!isset($this->__delegate)) {
156
                $client->unref($this->__java);
0 ignored issues
show
Bug introduced by
The method unref() does not exist on null. ( Ignorable by Annotation )

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

156
                $client->/** @scrutinizer ignore-call */ 
157
                         unref($this->__java);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
157
            }
158
        }
159 100
    }
160
161
    /**
162
     * Call a method on this JavaObject.
163
     *
164
     * @param string $method
165
     * @param array  $args
166
     *
167
     * @return mixed|null
168
     */
169 73
    public function __call(string $method, array $args)
170
    {
171 73
        $client = $this->__client;
172 73
        $sig = "@{$this->__signature}@$method";
173 73
        $len = count($args);
174 73
        $args2 = [$this->__java];
175 73
        for ($i = 0; $i < $len; ++$i) {
176 54
            switch (gettype($val = $args[$i])) {
177 54
                case 'boolean':
178 1
                    $args2[] = $val;
179 1
                    $sig .= '@b';
180 1
                    break;
181 54
                case 'integer':
182 5
                    $args2[] = $val;
183 5
                    $sig .= '@i';
184 5
                    break;
185 53
                case 'double':
186 1
                    $args2[] = $val;
187 1
                    $sig .= '@d';
188 1
                    break;
189 52
                case 'string':
190 40
                    $args2[] = htmlspecialchars($val, ENT_COMPAT, $this->__internal_encoding);
191 40
                    $sig .= '@s';
192 40
                    break;
193 31
                case 'array':
194
                    $sig = '~INVALID';
195
                    break;
196 31
                case 'object':
197 30
                    if ($val instanceof JavaType) {
198 30
                        $args2[] = $val->get__java();
199 30
                        $sig .= "@o{$val->get__signature()}";
200
                    } else {
201
                        $sig = '~INVALID';
202
                    }
203 30
                    break;
204 1
                case 'resource':
205
                    $args2[] = $val;
206
                    $sig .= '@r';
207
                    break;
208 1
                case 'NULL':
209 1
                    $args2[] = $val;
210 1
                    $sig .= '@N';
211 1
                    break;
212
                case 'unknown type':
213
                    $args2[] = $val;
214
                    $sig .= '@u';
215
                    break;
216
                default:
217
                    throw new Exception\IllegalArgumentException($val);
218
            }
219
        }
220 73
        if (array_key_exists($sig, $client->methodCache)) {
221
            $cacheEntry = &$client->methodCache[$sig];
222
            $client->sendBuffer .= $client->preparedToSendBuffer;
223
            if (strlen($client->sendBuffer) >= $this->__client->java_send_size) {
224
                if ($client->protocol->handler->write($client->sendBuffer) <= 0) {
225
                    throw new Exception\IllegalStateException('Out of sync. Check backend log for details.');
226
                }
227
                $client->sendBuffer = null;
228
            }
229
            $client->preparedToSendBuffer = vsprintf($cacheEntry->fmt, $args2);
230
            if ($cacheEntry->resultVoid) {
231
                ++$client->cancelProxyCreationTag;
232
233
                return null;
234
            } else {
235
                $result = clone $client->cachedJavaPrototype;
236
                $result->__factory = $cacheEntry->factory;
237
                $result->__java = ++$client->asyncCtx;
238
                $result->__signature = $cacheEntry->signature;
239
                $result->__cancelProxyCreationTag = ++$client->cancelProxyCreationTag;
240
241
                return $result;
242
            }
243
        } else {
244 73
            $client->currentCacheKey = $sig;
245 73
            $retval = parent::__call($method, $args);
246
247 68
            return $retval;
248
        }
249
    }
250
}
251