Skip to content

Commit d568f42

Browse files
committed
minor #72 chore: remove occurrences of empty() (alexandre-daubois, chr-hertel)
This PR was merged into the main branch. Discussion ---------- chore: remove occurrences of `empty()` | Q | A | ------------- | --- | Bug fix? | no | New feature? |no | Docs? | no | Issues | | License | MIT Cherry picking php-llm/llm-chain#374 Commits ------- c0d688f chore: use [] checks instead of count with zero on array fb3d1a8 chore: use isset instead of bool cast on $_ENV 14c448c chore: remove occurrences of `empty()` (#374)
2 parents 90e7cc2 + c7d4a88 commit d568f42

File tree

12 files changed

+16
-16
lines changed

12 files changed

+16
-16
lines changed

src/Bridge/Anthropic/Contract/AssistantMessageNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function normalize(mixed $data, ?string $format = null, array $context =
5858
'type' => 'tool_use',
5959
'id' => $toolCall->id,
6060
'name' => $toolCall->name,
61-
'input' => empty($toolCall->arguments) ? new \stdClass() : $toolCall->arguments,
61+
'input' => [] !== $toolCall->arguments ? $toolCall->arguments : new \stdClass(),
6262
];
6363
}, $data->toolCalls),
6464
];

src/Bridge/Anthropic/ResponseConverter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function convert(ResponseInterface $response, array $options = []): LlmRe
4242

4343
$data = $response->toArray();
4444

45-
if (!isset($data['content']) || 0 === \count($data['content'])) {
45+
if (!isset($data['content']) || [] === $data['content']) {
4646
throw new RuntimeException('Response does not contain any content');
4747
}
4848

@@ -53,11 +53,11 @@ public function convert(ResponseInterface $response, array $options = []): LlmRe
5353
}
5454
}
5555

56-
if (!isset($data['content'][0]['text']) && 0 === \count($toolCalls)) {
56+
if (!isset($data['content'][0]['text']) && [] === $toolCalls) {
5757
throw new RuntimeException('Response content does not contain any text nor tool calls.');
5858
}
5959

60-
if (!empty($toolCalls)) {
60+
if ([] !== $toolCalls) {
6161
return new ToolCallResponse(...$toolCalls);
6262
}
6363

src/Bridge/Bedrock/Anthropic/ClaudeHandler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function convert(InvokeModelResponse $bedrockResponse): ToolCallResponse|
6363
{
6464
$data = json_decode($bedrockResponse->getBody(), true, 512, \JSON_THROW_ON_ERROR);
6565

66-
if (!isset($data['content']) || 0 === \count($data['content'])) {
66+
if (!isset($data['content']) || [] === $data['content']) {
6767
throw new RuntimeException('Response does not contain any content');
6868
}
6969

@@ -77,7 +77,7 @@ public function convert(InvokeModelResponse $bedrockResponse): ToolCallResponse|
7777
$toolCalls[] = new ToolCall($content['id'], $content['name'], $content['input']);
7878
}
7979
}
80-
if (!empty($toolCalls)) {
80+
if ([] !== $toolCalls) {
8181
return new ToolCallResponse(...$toolCalls);
8282
}
8383

src/Bridge/Bedrock/Nova/Contract/AssistantMessageNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function normalize(mixed $data, ?string $format = null, array $context =
5757
'toolUse' => [
5858
'toolUseId' => $toolCall->id,
5959
'name' => $toolCall->name,
60-
'input' => empty($toolCall->arguments) ? new \stdClass() : $toolCall->arguments,
60+
'input' => [] !== $toolCall->arguments ? $toolCall->arguments : new \stdClass(),
6161
],
6262
];
6363
}, $data->toolCalls),

src/Bridge/Bedrock/Nova/NovaHandler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function convert(InvokeModelResponse $bedrockResponse): ToolCallResponse|
6464
{
6565
$data = json_decode($bedrockResponse->getBody(), true, 512, \JSON_THROW_ON_ERROR);
6666

67-
if (!isset($data['output']) || 0 === \count($data['output'])) {
67+
if (!isset($data['output']) || [] === $data['output']) {
6868
throw new RuntimeException('Response does not contain any content');
6969
}
7070

@@ -78,7 +78,7 @@ public function convert(InvokeModelResponse $bedrockResponse): ToolCallResponse|
7878
$toolCalls[] = new ToolCall($content['toolUse']['toolUseId'], $content['toolUse']['name'], $content['toolUse']['input']);
7979
}
8080
}
81-
if (!empty($toolCalls)) {
81+
if ([] !== $toolCalls) {
8282
return new ToolCallResponse(...$toolCalls);
8383
}
8484

src/Bridge/HuggingFace/ModelClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ private function getPayload(array|string $payload, array $options): array
7777
if (\is_string($payload) || !(isset($payload['body']) || isset($payload['json']))) {
7878
$payload = ['json' => ['inputs' => $payload]];
7979

80-
if (0 !== \count($options)) {
80+
if ([] !== $options) {
8181
$payload['json']['parameters'] = $options;
8282
}
8383
}

src/Contract/JsonSchema/Factory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public function buildProperties(string $className): ?array
9090
*/
9191
private function convertTypes(array $elements): ?array
9292
{
93-
if (0 === \count($elements)) {
93+
if ([] === $elements) {
9494
return null;
9595
}
9696

src/Message/AssistantMessage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@ public function getId(): Uuid
4343

4444
public function hasToolCalls(): bool
4545
{
46-
return null !== $this->toolCalls && 0 !== \count($this->toolCalls);
46+
return null !== $this->toolCalls && [] !== $this->toolCalls;
4747
}
4848
}

src/Response/Choice.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@ public function getToolCalls(): array
4545

4646
public function hasToolCall(): bool
4747
{
48-
return 0 !== \count($this->toolCalls);
48+
return [] !== $this->toolCalls;
4949
}
5050
}

src/Response/ChoiceResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ final class ChoiceResponse extends BaseResponse
2525

2626
public function __construct(Choice ...$choices)
2727
{
28-
if (0 === \count($choices)) {
28+
if ([] === $choices) {
2929
throw new InvalidArgumentException('Response must have at least one choice.');
3030
}
3131

0 commit comments

Comments
 (0)