Skip to content

Commit e964a1d

Browse files
Merge pull request #33 from WordPress/array-list-polyfill
2 parents d2e23b5 + 52b49e4 commit e964a1d

File tree

8 files changed

+137
-34
lines changed

8 files changed

+137
-34
lines changed

CONTRIBUTING.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,24 @@ The following naming conventions must be followed for consistency and autoloadin
2323
All code must be properly documented with PHPDoc blocks following these standards:
2424

2525
### General rules
26+
2627
- All descriptions must end with a period.
2728
- Use `@since n.e.x.t` for new code (will be replaced with actual version on release).
2829
- Place `@since` tags below the description and above `@param` tags, with blank comment lines around it.
2930

3031
### Method documentation
32+
3133
- Method descriptions must start with a third-person verb (e.g., "Creates", "Returns", "Checks").
3234
- Exceptions: Constructors and magic methods may use different phrasing.
3335
- All `@return` annotations must include a description.
3436

3537
### Interface implementations
38+
3639
- Use `{@inheritDoc}` instead of duplicating descriptions when implementing interface methods.
3740
- Only provide a unique description if it adds value beyond the interface documentation.
3841

3942
### Example
43+
4044
```php
4145
/**
4246
* Class for handling user authentication requests.
@@ -61,6 +65,12 @@ class AuthHandler
6165
}
6266
```
6367

68+
### Array Lists
69+
70+
When an array is a list — that is, an array where the keys are sequential, starting at 0 — use the `list` generic type within the docblock. For example, a parameter that is a list of strings would be documented as `@param list<string> $variable`.
71+
72+
Note that `list<string>` and `string[]` _are not_ the same. The latter is an alias for `array<int, string>` which does not enforce that the keys are sequential. That particular syntax, therefore, will rarely be used.
73+
6474
## PHP Compatibility
6575

6676
All code must be backward compatible with PHP 7.4, which is the minimum required PHP version for this project.

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@
3434
"autoload": {
3535
"psr-4": {
3636
"WordPress\\AiClient\\": "src/"
37-
}
37+
},
38+
"files": [
39+
"src/polyfills.php"
40+
]
3841
},
3942
"autoload-dev": {
4043
"psr-4": {

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,

0 commit comments

Comments
 (0)