Skip to content

Commit b72429a

Browse files
committed
minor #399 [Platform][Store] Refactor short-circuit evaluation to classic if statements (OskarStark)
This PR was merged into the main branch. Discussion ---------- [Platform][Store] Refactor short-circuit evaluation to classic if statements | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | Docs? | no | Issues | -- | License | MIT Replace all short-circuit evaluation patterns (`|| throw`) with classic if statements for better readability and consistency across the codebase. AFAIK this is more the Symfony-way of doing. Commits ------- 1846ab5 Refactor short-circuit evaluation to classic if statements
2 parents 6fb5e13 + 1846ab5 commit b72429a

File tree

14 files changed

+165
-55
lines changed

14 files changed

+165
-55
lines changed

src/platform/src/Bridge/Albert/PlatformFactory.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,18 @@ public static function create(
2929
string $baseUrl,
3030
?HttpClientInterface $httpClient = null,
3131
): Platform {
32-
str_starts_with($baseUrl, 'https://') || throw new InvalidArgumentException('The Albert URL must start with "https://".');
33-
!str_ends_with($baseUrl, '/') || throw new InvalidArgumentException('The Albert URL must not end with a trailing slash.');
34-
preg_match('/\/v\d+$/', $baseUrl) || throw new InvalidArgumentException('The Albert URL must include an API version (e.g., /v1, /v2).');
35-
'' !== $apiKey || throw new InvalidArgumentException('The API key must not be empty.');
32+
if (!str_starts_with($baseUrl, 'https://')) {
33+
throw new InvalidArgumentException('The Albert URL must start with "https://".');
34+
}
35+
if (str_ends_with($baseUrl, '/')) {
36+
throw new InvalidArgumentException('The Albert URL must not end with a trailing slash.');
37+
}
38+
if (!preg_match('/\/v\d+$/', $baseUrl)) {
39+
throw new InvalidArgumentException('The Albert URL must include an API version (e.g., /v1, /v2).');
40+
}
41+
if ('' === $apiKey) {
42+
throw new InvalidArgumentException('The API key must not be empty.');
43+
}
3644

3745
$httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);
3846

src/platform/src/Bridge/Azure/OpenAi/EmbeddingsModelClient.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,21 @@ public function __construct(
3434
#[\SensitiveParameter] private string $apiKey,
3535
) {
3636
$this->httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);
37-
!str_starts_with($this->baseUrl, 'http://') || throw new InvalidArgumentException('The base URL must not contain the protocol.');
38-
!str_starts_with($this->baseUrl, 'https://') || throw new InvalidArgumentException('The base URL must not contain the protocol.');
39-
'' !== $deployment || throw new InvalidArgumentException('The deployment must not be empty.');
40-
'' !== $apiVersion || throw new InvalidArgumentException('The API version must not be empty.');
41-
'' !== $apiKey || throw new InvalidArgumentException('The API key must not be empty.');
37+
if (str_starts_with($this->baseUrl, 'http://')) {
38+
throw new InvalidArgumentException('The base URL must not contain the protocol.');
39+
}
40+
if (str_starts_with($this->baseUrl, 'https://')) {
41+
throw new InvalidArgumentException('The base URL must not contain the protocol.');
42+
}
43+
if ('' === $deployment) {
44+
throw new InvalidArgumentException('The deployment must not be empty.');
45+
}
46+
if ('' === $apiVersion) {
47+
throw new InvalidArgumentException('The API version must not be empty.');
48+
}
49+
if ('' === $apiKey) {
50+
throw new InvalidArgumentException('The API key must not be empty.');
51+
}
4252
}
4353

4454
public function supports(Model $model): bool

src/platform/src/Bridge/Azure/OpenAi/GptModelClient.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,21 @@ public function __construct(
3434
#[\SensitiveParameter] private string $apiKey,
3535
) {
3636
$this->httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);
37-
!str_starts_with($this->baseUrl, 'http://') || throw new InvalidArgumentException('The base URL must not contain the protocol.');
38-
!str_starts_with($this->baseUrl, 'https://') || throw new InvalidArgumentException('The base URL must not contain the protocol.');
39-
'' !== $deployment || throw new InvalidArgumentException('The deployment must not be empty.');
40-
'' !== $apiVersion || throw new InvalidArgumentException('The API version must not be empty.');
41-
'' !== $apiKey || throw new InvalidArgumentException('The API key must not be empty.');
37+
if (str_starts_with($this->baseUrl, 'http://')) {
38+
throw new InvalidArgumentException('The base URL must not contain the protocol.');
39+
}
40+
if (str_starts_with($this->baseUrl, 'https://')) {
41+
throw new InvalidArgumentException('The base URL must not contain the protocol.');
42+
}
43+
if ('' === $deployment) {
44+
throw new InvalidArgumentException('The deployment must not be empty.');
45+
}
46+
if ('' === $apiVersion) {
47+
throw new InvalidArgumentException('The API version must not be empty.');
48+
}
49+
if ('' === $apiKey) {
50+
throw new InvalidArgumentException('The API key must not be empty.');
51+
}
4252
}
4353

4454
public function supports(Model $model): bool

src/platform/src/Bridge/Azure/OpenAi/WhisperModelClient.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,21 @@ public function __construct(
3535
#[\SensitiveParameter] private string $apiKey,
3636
) {
3737
$this->httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);
38-
!str_starts_with($this->baseUrl, 'http://') || throw new InvalidArgumentException('The base URL must not contain the protocol.');
39-
!str_starts_with($this->baseUrl, 'https://') || throw new InvalidArgumentException('The base URL must not contain the protocol.');
40-
'' !== $deployment || throw new InvalidArgumentException('The deployment must not be empty.');
41-
'' !== $apiVersion || throw new InvalidArgumentException('The API version must not be empty.');
42-
'' !== $apiKey || throw new InvalidArgumentException('The API key must not be empty.');
38+
if (str_starts_with($this->baseUrl, 'http://')) {
39+
throw new InvalidArgumentException('The base URL must not contain the protocol.');
40+
}
41+
if (str_starts_with($this->baseUrl, 'https://')) {
42+
throw new InvalidArgumentException('The base URL must not contain the protocol.');
43+
}
44+
if ('' === $deployment) {
45+
throw new InvalidArgumentException('The deployment must not be empty.');
46+
}
47+
if ('' === $apiVersion) {
48+
throw new InvalidArgumentException('The API version must not be empty.');
49+
}
50+
if ('' === $apiKey) {
51+
throw new InvalidArgumentException('The API key must not be empty.');
52+
}
4353
}
4454

4555
public function supports(Model $model): bool

src/platform/src/Bridge/OpenAi/DallE/Base64Image.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
public function __construct(
2222
public string $encodedImage,
2323
) {
24-
'' !== $encodedImage || throw new InvalidArgumentException('The base64 encoded image generated must be given.');
24+
if ('' === $encodedImage) {
25+
throw new InvalidArgumentException('The base64 encoded image generated must be given.');
26+
}
2527
}
2628
}

src/platform/src/Bridge/OpenAi/DallE/ModelClient.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,12 @@ public function __construct(
3030
#[\SensitiveParameter]
3131
private string $apiKey,
3232
) {
33-
'' !== $apiKey || throw new InvalidArgumentException('The API key must not be empty.');
34-
str_starts_with($apiKey, 'sk-') || throw new InvalidArgumentException('The API key must start with "sk-".');
33+
if ('' === $apiKey) {
34+
throw new InvalidArgumentException('The API key must not be empty.');
35+
}
36+
if (!str_starts_with($apiKey, 'sk-')) {
37+
throw new InvalidArgumentException('The API key must start with "sk-".');
38+
}
3539
}
3640

3741
public function supports(Model $model): bool

src/platform/src/Bridge/OpenAi/DallE/UrlImage.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
public function __construct(
2222
public string $url,
2323
) {
24-
'' !== $url || throw new InvalidArgumentException('The image url must be given.');
24+
if ('' === $url) {
25+
throw new InvalidArgumentException('The image url must be given.');
26+
}
2527
}
2628
}

src/platform/src/Bridge/OpenAi/Embeddings/ModelClient.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@ public function __construct(
2828
#[\SensitiveParameter]
2929
private string $apiKey,
3030
) {
31-
'' !== $apiKey || throw new InvalidArgumentException('The API key must not be empty.');
32-
str_starts_with($apiKey, 'sk-') || throw new InvalidArgumentException('The API key must start with "sk-".');
31+
if ('' === $apiKey) {
32+
throw new InvalidArgumentException('The API key must not be empty.');
33+
}
34+
if (!str_starts_with($apiKey, 'sk-')) {
35+
throw new InvalidArgumentException('The API key must start with "sk-".');
36+
}
3337
}
3438

3539
public function supports(Model $model): bool

src/platform/src/Bridge/OpenAi/Gpt/ModelClient.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,12 @@ public function __construct(
3232
private string $apiKey,
3333
) {
3434
$this->httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);
35-
'' !== $apiKey || throw new InvalidArgumentException('The API key must not be empty.');
36-
str_starts_with($apiKey, 'sk-') || throw new InvalidArgumentException('The API key must start with "sk-".');
35+
if ('' === $apiKey) {
36+
throw new InvalidArgumentException('The API key must not be empty.');
37+
}
38+
if (!str_starts_with($apiKey, 'sk-')) {
39+
throw new InvalidArgumentException('The API key must start with "sk-".');
40+
}
3741
}
3842

3943
public function supports(Model $model): bool

src/platform/src/Bridge/OpenAi/Whisper/ModelClient.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ public function __construct(
2828
#[\SensitiveParameter]
2929
private string $apiKey,
3030
) {
31-
'' !== $apiKey || throw new InvalidArgumentException('The API key must not be empty.');
31+
if ('' === $apiKey) {
32+
throw new InvalidArgumentException('The API key must not be empty.');
33+
}
3234
}
3335

3436
public function supports(Model $model): bool

0 commit comments

Comments
 (0)