Skip to content

Commit c774eb2

Browse files
committed
refactor: replace Webmozart assertions with native PHP exceptions for input validation
1 parent 1089e85 commit c774eb2

File tree

16 files changed

+63
-87
lines changed

16 files changed

+63
-87
lines changed

src/platform/composer.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,13 @@
3232
"symfony/property-info": "^6.4 || ^7.1",
3333
"symfony/serializer": "^6.4 || ^7.1",
3434
"symfony/type-info": "^7.2.3",
35-
"symfony/uid": "^6.4 || ^7.1",
36-
"webmozart/assert": "^1.11"
35+
"symfony/uid": "^6.4 || ^7.1"
3736
},
3837
"require-dev": {
3938
"async-aws/bedrock-runtime": "^0.1.0",
4039
"codewithkyrian/transformers": "^0.5.3",
4140
"phpstan/phpstan": "^2.1.17",
4241
"phpstan/phpstan-symfony": "^2.0.6",
43-
"phpstan/phpstan-webmozart-assert": "^2.0.0",
4442
"phpunit/phpunit": "^11.5",
4543
"symfony/console": "^6.4 || ^7.1",
4644
"symfony/dotenv": "^6.4 || ^7.1",

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use Symfony\Component\HttpClient\EventSourceHttpClient;
1818
use Symfony\Contracts\HttpClient\HttpClientInterface;
1919
use Symfony\Contracts\HttpClient\ResponseInterface;
20-
use Webmozart\Assert\Assert;
2120

2221
/**
2322
* @author Christopher Hertel <mail@christopher-hertel.de>
@@ -34,11 +33,10 @@ public function __construct(
3433
#[\SensitiveParameter] private string $apiKey,
3534
) {
3635
$this->httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);
37-
Assert::notStartsWith($baseUrl, 'http://', 'The base URL must not contain the protocol.');
38-
Assert::notStartsWith($baseUrl, 'https://', 'The base URL must not contain the protocol.');
39-
Assert::stringNotEmpty($deployment, 'The deployment must not be empty.');
40-
Assert::stringNotEmpty($apiVersion, 'The API version must not be empty.');
41-
Assert::stringNotEmpty($apiKey, 'The API key must not be empty.');
36+
str_starts_with($this->baseUrl, 'https://') || str_starts_with($this->baseUrl, 'http://') || throw new \InvalidArgumentException('The base URL must start with "https://" or "http://".');
37+
'' !== $deployment || throw new \InvalidArgumentException('The deployment must not be empty.');
38+
'' !== $apiVersion || throw new \InvalidArgumentException('The API version must not be empty.');
39+
'' !== $apiKey || throw new \InvalidArgumentException('The API key must not be empty.');
4240
}
4341

4442
public function supports(Model $model): bool

src/platform/src/Bridge/Azure/OpenAI/GPTModelClient.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use Symfony\Component\HttpClient\EventSourceHttpClient;
1818
use Symfony\Contracts\HttpClient\HttpClientInterface;
1919
use Symfony\Contracts\HttpClient\ResponseInterface;
20-
use Webmozart\Assert\Assert;
2120

2221
/**
2322
* @author Christopher Hertel <mail@christopher-hertel.de>
@@ -34,11 +33,10 @@ public function __construct(
3433
#[\SensitiveParameter] private string $apiKey,
3534
) {
3635
$this->httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);
37-
Assert::notStartsWith($baseUrl, 'http://', 'The base URL must not contain the protocol.');
38-
Assert::notStartsWith($baseUrl, 'https://', 'The base URL must not contain the protocol.');
39-
Assert::stringNotEmpty($deployment, 'The deployment must not be empty.');
40-
Assert::stringNotEmpty($apiVersion, 'The API version must not be empty.');
41-
Assert::stringNotEmpty($apiKey, 'The API key must not be empty.');
36+
str_starts_with($this->baseUrl, 'https://') || str_starts_with($this->baseUrl, 'http://') || throw new \InvalidArgumentException('The base URL must start with "https://" or "http://".');
37+
'' !== $deployment || throw new \InvalidArgumentException('The deployment must not be empty.');
38+
'' !== $apiVersion || throw new \InvalidArgumentException('The API version must not be empty.');
39+
'' !== $apiKey || throw new \InvalidArgumentException('The API key must not be empty.');
4240
}
4341

4442
public function supports(Model $model): bool

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use Symfony\Component\HttpClient\EventSourceHttpClient;
1919
use Symfony\Contracts\HttpClient\HttpClientInterface;
2020
use Symfony\Contracts\HttpClient\ResponseInterface;
21-
use Webmozart\Assert\Assert;
2221

2322
/**
2423
* @author Christopher Hertel <mail@christopher-hertel.de>
@@ -35,11 +34,10 @@ public function __construct(
3534
#[\SensitiveParameter] private string $apiKey,
3635
) {
3736
$this->httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);
38-
Assert::notStartsWith($baseUrl, 'http://', 'The base URL must not contain the protocol.');
39-
Assert::notStartsWith($baseUrl, 'https://', 'The base URL must not contain the protocol.');
40-
Assert::stringNotEmpty($deployment, 'The deployment must not be empty.');
41-
Assert::stringNotEmpty($apiVersion, 'The API version must not be empty.');
42-
Assert::stringNotEmpty($apiKey, 'The API key must not be empty.');
37+
str_starts_with($this->baseUrl, 'https://') || str_starts_with($this->baseUrl, 'http://') || throw new \InvalidArgumentException('The base URL must start with "https://" or "http://".');
38+
'' !== $deployment || throw new \InvalidArgumentException('The deployment must not be empty.');
39+
'' !== $apiVersion || throw new \InvalidArgumentException('The API version must not be empty.');
40+
'' !== $apiKey || throw new \InvalidArgumentException('The API key must not be empty.');
4341
}
4442

4543
public function supports(Model $model): bool

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111

1212
namespace Symfony\AI\Platform\Bridge\OpenAI\DallE;
1313

14-
use Webmozart\Assert\Assert;
15-
1614
/**
1715
* @author Denis Zunke <denis.zunke@gmail.com>
1816
*/
@@ -21,6 +19,6 @@
2119
public function __construct(
2220
public string $encodedImage,
2321
) {
24-
Assert::stringNotEmpty($encodedImage, 'The base64 encoded image generated must be given.');
22+
'' !== $encodedImage || throw new \InvalidArgumentException('The base64 encoded image must not be empty.');
2523
}
2624
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use Symfony\AI\Platform\ResponseConverterInterface as PlatformResponseConverter;
2020
use Symfony\Contracts\HttpClient\HttpClientInterface;
2121
use Symfony\Contracts\HttpClient\ResponseInterface as HttpResponse;
22-
use Webmozart\Assert\Assert;
2322

2423
/**
2524
* @see https://platform.openai.com/docs/api-reference/images/create
@@ -33,8 +32,8 @@ public function __construct(
3332
#[\SensitiveParameter]
3433
private string $apiKey,
3534
) {
36-
Assert::stringNotEmpty($apiKey, 'The API key must not be empty.');
37-
Assert::startsWith($apiKey, 'sk-', 'The API key must start with "sk-".');
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-".');
3837
}
3938

4039
public function supports(Model $model): bool

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111

1212
namespace Symfony\AI\Platform\Bridge\OpenAI\DallE;
1313

14-
use Webmozart\Assert\Assert;
15-
1614
/**
1715
* @author Denis Zunke <denis.zunke@gmail.com>
1816
*/
@@ -21,6 +19,6 @@
2119
public function __construct(
2220
public string $url,
2321
) {
24-
Assert::stringNotEmpty($url, 'The image url must be given.');
22+
'' !== $url || throw new \InvalidArgumentException('The image url must not be empty.');
2523
}
2624
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Symfony\AI\Platform\ModelClientInterface as PlatformResponseFactory;
1717
use Symfony\Contracts\HttpClient\HttpClientInterface;
1818
use Symfony\Contracts\HttpClient\ResponseInterface;
19-
use Webmozart\Assert\Assert;
2019

2120
/**
2221
* @author Christopher Hertel <mail@christopher-hertel.de>
@@ -28,8 +27,8 @@ public function __construct(
2827
#[\SensitiveParameter]
2928
private string $apiKey,
3029
) {
31-
Assert::stringNotEmpty($apiKey, 'The API key must not be empty.');
32-
Assert::startsWith($apiKey, 'sk-', 'The API key must start with "sk-".');
30+
'' !== $apiKey || throw new \InvalidArgumentException('The API key must not be empty.');
31+
str_starts_with($apiKey, 'sk-') || throw new \InvalidArgumentException('The API key must start with "sk-".');
3332
}
3433

3534
public function supports(Model $model): bool

src/platform/src/Bridge/OpenAI/GPT/ModelClient.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use Symfony\Component\HttpClient\EventSourceHttpClient;
1818
use Symfony\Contracts\HttpClient\HttpClientInterface;
1919
use Symfony\Contracts\HttpClient\ResponseInterface;
20-
use Webmozart\Assert\Assert;
2120

2221
/**
2322
* @author Christopher Hertel <mail@christopher-hertel.de>
@@ -32,8 +31,8 @@ public function __construct(
3231
private string $apiKey,
3332
) {
3433
$this->httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);
35-
Assert::stringNotEmpty($apiKey, 'The API key must not be empty.');
36-
Assert::startsWith($apiKey, 'sk-', 'The API key must start with "sk-".');
34+
'' !== $apiKey || throw new \InvalidArgumentException('The API key must not be empty.');
35+
str_starts_with($apiKey, 'sk-') || throw new \InvalidArgumentException('The API key must start with "sk-".');
3736
}
3837

3938
public function supports(Model $model): bool

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Symfony\AI\Platform\ModelClientInterface as BaseModelClient;
1717
use Symfony\Contracts\HttpClient\HttpClientInterface;
1818
use Symfony\Contracts\HttpClient\ResponseInterface;
19-
use Webmozart\Assert\Assert;
2019

2120
/**
2221
* @author Christopher Hertel <mail@christopher-hertel.de>
@@ -28,7 +27,7 @@ public function __construct(
2827
#[\SensitiveParameter]
2928
private string $apiKey,
3029
) {
31-
Assert::stringNotEmpty($apiKey, 'The API key must not be empty.');
30+
'' !== $apiKey || throw new \InvalidArgumentException('The API key must not be empty.');
3231
}
3332

3433
public function supports(Model $model): bool

0 commit comments

Comments
 (0)