Complex classes like Pjb62Driver often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Pjb62Driver, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class Pjb62Driver extends AbstractDriver |
||
17 | { |
||
18 | /** |
||
19 | * @var bool |
||
20 | */ |
||
21 | protected $connected = false; |
||
22 | |||
23 | /** |
||
24 | * @var PjbProxyClient |
||
25 | */ |
||
26 | protected $pjbProxyClient; |
||
27 | |||
28 | /** |
||
29 | * @var LoggerInterface |
||
30 | */ |
||
31 | protected $logger; |
||
32 | |||
33 | /** |
||
34 | * Constructor. |
||
35 | * |
||
36 | * <code> |
||
37 | * |
||
38 | * $ba = new Pjb62Driver([ |
||
39 | * 'servlet_address' => 'http://127.0.0.1:8080/javabridge-bundle/servlet.phpjavabridge' |
||
40 | * //'java_default_timezone' => null, |
||
41 | * //'java_prefer_values' => true, |
||
42 | * //'java_log_level' => null, |
||
43 | * //'java_send_size' => 8192, |
||
44 | * //'java_recv_size' => 8192, |
||
45 | * //'internal_encoding' => 'UTF-8', |
||
46 | * //'force_simple_xml_parser' => false |
||
47 | * ], $logger); |
||
48 | * |
||
49 | * </code> |
||
50 | * |
||
51 | * @param array $options |
||
52 | * @param LoggerInterface $logger |
||
53 | * |
||
54 | * @throws Exception\InvalidArgumentException |
||
55 | * @throws Exception\ConnectionException |
||
56 | */ |
||
57 | 139 | public function __construct(array $options, LoggerInterface $logger = null) |
|
78 | |||
79 | 1 | public function getLogger(): LoggerInterface |
|
80 | { |
||
81 | 1 | return $this->logger; |
|
82 | } |
||
83 | |||
84 | /** |
||
85 | * Return underlying bridge client. |
||
86 | * |
||
87 | * @return PjbProxyClient |
||
88 | */ |
||
89 | 9 | public function getClient(): ClientInterface |
|
90 | { |
||
91 | 9 | return $this->pjbProxyClient; |
|
92 | } |
||
93 | |||
94 | /** |
||
95 | * {@inheritdoc} |
||
96 | * |
||
97 | * @throws BrokenConnectionException |
||
98 | */ |
||
99 | 50 | public function getJavaClass(string $class_name): Interfaces\JavaClass |
|
100 | { |
||
101 | try { |
||
102 | 50 | $class = $this->pjbProxyClient->getJavaClass($class_name); |
|
103 | 3 | } catch (Pjb62BrokenConnectionException|Exception\InvalidUsageException $e) { |
|
104 | 1 | PjbProxyClient::unregisterInstance(); |
|
105 | 1 | throw new BrokenConnectionException($e->getMessage(), $e->getCode(), $e); |
|
106 | } |
||
107 | |||
108 | 47 | return $class; |
|
109 | } |
||
110 | |||
111 | /** |
||
112 | * {@inheritdoc} |
||
113 | * |
||
114 | * @throws BrokenConnectionException |
||
115 | */ |
||
116 | 65 | public function instanciate(string $class_name, ...$args): Interfaces\JavaObject |
|
117 | { |
||
118 | try { |
||
119 | 65 | $java = new Java($class_name, ...$args); |
|
120 | 4 | } catch (Pjb62BrokenConnectionException|Exception\InvalidUsageException $e) { |
|
121 | 2 | PjbProxyClient::unregisterInstance(); |
|
122 | 2 | throw new BrokenConnectionException($e->getMessage(), $e->getCode(), $e); |
|
123 | } |
||
124 | |||
125 | 62 | return $java; |
|
126 | } |
||
127 | |||
128 | /** |
||
129 | * Set the java file encoding, for example UTF-8, ISO-8859-1 or ASCII. |
||
130 | * |
||
131 | * Needed because php does not support unicode. All string to byte array |
||
132 | * conversions use this encoding. Example: |
||
133 | * |
||
134 | * @param string $encoding Please see Java file.encoding documentation for a list of valid encodings. |
||
135 | * |
||
136 | * @throws BrokenConnectionException |
||
137 | */ |
||
138 | 1 | public function setFileEncoding(string $encoding): void |
|
139 | { |
||
140 | 1 | $this->invoke(null, 'setFileEncoding', [$encoding]); |
|
141 | 1 | } |
|
142 | |||
143 | /** |
||
144 | * Return bridge connection options. |
||
145 | * |
||
146 | * @throws BrokenConnectionException |
||
147 | * |
||
148 | * @return Interfaces\JavaObject Java("io.soluble.pjb.bridge.Options") |
||
149 | */ |
||
150 | 1 | public function getConnectionOptions(): Interfaces\JavaObject |
|
151 | { |
||
152 | 1 | return $this->invoke(null, 'getOptions'); |
|
153 | } |
||
154 | |||
155 | /** |
||
156 | * {@inheritdoc} |
||
157 | * |
||
158 | * @throws BrokenConnectionException |
||
159 | */ |
||
160 | 4 | public function invoke(Interfaces\JavaType $javaObject = null, string $method, array $args = []) |
|
169 | |||
170 | /** |
||
171 | * Returns the jsr223 script context handle. |
||
172 | * |
||
173 | * @throws BrokenConnectionException |
||
174 | * |
||
175 | * @return Interfaces\JavaObject |
||
176 | */ |
||
177 | 5 | public function getContext(): Interfaces\JavaObject |
|
186 | |||
187 | /** |
||
188 | * Return java servlet session. |
||
189 | * |
||
190 | * <code> |
||
191 | * $session = $adapter->getDriver()->getJavaSession(); |
||
192 | * $counter = $session->get('counter'); |
||
193 | * if ($adapter->isNull($counter)) { |
||
194 | * $session->put('counter', 1); |
||
195 | * } else { |
||
196 | * $session->put('counter', $counter + 1); |
||
197 | * } |
||
198 | * </code> |
||
199 | * |
||
200 | * @param array $args |
||
201 | * |
||
202 | * @throws BrokenConnectionException |
||
203 | * |
||
204 | * @return Interfaces\JavaObject |
||
205 | */ |
||
206 | 2 | public function getJavaSession(array $args = []): Interfaces\JavaObject |
|
215 | |||
216 | /** |
||
217 | * Inspect the class internals. |
||
218 | * |
||
219 | * @param Interfaces\JavaObject $javaObject |
||
220 | * |
||
221 | * @throws BrokenConnectionException |
||
222 | * |
||
223 | * @return string |
||
224 | */ |
||
225 | 12 | public function inspect(Interfaces\JavaObject $javaObject): string |
|
226 | { |
||
227 | try { |
||
228 | 12 | $inspect = $this->pjbProxyClient->inspect($javaObject); |
|
229 | } catch (Pjb62BrokenConnectionException|Exception\InvalidUsageException $e) { |
||
230 | PjbProxyClient::unregisterInstance(); |
||
231 | throw new BrokenConnectionException($e->getMessage(), $e->getCode(), $e); |
||
232 | } |
||
233 | |||
234 | 12 | return $inspect; |
|
235 | } |
||
236 | |||
237 | /** |
||
238 | * {@inheritdoc} |
||
239 | * |
||
240 | * @throws BrokenConnectionException |
||
241 | */ |
||
242 | 8 | public function isInstanceOf(Interfaces\JavaObject $javaObject, $className): bool |
|
251 | |||
252 | /** |
||
253 | * {@inheritdoc} |
||
254 | * |
||
255 | * @throws BrokenConnectionException |
||
256 | */ |
||
257 | 12 | public function values(Interfaces\JavaObject $javaObject) |
|
266 | |||
267 | /** |
||
268 | * Return java bridge header or empty string if nothing. |
||
269 | * |
||
270 | * @param string $name |
||
271 | * @param array $array |
||
272 | * |
||
273 | * @return string header value or empty string if not exists |
||
274 | */ |
||
275 | 28 | public static function getJavaBridgeHeader(string $name, array $array): string |
|
276 | { |
||
277 | 28 | if (array_key_exists($name, $array)) { |
|
278 | 1 | return $array[$name]; |
|
279 | } |
||
280 | 28 | $name = "HTTP_$name"; |
|
281 | 28 | if (array_key_exists($name, $array)) { |
|
282 | 1 | return $array[$name]; |
|
283 | } |
||
284 | |||
285 | 28 | return ''; |
|
286 | } |
||
287 | |||
288 | /** |
||
289 | * Cast internal objects to a new type. |
||
290 | * |
||
291 | * @param Interfaces\JavaObject|JavaType $javaObject |
||
292 | * @param string $cast_type |
||
293 | * |
||
294 | * @return mixed |
||
295 | */ |
||
296 | 8 | public static function castPjbInternal($javaObject, string $cast_type) |
|
297 | { |
||
298 | 8 | if (!$javaObject instanceof JavaType) { |
|
299 | $first_char = strtoupper($cast_type[0]); |
||
300 | switch ($first_char) { |
||
301 | case 'S': |
||
302 | return (string) $javaObject; |
||
303 | case 'B': |
||
304 | return (bool) $javaObject; |
||
305 | case 'L': |
||
306 | case 'I': |
||
307 | return (int) $javaObject; |
||
308 | case 'D': |
||
309 | case 'F': |
||
310 | return (float) $javaObject; |
||
311 | case 'N': |
||
312 | return null; |
||
313 | case 'A': |
||
314 | return (array) $javaObject; |
||
315 | case 'O': |
||
316 | return (object) $javaObject; |
||
317 | } |
||
318 | } |
||
319 | |||
320 | 8 | return $javaObject->__cast($cast_type); |
|
321 | } |
||
322 | |||
323 | /** |
||
324 | * {@inheritdoc} |
||
325 | */ |
||
326 | public function cast(Interfaces\JavaObject $javaObject, string $cast_type) |
||
360 | |||
361 | /** |
||
362 | * Return object java class name. |
||
363 | * |
||
364 | * @throws Exception\UnexpectedException |
||
365 | * @throws BrokenConnectionException |
||
366 | * |
||
367 | * @param Interfaces\JavaObject $javaObject |
||
368 | * |
||
369 | * @return string |
||
370 | */ |
||
371 | 11 | public function getClassName(Interfaces\JavaObject $javaObject): string |
|
384 | } |
||
385 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.