Skip to content

Commit

Permalink
chore: lint and types
Browse files Browse the repository at this point in the history
  • Loading branch information
justlevine committed Apr 6, 2024
1 parent a048ad9 commit 381c4a8
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 36 deletions.
6 changes: 5 additions & 1 deletion src/Connection/AbstractConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public static function register_hooks(): void {

/**
* Gets custom connection configuration arguments, such as the resolver, edgeFields, connectionArgs, etc.
*
* @return array<string,array<string,mixed>>
*/
public static function get_connection_args(): array {
return [];
Expand All @@ -39,7 +41,9 @@ public static function get_connection_args(): array {
/**
* Returns a filtered array of connection args.
*
* @param array $filter_by .
* @param string[] $filter_by .
*
* @return array<string,array<string,mixed>>
*/
public static function get_filtered_connection_args( ?array $filter_by = null ): array {
$connection_args = static::get_connection_args();
Expand Down
2 changes: 2 additions & 0 deletions src/Interfaces/TypeWithInterfaces.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
interface TypeWithInterfaces {
/**
* Gets the the connection config for the GraphQL Type.
*
* @return string[]
*/
public static function get_interfaces(): array;
}
1 change: 0 additions & 1 deletion src/Type/WPObject/FormField/FieldValue/FieldValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,6 @@ public static function values(): array {

// Sometimes GF likes to nest their jsons twice.
if ( is_string( $values ) ) {

$values = Utils::maybe_decode_json( $values );
}

Expand Down
68 changes: 40 additions & 28 deletions src/Utils/GFUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ public static function get_ip( string $ip ): string {
* @param int $form_id .
* @param bool $active_only Whether to only return the form if it is active.
*
* @return array<string,mixed> The form object.
*
* @throws \GraphQL\Error\UserError .
*/
public static function get_form( int $form_id, bool $active_only = true ): array {
$form = GFAPI::get_form( $form_id );

if ( ! $form ) {
if ( ! is_array( $form ) ) {
throw new UserError(
// translators: Gravity Forms form id.
sprintf( esc_html__( 'Unable to retrieve the form for the given ID %s.', 'wp-graphql-gravity-forms' ), absint( $form_id ) ),
Expand All @@ -66,6 +68,9 @@ public static function get_form( int $form_id, bool $active_only = true ): array
);
}

/**
* @var array<string,mixed> $form
*/
return $form;
}

Expand All @@ -76,13 +81,13 @@ public static function get_form( int $form_id, bool $active_only = true ): array
*
* @see https://docs.gravityforms.com/api-functions/#get-forms
*
* @param array $ids Array of form ids to limit results. Empty if all.
* @param int[] $ids Array of form ids to limit results. Empty if all.
* @param bool $active True if active forms are returned. False to get inactive forms. Defaults to true.
* @param bool $trash True if trashed forms are returned. False to exclude trash. Defaults to false.
* @param string $sort_column The column to sort the results on.
* @param string $sort_dir The sort direction, ASC or DESC.
*
* @return array The array of Form Objects.
* @return array<string,mixed>[] The array of Form Objects.
*/
public static function get_forms( array $ids = [], bool $active = true, bool $trash = false, string $sort_column = 'id', string $sort_dir = 'DESC' ): array {
$form_ids = ! empty( $ids ) ? $ids : GFFormsModel::get_form_ids( $active, $trash, $sort_column, $sort_dir );
Expand All @@ -94,7 +99,7 @@ public static function get_forms( array $ids = [], bool $active = true, bool $tr
$forms = [];

foreach ( $form_ids as $form_id ) {
$forms[] = self::get_form( $form_id, false );
$forms[] = self::get_form( (int) $form_id, false );
}

return $forms;
Expand All @@ -103,7 +108,7 @@ public static function get_forms( array $ids = [], bool $active = true, bool $tr
/**
* Gets the last page of the form. Useful for form submissions.
*
* @param array $form .
* @param array<string,mixed> $form The form object.
*/
public static function get_last_form_page( array $form ): int {
require_once GFCommon::get_base_path() . '/form_display.php'; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingCustomConstant
Expand All @@ -129,8 +134,8 @@ public static function get_form_unique_id( int $form_id ): string {
/**
* Returns Gravity Forms Field object for given field id.
*
* @param array $form The form.
* @param int $field_id Field ID.
* @param array<string,mixed> $form The form.
* @param int $field_id Field ID.
*
* @throws \GraphQL\Error\UserError .
*/
Expand Down Expand Up @@ -162,12 +167,14 @@ static function ( GF_Field $field ) use ( $field_id ): bool {
*
* @param int $entry_id .
*
* @return array<int|string,mixed> The entry object.
*
* @throws \GraphQL\Error\UserError .
*/
public static function get_entry( int $entry_id ): array {
$entry = GFAPI::get_entry( $entry_id );

if ( is_wp_error( $entry ) ) {
if ( $entry instanceof \WP_Error ) {
throw new UserError(
// translators: Gravity Forms form id.
sprintf( esc_html__( 'The entry for the given ID %s was not found. Error: .', 'wp-graphql-gravity-forms' ), absint( $entry_id ) ) . esc_html( $entry->get_error_message() )
Expand All @@ -183,8 +190,8 @@ public static function get_entry( int $entry_id ): array {
*
* @see https://docs.gravityforms.com/api-functions/#update-entry
*
* @param array $entry_data .
* @param int $entry_id .
* @param array<int|string,mixed> $entry_data .
* @param int $entry_id .
*
* @throws \GraphQL\Error\UserError .
*/
Expand All @@ -193,7 +200,7 @@ public static function update_entry( array $entry_data, ?int $entry_id = null ):

$is_entry_updated = GFAPI::update_entry( $entry_data, $entry_id );

if ( is_wp_error( ( $is_entry_updated ) ) ) {
if ( $is_entry_updated instanceof \WP_Error ) {
throw new UserError(
// translators: Gravity Forms entry id.
sprintf( esc_html__( 'An error occured while trying to update the entry (ID: %s). Error: .', 'wp-graphql-gravity-forms' ), esc_html( $entry_data['id'] ) ) . esc_html( $is_entry_updated->get_error_message() )
Expand All @@ -209,6 +216,7 @@ public static function update_entry( array $entry_data, ?int $entry_id = null ):
*
* @param string $resume_token .
*
* @return array<int|string,mixed> Draft entry.
* @throws \GraphQL\Error\UserError .
*/
public static function get_draft_entry( string $resume_token ): array {
Expand All @@ -229,6 +237,8 @@ public static function get_draft_entry( string $resume_token ): array {
*
* @param string $resume_token Draft entry resume token.
*
* @return array<int|string,mixed> Draft entry submission data.
*
* @throws \GraphQL\Error\UserError .
*/
public static function get_draft_submission( string $resume_token ): array {
Expand All @@ -252,9 +262,9 @@ public static function get_draft_submission( string $resume_token ): array {
/**
* Get the draft resume URL.
*
* @param string $resume_token Resume token.
* @param string $source_url Source URL.
* @param array|null $form Form object.
* @param string $resume_token Resume token.
* @param string $source_url Source URL.
* @param array $form Form object.
*
* @return string Resume URL, or empty string if no source URL was provided.
*/
Expand Down Expand Up @@ -287,15 +297,15 @@ public static function get_resume_url( string $resume_token, string $source_url
* Saves Gravity Forms draft entry.
* Uses GFFormsModel::save_draft_submission().
*
* @param array $form .
* @param array $entry .
* @param array $field_values .
* @param int $page_number .
* @param array $files .
* @param string $form_unique_id .
* @param string $ip .
* @param string $source_url .
* @param string $resume_token .
* @param array<string,mixed> $form .
* @param array<int|string,mixed> $entry .
* @param array $field_values .
* @param int $page_number .
* @param array $files .
* @param string $form_unique_id .
* @param string $ip .
* @param string $source_url .
* @param string $resume_token .
*
* @throws \GraphQL\Error\UserError .
*/
Expand Down Expand Up @@ -337,6 +347,8 @@ public static function save_draft_submission( array $form, array $entry, ?array
* @param int $target_page .
* @param int $source_page .
*
* @return array<int|string,mixed> The submission object.
*
* @throws \GraphQL\Error\UserError .
*/
public static function submit_form( int $form_id, array $input_values, array $field_values = [], int $target_page = 0, int $source_page = 0 ): array {
Expand All @@ -348,7 +360,7 @@ public static function submit_form( int $form_id, array $input_values, array $fi
$source_page,
);

if ( is_wp_error( $submission ) ) {
if ( $submission instanceof \WP_Error ) {
throw new UserError( esc_html( $submission->get_error_message() ) );
}

Expand All @@ -364,7 +376,7 @@ public static function submit_form( int $form_id, array $input_values, array $fi
*
* @param int $form_id GF form ID.
*
* @return array GF uploads dir config.
* @return array<string,mixed> GF uploads dir config.
* @throws \GraphQL\Error\UserError If directory doesn't exist or cant be created.
*/
public static function get_gravity_forms_upload_dir( int $form_id ): array {
Expand Down Expand Up @@ -429,10 +441,10 @@ public static function get_gravity_forms_upload_dir( int $form_id ): array {
*
* @author WebDevStudios
* @source https://github.com/WebDevStudios/wds-headless-wordpress/blob/5a8e84a2dbb7a0bb537422223ab409ecd2568b00/themes/wds_headless/inc/wp-graphql.php#L452
* @param array $file File data to upload.
* @param array $target Target upload directory; WP uploads dir will be used if none provided.
* @param array<string,mixed> $file File data to upload.
* @param array<string,mixed> $target Target upload directory; WP uploads dir will be used if none provided.
*
* @return array Uploaded file data.
* @return array{file:string,url:string,type:mixed}
*
* @deprecated 0.11.0
*
Expand Down
26 changes: 20 additions & 6 deletions src/Utils/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ class Utils {
/**
* Adds deprecation reason to GraphQL field property.
*
* @param array $property The field property to deprecate.
* @param string $reason The reason for the deprecation. Should be wrapped in __().
* @param array<string,mixed> $property The field property to deprecate.
* @param string $reason The reason for the deprecation. Should be wrapped in __().
*
* @return array<string,mixed>
* @since 0.2.0
*/
public static function deprecate_property( array $property, string $reason ): array {
Expand Down Expand Up @@ -139,6 +140,8 @@ public static function is_graphql_upload_enabled(): bool {
* Gets an array of all GF type names paired with their GraphQL type names.
*
* E.g. `[ 'text' => 'TextField' ]`.
*
* @return array<string,string>
*/
public static function get_registered_form_field_types(): array {
$types = [];
Expand All @@ -150,13 +153,18 @@ public static function get_registered_form_field_types(): array {
}
}

/**
* @var array<string,string> $types An array of GF_Field::$type => GraphQL Type names.
*/
return $types;
}

/**
* Gets an array of GF entry types paired with their GraphQL type names.
*
* E.g. `[ 'draft_entry' => 'GfDraftEntry' ]`
*
* @return array<string,string>
*/
public static function get_registered_entry_types(): array {
$types = [
Expand All @@ -167,7 +175,7 @@ public static function get_registered_entry_types(): array {
/**
* Filter for modifying the Gravity Forms Entry types supported by WPGraphQL.
*
* @param array $entry_types An array of Data Loader names => GraphQL Types.
* @param array<string,string> $entry_types An array of Data Loader names => GraphQL Types.
*/
return apply_filters( 'graphql_gf_registered_entry_types', $types );
}
Expand All @@ -176,6 +184,8 @@ public static function get_registered_entry_types(): array {
* Returns an array of possible form field input types for GraphQL object generation.
*
* @param string $type The current GF field type.
*
* @return array<string,string>
*/
public static function get_possible_form_field_child_types( string $type ): array {
$prefix = self::get_safe_form_field_type_name( $type );
Expand Down Expand Up @@ -263,14 +273,16 @@ public static function get_possible_form_field_child_types( string $type ): arra
/**
* Filter for altering the child types of a specific GF_Field.
*
* @param array $child_types An array of GF_Field::$type => GraphQL type names.
* @param string $field_type The 'parent' GF_Field type.
* @param array<string,string> $child_types An array of GF_Field::$type => GraphQL type names.
* @param string $field_type The 'parent' GF_Field type.
*/
return apply_filters( 'graphql_gf_form_field_child_types', $child_types, $type );
}

/**
* Gets a filterable list of Gravity Forms field types that should be disabled for this instance.
*
* @return string[]
*/
public static function get_ignored_gf_field_types(): array {
$ignored_fields = [
Expand All @@ -289,7 +301,7 @@ public static function get_ignored_gf_field_types(): array {
*
* Useful for adding/removing support for a specific Gravity Forms field.
*
* @param array $ignored_fields An array of GF_Field $type names to be ignored by WPGraphQL.
* @param string[] $ignored_fields An array of GF_Field $type names to be ignored by WPGraphQL.
*/
$ignored_fields = apply_filters( 'graphql_gf_ignored_field_types', $ignored_fields );

Expand All @@ -298,6 +310,8 @@ public static function get_ignored_gf_field_types(): array {

/**
* Returns an array of Gravity Forms field settings to ignore.
*
* @return string[]
*/
public static function get_ignored_gf_settings(): array {
return [
Expand Down
2 changes: 2 additions & 0 deletions wp-graphql-gravity-forms.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ function gf_graphql_constants(): void {

/**
* Checks if all the the required plugins are installed and activated.
*
* @return array<string,string> List of dependencies not ready.
*/
function gf_graphql_dependencies_not_ready(): array {
$wpgraphql_version = '1.9.0';
Expand Down

0 comments on commit 381c4a8

Please sign in to comment.