Skip to content

Commit a4260b6

Browse files
committed
feat: validates lists and improves type checking
1 parent e082831 commit a4260b6

File tree

5 files changed

+85
-33
lines changed

5 files changed

+85
-33
lines changed

src/Providers/DTO/ProviderModelsMetadata.php

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

55
namespace WordPress\AiClient\Providers\DTO;
66

7+
use InvalidArgumentException;
78
use WordPress\AiClient\Common\AbstractDataValueObject;
89
use WordPress\AiClient\Providers\Models\DTO\ModelMetadata;
910

@@ -36,7 +37,7 @@ class ProviderModelsMetadata extends AbstractDataValueObject
3637
protected ProviderMetadata $provider;
3738

3839
/**
39-
* @var ModelMetadata[] The available models.
40+
* @var list<ModelMetadata> The available models.
4041
*/
4142
protected array $models;
4243

@@ -46,10 +47,16 @@ class ProviderModelsMetadata extends AbstractDataValueObject
4647
* @since n.e.x.t
4748
*
4849
* @param ProviderMetadata $provider The provider metadata.
49-
* @param ModelMetadata[] $models The available models.
50+
* @param list<ModelMetadata> $models The available models.
51+
*
52+
* @throws InvalidArgumentException If models is not a list.
5053
*/
5154
public function __construct(ProviderMetadata $provider, array $models)
5255
{
56+
if (!array_is_list($models)) {
57+
throw new InvalidArgumentException('Models must be a list array.');
58+
}
59+
5360
$this->provider = $provider;
5461
$this->models = $models;
5562
}
@@ -71,7 +78,7 @@ public function getProvider(): ProviderMetadata
7178
*
7279
* @since n.e.x.t
7380
*
74-
* @return ModelMetadata[] The available models.
81+
* @return list<ModelMetadata> The available models.
7582
*/
7683
public function getModels(): array
7784
{
@@ -108,7 +115,6 @@ public static function getJsonSchema(): array
108115
*/
109116
public function toArray(): array
110117
{
111-
/** @phpstan-ignore-next-line return.type (array_map doesn't guarantee list, validation will be added later) */
112118
return [
113119
self::KEY_PROVIDER => $this->provider->toArray(),
114120
self::KEY_MODELS => array_map(

src/Providers/Models/DTO/ModelConfig.php

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

55
namespace WordPress\AiClient\Providers\Models\DTO;
66

7+
use InvalidArgumentException;
78
use WordPress\AiClient\Common\AbstractDataValueObject;
89
use WordPress\AiClient\Messages\Enums\ModalityEnum;
910
use WordPress\AiClient\Tools\DTO\Tool;
@@ -60,7 +61,7 @@ class ModelConfig extends AbstractDataValueObject
6061
public const KEY_CUSTOM_OPTIONS = 'customOptions';
6162

6263
/**
63-
* @var ModalityEnum[]|null Output modalities for the model.
64+
* @var list<ModalityEnum>|null Output modalities for the model.
6465
*/
6566
protected ?array $outputModalities = null;
6667

@@ -95,7 +96,7 @@ class ModelConfig extends AbstractDataValueObject
9596
protected ?int $topK = null;
9697

9798
/**
98-
* @var string[]|null Stop sequences.
99+
* @var list<string>|null Stop sequences.
99100
*/
100101
protected ?array $stopSequences = null;
101102

@@ -120,7 +121,7 @@ class ModelConfig extends AbstractDataValueObject
120121
protected ?int $topLogprobs = null;
121122

122123
/**
123-
* @var Tool[]|null Tools available to the model.
124+
* @var list<Tool>|null Tools available to the model.
124125
*/
125126
protected ?array $tools = null;
126127

@@ -144,10 +145,16 @@ class ModelConfig extends AbstractDataValueObject
144145
*
145146
* @since n.e.x.t
146147
*
147-
* @param ModalityEnum[] $outputModalities The output modalities.
148+
* @param list<ModalityEnum> $outputModalities The output modalities.
149+
*
150+
* @throws InvalidArgumentException If the array is not a list.
148151
*/
149152
public function setOutputModalities(array $outputModalities): void
150153
{
154+
if (!array_is_list($outputModalities)) {
155+
throw new InvalidArgumentException('Output modalities must be a list array.');
156+
}
157+
151158
$this->outputModalities = $outputModalities;
152159
}
153160

@@ -156,7 +163,7 @@ public function setOutputModalities(array $outputModalities): void
156163
*
157164
* @since n.e.x.t
158165
*
159-
* @return ModalityEnum[]|null The output modalities.
166+
* @return list<ModalityEnum>|null The output modalities.
160167
*/
161168
public function getOutputModalities(): ?array
162169
{
@@ -312,10 +319,16 @@ public function getTopK(): ?int
312319
*
313320
* @since n.e.x.t
314321
*
315-
* @param string[] $stopSequences The stop sequences.
322+
* @param list<string> $stopSequences The stop sequences.
323+
*
324+
* @throws InvalidArgumentException If the array is not a list.
316325
*/
317326
public function setStopSequences(array $stopSequences): void
318327
{
328+
if (!array_is_list($stopSequences)) {
329+
throw new InvalidArgumentException('Stop sequences must be a list array.');
330+
}
331+
319332
$this->stopSequences = $stopSequences;
320333
}
321334

@@ -324,7 +337,7 @@ public function setStopSequences(array $stopSequences): void
324337
*
325338
* @since n.e.x.t
326339
*
327-
* @return string[]|null The stop sequences.
340+
* @return list<string>|null The stop sequences.
328341
*/
329342
public function getStopSequences(): ?array
330343
{
@@ -432,10 +445,16 @@ public function getTopLogprobs(): ?int
432445
*
433446
* @since n.e.x.t
434447
*
435-
* @param Tool[] $tools The tools.
448+
* @param list<Tool> $tools The tools.
449+
*
450+
* @throws InvalidArgumentException If the array is not a list.
436451
*/
437452
public function setTools(array $tools): void
438453
{
454+
if (!array_is_list($tools)) {
455+
throw new InvalidArgumentException('Tools must be a list array.');
456+
}
457+
439458
$this->tools = $tools;
440459
}
441460

@@ -444,7 +463,7 @@ public function setTools(array $tools): void
444463
*
445464
* @since n.e.x.t
446465
*
447-
* @return Tool[]|null The tools.
466+
* @return list<Tool>|null The tools.
448467
*/
449468
public function getTools(): ?array
450469
{
@@ -721,7 +740,6 @@ static function (ModalityEnum $modality): string {
721740

722741
$data[self::KEY_CUSTOM_OPTIONS] = $this->customOptions;
723742

724-
/** @phpstan-ignore-next-line return.type (array_map doesn't guarantee list, validation will be added later) */
725743
return $data;
726744
}
727745

src/Providers/Models/DTO/ModelMetadata.php

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

55
namespace WordPress\AiClient\Providers\Models\DTO;
66

7+
use InvalidArgumentException;
78
use WordPress\AiClient\Common\AbstractDataValueObject;
89
use WordPress\AiClient\Providers\Models\Enums\CapabilityEnum;
910

@@ -44,12 +45,12 @@ class ModelMetadata extends AbstractDataValueObject
4445
protected string $name;
4546

4647
/**
47-
* @var CapabilityEnum[] The model's supported capabilities.
48+
* @var list<CapabilityEnum> The model's supported capabilities.
4849
*/
4950
protected array $supportedCapabilities;
5051

5152
/**
52-
* @var SupportedOption[] The model's supported configuration options.
53+
* @var list<SupportedOption> The model's supported configuration options.
5354
*/
5455
protected array $supportedOptions;
5556

@@ -60,11 +61,21 @@ class ModelMetadata extends AbstractDataValueObject
6061
*
6162
* @param string $id The model's unique identifier.
6263
* @param string $name The model's display name.
63-
* @param CapabilityEnum[] $supportedCapabilities The model's supported capabilities.
64-
* @param SupportedOption[] $supportedOptions The model's supported configuration options.
64+
* @param list<CapabilityEnum> $supportedCapabilities The model's supported capabilities.
65+
* @param list<SupportedOption> $supportedOptions The model's supported configuration options.
66+
*
67+
* @throws InvalidArgumentException If arrays are not lists.
6568
*/
6669
public function __construct(string $id, string $name, array $supportedCapabilities, array $supportedOptions)
6770
{
71+
if (!array_is_list($supportedCapabilities)) {
72+
throw new InvalidArgumentException('Supported capabilities must be a list array.');
73+
}
74+
75+
if (!array_is_list($supportedOptions)) {
76+
throw new InvalidArgumentException('Supported options must be a list array.');
77+
}
78+
6879
$this->id = $id;
6980
$this->name = $name;
7081
$this->supportedCapabilities = $supportedCapabilities;
@@ -100,7 +111,7 @@ public function getName(): string
100111
*
101112
* @since n.e.x.t
102113
*
103-
* @return CapabilityEnum[] The supported capabilities.
114+
* @return list<CapabilityEnum> The supported capabilities.
104115
*/
105116
public function getSupportedCapabilities(): array
106117
{
@@ -112,7 +123,7 @@ public function getSupportedCapabilities(): array
112123
*
113124
* @since n.e.x.t
114125
*
115-
* @return SupportedOption[] The supported options.
126+
* @return list<SupportedOption> The supported options.
116127
*/
117128
public function getSupportedOptions(): array
118129
{
@@ -164,7 +175,6 @@ public static function getJsonSchema(): array
164175
*/
165176
public function toArray(): array
166177
{
167-
/** @phpstan-ignore-next-line return.type (array_map doesn't guarantee list, validation will be added later) */
168178
return [
169179
self::KEY_ID => $this->id,
170180
self::KEY_NAME => $this->name,

src/Providers/Models/DTO/ModelRequirements.php

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

55
namespace WordPress\AiClient\Providers\Models\DTO;
66

7+
use InvalidArgumentException;
78
use WordPress\AiClient\Common\AbstractDataValueObject;
89
use WordPress\AiClient\Providers\Models\Enums\CapabilityEnum;
910

@@ -30,12 +31,12 @@ class ModelRequirements extends AbstractDataValueObject
3031
public const KEY_REQUIRED_OPTIONS = 'requiredOptions';
3132

3233
/**
33-
* @var CapabilityEnum[] The capabilities that the model must support.
34+
* @var list<CapabilityEnum> The capabilities that the model must support.
3435
*/
3536
protected array $requiredCapabilities;
3637

3738
/**
38-
* @var RequiredOption[] The options that the model must support with specific values.
39+
* @var list<RequiredOption> The options that the model must support with specific values.
3940
*/
4041
protected array $requiredOptions;
4142

@@ -44,11 +45,21 @@ class ModelRequirements extends AbstractDataValueObject
4445
*
4546
* @since n.e.x.t
4647
*
47-
* @param CapabilityEnum[] $requiredCapabilities The capabilities that the model must support.
48-
* @param RequiredOption[] $requiredOptions The options that the model must support with specific values.
48+
* @param list<CapabilityEnum> $requiredCapabilities The capabilities that the model must support.
49+
* @param list<RequiredOption> $requiredOptions The options that the model must support with specific values.
50+
*
51+
* @throws InvalidArgumentException If arrays are not lists.
4952
*/
5053
public function __construct(array $requiredCapabilities, array $requiredOptions)
5154
{
55+
if (!array_is_list($requiredCapabilities)) {
56+
throw new InvalidArgumentException('Required capabilities must be a list array.');
57+
}
58+
59+
if (!array_is_list($requiredOptions)) {
60+
throw new InvalidArgumentException('Required options must be a list array.');
61+
}
62+
5263
$this->requiredCapabilities = $requiredCapabilities;
5364
$this->requiredOptions = $requiredOptions;
5465
}
@@ -58,7 +69,7 @@ public function __construct(array $requiredCapabilities, array $requiredOptions)
5869
*
5970
* @since n.e.x.t
6071
*
61-
* @return CapabilityEnum[] The required capabilities.
72+
* @return list<CapabilityEnum> The required capabilities.
6273
*/
6374
public function getRequiredCapabilities(): array
6475
{
@@ -70,7 +81,7 @@ public function getRequiredCapabilities(): array
7081
*
7182
* @since n.e.x.t
7283
*
73-
* @return RequiredOption[] The required options.
84+
* @return list<RequiredOption> The required options.
7485
*/
7586
public function getRequiredOptions(): array
7687
{
@@ -114,7 +125,6 @@ public static function getJsonSchema(): array
114125
*/
115126
public function toArray(): array
116127
{
117-
/** @phpstan-ignore-next-line return.type (array_map doesn't guarantee list, validation will be added later) */
118128
return [
119129
self::KEY_REQUIRED_CAPABILITIES => array_map(
120130
static fn(CapabilityEnum $capability): string => $capability->value,

src/Providers/Models/DTO/SupportedOption.php

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

55
namespace WordPress\AiClient\Providers\Models\DTO;
66

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

910
/**
@@ -32,7 +33,7 @@ class SupportedOption extends AbstractDataValueObject
3233
protected string $name;
3334

3435
/**
35-
* @var mixed[]|null The supported values for this option.
36+
* @var list<mixed>|null The supported values for this option.
3637
*/
3738
protected ?array $supportedValues;
3839

@@ -42,10 +43,16 @@ class SupportedOption extends AbstractDataValueObject
4243
* @since n.e.x.t
4344
*
4445
* @param string $name The option name.
45-
* @param mixed[]|null $supportedValues The supported values for this option, or null if any value is supported.
46+
* @param list<mixed>|null $supportedValues The supported values for this option, or null if any value is supported.
47+
*
48+
* @throws InvalidArgumentException If supportedValues is not null and not a list.
4649
*/
4750
public function __construct(string $name, ?array $supportedValues = null)
4851
{
52+
if ($supportedValues !== null && !array_is_list($supportedValues)) {
53+
throw new InvalidArgumentException('Supported values must be a list array.');
54+
}
55+
4956
$this->name = $name;
5057
$this->supportedValues = $supportedValues;
5158
}
@@ -85,7 +92,7 @@ public function isSupportedValue($value): bool
8592
*
8693
* @since n.e.x.t
8794
*
88-
* @return mixed[]|null The supported values, or null if any value is supported.
95+
* @return list<mixed>|null The supported values, or null if any value is supported.
8996
*/
9097
public function getSupportedValues(): ?array
9198
{
@@ -139,10 +146,11 @@ public function toArray(): array
139146
];
140147

141148
if ($this->supportedValues !== null) {
142-
$data[self::KEY_SUPPORTED_VALUES] = $this->supportedValues;
149+
/** @var list<mixed> $supportedValues */
150+
$supportedValues = $this->supportedValues;
151+
$data[self::KEY_SUPPORTED_VALUES] = $supportedValues;
143152
}
144153

145-
/** @phpstan-ignore-next-line return.type (array doesn't guarantee list, validation will be added later) */
146154
return $data;
147155
}
148156

0 commit comments

Comments
 (0)