From a7fed252b80aaaaf46d7ae61c7d923fd60f54da4 Mon Sep 17 00:00:00 2001 From: Dovid Levine Date: Sat, 6 Apr 2024 14:55:12 +0300 Subject: [PATCH] chore: implement strict PHPStan rules and fix resulting issues (#409) * chore: implement strict PHPStan rules and fix resulting issues * chore: lint and types * fix: undefined $form in loadKeys() * chore: return types --- CHANGELOG.md | 5 +- phpstan.neon.dist | 17 ++++- phpstan/constants.php | 1 - src/Connection/AbstractConnection.php | 6 +- src/Connection/FormFieldsConnection.php | 2 +- .../FormFieldsConnectionResolver.php | 2 +- .../AbstractFieldValueInput.php | 24 +++---- .../FieldValueInput/AddressValuesInput.php | 2 + .../FieldValueInput/CheckboxValuesInput.php | 2 + .../FieldValueInput/ConsentValueInput.php | 2 + .../FieldValueInput/FileUploadValuesInput.php | 2 + src/Data/FieldValueInput/ImageValuesInput.php | 2 + src/Data/FieldValueInput/NameValuesInput.php | 2 + .../FieldValueInput/ProductValueInput.php | 2 + src/Data/Loader/DraftEntriesLoader.php | 18 ++--- src/Data/Loader/EntriesLoader.php | 22 ++---- src/Data/Loader/FormsLoader.php | 20 ++---- .../Type/WPObject/QuizResults/QuizResults.php | 2 +- src/Interfaces/Enum.php | 2 + src/Interfaces/Mutation.php | 4 ++ src/Interfaces/TypeWithConnections.php | 2 + src/Interfaces/TypeWithFields.php | 4 +- src/Interfaces/TypeWithInterfaces.php | 2 + src/Mutation/AbstractMutation.php | 2 +- src/Registry/FormFieldRegistry.php | 14 ++-- src/Registry/TypeRegistry.php | 6 +- src/Type/AbstractType.php | 2 + src/Type/WPInterface/AbstractInterface.php | 2 + .../FormField/FieldValue/FieldValues.php | 3 +- src/Utils/GFUtils.php | 68 +++++++++++-------- src/Utils/Utils.php | 26 +++++-- vendor/composer/installed.php | 4 +- wp-graphql-gravity-forms.php | 2 + 33 files changed, 167 insertions(+), 109 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 340fe329..77d48fc9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,8 +3,9 @@ ## [Unreleased] - feat: Add plugin dependency header. -- chore: update Composer deps and lint. -- chore: lock WPBrowser to <3.5.0 to prevent conflicts with Codeception. +- chore: Update Composer deps and lint. +- chore: Lock WPBrowser to <3.5.0 to prevent conflicts with Codeception. +- chore: Implement strict PHPStan rules and fix resulting issues. - ci: Update GitHub Actions to latest versions. - ci: Test plugin compatibility with WordPress 6.5.0. - ci: Test plugin compatibility with PHP 8.2. diff --git a/phpstan.neon.dist b/phpstan.neon.dist index e368f7ac..b2ee1e18 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -1,9 +1,24 @@ parameters: level: 8 + checkAlwaysTrueCheckTypeFunctionCall: true + checkAlwaysTrueInstanceof: true + checkAlwaysTrueStrictComparison: true + checkExplicitMixedMissingReturn: true + checkFunctionNameCase: true + checkInternalClassCaseSensitivity: true + checkMissingIterableValueType: false # @todo make true + checkTooWideReturnTypesInProtectedAndPublicMethods: true inferPrivatePropertyTypeFromConstructor: true - checkMissingIterableValueType: false + polluteScopeWithAlwaysIterableForeach: false + polluteScopeWithLoopInitialAssignments: false + reportAlwaysTrueInLastCondition: true + reportStaticMethodSignatures: true + reportWrongPhpDocTypeInVarTag: true + treatPhpDocTypesAsCertain: false featureToggles: disableRuntimeReflectionProvider: true + dynamicConstantNames: + - WPGRAPHQL_GF_AUTOLOAD stubFiles: # Simulate added properties - phpstan/class-app-context.stub diff --git a/phpstan/constants.php b/phpstan/constants.php index 31c175b8..4a73c007 100644 --- a/phpstan/constants.php +++ b/phpstan/constants.php @@ -3,7 +3,6 @@ * Constants defined in this file are to help phpstan analyze code where constants outside the plugin (WordPress core constants, etc) are being used. */ -define( 'WPGRAPHQL_GF_AUTOLOAD', true ); define( 'CRGEARY_JAMSTACK_DEPLOYMENTS_OPTIONS_KEY', 'wp-jamstack-deployments' ); define( 'WPGRAPHQL_GF_PLUGIN_FILE', 'wp-graphql-gravity-forms.php' ); define( 'WPGRAPHQL_GF_VERSION', '0.12.5' ); diff --git a/src/Connection/AbstractConnection.php b/src/Connection/AbstractConnection.php index f0b78668..d52b6a83 100644 --- a/src/Connection/AbstractConnection.php +++ b/src/Connection/AbstractConnection.php @@ -31,6 +31,8 @@ public static function register_hooks(): void { /** * Gets custom connection configuration arguments, such as the resolver, edgeFields, connectionArgs, etc. + * + * @return array> */ public static function get_connection_args(): array { return []; @@ -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> */ public static function get_filtered_connection_args( ?array $filter_by = null ): array { $connection_args = static::get_connection_args(); diff --git a/src/Connection/FormFieldsConnection.php b/src/Connection/FormFieldsConnection.php index ec2e46fa..aa2129e6 100644 --- a/src/Connection/FormFieldsConnection.php +++ b/src/Connection/FormFieldsConnection.php @@ -31,7 +31,7 @@ public static function register(): void { } /** - * Gets custom connection configuration arguments, such as the resolver, edgeFields, connectionArgs, etc. + * {@inheritDoc} */ public static function get_connection_args(): array { return [ diff --git a/src/Data/Connection/FormFieldsConnectionResolver.php b/src/Data/Connection/FormFieldsConnectionResolver.php index a4403d01..fb2f2b79 100644 --- a/src/Data/Connection/FormFieldsConnectionResolver.php +++ b/src/Data/Connection/FormFieldsConnectionResolver.php @@ -19,7 +19,7 @@ */ class FormFieldsConnectionResolver { /** - * @var array + * @var array */ private const EMPTY_CHOICES = [ 'text' => null, diff --git a/src/Data/FieldValueInput/AbstractFieldValueInput.php b/src/Data/FieldValueInput/AbstractFieldValueInput.php index 91708ce0..149a0bca 100644 --- a/src/Data/FieldValueInput/AbstractFieldValueInput.php +++ b/src/Data/FieldValueInput/AbstractFieldValueInput.php @@ -26,7 +26,7 @@ abstract class AbstractFieldValueInput { /** * The Gravity Forms entry object, if it exists. * - * @var array|null + * @var array|null */ protected ?array $entry; @@ -47,7 +47,7 @@ abstract class AbstractFieldValueInput { /** * The Gravity Forms form object. * - * @var array + * @var array */ protected array $form; @@ -75,11 +75,11 @@ abstract class AbstractFieldValueInput { /** * The class constructor. * - * @param array $input_args The GraphQL input args for the form field. - * @param array $form The current Gravity Forms form object. - * @param bool $is_draft Whether the mutation is handling a Draft Entry. - * @param \GF_Field $field The current Gravity Forms field object. - * @param array|null $entry The current Gravity Forms entry object. + * @param array $input_args The GraphQL input args for the form field. + * @param array $form The current Gravity Forms form object. + * @param bool $is_draft Whether the mutation is handling a Draft Entry. + * @param \GF_Field $field The current Gravity Forms field object. + * @param array|null $entry The current Gravity Forms entry object. * * @throws \GraphQL\Error\UserError . */ @@ -93,11 +93,11 @@ public function __construct( array $input_args, array $form, bool $is_draft, ?GF /** * Filters the accepted GraphQL input value key for the form field. * - * @param string $name The GraphQL input value name to use. E.g. `nameValues`. - * @param \GF_Field $field The current Gravity Forms field object. - * @param array $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 $name The GraphQL input value name to use. E.g. `nameValues`. + * @param \GF_Field $field The current Gravity Forms field object. + * @param array $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`). */ $this->field_name = apply_filters( 'graphql_gf_field_value_input_name', diff --git a/src/Data/FieldValueInput/AddressValuesInput.php b/src/Data/FieldValueInput/AddressValuesInput.php index 073fb4c6..8a315f78 100644 --- a/src/Data/FieldValueInput/AddressValuesInput.php +++ b/src/Data/FieldValueInput/AddressValuesInput.php @@ -35,6 +35,8 @@ protected function get_field_name(): string { /** * {@inheritDoc} + * + * @return array */ protected function prepare_value() { $value = $this->args; diff --git a/src/Data/FieldValueInput/CheckboxValuesInput.php b/src/Data/FieldValueInput/CheckboxValuesInput.php index 102adc93..a3ebb6d5 100644 --- a/src/Data/FieldValueInput/CheckboxValuesInput.php +++ b/src/Data/FieldValueInput/CheckboxValuesInput.php @@ -39,6 +39,8 @@ protected function get_field_name(): string { /** * {@inheritDoc} * + * @return array + * * @throws \GraphQL\Error\UserError . */ protected function prepare_value() { diff --git a/src/Data/FieldValueInput/ConsentValueInput.php b/src/Data/FieldValueInput/ConsentValueInput.php index b1e77408..dfec108c 100644 --- a/src/Data/FieldValueInput/ConsentValueInput.php +++ b/src/Data/FieldValueInput/ConsentValueInput.php @@ -37,6 +37,8 @@ protected function get_field_name(): string { /** * {@inheritDoc} + * + * @return array */ protected function prepare_value() { $field = $this->field; diff --git a/src/Data/FieldValueInput/FileUploadValuesInput.php b/src/Data/FieldValueInput/FileUploadValuesInput.php index 032a41e3..bbdb6cff 100644 --- a/src/Data/FieldValueInput/FileUploadValuesInput.php +++ b/src/Data/FieldValueInput/FileUploadValuesInput.php @@ -32,6 +32,8 @@ protected function get_field_name(): string { /** * {@inheritDoc} * + * @return string|mixed + * * @throws \GraphQL\Error\UserError */ protected function prepare_value() { diff --git a/src/Data/FieldValueInput/ImageValuesInput.php b/src/Data/FieldValueInput/ImageValuesInput.php index cf234b58..2a50f581 100644 --- a/src/Data/FieldValueInput/ImageValuesInput.php +++ b/src/Data/FieldValueInput/ImageValuesInput.php @@ -37,6 +37,8 @@ protected function get_field_name(): string { /** * {@inheritDoc} * + * @return array + * * @throws \GraphQL\Error\UserError */ protected function prepare_value() { diff --git a/src/Data/FieldValueInput/NameValuesInput.php b/src/Data/FieldValueInput/NameValuesInput.php index 67542868..5dfcb30b 100644 --- a/src/Data/FieldValueInput/NameValuesInput.php +++ b/src/Data/FieldValueInput/NameValuesInput.php @@ -35,6 +35,8 @@ protected function get_field_name(): string { /** * {@inheritDoc} + * + * @return array */ protected function prepare_value() { $value = $this->args; diff --git a/src/Data/FieldValueInput/ProductValueInput.php b/src/Data/FieldValueInput/ProductValueInput.php index baf30c5c..e673b7e3 100644 --- a/src/Data/FieldValueInput/ProductValueInput.php +++ b/src/Data/FieldValueInput/ProductValueInput.php @@ -92,6 +92,8 @@ public function get_args() { /** * {@inheritDoc} + * + * @return array */ protected function prepare_value() { $field = $this->field; diff --git a/src/Data/Loader/DraftEntriesLoader.php b/src/Data/Loader/DraftEntriesLoader.php index ca6e40d7..bedd7c4f 100644 --- a/src/Data/Loader/DraftEntriesLoader.php +++ b/src/Data/Loader/DraftEntriesLoader.php @@ -34,19 +34,7 @@ protected function get_model( $entry, $key ): DraftEntry { } /** - * Given array of keys, loads and returns a map consisting of keys from `keys` array and loaded - * posts as the values - * - * Note that order of returned values must match exactly the order of keys. - * If some entry is not available for given key - it must include null for the missing key. - * - * For example: - * loadKeys(['a', 'b', 'c']) -> ['a' => 'value1, 'b' => null, 'c' => 'value3'] - * - * @param array $keys . - * - * @return array|false - * @throws \Exception . + * {@inheritDoc} */ public function loadKeys( array $keys ) { if ( empty( $keys ) ) { @@ -55,6 +43,10 @@ public function loadKeys( array $keys ) { $loaded_entries = []; foreach ( $keys as $key ) { + if ( empty( $key ) ) { + continue; + } + $loaded_entries[ $key ] = GFFormsModel::get_draft_submission_values( $key ) ?: null; } diff --git a/src/Data/Loader/EntriesLoader.php b/src/Data/Loader/EntriesLoader.php index e2dc9a01..7f353036 100644 --- a/src/Data/Loader/EntriesLoader.php +++ b/src/Data/Loader/EntriesLoader.php @@ -33,19 +33,7 @@ protected function get_model( $entry, $key ): SubmittedEntry { } /** - * Given array of keys, loads and returns a map consisting of keys from `keys` array and loaded - * posts as the values - * - * Note that order of returned values must match exactly the order of keys. - * If some entry is not available for given key - it must include null for the missing key. - * - * For example: - * loadKeys(['a', 'b', 'c']) -> ['a' => 'value1, 'b' => null, 'c' => 'value3'] - * - * @param array $keys . - * - * @return array|false - * @throws \Exception . + * {@inheritDoc} */ protected function loadKeys( array $keys ) { if ( empty( $keys ) ) { @@ -55,8 +43,12 @@ protected function loadKeys( array $keys ) { // Associate the requested keys with their loaded entry. $loaded_entries = []; foreach ( $keys as $key ) { - $entry = GFAPI::get_entry( $key ); - $loaded_entries[ $key ] = ! is_wp_error( $entry ) ? $entry : null; + if ( empty( $key ) ) { + continue; + } + + $entry = GFAPI::get_entry( (int) $key ); + $loaded_entries[ $key ] = ! $entry instanceof \WP_Error ? $entry : null; } return $loaded_entries; diff --git a/src/Data/Loader/FormsLoader.php b/src/Data/Loader/FormsLoader.php index a50a2e8f..6c282b1c 100644 --- a/src/Data/Loader/FormsLoader.php +++ b/src/Data/Loader/FormsLoader.php @@ -33,19 +33,7 @@ protected function get_model( $entry, $key ): Form { } /** - * Given array of keys, loads and returns a map consisting of keys from `keys` array and loaded - * posts as the values - * - * Note that order of returned values must match exactly the order of keys. - * If some entry is not available for given key - it must include null for the missing key. - * - * For example: - * loadKeys(['a', 'b', 'c']) -> ['a' => 'value1, 'b' => null, 'c' => 'value3'] - * - * @param array $keys . - * - * @return array|false - * @throws \Exception . + * {@inheritDoc} */ protected function loadKeys( array $keys ) { if ( empty( $keys ) ) { @@ -54,7 +42,11 @@ protected function loadKeys( array $keys ) { $loaded_forms = []; foreach ( $keys as $key ) { - $form = GFAPI::get_form( $key ); + if ( empty( $key ) ) { + continue; + } + + $form = GFAPI::get_form( (int) $key ); // Run the form through `gform_pre_render` to support 3rd party plugins like Populate Anything. if ( ! empty( $form ) ) { diff --git a/src/Extensions/GFQuiz/Type/WPObject/QuizResults/QuizResults.php b/src/Extensions/GFQuiz/Type/WPObject/QuizResults/QuizResults.php index d4c01c8d..ec0a609d 100644 --- a/src/Extensions/GFQuiz/Type/WPObject/QuizResults/QuizResults.php +++ b/src/Extensions/GFQuiz/Type/WPObject/QuizResults/QuizResults.php @@ -38,7 +38,7 @@ class QuizResults extends AbstractObject implements Field { // @todo grab search criteria from connection args. /** - * @var array + * @var array */ private const SEARCH_CRITERIA = [ 'status' => 'active' ]; diff --git a/src/Interfaces/Enum.php b/src/Interfaces/Enum.php index d26973e6..68fa973a 100644 --- a/src/Interfaces/Enum.php +++ b/src/Interfaces/Enum.php @@ -15,6 +15,8 @@ interface Enum { /** * Gets the Enum type values. + * + * @return array> */ public static function get_values(): array; } diff --git a/src/Interfaces/Mutation.php b/src/Interfaces/Mutation.php index aa0584c5..51ad53e1 100644 --- a/src/Interfaces/Mutation.php +++ b/src/Interfaces/Mutation.php @@ -15,6 +15,8 @@ interface Mutation { /** * Defines the input field configuration. * + * @return array> + * * @since 0.4.0 */ public static function get_input_fields(): array; @@ -22,6 +24,8 @@ public static function get_input_fields(): array; /** * Defines the output field configuration. * + * @return array> + * * @since 0.4.0 */ public static function get_output_fields(): array; diff --git a/src/Interfaces/TypeWithConnections.php b/src/Interfaces/TypeWithConnections.php index e53d7865..b8240564 100644 --- a/src/Interfaces/TypeWithConnections.php +++ b/src/Interfaces/TypeWithConnections.php @@ -14,6 +14,8 @@ interface TypeWithConnections { /** * Gets the the connection config for the GraphQL Type. + * + * @return array> */ public static function get_connections(): array; } diff --git a/src/Interfaces/TypeWithFields.php b/src/Interfaces/TypeWithFields.php index 8a26945b..18f7238d 100644 --- a/src/Interfaces/TypeWithFields.php +++ b/src/Interfaces/TypeWithFields.php @@ -13,7 +13,9 @@ */ interface TypeWithFields { /** - * Gets the properties for the type. + * Gets the GraphQL fields for the type. + * + * @return array> The GraphQL field configs for the type. */ public static function get_fields(): array; } diff --git a/src/Interfaces/TypeWithInterfaces.php b/src/Interfaces/TypeWithInterfaces.php index deb18fa7..814d2e21 100644 --- a/src/Interfaces/TypeWithInterfaces.php +++ b/src/Interfaces/TypeWithInterfaces.php @@ -14,6 +14,8 @@ interface TypeWithInterfaces { /** * Gets the the connection config for the GraphQL Type. + * + * @return string[] */ public static function get_interfaces(): array; } diff --git a/src/Mutation/AbstractMutation.php b/src/Mutation/AbstractMutation.php index d85685e4..51d752a9 100644 --- a/src/Mutation/AbstractMutation.php +++ b/src/Mutation/AbstractMutation.php @@ -51,7 +51,7 @@ public static function get_type_config(): array { * @throws \GraphQL\Error\UserError . */ protected static function check_required_inputs( ?array $input ): void { - if ( empty( $input ) || ! is_array( $input ) ) { + if ( empty( $input ) ) { throw new UserError( esc_html__( 'Mutation not processed. The input data was missing or invalid.', 'wp-graphql-gravity-forms' ) ); } } diff --git a/src/Registry/FormFieldRegistry.php b/src/Registry/FormFieldRegistry.php index bca5917e..68e8628c 100644 --- a/src/Registry/FormFieldRegistry.php +++ b/src/Registry/FormFieldRegistry.php @@ -214,6 +214,8 @@ static function ( TypeRegistry $type_registry ) use ( $field, $interface_setting * Gets the registered field settings, including those of input types. * * @param \GF_Field $field . + * + * @return string[] */ public static function get_field_settings( GF_Field $field ): array { $settings = $field->get_form_editor_field_settings(); @@ -242,6 +244,8 @@ public static function get_field_settings( GF_Field $field ): array { * * @param \GF_Field $field . * @param array $settings . + * + * @return array{interfaces:string[],fields:array} */ public static function get_config_from_settings( GF_Field $field, array $settings ): array { // Every GF_field is a FormField. @@ -339,6 +343,8 @@ public static function get_description( string $field_name ): string { * @param \GF_Field $field The Gravity Forms field object. * @param array $settings The Gravity Forms field settings. * @param array $interfaces The list of interfaces to add to the field. + * + * @return array> The GraphQL fields config. */ public static function get_fields( GF_Field $field, array $settings, $interfaces ): array { $fields = []; @@ -362,10 +368,10 @@ public static function get_fields( GF_Field $field, array $settings, $interfaces /** * Filter to modify the Form Field GraphQL fields. * - * @param array $fields An array of GraphQL field configs. See https://www.wpgraphql.com/functions/register_graphql_fields/ - * @param \GF_Field $field The Gravity Forms Field object. - * @param array $settings The `form_editor_field_settings()` key. - * @param array $interfaces The list of interfaces for the GraphQL type. + * @param array> $fields An array of GraphQL field configs. See https://www.wpgraphql.com/functions/register_graphql_fields/ + * @param \GF_Field $field The Gravity Forms Field object. + * @param array $settings The `form_editor_field_settings()` key. + * @param array $interfaces The list of interfaces for the GraphQL type. */ $fields = apply_filters( 'graphql_gf_form_field_setting_fields', $fields, $field, $settings, $interfaces ); diff --git a/src/Registry/TypeRegistry.php b/src/Registry/TypeRegistry.php index e2fa3058..5a163639 100644 --- a/src/Registry/TypeRegistry.php +++ b/src/Registry/TypeRegistry.php @@ -310,7 +310,7 @@ public static function objects(): array { /** * Returns an array of Gravity Forms field settings mapped to their corresponding PHP class. * - * @return array + * @return array */ public static function form_field_settings(): array { // Key-value pairs of settings and their corresponding class name. @@ -397,7 +397,7 @@ public static function form_field_settings(): array { /** * Returns an array of Gravity Forms field settings mapped to their InputProperty's corresponding interface PHP class. * - * @return array + * @return array */ public static function form_field_setting_inputs(): array { // Key-value pairs of settings and their corresponding class name. @@ -425,7 +425,7 @@ public static function form_field_setting_inputs(): array { /** * Returns an array of Gravity Forms field settings mapped to their FieldChoice's corresponding interface PHP class. * - * @return array + * @return array */ public static function form_field_setting_choices(): array { // Key-value pairs of settings and their corresponding class name. diff --git a/src/Type/AbstractType.php b/src/Type/AbstractType.php index 3b7232ab..9b4d5800 100644 --- a/src/Type/AbstractType.php +++ b/src/Type/AbstractType.php @@ -40,6 +40,8 @@ public static function register_hooks(): void { /** * Gets the WPGraphQL config array used to register the type. + * + * @return array */ abstract public static function get_type_config(): array; } diff --git a/src/Type/WPInterface/AbstractInterface.php b/src/Type/WPInterface/AbstractInterface.php index ae95db8e..eec7ec83 100644 --- a/src/Type/WPInterface/AbstractInterface.php +++ b/src/Type/WPInterface/AbstractInterface.php @@ -46,6 +46,8 @@ public static function register( ?TypeRegistry $type_registry = null ): void { /** * {@inheritDoc} + * + * @return array */ public static function get_type_config( ?TypeRegistry $type_registry = null ): array { return [ diff --git a/src/Type/WPObject/FormField/FieldValue/FieldValues.php b/src/Type/WPObject/FormField/FieldValue/FieldValues.php index e6a0ffd1..8652d257 100644 --- a/src/Type/WPObject/FormField/FieldValue/FieldValues.php +++ b/src/Type/WPObject/FormField/FieldValue/FieldValues.php @@ -401,8 +401,7 @@ public static function values(): array { $values = Utils::maybe_decode_json( $values ); // Sometimes GF likes to nest their jsons twice. - if ( is_string( $values ) ) { // @phpstan-ignore-line - + if ( is_string( $values ) ) { $values = Utils::maybe_decode_json( $values ); } diff --git a/src/Utils/GFUtils.php b/src/Utils/GFUtils.php index d46ca37e..9183b911 100644 --- a/src/Utils/GFUtils.php +++ b/src/Utils/GFUtils.php @@ -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 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 ) ), @@ -66,6 +68,9 @@ public static function get_form( int $form_id, bool $active_only = true ): array ); } + /** + * @var array $form + */ return $form; } @@ -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[] 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 ); @@ -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; @@ -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 $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 @@ -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 $form The form. + * @param int $field_id Field ID. * * @throws \GraphQL\Error\UserError . */ @@ -162,12 +167,14 @@ static function ( GF_Field $field ) use ( $field_id ): bool { * * @param int $entry_id . * + * @return array 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() ) @@ -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 $entry_data . + * @param int $entry_id . * * @throws \GraphQL\Error\UserError . */ @@ -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() ) @@ -209,6 +216,7 @@ public static function update_entry( array $entry_data, ?int $entry_id = null ): * * @param string $resume_token . * + * @return array Draft entry. * @throws \GraphQL\Error\UserError . */ public static function get_draft_entry( string $resume_token ): array { @@ -229,6 +237,8 @@ public static function get_draft_entry( string $resume_token ): array { * * @param string $resume_token Draft entry resume token. * + * @return array Draft entry submission data. + * * @throws \GraphQL\Error\UserError . */ public static function get_draft_submission( string $resume_token ): array { @@ -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. */ @@ -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 $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 . * * @throws \GraphQL\Error\UserError . */ @@ -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 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 { @@ -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() ) ); } @@ -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 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 { @@ -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 $file File data to upload. + * @param array $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 * diff --git a/src/Utils/Utils.php b/src/Utils/Utils.php index 0e3a7baf..7f5b5012 100644 --- a/src/Utils/Utils.php +++ b/src/Utils/Utils.php @@ -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 $property The field property to deprecate. + * @param string $reason The reason for the deprecation. Should be wrapped in __(). * + * @return array * @since 0.2.0 */ public static function deprecate_property( array $property, string $reason ): array { @@ -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 */ public static function get_registered_form_field_types(): array { $types = []; @@ -150,6 +153,9 @@ public static function get_registered_form_field_types(): array { } } + /** + * @var array $types An array of GF_Field::$type => GraphQL Type names. + */ return $types; } @@ -157,6 +163,8 @@ public static function get_registered_form_field_types(): array { * Gets an array of GF entry types paired with their GraphQL type names. * * E.g. `[ 'draft_entry' => 'GfDraftEntry' ]` + * + * @return array */ public static function get_registered_entry_types(): array { $types = [ @@ -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 $entry_types An array of Data Loader names => GraphQL Types. */ return apply_filters( 'graphql_gf_registered_entry_types', $types ); } @@ -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 */ public static function get_possible_form_field_child_types( string $type ): array { $prefix = self::get_safe_form_field_type_name( $type ); @@ -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 $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 = [ @@ -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 ); @@ -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 [ diff --git a/vendor/composer/installed.php b/vendor/composer/installed.php index 14c77d57..f04205d4 100644 --- a/vendor/composer/installed.php +++ b/vendor/composer/installed.php @@ -3,7 +3,7 @@ 'name' => 'harness-software/wp-graphql-gravity-forms', 'pretty_version' => 'dev-develop', 'version' => 'dev-develop', - 'reference' => '47727557e678b957bbaa36cea53ec5580cb1dc6b', + 'reference' => '7b23b491eeec3889e5300440e9d4d48f71ecc61c', 'type' => 'wordpress-plugin', 'install_path' => __DIR__ . '/../../', 'aliases' => array(), @@ -13,7 +13,7 @@ 'harness-software/wp-graphql-gravity-forms' => array( 'pretty_version' => 'dev-develop', 'version' => 'dev-develop', - 'reference' => '47727557e678b957bbaa36cea53ec5580cb1dc6b', + 'reference' => '7b23b491eeec3889e5300440e9d4d48f71ecc61c', 'type' => 'wordpress-plugin', 'install_path' => __DIR__ . '/../../', 'aliases' => array(), diff --git a/wp-graphql-gravity-forms.php b/wp-graphql-gravity-forms.php index 87361246..5e44e3b2 100644 --- a/wp-graphql-gravity-forms.php +++ b/wp-graphql-gravity-forms.php @@ -70,6 +70,8 @@ function gf_graphql_constants(): void { /** * Checks if all the the required plugins are installed and activated. + * + * @return array List of dependencies not ready. */ function gf_graphql_dependencies_not_ready(): array { $wpgraphql_version = '1.9.0';