Skip to content

Commit decde49

Browse files
committed
Optimize the code to reduce redundancy
1 parent b03e4b8 commit decde49

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

src/core/RemoteObject/Context.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,8 @@ public function __construct(Request $request, Response $response)
3434

3535
public function end(array $data): void
3636
{
37-
$resp = serialize($data);
38-
if (!$resp) {
39-
throw new Exception('json_encode error, Error: ' . json_last_error_msg());
40-
}
4137
$this->response->header('Content-Type', 'application/octet-stream');
42-
$this->response->end($resp);
38+
$this->response->end(serialize($data));
4339
}
4440

4541
public function getHandler(): string
@@ -48,13 +44,17 @@ public function getHandler(): string
4844
return str_replace('/', '_', $path);
4945
}
5046

51-
public function getParam(string $name, bool $required = true, $default = null): string
47+
public function getParam(string $name): string
5248
{
53-
$value = $this->request->post[$name] ?? $default;
54-
if ($required and $value === null) {
49+
if (!isset($this->request->post[$name])) {
5550
throw new Exception("param[{$name}] is empty");
5651
}
57-
return $value;
52+
return $this->request->post[$name];
53+
}
54+
55+
public function getDataParam(string $name): mixed
56+
{
57+
return unserialize($this->getParam($name));
5858
}
5959

6060
public function getCoroutineId(): int

src/core/RemoteObject/Server.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ private function _new(Context $ctx): void
145145
throw new Exception("class[{$class}] not allowed");
146146
}
147147
$class = '\\' . $class;
148-
$args = unserialize($ctx->getParam('args'));
148+
$args = $ctx->getDataParam('args');
149149
foreach ($args as $key => $value) {
150150
$args[$key] = $this->unmarshal($value);
151151
}
@@ -160,7 +160,7 @@ private function _call_function(Context $ctx): void
160160
if (count($this->allowedFunctions) > 0 and !isset($this->allowedFunctions[$fn])) {
161161
throw new Exception("function[{$fn}] not allowed");
162162
}
163-
$args = unserialize($ctx->getParam('args'));
163+
$args = $ctx->getDataParam('args');
164164
foreach ($args as $key => $value) {
165165
$args[$key] = $this->unmarshal($value);
166166
}
@@ -182,7 +182,7 @@ private function _call_method(Context $ctx): void
182182
throw new Exception("object[#{$object_id}] not found");
183183
}
184184
$method = $ctx->getParam('method');
185-
$args = unserialize($ctx->getParam('args'));
185+
$args = $ctx->getDataParam('args');
186186
foreach ($args as $key => $value) {
187187
$args[$key] = $this->unmarshal($value);
188188
}
@@ -217,7 +217,7 @@ private function _write_property(Context $ctx): void
217217
{
218218
$object_id = $ctx->getParam('object');
219219
$property = $ctx->getParam('property');
220-
$value = unserialize($ctx->getParam('value'));
220+
$value = $ctx->getDataParam('value');
221221
if (!isset($this->objects[$object_id])) {
222222
throw new Exception("object[#{$object_id}] not found");
223223
}
@@ -265,7 +265,7 @@ private function _offset_set(Context $ctx): void
265265
{
266266
$object_id = $ctx->getParam('object');
267267
$offset = $ctx->getParam('offset');
268-
$value = unserialize($ctx->getParam('value'));
268+
$value = $ctx->getDataParam('value');
269269
if (!isset($this->objects[$object_id])) {
270270
throw new Exception("object[#{$object_id}] not found");
271271
}

tests/unit/RemoteObjectTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,14 @@ public function testResource()
6161
{
6262
run(function () {
6363
$client = new RemoteObject\Client('127.0.0.1', RemoteObject\Server::DEFAULT_PORT);
64-
$fp = $client->call('fopen', '/tmp/data.txt', 'w');
64+
$fp = $client->call('fopen', '/tmp/data.txt', 'w');
6565

66-
$n = random_int(1024, 65536);
66+
$n = random_int(1024, 65536);
6767
$wdata = random_bytes($n);
6868
$client->call('fwrite', $fp, $wdata);
6969
$client->call('fclose', $fp);
7070

71-
$fp = $client->call('fopen', '/tmp/data.txt', 'r');
71+
$fp = $client->call('fopen', '/tmp/data.txt', 'r');
7272
$rdata = $client->call('fread', $fp, $n);
7373
$client->call('fclose', $fp);
7474
$this->assertEquals($wdata, $rdata);

0 commit comments

Comments
 (0)