Skip to content

Commit 6f6d762

Browse files
Merge pull request #139 from sonarta/master
fix: handle empty parseResponse for nested script execution (issue #136)
2 parents ac697bd + 26cef90 commit 6f6d762

2 files changed

Lines changed: 30 additions & 4 deletions

File tree

src/Client.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ public function readRAW(array $options = [])
252252
}
253253

254254
// Read answer from socket in loop, or until timeout reached
255-
$startTime = time();
255+
$startTime = time();
256256
while (true) {
257257
// Exit from loop if timeout reached
258258
if (time() > $startTime + $this->config('socket_timeout')) {
@@ -374,7 +374,10 @@ private function rosario(array $raw): array
374374
}
375375

376376
// Save as result
377-
if(null != $this->parseResponse($item)) $result[] = $this->parseResponse($item)[0];
377+
$parsed = $this->parseResponse($item);
378+
if (!empty($parsed) && isset($parsed[0])) {
379+
$result[] = $parsed[0];
380+
}
378381
}
379382

380383
} else {

tests/ClientTest.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,17 @@ public function setUp(): void
4343
'ssh_port' => (int) getenv('ROS_SSH_PORT'),
4444
];
4545

46-
$this->client = new class($this->config) extends Client {
46+
$this->client =
47+
48+
new class ($this->config) extends Client {
49+
4750
// Convert protected method to public
4851
public function pregResponse(string $value, ?array &$matches): void
4952
{
5053
parent::pregResponse($value, $matches);
5154
}
52-
};
55+
56+
};
5357

5458
$this->portModern = (int) getenv('ROS_PORT_MODERN');
5559
$this->portLegacy = (int) getenv('ROS_PORT_LEGACY');
@@ -353,4 +357,23 @@ public function test_export_asQuery(): void
353357
$result = $this->client->query('/export');
354358
self::assertNotEmpty($result);
355359
}
360+
361+
public function test_parseResponse_emptyArrayHandling(): void
362+
{
363+
// Simulate responses that could come from nested script execution
364+
$emptyResponse = [];
365+
$result = $this->client->parseResponse($emptyResponse);
366+
self::assertIsArray($result);
367+
self::assertEmpty($result);
368+
369+
// Response with only control words (no data)
370+
$doneOnlyResponse = ['!done'];
371+
$result = $this->client->parseResponse($doneOnlyResponse);
372+
self::assertIsArray($result);
373+
374+
// Response with trap (error case)
375+
$trapResponse = ['!trap', '=message=no such command'];
376+
$result = $this->client->parseResponse($trapResponse);
377+
self::assertIsArray($result);
378+
}
356379
}

0 commit comments

Comments
 (0)