Skip to content

Commit

Permalink
chore: use iterable type hints (#419)
Browse files Browse the repository at this point in the history
* chore: add iterable type hints

* chore: use inheritDoc
  • Loading branch information
justlevine authored Jun 2, 2024
1 parent 5cff081 commit a050062
Show file tree
Hide file tree
Showing 23 changed files with 169 additions and 167 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- feat!: Refactor `FormsConnectionResolver` and `EntriesConnectionResolver` for compatibility with WPGraphQL v1.26.0 improvements.
- feat!: Narrow `FormField.choices` and `FormField.inputs` field types to their implementations.
- dev: Use `FormFieldsDataLoader` to resolve fields instead of instantiating a new `Model`.
- chore: Add iterable type hints.
- chore!: Bump minimum WPGraphQL version to v1.26.0.
- chore!: Bump minimum WordPress version to v6.0.0.
- chore!: Bump minimum Gravity Forms version to v2.7.0.
Expand Down
1 change: 0 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,4 @@ parameters:
- ../wp-gatsby/
- ../wp-jamstack-deployments/
ignoreErrors:
- identifier: missingType.iterableValue
- '#^Function gf_apply_filters(_ref_array)? invoked with ([1-9]|1[0-2]) parameters, 2 required\.$#'
10 changes: 4 additions & 6 deletions src/Data/Connection/EntriesConnectionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,7 @@ public function get_offset_for_cursor( ?string $cursor = null ) {
}

/**
* Validates Model.
*
* @param mixed $model model.
*
* @return bool
* {@inheritDoc}
*/
protected function is_valid_model( $model ) {
return ! empty( $model->databaseId ) || ! empty( $model->resumeToken );
Expand Down Expand Up @@ -327,6 +323,8 @@ private function apply_status_to_search_criteria( array $search_criteria ): arra
* Correctly formats the field filters for search criteria.
*
* @param array<string,mixed>[] $field_filters The field filters.
*
* @return array{key?:string,operator:string,value:mixed}[]
*/
private function format_field_filters( array $field_filters ): array {
return array_reduce(
Expand Down Expand Up @@ -392,7 +390,7 @@ private function should_field_filter_be_limited_to_single_value( string $operato
*
* @param array<string,mixed> $field_filter Field filter.
*
* @return string[]
* @return mixed[]
*/
private function get_field_filter_value_fields( array $field_filter ): array {
return array_values(
Expand Down
9 changes: 1 addition & 8 deletions src/Data/Connection/FormsConnectionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,7 @@ public function is_valid_offset( $offset ) {
}

/**
* Validates Model.
*
* If model isn't a class with a `fields` member, this function with have be overridden in
* the Connection class.
*
* @param mixed $model model.
*
* @return bool
* {@inheritDoc}
*/
protected function is_valid_model( $model ) {
return ! empty( $model->databaseId );
Expand Down
12 changes: 6 additions & 6 deletions src/Data/EntryObjectMutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ public static function get_field_value_input( array $args, array $form, bool $is
*
* Useful for adding mutation support for custom fields.
*
* @param string $field_value_input_class The FieldValueInput class to use. The referenced class must extend AbstractFieldValueInput.
* @param array $args The GraphQL input args for the form field.
* @param \GF_Field $field The current Gravity Forms field object.
* @param array<string,mixed> $form The current Gravity Forms form object.
* @param array|null $entry The current Gravity Forms entry object. Only available when using update (`gfUpdateEntry`, `gfUpdateDraftEntry`) mutations.
* @param bool $is_draft_mutation Whether the mutation is handling a Draft Entry (`gfUpdateDraftEntry`, or `gfSubmitForm` when `saveAsDraft` is `true`).
* @param string $field_value_input_class The FieldValueInput class to use. The referenced class must extend AbstractFieldValueInput.
* @param array $args The GraphQL input args for the form field.
* @param \GF_Field $field The current Gravity Forms field object.
* @param array<string,mixed> $form The current Gravity Forms form object.
* @param ?array<int|string,mixed> $entry The current Gravity Forms entry object. Only available when using update (`gfUpdateEntry`, `gfUpdateDraftEntry`) mutations.
* @param bool $is_draft_mutation Whether the mutation is handling a Draft Entry (`gfUpdateDraftEntry`, or `gfSubmitForm` when `saveAsDraft` is `true`).
*/
$field_value_input = apply_filters( 'graphql_gf_field_value_input_class', $field_value_input, $args, $field, $form, $entry, $is_draft );

Expand Down
38 changes: 19 additions & 19 deletions src/Data/FieldValueInput/AbstractFieldValueInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ abstract class AbstractFieldValueInput {
/**
* The GraphQL field input args provided to the mutation.
*
* @var array|string
* @var mixed[]|string
*/
protected $args;

Expand Down Expand Up @@ -70,7 +70,7 @@ abstract class AbstractFieldValueInput {
/**
* The field value for submission.
*
* @var array|string
* @var mixed[]|string
*/
public $value;

Expand Down Expand Up @@ -124,12 +124,12 @@ public function __construct( array $input_args, array $form, bool $is_draft, ?GF
/**
* Filters the GraphQL input args for the field value input.
*
* @param array|string $args Field value input args.
* @param \GF_Field $field The current Gravity Forms field object.
* @param array<string,mixed> $form The current Gravity Forms form object.
* @param array|null $entry The current Gravity Forms entry object. Only available when using update (`gfUpdateEntry`, `gfUpdateDraftEntry`) mutations.
* @param bool $is_draft_mutation Whether the mutation is handling a Draft Entry (`gfUpdateDraftEntry`, or `gfSubmitForm` when `saveAsDraft` is `true`).
* @param string $field_name The GraphQL input field name. E.g. `nameValues`.
* @param array<string,mixed>|string $args Field value input args.
* @param \GF_Field $field The current Gravity Forms field object.
* @param array<string,mixed> $form The current Gravity Forms form object.
* @param ?array<int|string,mixed> $entry The current Gravity Forms entry object. Only available when using update (`gfUpdateEntry`, `gfUpdateDraftEntry`) mutations.
* @param bool $is_draft_mutation Whether the mutation is handling a Draft Entry (`gfUpdateDraftEntry`, or `gfSubmitForm` when `saveAsDraft` is `true`).
* @param string $field_name The GraphQL input field name. E.g. `nameValues`.
*/
$this->args = apply_filters(
'graphql_gf_field_value_input_args',
Expand All @@ -144,13 +144,13 @@ public function __construct( array $input_args, array $form, bool $is_draft, ?GF
/**
* Filters the prepared field value to be submitted to Gravity Forms.
*
* @param array|string $prepared_field_value The field value formatted in a way Gravity Forms can understand.
* @param array|string $args Field value input args.
* @param \GF_Field $field The current Gravity Forms field object.
* @param array<string,mixed> $form The current Gravity Forms form object.
* @param array|null $entry The current Gravity Forms entry object. Only available when using update (`gfUpdateEntry`, `gfUpdateDraftEntry`) mutations.
* @param bool $is_draft_mutation Whether the mutation is handling a Draft Entry (`gfUpdateDraftEntry`, or `gfSubmitForm` when `saveAsDraft` is `true`).
* @param string $field_name The GraphQL input field name. E.g. `nameValues`.
* @param array|string $prepared_field_value The field value formatted in a way Gravity Forms can understand.
* @param array<string,mixed>|string $args Field value input args.
* @param \GF_Field $field The current Gravity Forms field object.
* @param array<string,mixed> $form The current Gravity Forms form object.
* @param ?array<int|string,mixed> $entry The current Gravity Forms entry object. Only available when using update (`gfUpdateEntry`, `gfUpdateDraftEntry`) mutations.
* @param bool $is_draft_mutation Whether the mutation is handling a Draft Entry (`gfUpdateDraftEntry`, or `gfSubmitForm` when `saveAsDraft` is `true`).
* @param string $field_name The GraphQL input field name. E.g. `nameValues`.
*/
$this->value = apply_filters(
'graphql_gf_field_value_input_prepared_value',
Expand Down Expand Up @@ -189,7 +189,7 @@ protected function is_valid_input_type(): bool {
/**
* Gets the input args for the specified field value input.
*
* @return string|array
* @return string|mixed[]
*/
public function get_args() {
$key = $this->field_name;
Expand All @@ -200,7 +200,7 @@ public function get_args() {
/**
* Converts the field value args to a format GravityForms can understand.
*
* @return string|array the sanitized value.
* @return string|mixed[] the sanitized value.
*/
protected function prepare_value() {
// You probably want to replace this.
Expand All @@ -210,7 +210,7 @@ protected function prepare_value() {
/**
* Manually runs GF_Field::validate, and grabs any validation errors.
*
* @param array $errors .
* @param array{id:int,message:string}[] $errors the array of validation errors.
*/
public function validate_value( array &$errors ): void {
$this->field->validate( $this->value, $this->form );
Expand All @@ -226,7 +226,7 @@ public function validate_value( array &$errors ): void {
/**
* Adds the prepared value to the field values array for processing by Gravity Forms.
*
* @param array $field_values the existing field values array.
* @param array<int|string,mixed> $field_values the existing field values array.
*/
public function add_value_to_submission( array &$field_values ): void {
$field_values[ $this->field->id ] = $this->value;
Expand Down
6 changes: 3 additions & 3 deletions src/Data/FieldValueInput/AddressValuesInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ class AddressValuesInput extends AbstractFieldValueInput {
/**
* {@inheritDoc}
*
* @var array
* @var array{street?:string,lineTwo?:string,city?:string,state?:string,zip?:string,country?:string}
*/
protected $args;

/**
* {@inheritDoc}
*
* @var array
* @var array<string,?string>
*/
public $value;

Expand All @@ -38,7 +38,7 @@ protected function get_field_name(): string {
/**
* {@inheritDoc}
*
* @return array
* @return array<int|string,?string>
*/
protected function prepare_value() {
$value = $this->args;
Expand Down
8 changes: 4 additions & 4 deletions src/Data/FieldValueInput/CheckboxValuesInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ class CheckboxValuesInput extends AbstractFieldValueInput {
/**
* {@inheritDoc}
*
* @var array
* @var array{inputId:float,value:string}[]
*/
protected $args;

/**
* {@inheritDoc}
*
* @var array
* @var array<string,string>
*/
public $value;

Expand All @@ -41,7 +41,7 @@ protected function get_field_name(): string {
/**
* {@inheritDoc}
*
* @return array
* @return array<string,string>
*
* @throws \GraphQL\Error\UserError .
*/
Expand All @@ -65,7 +65,7 @@ static function ( array $values_to_save, array $input ): array {
// translators: field ID, input ID.
esc_html__( 'Field %1$s has no input with an id of %2$s.', 'wp-graphql-gravity-forms' ),
esc_html( $this->field->id ),
esc_html( $single_value['inputId'] ),
esc_html( (string) $single_value['inputId'] ),
)
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Data/FieldValueInput/ConsentValueInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ConsentValueInput extends AbstractFieldValueInput {
/**
* {@inheritDoc}
*
* @var array
* @var array<string,mixed>
*/
public $value;

Expand All @@ -40,7 +40,7 @@ protected function get_field_name(): string {
/**
* {@inheritDoc}
*
* @return array
* @return array<int|string,mixed>
*/
protected function prepare_value() {
$field = $this->field;
Expand Down
10 changes: 5 additions & 5 deletions src/Data/FieldValueInput/EmailValuesInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ class EmailValuesInput extends AbstractFieldValueInput {
/**
* {@inheritDoc}
*
* @var array
* @var array{value?:string,confirmationValue?:string}
*/
protected $args;

/**
* {@inheritDoc}
*
* @var array
* @var array<int,?string>
*/
public $value;

Expand All @@ -38,15 +38,15 @@ protected function get_field_name(): string {
/**
* {@inheritDoc}
*
* @return mixed[]
* @return array<int,?string>
*/
protected function prepare_value() {
$value = $this->args;

$values_to_save = [];
$values_to_save[] = $value['value'] ?? null;

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

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

// Confirmation values are stored in a subfield.
if ( isset( $this->value[1] ) ) {
if ( isset( $this->value[1] ) && isset( $this->field->inputs[1]['id'] ) ) {
$field_values[ $this->field->inputs[1]['id'] ] = $this->value[0];
}
}
Expand Down
7 changes: 1 addition & 6 deletions src/Data/FieldValueInput/FileUploadValuesInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class FileUploadValuesInput extends AbstractFieldValueInput {
/**
* {@inheritDoc}
*
* @var array
* @var array{error:int,name:string,size:int,tmp_name:string,type:string}[]
*/
protected $args;

Expand All @@ -37,11 +37,8 @@ protected function get_field_name(): string {
* @return string|mixed
*
* @throws \GraphQL\Error\UserError
*
* @return string|mixed
*/
protected function prepare_value() {
// Draft entries don't upload files.
if ( $this->is_draft ) {
return '';
}
Expand All @@ -66,8 +63,6 @@ protected function prepare_value() {

/**
* {@inheritDoc}
*
* @param array $field_values.
*/
public function add_value_to_submission( array &$field_values ): void {
if ( empty( $this->entry ) || $this->is_draft || ! empty( $this->field->multipleFields ) ) {
Expand Down
4 changes: 2 additions & 2 deletions src/Data/FieldValueInput/ImageValuesInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ protected function get_field_name(): string {
/**
* {@inheritDoc}
*
* @var array
* @var array<string,?string>
*/
public $value;

/**
* {@inheritDoc}
*
* @return array<string,mixed>
* @return array<string,?string>
*
* @throws \GraphQL\Error\UserError
*/
Expand Down
6 changes: 4 additions & 2 deletions src/Data/FieldValueInput/ListValuesInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ class ListValuesInput extends AbstractFieldValueInput {
/**
* {@inheritDoc}
*
* @var array
* @var array{rowValues:string[]}[]
*/
protected $args;

/**
* {@inheritDoc}
*
* @var array
* @var string[]|string
*/
public $value;

Expand All @@ -40,6 +40,8 @@ protected function get_field_name(): string {
/**
* {@inheritDoc}
*
* @return string[]|string
*
* @throws \GraphQL\Error\UserError .
*/
protected function prepare_value() {
Expand Down
6 changes: 3 additions & 3 deletions src/Data/FieldValueInput/NameValuesInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ class NameValuesInput extends AbstractFieldValueInput {
/**
* {@inheritDoc}
*
* @var array
* @var array{prefix?:string,first?:string,middle?:string,last?:string,suffix?:string}
*/
protected $args;

/**
* {@inheritDoc}
*
* @var array
* @var array<string,?string>
*/
public $value;

Expand All @@ -38,7 +38,7 @@ protected function get_field_name(): string {
/**
* {@inheritDoc}
*
* @return array
* @return array<int|string,?string>
*/
protected function prepare_value() {
$value = $this->args;
Expand Down
Loading

0 comments on commit a050062

Please sign in to comment.