Skip to content

Commit a050062

Browse files
authored
chore: use iterable type hints (#419)
* chore: add iterable type hints * chore: use inheritDoc
1 parent 5cff081 commit a050062

23 files changed

+169
-167
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- feat!: Refactor `FormsConnectionResolver` and `EntriesConnectionResolver` for compatibility with WPGraphQL v1.26.0 improvements.
77
- feat!: Narrow `FormField.choices` and `FormField.inputs` field types to their implementations.
88
- dev: Use `FormFieldsDataLoader` to resolve fields instead of instantiating a new `Model`.
9+
- chore: Add iterable type hints.
910
- chore!: Bump minimum WPGraphQL version to v1.26.0.
1011
- chore!: Bump minimum WordPress version to v6.0.0.
1112
- chore!: Bump minimum Gravity Forms version to v2.7.0.

phpstan.neon.dist

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,4 @@ parameters:
3535
- ../wp-gatsby/
3636
- ../wp-jamstack-deployments/
3737
ignoreErrors:
38-
- identifier: missingType.iterableValue
3938
- '#^Function gf_apply_filters(_ref_array)? invoked with ([1-9]|1[0-2]) parameters, 2 required\.$#'

src/Data/Connection/EntriesConnectionResolver.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,7 @@ public function get_offset_for_cursor( ?string $cursor = null ) {
179179
}
180180

181181
/**
182-
* Validates Model.
183-
*
184-
* @param mixed $model model.
185-
*
186-
* @return bool
182+
* {@inheritDoc}
187183
*/
188184
protected function is_valid_model( $model ) {
189185
return ! empty( $model->databaseId ) || ! empty( $model->resumeToken );
@@ -327,6 +323,8 @@ private function apply_status_to_search_criteria( array $search_criteria ): arra
327323
* Correctly formats the field filters for search criteria.
328324
*
329325
* @param array<string,mixed>[] $field_filters The field filters.
326+
*
327+
* @return array{key?:string,operator:string,value:mixed}[]
330328
*/
331329
private function format_field_filters( array $field_filters ): array {
332330
return array_reduce(
@@ -392,7 +390,7 @@ private function should_field_filter_be_limited_to_single_value( string $operato
392390
*
393391
* @param array<string,mixed> $field_filter Field filter.
394392
*
395-
* @return string[]
393+
* @return mixed[]
396394
*/
397395
private function get_field_filter_value_fields( array $field_filter ): array {
398396
return array_values(

src/Data/Connection/FormsConnectionResolver.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,7 @@ public function is_valid_offset( $offset ) {
4141
}
4242

4343
/**
44-
* Validates Model.
45-
*
46-
* If model isn't a class with a `fields` member, this function with have be overridden in
47-
* the Connection class.
48-
*
49-
* @param mixed $model model.
50-
*
51-
* @return bool
44+
* {@inheritDoc}
5245
*/
5346
protected function is_valid_model( $model ) {
5447
return ! empty( $model->databaseId );

src/Data/EntryObjectMutation.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,12 @@ public static function get_field_value_input( array $args, array $form, bool $is
9494
*
9595
* Useful for adding mutation support for custom fields.
9696
*
97-
* @param string $field_value_input_class The FieldValueInput class to use. The referenced class must extend AbstractFieldValueInput.
98-
* @param array $args The GraphQL input args for the form field.
99-
* @param \GF_Field $field The current Gravity Forms field object.
100-
* @param array<string,mixed> $form The current Gravity Forms form object.
101-
* @param array|null $entry The current Gravity Forms entry object. Only available when using update (`gfUpdateEntry`, `gfUpdateDraftEntry`) mutations.
102-
* @param bool $is_draft_mutation Whether the mutation is handling a Draft Entry (`gfUpdateDraftEntry`, or `gfSubmitForm` when `saveAsDraft` is `true`).
97+
* @param string $field_value_input_class The FieldValueInput class to use. The referenced class must extend AbstractFieldValueInput.
98+
* @param array $args The GraphQL input args for the form field.
99+
* @param \GF_Field $field The current Gravity Forms field object.
100+
* @param array<string,mixed> $form The current Gravity Forms form object.
101+
* @param ?array<int|string,mixed> $entry The current Gravity Forms entry object. Only available when using update (`gfUpdateEntry`, `gfUpdateDraftEntry`) mutations.
102+
* @param bool $is_draft_mutation Whether the mutation is handling a Draft Entry (`gfUpdateDraftEntry`, or `gfSubmitForm` when `saveAsDraft` is `true`).
103103
*/
104104
$field_value_input = apply_filters( 'graphql_gf_field_value_input_class', $field_value_input, $args, $field, $form, $entry, $is_draft );
105105

src/Data/FieldValueInput/AbstractFieldValueInput.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ abstract class AbstractFieldValueInput {
2121
/**
2222
* The GraphQL field input args provided to the mutation.
2323
*
24-
* @var array|string
24+
* @var mixed[]|string
2525
*/
2626
protected $args;
2727

@@ -70,7 +70,7 @@ abstract class AbstractFieldValueInput {
7070
/**
7171
* The field value for submission.
7272
*
73-
* @var array|string
73+
* @var mixed[]|string
7474
*/
7575
public $value;
7676

@@ -124,12 +124,12 @@ public function __construct( array $input_args, array $form, bool $is_draft, ?GF
124124
/**
125125
* Filters the GraphQL input args for the field value input.
126126
*
127-
* @param array|string $args Field value input args.
128-
* @param \GF_Field $field The current Gravity Forms field object.
129-
* @param array<string,mixed> $form The current Gravity Forms form object.
130-
* @param array|null $entry The current Gravity Forms entry object. Only available when using update (`gfUpdateEntry`, `gfUpdateDraftEntry`) mutations.
131-
* @param bool $is_draft_mutation Whether the mutation is handling a Draft Entry (`gfUpdateDraftEntry`, or `gfSubmitForm` when `saveAsDraft` is `true`).
132-
* @param string $field_name The GraphQL input field name. E.g. `nameValues`.
127+
* @param array<string,mixed>|string $args Field value input args.
128+
* @param \GF_Field $field The current Gravity Forms field object.
129+
* @param array<string,mixed> $form The current Gravity Forms form object.
130+
* @param ?array<int|string,mixed> $entry The current Gravity Forms entry object. Only available when using update (`gfUpdateEntry`, `gfUpdateDraftEntry`) mutations.
131+
* @param bool $is_draft_mutation Whether the mutation is handling a Draft Entry (`gfUpdateDraftEntry`, or `gfSubmitForm` when `saveAsDraft` is `true`).
132+
* @param string $field_name The GraphQL input field name. E.g. `nameValues`.
133133
*/
134134
$this->args = apply_filters(
135135
'graphql_gf_field_value_input_args',
@@ -144,13 +144,13 @@ public function __construct( array $input_args, array $form, bool $is_draft, ?GF
144144
/**
145145
* Filters the prepared field value to be submitted to Gravity Forms.
146146
*
147-
* @param array|string $prepared_field_value The field value formatted in a way Gravity Forms can understand.
148-
* @param array|string $args Field value input args.
149-
* @param \GF_Field $field The current Gravity Forms field object.
150-
* @param array<string,mixed> $form The current Gravity Forms form object.
151-
* @param array|null $entry The current Gravity Forms entry object. Only available when using update (`gfUpdateEntry`, `gfUpdateDraftEntry`) mutations.
152-
* @param bool $is_draft_mutation Whether the mutation is handling a Draft Entry (`gfUpdateDraftEntry`, or `gfSubmitForm` when `saveAsDraft` is `true`).
153-
* @param string $field_name The GraphQL input field name. E.g. `nameValues`.
147+
* @param array|string $prepared_field_value The field value formatted in a way Gravity Forms can understand.
148+
* @param array<string,mixed>|string $args Field value input args.
149+
* @param \GF_Field $field The current Gravity Forms field object.
150+
* @param array<string,mixed> $form The current Gravity Forms form object.
151+
* @param ?array<int|string,mixed> $entry The current Gravity Forms entry object. Only available when using update (`gfUpdateEntry`, `gfUpdateDraftEntry`) mutations.
152+
* @param bool $is_draft_mutation Whether the mutation is handling a Draft Entry (`gfUpdateDraftEntry`, or `gfSubmitForm` when `saveAsDraft` is `true`).
153+
* @param string $field_name The GraphQL input field name. E.g. `nameValues`.
154154
*/
155155
$this->value = apply_filters(
156156
'graphql_gf_field_value_input_prepared_value',
@@ -189,7 +189,7 @@ protected function is_valid_input_type(): bool {
189189
/**
190190
* Gets the input args for the specified field value input.
191191
*
192-
* @return string|array
192+
* @return string|mixed[]
193193
*/
194194
public function get_args() {
195195
$key = $this->field_name;
@@ -200,7 +200,7 @@ public function get_args() {
200200
/**
201201
* Converts the field value args to a format GravityForms can understand.
202202
*
203-
* @return string|array the sanitized value.
203+
* @return string|mixed[] the sanitized value.
204204
*/
205205
protected function prepare_value() {
206206
// You probably want to replace this.
@@ -210,7 +210,7 @@ protected function prepare_value() {
210210
/**
211211
* Manually runs GF_Field::validate, and grabs any validation errors.
212212
*
213-
* @param array $errors .
213+
* @param array{id:int,message:string}[] $errors the array of validation errors.
214214
*/
215215
public function validate_value( array &$errors ): void {
216216
$this->field->validate( $this->value, $this->form );
@@ -226,7 +226,7 @@ public function validate_value( array &$errors ): void {
226226
/**
227227
* Adds the prepared value to the field values array for processing by Gravity Forms.
228228
*
229-
* @param array $field_values the existing field values array.
229+
* @param array<int|string,mixed> $field_values the existing field values array.
230230
*/
231231
public function add_value_to_submission( array &$field_values ): void {
232232
$field_values[ $this->field->id ] = $this->value;

src/Data/FieldValueInput/AddressValuesInput.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ class AddressValuesInput extends AbstractFieldValueInput {
1717
/**
1818
* {@inheritDoc}
1919
*
20-
* @var array
20+
* @var array{street?:string,lineTwo?:string,city?:string,state?:string,zip?:string,country?:string}
2121
*/
2222
protected $args;
2323

2424
/**
2525
* {@inheritDoc}
2626
*
27-
* @var array
27+
* @var array<string,?string>
2828
*/
2929
public $value;
3030

@@ -38,7 +38,7 @@ protected function get_field_name(): string {
3838
/**
3939
* {@inheritDoc}
4040
*
41-
* @return array
41+
* @return array<int|string,?string>
4242
*/
4343
protected function prepare_value() {
4444
$value = $this->args;

src/Data/FieldValueInput/CheckboxValuesInput.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ class CheckboxValuesInput extends AbstractFieldValueInput {
2020
/**
2121
* {@inheritDoc}
2222
*
23-
* @var array
23+
* @var array{inputId:float,value:string}[]
2424
*/
2525
protected $args;
2626

2727
/**
2828
* {@inheritDoc}
2929
*
30-
* @var array
30+
* @var array<string,string>
3131
*/
3232
public $value;
3333

@@ -41,7 +41,7 @@ protected function get_field_name(): string {
4141
/**
4242
* {@inheritDoc}
4343
*
44-
* @return array
44+
* @return array<string,string>
4545
*
4646
* @throws \GraphQL\Error\UserError .
4747
*/
@@ -65,7 +65,7 @@ static function ( array $values_to_save, array $input ): array {
6565
// translators: field ID, input ID.
6666
esc_html__( 'Field %1$s has no input with an id of %2$s.', 'wp-graphql-gravity-forms' ),
6767
esc_html( $this->field->id ),
68-
esc_html( $single_value['inputId'] ),
68+
esc_html( (string) $single_value['inputId'] ),
6969
)
7070
);
7171
}

src/Data/FieldValueInput/ConsentValueInput.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class ConsentValueInput extends AbstractFieldValueInput {
2626
/**
2727
* {@inheritDoc}
2828
*
29-
* @var array
29+
* @var array<string,mixed>
3030
*/
3131
public $value;
3232

@@ -40,7 +40,7 @@ protected function get_field_name(): string {
4040
/**
4141
* {@inheritDoc}
4242
*
43-
* @return array
43+
* @return array<int|string,mixed>
4444
*/
4545
protected function prepare_value() {
4646
$field = $this->field;

src/Data/FieldValueInput/EmailValuesInput.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ class EmailValuesInput extends AbstractFieldValueInput {
1717
/**
1818
* {@inheritDoc}
1919
*
20-
* @var array
20+
* @var array{value?:string,confirmationValue?:string}
2121
*/
2222
protected $args;
2323

2424
/**
2525
* {@inheritDoc}
2626
*
27-
* @var array
27+
* @var array<int,?string>
2828
*/
2929
public $value;
3030

@@ -38,15 +38,15 @@ protected function get_field_name(): string {
3838
/**
3939
* {@inheritDoc}
4040
*
41-
* @return mixed[]
41+
* @return array<int,?string>
4242
*/
4343
protected function prepare_value() {
4444
$value = $this->args;
4545

4646
$values_to_save = [];
4747
$values_to_save[] = $value['value'] ?? null;
4848

49-
if ( $this->field->emailConfirmEnabled ) {
49+
if ( ! empty( $this->field->emailConfirmEnabled ) ) {
5050
$values_to_save[] = $value['confirmationValue'] ?? null;
5151
}
5252

@@ -61,7 +61,7 @@ public function add_value_to_submission( array &$field_values ): void {
6161
$field_values[ $this->field->id ] = $this->value[0];
6262

6363
// Confirmation values are stored in a subfield.
64-
if ( isset( $this->value[1] ) ) {
64+
if ( isset( $this->value[1] ) && isset( $this->field->inputs[1]['id'] ) ) {
6565
$field_values[ $this->field->inputs[1]['id'] ] = $this->value[0];
6666
}
6767
}

0 commit comments

Comments
 (0)