Skip to content

Commit 640e050

Browse files
authored
chore: add traversable type hints (first pass) (#410)
* chore: add traverable typehints * tests: lint tests * chore: second pass
1 parent a7fed25 commit 640e050

File tree

132 files changed

+1576
-1592
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

132 files changed

+1576
-1592
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
- chore: Update Composer deps and lint.
77
- chore: Lock WPBrowser to <3.5.0 to prevent conflicts with Codeception.
88
- chore: Implement strict PHPStan rules and fix resulting issues.
9+
- chore: Add traversable type hints throughout the codebase.
10+
- tests: Lint tests.
911
- ci: Update GitHub Actions to latest versions.
1012
- ci: Test plugin compatibility with WordPress 6.5.0.
1113
- ci: Test plugin compatibility with PHP 8.2.

src/CoreSchemaFilters.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ public static function register_hooks(): void {
3535
/**
3636
* Strip `Connection` interface from form fields.
3737
*
38-
* @param array $interfaces Array of interfaces.
39-
* @param array $config The type config.
38+
* @param string[] $interfaces Array of interfaces.
39+
* @param array<string,mixed> $config The type config.
40+
*
41+
* @return string[]
4042
*/
4143
public static function strip_connection_interface_from_gf_fields( array $interfaces, array $config ): array {
4244
// Bail early if Connection, 'Edge, or 'OneToOneConnection' arent in the interfaces.

src/Data/Connection/EntriesConnectionResolver.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function __construct( $source, array $args, AppContext $context, ResolveI
5353
}
5454

5555
/**
56-
* Return the name of the loader to be used with the connection resolver
56+
* {@inheritDoc}
5757
*/
5858
public function get_loader_name(): string {
5959
return EntriesLoader::$name;
@@ -220,6 +220,8 @@ public function get_query_offset_index(): int {
220220
* Gets index (array offset) and offset (entry id) from decoded cursor.
221221
*
222222
* @param string $cursor .
223+
*
224+
* @return array{index:int,offset:int}
223225
*/
224226
protected function parse_cursor( string $cursor ): array {
225227
$decoded = base64_decode( $cursor );
@@ -234,7 +236,7 @@ protected function parse_cursor( string $cursor ): array {
234236
/**
235237
* Returns form ids.
236238
*
237-
* @return array|int
239+
* @return int[]|int
238240
*/
239241
private function get_form_ids() {
240242
if ( empty( $this->args['where']['formIds'] ) ) {
@@ -250,6 +252,8 @@ private function get_form_ids() {
250252

251253
/**
252254
* Gets search criteria for entry Ids.
255+
*
256+
* @return array<string,mixed>
253257
*/
254258
private function get_search_criteria(): array {
255259
$search_criteria = $this->apply_status_to_search_criteria( [] );
@@ -275,7 +279,9 @@ private function get_search_criteria(): array {
275279
/**
276280
* Adds 'status' value to search criteria.
277281
*
278-
* @param array $search_criteria The search criteria for the entry Ids.
282+
* @param array<string,mixed> $search_criteria The search criteria for the entry Ids.
283+
*
284+
* @return array<string,mixed>
279285
*/
280286
private function apply_status_to_search_criteria( array $search_criteria ): array {
281287
$status = $this->args['where']['status'] ?? EntryStatusEnum::ACTIVE; // Default to active entries.
@@ -372,6 +378,8 @@ static function ( $value_field ) use ( $field_filter ): bool {
372378

373379
/**
374380
* Get sort argument for entry ID query.
381+
*
382+
* @return array<string,mixed>
375383
*/
376384
private function get_sort(): array {
377385
// Set default sort direction.
@@ -398,6 +406,8 @@ private function get_sort(): array {
398406
/**
399407
* Get paging arguments for entry ID query.
400408
*
409+
* @return array{offset:int,page_size:int}
410+
*
401411
* @throws \GraphQL\Error\UserError When using unsupported pagination.
402412
*/
403413
private function get_paging(): array {

src/Data/Connection/FormFieldsConnectionResolver.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ class FormFieldsConnectionResolver {
3333
*
3434
* Instead of a Model.
3535
*
36-
* @param array $data array of form fields.
36+
* @param \GF_Field[] $data array of form fields.
37+
*
38+
* @return \GF_Field[]
3739
*/
3840
public static function prepare_data( array $data ): array {
3941
foreach ( $data as &$field ) {
@@ -73,11 +75,11 @@ public static function prepare_data( array $data ): array {
7375
* The connection resolve method.
7476
*
7577
* @param mixed $source The object the connection is coming from.
76-
* @param array $args Array of args to be passed down to the resolve method.
78+
* @param array<string,mixed> $args Array of args to be passed down to the resolve method.
7779
* @param \WPGraphQL\AppContext $context The AppContext object to be passed down.
7880
* @param \GraphQL\Type\Definition\ResolveInfo $info The ResolveInfo object.
7981
*
80-
* @return mixed|array|\WPGraphQL\GF\Data\Connection\Deferred
82+
* @return ?array<string,mixed>
8183
*/
8284
public static function resolve( $source, array $args, AppContext $context, ResolveInfo $info ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
8385
if ( ! is_array( $source ) || empty( $source ) ) {
@@ -104,6 +106,8 @@ public static function resolve( $source, array $args, AppContext $context, Resol
104106

105107
/**
106108
* Returns input keys for Address field.
109+
*
110+
* @return string[]
107111
*/
108112
private static function get_address_input_keys(): array {
109113
return [
@@ -118,6 +122,8 @@ private static function get_address_input_keys(): array {
118122

119123
/**
120124
* Returns input keys for Name field.
125+
*
126+
* @return string[]
121127
*/
122128
private static function get_name_input_keys(): array {
123129
return [
@@ -132,8 +138,10 @@ private static function get_name_input_keys(): array {
132138
/**
133139
* Filters the form fields by the connection's where args.
134140
*
135-
* @param array $fields .
136-
* @param array $args .
141+
* @param \GF_Field[] $fields .
142+
* @param array<string,mixed> $args .
143+
*
144+
* @return \GF_Field[]
137145
*/
138146
private static function filter_form_fields_by_connection_args( $fields, $args ): array {
139147
if ( isset( $args['where']['ids'] ) ) {

src/Data/Connection/FormsConnectionResolver.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function get_query_args(): array {
9393
/**
9494
* {@inheritDoc}
9595
*
96-
* @return array
96+
* @return array<int|string,array<string,mixed>>
9797
*/
9898
public function get_query(): array {
9999
$form_ids = $this->query_args['form_ids'];
@@ -118,7 +118,7 @@ public function get_query(): array {
118118
* client. This hook is somewhat similar to Gravity Forms' gform_pre_render hook
119119
* and can be used for dynamic field input population, among other things.
120120
*
121-
* @param array $form Form meta array.
121+
* @param array<string,mixed> $form Form meta array.
122122
*/
123123
$modified_form = apply_filters( 'graphql_gf_form_object', $form );
124124

@@ -147,6 +147,8 @@ public function get_ids_from_query() {
147147

148148
/**
149149
* Returns form ids.
150+
*
151+
* @return int[]
150152
*/
151153
private function get_form_ids(): array {
152154
if ( empty( $this->args['where']['formIds'] ) ) {
@@ -162,6 +164,8 @@ private function get_form_ids(): array {
162164

163165
/**
164166
* Gets form status from query.
167+
*
168+
* @return array{active:bool,trash:bool}
165169
*/
166170
private function get_form_status(): array {
167171
$status = $this->args['where']['status'] ?? FormStatusEnum::ACTIVE;
@@ -196,6 +200,8 @@ private function get_form_status(): array {
196200
/**
197201
* Get sort argument for forms ID query.
198202
*
203+
* @return array{key:string,direction:string}
204+
*
199205
* @throws \GraphQL\Error\UserError .
200206
*/
201207
private function get_sort(): array {

src/Data/EntryObjectMutation.php

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ class EntryObjectMutation {
2121
/**
2222
* Returns the FieldValueInput object relative to the field type.
2323
*
24-
* @param array $args The GraphQL mutation input args for the field.
25-
* @param array $form The GF form object.
26-
* @param bool $is_draft If the mutation is for a draft entry.
27-
* @param array $entry The GF entry object. Used when updating.
24+
* @param array<string,mixed> $args The GraphQL mutation input args for the field.
25+
* @param array<string,mixed> $form The GF form object.
26+
* @param bool $is_draft If the mutation is for a draft entry.
27+
* @param array<int|string,mixed> $entry The GF entry object. Used when updating.
2828
*
2929
* @throws \Exception .
3030
*/
@@ -92,12 +92,12 @@ public static function get_field_value_input( array $args, array $form, bool $is
9292
*
9393
* Useful for adding mutation support for custom fields.
9494
*
95-
* @param string $field_value_input_class The FieldValueInput class to use. The referenced class must extend AbstractFieldValueInput.
96-
* @param array $args The GraphQL input args for the form field.
97-
* @param \GF_Field $field The current Gravity Forms field object.
98-
* @param array $form The current Gravity Forms form object.
99-
* @param array|null $entry The current Gravity Forms entry object. Only available when using update (`gfUpdateEntry`, `gfUpdateDraftEntry`) mutations.
100-
* @param bool $is_draft_mutation Whether the mutation is handling a Draft Entry (`gfUpdateDraftEntry`, or `gfSubmitForm` when `saveAsDraft` is `true`).
95+
* @param string $field_value_input_class The FieldValueInput class to use. The referenced class must extend AbstractFieldValueInput.
96+
* @param array $args The GraphQL input args for the form field.
97+
* @param \GF_Field $field The current Gravity Forms field object.
98+
* @param array<string,mixed> $form The current Gravity Forms form object.
99+
* @param array|null $entry The current Gravity Forms entry object. Only available when using update (`gfUpdateEntry`, `gfUpdateDraftEntry`) mutations.
100+
* @param bool $is_draft_mutation Whether the mutation is handling a Draft Entry (`gfUpdateDraftEntry`, or `gfSubmitForm` when `saveAsDraft` is `true`).
101101
*/
102102
$field_value_input = apply_filters( 'graphql_gf_field_value_input_class', $field_value_input, $args, $field, $form, $entry, $is_draft );
103103

@@ -111,7 +111,9 @@ public static function get_field_value_input( array $args, array $form, bool $is
111111
/**
112112
* Generates array of field errors from the submission.
113113
*
114-
* @param array $messages The Gravity Forms submission validation messages.
114+
* @param array<int|string,string> $messages The Gravity Forms submission validation messages.
115+
*
116+
* @return array{message:string,id:int|string}[]
115117
*/
116118
public static function get_submission_errors( array $messages ): array {
117119
return array_map(
@@ -129,7 +131,9 @@ static function ( $id, $message ): array {
129131
/**
130132
* Gets the submission confirmation information in an array formated for WPGraphQL.
131133
*
132-
* @param array $payload the submission response.
134+
* @param array<string,mixed> $payload the submission response.
135+
*
136+
* @return ?array{type:string,message:?string,url:?string}
133137
*/
134138
public static function get_submission_confirmation( array $payload ): ?array {
135139
if ( empty( $payload['confirmation_type'] ) ) {
@@ -146,13 +150,15 @@ public static function get_submission_confirmation( array $payload ): ?array {
146150
/**
147151
* Renames $field_value keys to input_{id}_{sub_id}, so Gravity Forms can read them.
148152
*
149-
* @param array $field_values .
153+
* @param array<int|string,mixed> $field_values .
154+
*
155+
* @return array<string,mixed> $formatted .
150156
* */
151157
public static function rename_field_names_for_submission( array $field_values ): array {
152158
$formatted = [];
153159

154160
foreach ( $field_values as $key => $value ) {
155-
$formatted[ 'input_' . str_replace( '.', '_', $key ) ] = $value;
161+
$formatted[ 'input_' . str_replace( '.', '_', (string) $key ) ] = $value;
156162
}
157163

158164
return $formatted;
@@ -162,9 +168,11 @@ public static function rename_field_names_for_submission( array $field_values ):
162168
* Initializes the globals needed for file uploads to work.
163169
* This prevents any notices about missing array keys.
164170
*
165-
* @param \GF_Field[] $form_fields .
166-
* @param array $input_field_values .
167-
* @param bool $save_as_draft .
171+
* @param \GF_Field[] $form_fields .
172+
* @param array<string,mixed>[] $input_field_values .
173+
* @param bool $save_as_draft .
174+
*
175+
* @return array<string,array<string,mixed>[]>
168176
*
169177
* @throws \GraphQL\Error\UserError .
170178
*/

src/Data/Factory.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ class Factory {
2727
/**
2828
* Registers loaders to AppContext.
2929
*
30-
* @param array $loaders Data loaders.
31-
* @param \WPGraphQL\AppContext $context App context.
30+
* @param array<string,\WPGraphQL\Data\Loader\AbstractDataLoader> $loaders Data loaders.
31+
* @param \WPGraphQL\AppContext $context App context.
3232
*
33-
* @return array Data loaders, with new ones added.
33+
* @return array<string,\WPGraphQL\Data\Loader\AbstractDataLoader> Data loaders.
3434
*/
3535
public static function register_loaders( array $loaders, AppContext $context ): array {
3636
$loaders[ DraftEntriesLoader::$name ] = new DraftEntriesLoader( $context );
@@ -69,7 +69,7 @@ public static function resolve_node_type( $type, $node ) {
6969
*
7070
* @param int $max_query_amount Max query amount.
7171
* @param mixed $source source passed down from the resolve tree.
72-
* @param array $args array of arguments input in the field as part of the GraphQL query.
72+
* @param array<string,mixed> $args array of arguments input in the field as part of the GraphQL query.
7373
* @param \WPGraphQL\AppContext $context Object containing app context that gets passed down the resolve tree.
7474
* @param \GraphQL\Type\Definition\ResolveInfo $info Info about fields passed down the resolve tree.
7575
*
@@ -97,11 +97,11 @@ public static function resolve_form( $id, AppContext $context ): ?Deferred {
9797
* Wrapper for the FormsConnectionResolver::resolve method.
9898
*
9999
* @param mixed $source The object the connection is coming from.
100-
* @param array $args Array of args to be passed down to the resolve method.
100+
* @param array<string,mixed> $args Array of args to be passed down to the resolve method.
101101
* @param \WPGraphQL\AppContext $context The AppContext object to be passed down.
102102
* @param \GraphQL\Type\Definition\ResolveInfo $info The ResolveInfo object.
103103
*
104-
* @return mixed|array|\GraphQL\Deferred
104+
* @return \GraphQL\Deferred
105105
*/
106106
public static function resolve_forms_connection( $source, array $args, AppContext $context, ResolveInfo $info ) {
107107
$resolver = new FormsConnectionResolver( $source, $args, $context, $info );
@@ -133,11 +133,11 @@ public static function resolve_entry( $id, AppContext $context ): ?Deferred {
133133
* Wrapper for the EntriesConnectionResolver::resolve method.
134134
*
135135
* @param mixed $source The object the connection is coming from.
136-
* @param array $args Array of args to be passed down to the resolve method.
136+
* @param array<string,mixed> $args Array of args to be passed down to the resolve method.
137137
* @param \WPGraphQL\AppContext $context The AppContext object to be passed down.
138138
* @param \GraphQL\Type\Definition\ResolveInfo $info The ResolveInfo object.
139139
*
140-
* @return mixed|array|\GraphQL\Deferred
140+
* @return \GraphQL\Deferred
141141
*/
142142
public static function resolve_entries_connection( $source, array $args, AppContext $context, ResolveInfo $info ) {
143143
$resolver = new EntriesConnectionResolver( $source, $args, $context, $info );

src/Data/FieldValueInput/AbstractFieldValueInput.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ abstract class AbstractFieldValueInput {
5454
/**
5555
* The GraphQL input args passed to `fieldValues`.
5656
*
57-
* @var array
57+
* @var array<string,mixed>
5858
*/
5959
protected array $input_args;
6060

@@ -75,11 +75,11 @@ abstract class AbstractFieldValueInput {
7575
/**
7676
* The class constructor.
7777
*
78-
* @param array $input_args The GraphQL input args for the form field.
79-
* @param array<string,mixed> $form The current Gravity Forms form object.
80-
* @param bool $is_draft Whether the mutation is handling a Draft Entry.
81-
* @param \GF_Field $field The current Gravity Forms field object.
82-
* @param array<int|string,mixed>|null $entry The current Gravity Forms entry object.
78+
* @param array<string,mixed> $input_args The GraphQL input args for the form field.
79+
* @param array<string,mixed> $form The current Gravity Forms form object.
80+
* @param bool $is_draft Whether the mutation is handling a Draft Entry.
81+
* @param \GF_Field $field The current Gravity Forms field object.
82+
* @param array<int|string,mixed>|null $entry The current Gravity Forms entry object.
8383
*
8484
* @throws \GraphQL\Error\UserError .
8585
*/
@@ -122,12 +122,12 @@ public function __construct( array $input_args, array $form, bool $is_draft, ?GF
122122
/**
123123
* Filters the GraphQL input args for the field value input.
124124
*
125-
* @param array|string $args field value input args.
126-
* @param \GF_Field $field The current Gravity Forms field object.
127-
* @param array $form The current Gravity Forms form object.
128-
* @param array|null $entry The current Gravity Forms entry object. Only available when using update (`gfUpdateEntry`, `gfUpdateDraftEntry`) mutations.
129-
* @param bool $is_draft_mutation Whether the mutation is handling a Draft Entry (`gfUpdateDraftEntry`, or `gfSubmitForm` when `saveAsDraft` is `true`).
130-
* @param string $field_name The GraphQL input field name. E.g. `nameValues`.
125+
* @param array|string $args Field value input args.
126+
* @param \GF_Field $field The current Gravity Forms field object.
127+
* @param array<string,mixed> $form The current Gravity Forms form object.
128+
* @param array|null $entry The current Gravity Forms entry object. Only available when using update (`gfUpdateEntry`, `gfUpdateDraftEntry`) mutations.
129+
* @param bool $is_draft_mutation Whether the mutation is handling a Draft Entry (`gfUpdateDraftEntry`, or `gfSubmitForm` when `saveAsDraft` is `true`).
130+
* @param string $field_name The GraphQL input field name. E.g. `nameValues`.
131131
*/
132132
$this->args = apply_filters(
133133
'graphql_gf_field_value_input_args',
@@ -142,13 +142,13 @@ public function __construct( array $input_args, array $form, bool $is_draft, ?GF
142142
/**
143143
* Filters the prepared field value to be submitted to Gravity Forms.
144144
*
145-
* @param array|string $prepared_field_value The field value formatted in a way Gravity Forms can understand.
146-
* @param array|string $args field value input args.
147-
* @param \GF_Field $field The current Gravity Forms field object.
148-
* @param array $form The current Gravity Forms form object.
149-
* @param array|null $entry The current Gravity Forms entry object. Only available when using update (`gfUpdateEntry`, `gfUpdateDraftEntry`) mutations.
150-
* @param bool $is_draft_mutation Whether the mutation is handling a Draft Entry (`gfUpdateDraftEntry`, or `gfSubmitForm` when `saveAsDraft` is `true`).
151-
* @param string $field_name The GraphQL input field name. E.g. `nameValues`.
145+
* @param array|string $prepared_field_value The field value formatted in a way Gravity Forms can understand.
146+
* @param array|string $args Field value input args.
147+
* @param \GF_Field $field The current Gravity Forms field object.
148+
* @param array<string,mixed> $form The current Gravity Forms form object.
149+
* @param array|null $entry The current Gravity Forms entry object. Only available when using update (`gfUpdateEntry`, `gfUpdateDraftEntry`) mutations.
150+
* @param bool $is_draft_mutation Whether the mutation is handling a Draft Entry (`gfUpdateDraftEntry`, or `gfSubmitForm` when `saveAsDraft` is `true`).
151+
* @param string $field_name The GraphQL input field name. E.g. `nameValues`.
152152
*/
153153
$this->value = apply_filters(
154154
'graphql_gf_field_value_input_prepared_value',

0 commit comments

Comments
 (0)