Skip to content

Commit f663eba

Browse files
committed
chore: narrow and fix PHPStan doc types in WPGraphQL + Utils namespaces [first pass]
1 parent faa1353 commit f663eba

File tree

13 files changed

+242
-177
lines changed

13 files changed

+242
-177
lines changed

access-functions.php

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
function graphql_format_name( string $name, string $replacement = '_', string $regex = '/[^A-Za-z0-9_]/i' ): string {
3030
return Utils::format_graphql_name( $name, $replacement, $regex );
3131
}
32+
3233
/**
3334
* Formats the name of a field so that it plays nice with GraphiQL
3435
*
@@ -89,6 +90,8 @@ function graphql_format_type_name( $type_name ): string {
8990
* @param bool $return_request If true, return the Request object, else return the results of the request execution
9091
*
9192
* @return array<string,mixed>|\WPGraphQL\Request
93+
* @phpstan-return ( $return_request is true ? \WPGraphQL\Request : array<string,mixed> )
94+
*
9295
* @throws \Exception
9396
* @since 0.2.0
9497
*/
@@ -114,6 +117,9 @@ function graphql( array $request_data = [], bool $return_request = false ) {
114117
* @param bool $return_request If true, return the Request object, else return the results of the request execution
115118
*
116119
* @return array<string,mixed>|\WPGraphQL\Request
120+
*
121+
* @phpstan-return ( $return_request is true ? \WPGraphQL\Request : array<string,mixed> )
122+
*
117123
* @throws \Exception
118124
* @since 0.0.2
119125
*/
@@ -129,7 +135,9 @@ function do_graphql_request( $query, $operation_name = '', $variables = [], $ret
129135
}
130136

131137
/**
132-
* Determine when to register types
138+
* Determine when to register types.
139+
*
140+
* @return 'graphql_register_initial_types'|'graphql_register_types'|'graphql_register_types_late'
133141
*/
134142
function get_graphql_register_action(): string {
135143
$action = 'graphql_register_types_late';
@@ -157,6 +165,11 @@ function get_graphql_register_action(): string {
157165
* register_graphql_interfaces_to_types( [ 'MyNewInterface' ], [ 'Post', 'Page' ] );
158166
*/
159167
function register_graphql_interfaces_to_types( $interface_names, $type_names ): void {
168+
// Bail if no interfaces or types.
169+
if ( empty( $type_names ) || empty( $interface_names ) ) {
170+
return;
171+
}
172+
160173
if ( is_string( $type_names ) ) {
161174
$type_names = [ $type_names ];
162175
}
@@ -165,25 +178,27 @@ function register_graphql_interfaces_to_types( $interface_names, $type_names ):
165178
$interface_names = [ $interface_names ];
166179
}
167180

168-
if ( ! empty( $type_names ) && is_array( $type_names ) && ! empty( $interface_names ) && is_array( $interface_names ) ) {
169-
foreach ( $type_names as $type_name ) {
170-
171-
// Filter the GraphQL Object Type Interface to apply the interface
172-
add_filter(
173-
'graphql_type_interfaces',
174-
static function ( $interfaces, $config ) use ( $type_name, $interface_names ) {
175-
$interfaces = is_array( $interfaces ) ? $interfaces : [];
176-
177-
if ( strtolower( $type_name ) === strtolower( $config['name'] ) ) {
178-
$interfaces = array_unique( array_merge( $interfaces, $interface_names ) );
179-
}
181+
// Bail if they're still not arrays.
182+
if ( ! is_array( $type_names ) || ! is_array( $interface_names ) ) {
183+
return;
184+
}
180185

181-
return $interfaces;
182-
},
183-
10,
184-
2
185-
);
186-
}
186+
foreach ( $type_names as $type_name ) {
187+
// Filter the GraphQL Object Type Interface to apply the interface
188+
add_filter(
189+
'graphql_type_interfaces',
190+
static function ( $interfaces, $config ) use ( $type_name, $interface_names ) {
191+
$interfaces = is_array( $interfaces ) ? $interfaces : [];
192+
193+
if ( strtolower( $type_name ) === strtolower( $config['name'] ) ) {
194+
$interfaces = array_unique( array_merge( $interfaces, $interface_names ) );
195+
}
196+
197+
return $interfaces;
198+
},
199+
10,
200+
2
201+
);
187202
}
188203
}
189204

@@ -192,8 +207,6 @@ static function ( $interfaces, $config ) use ( $type_name, $interface_names ) {
192207
*
193208
* @param string $type_name The name of the Type to register
194209
* @param array<string,mixed> $config The Type config
195-
*
196-
* @throws \Exception
197210
*/
198211
function register_graphql_type( string $type_name, array $config ): void {
199212
add_action(
@@ -210,8 +223,6 @@ static function ( TypeRegistry $type_registry ) use ( $type_name, $config ): voi
210223
*
211224
* @param string $type_name The name of the Type to register
212225
* @param mixed|array<string,mixed>|\GraphQL\Type\Definition\Type $config The Type config
213-
*
214-
* @throws \Exception
215226
*/
216227
function register_graphql_interface_type( string $type_name, $config ): void {
217228
add_action(
@@ -484,8 +495,6 @@ static function ( TypeRegistry $type_registry ) use ( $type_name, $new_type_name
484495
*
485496
* @param array<string,mixed> $config Array to configure the connection
486497
*
487-
* @throws \Exception
488-
*
489498
* @since 0.1.0
490499
*/
491500
function register_graphql_connection( array $config ): void {
@@ -504,8 +513,6 @@ static function ( TypeRegistry $type_registry ) use ( $config ): void {
504513
* @param string $mutation_name The name of the Mutation to register
505514
* @param array<string,mixed> $config The config for the mutation
506515
*
507-
* @throws \Exception
508-
*
509516
* @since 0.1.0
510517
*/
511518
function register_graphql_mutation( string $mutation_name, array $config ): void {
@@ -524,8 +531,6 @@ static function ( TypeRegistry $type_registry ) use ( $mutation_name, $config ):
524531
* @param string $type_name The name of the Type to register
525532
* @param array<string,mixed> $config The config for the scalar type to register
526533
*
527-
* @throws \Exception
528-
*
529534
* @since 0.8.4
530535
*/
531536
function register_graphql_scalar( string $type_name, array $config ): void {

src/AppContext.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
*
2929
* @package WPGraphQL
3030
*/
31-
// @phpcs:ignore
3231
#[\AllowDynamicProperties]
3332
class AppContext {
3433

@@ -129,15 +128,13 @@ public function __construct() {
129128
* added to the AppContext or for existing loaders to be replaced if
130129
* needed.
131130
*
132-
* @params array $loaders The loaders accessible in the AppContext
133-
* @params AppContext $this The AppContext
131+
* @param array<string,\WPGraphQL\Data\Loader\AbstractDataLoader> $loaders The loaders accessible in the AppContext
132+
* @param \WPGraphQL\AppContext $context The AppContext
134133
*/
135134
$this->loaders = apply_filters( 'graphql_data_loaders', $loaders, $this );
136135

137136
/**
138137
* This sets up the NodeResolver to allow nodes to be resolved by URI
139-
*
140-
* @param \WPGraphQL\AppContext $app_context The AppContext instance
141138
*/
142139
$this->node_resolver = new NodeResolver( $this );
143140

@@ -147,7 +144,7 @@ public function __construct() {
147144
* This can be used to store additional context config, which is available to resolvers
148145
* throughout the resolution of a GraphQL request.
149146
*
150-
* @params array $config The config array of the AppContext object
147+
* @param mixed[] $config The config array of the AppContext object
151148
*/
152149
$this->config = apply_filters( 'graphql_app_context_config', $this->config );
153150
}
@@ -157,7 +154,7 @@ public function __construct() {
157154
*
158155
* @param string $key The name of the loader to get
159156
*
160-
* @return \WPGraphQL\Data\Loader\AbstractDataLoader|mixed
157+
* @return \WPGraphQL\Data\Loader\AbstractDataLoader
161158
*
162159
* @deprecated Use get_loader instead.
163160
*/
@@ -171,7 +168,7 @@ public function getLoader( $key ) {
171168
*
172169
* @param string $key The name of the loader to get
173170
*
174-
* @return \WPGraphQL\Data\Loader\AbstractDataLoader|mixed
171+
* @return \WPGraphQL\Data\Loader\AbstractDataLoader
175172
* @throws \GraphQL\Error\UserError If the loader is not found.
176173
*/
177174
public function get_loader( $key ) {

src/Data/Connection/AbstractConnectionResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,7 @@ protected function get_pre_should_execute( $source, array $args, AppContext $con
880880
*
881881
* @param bool $should_execute Whether or not the query should execute.
882882
* @param mixed $source The source that's passed down the GraphQL queries.
883-
* @param array $args The inputArgs on the field.
883+
* @param array<string,mixed> $args The inputArgs on the field.
884884
* @param \WPGraphQL\AppContext $context The AppContext passed down the GraphQL tree.
885885
* @param \GraphQL\Type\Definition\ResolveInfo $info The ResolveInfo passed down the GraphQL tree.
886886
*/

src/Data/DataSource.php

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ public static function resolve_node_type( $node ) {
659659
* @param \WPGraphQL\AppContext $context The Context of the GraphQL Request
660660
* @param \GraphQL\Type\Definition\ResolveInfo $info The ResolveInfo for the GraphQL Request
661661
*
662-
* @return string|null
662+
* @return ?\GraphQL\Deferred
663663
* @throws \GraphQL\Error\UserError If no ID is passed.
664664
*/
665665
public static function resolve_node( $global_id, AppContext $context, ResolveInfo $info ) {
@@ -675,29 +675,28 @@ public static function resolve_node( $global_id, AppContext $context, ResolveInf
675675
$id_components = Relay::fromGlobalId( $global_id );
676676

677677
/**
678-
* If the $id_components is a proper array with a type and id
678+
* $id_components is an array with the id and type
679679
*
680680
* @since 0.0.5
681681
*/
682-
if ( is_array( $id_components ) && ! empty( $id_components['id'] ) && ! empty( $id_components['type'] ) ) {
683-
684-
/**
685-
* Get the allowed_post_types and allowed_taxonomies
686-
*
687-
* @since 0.0.5
688-
*/
682+
if ( empty( $id_components['id'] ) || empty( $id_components['type'] ) ) {
683+
// translators: %s is the global ID.
684+
throw new UserError( esc_html( sprintf( __( 'The global ID isn\'t recognized ID: %s', 'wp-graphql' ), $global_id ) ) );
685+
}
689686

690-
$loader = $context->get_loader( $id_components['type'] );
687+
/**
688+
* Get the allowed_post_types and allowed_taxonomies
689+
*
690+
* @since 0.0.5
691+
*/
691692

692-
if ( $loader ) {
693-
return $loader->load_deferred( $id_components['id'] );
694-
}
693+
$loader = $context->get_loader( $id_components['type'] );
695694

696-
return null;
697-
} else {
698-
// translators: %s is the global ID.
699-
throw new UserError( esc_html( sprintf( __( 'The global ID isn\'t recognized ID: %s', 'wp-graphql' ), $global_id ) ) );
695+
if ( $loader ) {
696+
return $loader->load_deferred( $id_components['id'] );
700697
}
698+
699+
return null;
701700
}
702701

703702
/**

src/Data/Loader/AbstractDataLoader.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ public function __construct( AppContext $context ) {
6262
*
6363
* @return \GraphQL\Deferred|null
6464
* @throws \Exception
65+
*
66+
* @phpstan-return ($database_id is int|string ? \GraphQL\Deferred : null)
6567
*/
6668
public function load_deferred( $database_id ) {
6769
if ( empty( $database_id ) ) {

src/Request.php

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class Request {
3636
/**
3737
* Request data.
3838
*
39-
* @var mixed|array<string,mixed>|\GraphQL\Server\OperationParams
39+
* @var array<string,mixed>|\GraphQL\Server\OperationParams
4040
*/
4141
public $data;
4242

@@ -86,7 +86,7 @@ class Request {
8686
/**
8787
* Validation rules for execution.
8888
*
89-
* @var array<int|string,\GraphQL\Validator\Rules\ValidationRule>
89+
* @var array<string,\GraphQL\Validator\Rules\ValidationRule>
9090
*/
9191
protected $validation_rules;
9292

@@ -195,7 +195,7 @@ protected function get_field_resolver() {
195195
/**
196196
* Return the validation rules to use in the request
197197
*
198-
* @return array<int|string,\GraphQL\Validator\Rules\ValidationRule>
198+
* @return array<string,\GraphQL\Validator\Rules\ValidationRule>
199199
*/
200200
protected function get_validation_rules(): array {
201201
$validation_rules = GraphQL::getStandardValidationRules();
@@ -207,7 +207,7 @@ protected function get_validation_rules(): array {
207207
/**
208208
* Return the validation rules to use in the request
209209
*
210-
* @param array<int|string,\GraphQL\Validator\Rules\ValidationRule> $validation_rules The validation rules to use in the request
210+
* @param array<string,\GraphQL\Validator\Rules\ValidationRule> $validation_rules The validation rules to use in the request
211211
* @param \WPGraphQL\Request $request The Request instance
212212
*/
213213
return apply_filters( 'graphql_validation_rules', $validation_rules, $this );
@@ -222,7 +222,7 @@ protected function get_root_value() {
222222
/**
223223
* Set the root value based on what was passed to the request
224224
*/
225-
$root_value = isset( $this->data['root_value'] ) && ! empty( $this->data['root_value'] ) ? $this->data['root_value'] : null;
225+
$root_value = is_array( $this->data ) && ! empty( $this->data['root_value'] ) ? $this->data['root_value'] : null;
226226

227227
/**
228228
* Return the filtered root value
@@ -456,8 +456,8 @@ private function after_execute( $response ) {
456456
/**
457457
* Run an action after GraphQL Execution
458458
*
459-
* @param mixed[] $filtered_response The response of the entire operation. Could be a single operation or a batch operation
460-
* @param \WPGraphQL\Request $request Instance of the Request being executed
459+
* @param mixed[] $filtered_response The response of the entire operation. Could be a single operation or a batch operation
460+
* @param \WPGraphQL\Request $request Instance of the Request being executed
461461
*/
462462
do_action( 'graphql_after_execute', $filtered_response, $this );
463463

@@ -471,7 +471,7 @@ private function after_execute( $response ) {
471471
* Apply filters and do actions after GraphQL execution
472472
*
473473
* @param mixed|array<string,mixed>|object $response The response for your GraphQL request
474-
* @param mixed|int|null $key The array key of the params for batch requests
474+
* @param int|null $key The array key of the params for batch requests
475475
*
476476
* @return mixed|array<string,mixed>|object
477477
*/
@@ -581,11 +581,11 @@ private function do_action( OperationParams $params ): void {
581581
/**
582582
* Run an action for each request.
583583
*
584-
* @param ?string $query The GraphQL query
585-
* @param ?string $operation The name of the operation
586-
* @param ?array $variables Variables to be passed to your GraphQL request
587-
* @param \GraphQL\Server\OperationParams $params The Operation Params. This includes any extra params,
588-
* such as extensions or any other modifications to the request body
584+
* @param ?string $query The GraphQL query
585+
* @param ?string $operation The name of the operation
586+
* @param ?array<string,mixed> $variables Variables to be passed to your GraphQL request
587+
* @param \GraphQL\Server\OperationParams $params The Operation Params. This includes any extra params,
588+
* such as extensions or any other modifications to the request body
589589
*/
590590
do_action( 'do_graphql_request', $params->query, $params->operation, $params->variables, $params );
591591
}
@@ -751,23 +751,26 @@ private function get_content_type(): string {
751751
/**
752752
* Returns the error response for invalid content type
753753
*
754-
* @return array<string,mixed>
754+
* @return array{errors: array<int, array{message:string}>}
755755
*/
756756
private function get_invalid_content_type_response(): array {
757757
$content_type = $this->get_content_type();
758758

759759
/**
760760
* Filter the status code to return when the content type is invalid
761761
*
762-
* @param int $status_code The status code to return
763-
* @param string $content_type The content type header value that was received
762+
* @param int $status_code The status code to return. Default 415.
763+
* @param string $content_type The content type header value that was received.
764764
*/
765765
$filtered_status_code = apply_filters( 'graphql_invalid_content_type_status_code', 415, $content_type );
766766

767-
// validate that the status code is in valid http status code ranges (100-599)
768-
if ( is_numeric( $filtered_status_code ) && ( $filtered_status_code > 100 && $filtered_status_code < 599 ) ) {
769-
// Set status code to 415 (Unsupported Media Type)
770-
Router::$http_status_code = $filtered_status_code;
767+
// Set the status code to the filtered value if it's a valid status code.
768+
if ( is_numeric( $filtered_status_code ) ) {
769+
$filtered_status_code = (int) $filtered_status_code;
770+
771+
if ( $filtered_status_code > 100 && $filtered_status_code < 599 ) {
772+
Router::$http_status_code = $filtered_status_code;
773+
}
771774
}
772775

773776
return [

0 commit comments

Comments
 (0)