Skip to content

Commit 0a5ed46

Browse files
committed
refactor: imports built-in PHP classes and interfaces
1 parent f5d4251 commit 0a5ed46

24 files changed

+104
-74
lines changed

src/Common/AbstractEnum.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use BadMethodCallException;
88
use InvalidArgumentException;
99
use ReflectionClass;
10+
use RuntimeException;
1011

1112
/**
1213
* Abstract base class for enum-like behavior in PHP 7.4.
@@ -250,7 +251,7 @@ private static function getInstance(string $value, string $name): self
250251
* @since n.e.x.t
251252
*
252253
* @return array<string, string> Map of constant names to values.
253-
* @throws \RuntimeException If invalid constant found.
254+
* @throws RuntimeException If invalid constant found.
254255
*/
255256
final protected static function getConstants(): array
256257
{
@@ -265,7 +266,7 @@ final protected static function getConstants(): array
265266
foreach ($constants as $name => $value) {
266267
// Check if constant name follows uppercase snake_case pattern
267268
if (!preg_match('/^[A-Z][A-Z0-9_]*$/', $name)) {
268-
throw new \RuntimeException(
269+
throw new RuntimeException(
269270
sprintf(
270271
'Invalid enum constant name "%s" in %s. Constants must be UPPER_SNAKE_CASE.',
271272
$name,
@@ -276,7 +277,7 @@ final protected static function getConstants(): array
276277

277278
// Check if value is valid type
278279
if (!is_string($value)) {
279-
throw new \RuntimeException(
280+
throw new RuntimeException(
280281
sprintf(
281282
'Invalid enum value type for constant %s::%s. ' .
282283
'Only string values are allowed, %s given.',

src/Files/DTO/File.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace WordPress\AiClient\Files\DTO;
66

7+
use InvalidArgumentException;
8+
use RuntimeException;
79
use WordPress\AiClient\Common\AbstractDataValueObject;
810
use WordPress\AiClient\Files\Enums\FileTypeEnum;
911
use WordPress\AiClient\Files\ValueObjects\MimeType;
@@ -54,7 +56,7 @@ final class File extends AbstractDataValueObject
5456
*
5557
* @param string $file The file string (URL, base64 data, or local path).
5658
* @param string|null $mimeType The MIME type of the file (optional).
57-
* @throws \InvalidArgumentException If the file format is invalid or MIME type cannot be determined.
59+
* @throws InvalidArgumentException If the file format is invalid or MIME type cannot be determined.
5860
*/
5961
public function __construct(string $file, ?string $mimeType = null)
6062
{
@@ -69,7 +71,7 @@ public function __construct(string $file, ?string $mimeType = null)
6971
*
7072
* @param string $file The file string to process.
7173
* @param string|null $providedMimeType The explicitly provided MIME type.
72-
* @throws \InvalidArgumentException If the file format is invalid or MIME type cannot be determined.
74+
* @throws InvalidArgumentException If the file format is invalid or MIME type cannot be determined.
7375
*/
7476
private function detectAndProcessFile(string $file, ?string $providedMimeType): void
7577
{
@@ -104,7 +106,7 @@ private function detectAndProcessFile(string $file, ?string $providedMimeType):
104106
// Check if it's plain base64
105107
if (preg_match('/^[A-Za-z0-9+\/]*={0,2}$/', $file)) {
106108
if ($providedMimeType === null) {
107-
throw new \InvalidArgumentException(
109+
throw new InvalidArgumentException(
108110
'MIME type is required when providing plain base64 data without data URI format.'
109111
);
110112
}
@@ -114,7 +116,7 @@ private function detectAndProcessFile(string $file, ?string $providedMimeType):
114116
return;
115117
}
116118

117-
throw new \InvalidArgumentException(
119+
throw new InvalidArgumentException(
118120
'Invalid file provided. Expected URL, base64 data, or valid local file path.'
119121
);
120122
}
@@ -140,14 +142,14 @@ private function isUrl(string $string): bool
140142
*
141143
* @param string $filePath The path to the local file.
142144
* @return string The base64-encoded file data.
143-
* @throws \RuntimeException If the file cannot be read.
145+
* @throws RuntimeException If the file cannot be read.
144146
*/
145147
private function convertFileToBase64(string $filePath): string
146148
{
147149
$fileContent = @file_get_contents($filePath);
148150

149151
if ($fileContent === false) {
150-
throw new \RuntimeException(
152+
throw new RuntimeException(
151153
sprintf('Unable to read file: %s', $filePath)
152154
);
153155
}
@@ -288,7 +290,7 @@ public function isText(): bool
288290
* @param string|null $extractedMimeType The MIME type extracted from data URI.
289291
* @param string|null $pathOrUrl The file path or URL to extract extension from.
290292
* @return MimeType The determined MIME type.
291-
* @throws \InvalidArgumentException If MIME type cannot be determined.
293+
* @throws InvalidArgumentException If MIME type cannot be determined.
292294
*/
293295
private function determineMimeType(
294296
?string $providedMimeType,
@@ -320,14 +322,14 @@ private function determineMimeType(
320322
if (!empty($extension)) {
321323
try {
322324
return MimeType::fromExtension($extension);
323-
} catch (\InvalidArgumentException $e) {
325+
} catch (InvalidArgumentException $e) {
324326
// Extension not recognized, continue to error
325327
unset($e);
326328
}
327329
}
328330
}
329331

330-
throw new \InvalidArgumentException(
332+
throw new InvalidArgumentException(
331333
'Unable to determine MIME type. Please provide it explicitly.'
332334
);
333335
}
@@ -421,12 +423,12 @@ public static function fromArray(array $array): File
421423

422424
if ($fileType->isRemote()) {
423425
if (!isset($array['url']) || !isset($array['mimeType'])) {
424-
throw new \InvalidArgumentException('Remote file requires url and mimeType.');
426+
throw new InvalidArgumentException('Remote file requires url and mimeType.');
425427
}
426428
return new self($array['url'], $array['mimeType']);
427429
} else {
428430
if (!isset($array['mimeType']) || !isset($array['base64Data'])) {
429-
throw new \InvalidArgumentException('Inline file requires mimeType and base64Data.');
431+
throw new InvalidArgumentException('Inline file requires mimeType and base64Data.');
430432
}
431433
return new self($array['base64Data'], $array['mimeType']);
432434
}

src/Files/ValueObjects/MimeType.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace WordPress\AiClient\Files\ValueObjects;
66

7+
use InvalidArgumentException;
8+
79
/**
810
* Value object representing a MIME type.
911
*
@@ -115,12 +117,12 @@ final class MimeType
115117
* @since n.e.x.t
116118
*
117119
* @param string $value The MIME type value.
118-
* @throws \InvalidArgumentException If the MIME type is invalid.
120+
* @throws InvalidArgumentException If the MIME type is invalid.
119121
*/
120122
public function __construct(string $value)
121123
{
122124
if (!self::isValid($value)) {
123-
throw new \InvalidArgumentException(
125+
throw new InvalidArgumentException(
124126
sprintf('Invalid MIME type: %s', $value)
125127
);
126128
}
@@ -135,14 +137,14 @@ public function __construct(string $value)
135137
*
136138
* @param string $extension The file extension (without the dot).
137139
* @return self The MimeType instance.
138-
* @throws \InvalidArgumentException If the extension is not recognized.
140+
* @throws InvalidArgumentException If the extension is not recognized.
139141
*/
140142
public static function fromExtension(string $extension): self
141143
{
142144
$extension = strtolower($extension);
143145

144146
if (!isset(self::$extensionMap[$extension])) {
145-
throw new \InvalidArgumentException(
147+
throw new InvalidArgumentException(
146148
sprintf('Unknown file extension: %s', $extension)
147149
);
148150
}
@@ -245,7 +247,7 @@ public function equals($other): bool
245247
return $this->value === strtolower($other);
246248
}
247249

248-
throw new \InvalidArgumentException(
250+
throw new InvalidArgumentException(
249251
sprintf('Invalid MIME type comparison: %s', gettype($other))
250252
);
251253
}

src/Messages/DTO/MessagePart.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace WordPress\AiClient\Messages\DTO;
66

7+
use InvalidArgumentException;
78
use WordPress\AiClient\Common\AbstractDataValueObject;
89
use WordPress\AiClient\Files\DTO\File;
910
use WordPress\AiClient\Messages\Enums\MessagePartTypeEnum;
@@ -65,7 +66,7 @@ final class MessagePart extends AbstractDataValueObject
6566
* @since n.e.x.t
6667
*
6768
* @param mixed $content The content of this message part.
68-
* @throws \InvalidArgumentException If an unsupported content type is provided.
69+
* @throws InvalidArgumentException If an unsupported content type is provided.
6970
*/
7071
public function __construct($content)
7172
{
@@ -83,7 +84,7 @@ public function __construct($content)
8384
$this->functionResponse = $content;
8485
} else {
8586
$type = is_object($content) ? get_class($content) : gettype($content);
86-
throw new \InvalidArgumentException(
87+
throw new InvalidArgumentException(
8788
sprintf(
8889
'Unsupported content type %s. Expected string, File, '
8990
. 'FunctionCall, or FunctionResponse.',
@@ -252,25 +253,25 @@ public static function fromArray(array $array): MessagePart
252253

253254
if ($type->isText()) {
254255
if (!isset($array['text'])) {
255-
throw new \InvalidArgumentException('Text message part requires text field.');
256+
throw new InvalidArgumentException('Text message part requires text field.');
256257
}
257258
return new self($array['text']);
258259
} elseif ($type->isFile()) {
259260
if (!isset($array['file'])) {
260-
throw new \InvalidArgumentException('File message part requires file field.');
261+
throw new InvalidArgumentException('File message part requires file field.');
261262
}
262263
$fileData = $array['file'];
263264
return new self(File::fromArray($fileData));
264265
} elseif ($type->isFunctionCall()) {
265266
if (!isset($array['functionCall'])) {
266-
throw new \InvalidArgumentException('Function call message part requires functionCall field.');
267+
throw new InvalidArgumentException('Function call message part requires functionCall field.');
267268
}
268269
$functionCallData = $array['functionCall'];
269270
return new self(FunctionCall::fromArray($functionCallData));
270271
} else {
271272
// Function response is the only remaining option
272273
if (!isset($array['functionResponse'])) {
273-
throw new \InvalidArgumentException('Function response message part requires functionResponse field.');
274+
throw new InvalidArgumentException('Function response message part requires functionResponse field.');
274275
}
275276
$functionResponseData = $array['functionResponse'];
276277
return new self(FunctionResponse::fromArray($functionResponseData));

src/Results/DTO/Candidate.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace WordPress\AiClient\Results\DTO;
66

7+
use InvalidArgumentException;
78
use WordPress\AiClient\Common\AbstractDataValueObject;
89
use WordPress\AiClient\Messages\DTO\Message;
910
use WordPress\AiClient\Results\Enums\FinishReasonEnum;
@@ -51,7 +52,7 @@ final class Candidate extends AbstractDataValueObject
5152
public function __construct(Message $message, FinishReasonEnum $finishReason, int $tokenCount)
5253
{
5354
if (!$message->getRole()->isModel()) {
54-
throw new \InvalidArgumentException(
55+
throw new InvalidArgumentException(
5556
'Message must be a model message.'
5657
);
5758
}

src/Results/DTO/GenerativeAiResult.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace WordPress\AiClient\Results\DTO;
66

7+
use InvalidArgumentException;
8+
use RuntimeException;
79
use WordPress\AiClient\Common\AbstractDataValueObject;
810
use WordPress\AiClient\Files\DTO\File;
911
use WordPress\AiClient\Messages\DTO\Message;
@@ -60,12 +62,12 @@ final class GenerativeAiResult extends AbstractDataValueObject implements Result
6062
* @param Candidate[] $candidates The generated candidates.
6163
* @param TokenUsage $tokenUsage Token usage statistics.
6264
* @param array<string, mixed> $providerMetadata Provider-specific metadata.
63-
* @throws \InvalidArgumentException If no candidates provided.
65+
* @throws InvalidArgumentException If no candidates provided.
6466
*/
6567
public function __construct(string $id, array $candidates, TokenUsage $tokenUsage, array $providerMetadata = [])
6668
{
6769
if (empty($candidates)) {
68-
throw new \InvalidArgumentException('At least one candidate must be provided');
70+
throw new InvalidArgumentException('At least one candidate must be provided');
6971
}
7072

7173
$this->id = $id;
@@ -146,7 +148,7 @@ public function hasMultipleCandidates(): bool
146148
* @since n.e.x.t
147149
*
148150
* @return string The text content.
149-
* @throws \RuntimeException If no text content.
151+
* @throws RuntimeException If no text content.
150152
*/
151153
public function toText(): string
152154
{
@@ -158,7 +160,7 @@ public function toText(): string
158160
}
159161
}
160162

161-
throw new \RuntimeException('No text content found in first candidate');
163+
throw new RuntimeException('No text content found in first candidate');
162164
}
163165

164166
/**
@@ -167,7 +169,7 @@ public function toText(): string
167169
* @since n.e.x.t
168170
*
169171
* @return File The file.
170-
* @throws \RuntimeException If no file content.
172+
* @throws RuntimeException If no file content.
171173
*/
172174
public function toFile(): File
173175
{
@@ -179,7 +181,7 @@ public function toFile(): File
179181
}
180182
}
181183

182-
throw new \RuntimeException('No file content found in first candidate');
184+
throw new RuntimeException('No file content found in first candidate');
183185
}
184186

185187
/**
@@ -188,14 +190,14 @@ public function toFile(): File
188190
* @since n.e.x.t
189191
*
190192
* @return File The image file.
191-
* @throws \RuntimeException If no image content.
193+
* @throws RuntimeException If no image content.
192194
*/
193195
public function toImageFile(): File
194196
{
195197
$file = $this->toFile();
196198

197199
if (!$file->isImage()) {
198-
throw new \RuntimeException(
200+
throw new RuntimeException(
199201
sprintf('File is not an image. MIME type: %s', $file->getMimeType())
200202
);
201203
}
@@ -209,14 +211,14 @@ public function toImageFile(): File
209211
* @since n.e.x.t
210212
*
211213
* @return File The audio file.
212-
* @throws \RuntimeException If no audio content.
214+
* @throws RuntimeException If no audio content.
213215
*/
214216
public function toAudioFile(): File
215217
{
216218
$file = $this->toFile();
217219

218220
if (!$file->isAudio()) {
219-
throw new \RuntimeException(
221+
throw new RuntimeException(
220222
sprintf('File is not an audio file. MIME type: %s', $file->getMimeType())
221223
);
222224
}
@@ -230,14 +232,14 @@ public function toAudioFile(): File
230232
* @since n.e.x.t
231233
*
232234
* @return File The video file.
233-
* @throws \RuntimeException If no video content.
235+
* @throws RuntimeException If no video content.
234236
*/
235237
public function toVideoFile(): File
236238
{
237239
$file = $this->toFile();
238240

239241
if (!$file->isVideo()) {
240-
throw new \RuntimeException(
242+
throw new RuntimeException(
241243
sprintf('File is not a video file. MIME type: %s', $file->getMimeType())
242244
);
243245
}

src/Tools/DTO/FunctionCall.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace WordPress\AiClient\Tools\DTO;
66

7+
use InvalidArgumentException;
78
use WordPress\AiClient\Common\AbstractDataValueObject;
89

910
/**
@@ -43,12 +44,12 @@ final class FunctionCall extends AbstractDataValueObject
4344
* @param string|null $id Unique identifier for this function call.
4445
* @param string|null $name The name of the function to call.
4546
* @param array<string, mixed> $args The arguments to pass to the function.
46-
* @throws \InvalidArgumentException If neither id nor name is provided.
47+
* @throws InvalidArgumentException If neither id nor name is provided.
4748
*/
4849
public function __construct(?string $id = null, ?string $name = null, array $args = [])
4950
{
5051
if ($id === null && $name === null) {
51-
throw new \InvalidArgumentException('At least one of id or name must be provided.');
52+
throw new InvalidArgumentException('At least one of id or name must be provided.');
5253
}
5354

5455
$this->id = $id;

0 commit comments

Comments
 (0)