Skip to content

Commit e15d155

Browse files
committed
Fix type issues identified by PHPStan rule level 10
1 parent 2307bf3 commit e15d155

2 files changed

Lines changed: 49 additions & 36 deletions

File tree

src/wp-includes/class-wp-connector-registry.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
*
3030
* @phpstan-type Connector array{
3131
* name: non-empty-string,
32-
* description: non-empty-string,
32+
* description: string,
3333
* logo_url?: non-empty-string,
3434
* type: non-empty-string,
3535
* authentication: array{
@@ -120,7 +120,23 @@ final class WP_Connector_Registry {
120120
* }
121121
* @return array|null The registered connector data on success, null on failure.
122122
*
123-
* @phpstan-param Connector $args
123+
* @phpstan-param array{
124+
* name: non-empty-string,
125+
* description?: string,
126+
* logo_url?: non-empty-string,
127+
* type: non-empty-string,
128+
* authentication: array{
129+
* method: 'api_key'|'none',
130+
* credentials_url?: non-empty-string,
131+
* setting_name?: non-empty-string,
132+
* constant_name?: non-empty-string,
133+
* env_var_name?: non-empty-string
134+
* },
135+
* plugin?: array{
136+
* file?: non-empty-string,
137+
* is_active?: callable(): bool
138+
* }
139+
* } $args
124140
* @phpstan-return Connector|null
125141
*/
126142
public function register( string $id, array $args ): ?array {

src/wp-includes/connectors.php

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ function wp_is_connector_registered( string $id ): bool {
6464
* }
6565
* @phpstan-return ?array{
6666
* name: non-empty-string,
67-
* description: non-empty-string,
67+
* description: string,
6868
* logo_url?: non-empty-string,
6969
* type: non-empty-string,
7070
* authentication: array{
@@ -75,7 +75,8 @@ function wp_is_connector_registered( string $id ): bool {
7575
* env_var_name?: non-empty-string
7676
* },
7777
* plugin?: array{
78-
* file: non-empty-string
78+
* file?: non-empty-string,
79+
* is_active: callable(): bool,
7980
* }
8081
* }
8182
*/
@@ -126,7 +127,7 @@ function wp_get_connector( string $id ): ?array {
126127
* }
127128
* @phpstan-return array<string, array{
128129
* name: non-empty-string,
129-
* description: non-empty-string,
130+
* description: string,
130131
* logo_url?: non-empty-string,
131132
* type: non-empty-string,
132133
* authentication: array{
@@ -137,7 +138,8 @@ function wp_get_connector( string $id ): ?array {
137138
* env_var_name?: non-empty-string
138139
* },
139140
* plugin?: array{
140-
* file: non-empty-string
141+
* file?: non-empty-string,
142+
* is_active: callable(): bool,
141143
* }
142144
* }>
143145
*/
@@ -160,7 +162,7 @@ function wp_get_connectors(): array {
160162
* @access private
161163
*
162164
* @param string $path Absolute path to the logo file.
163-
* @return string|null The URL to the logo file, or null if the path is invalid.
165+
* @return non-empty-string|null The URL to the logo file, or null if the path is invalid.
164166
*/
165167
function _wp_connectors_resolve_ai_provider_logo_url( string $path ): ?string {
166168
if ( ! $path ) {
@@ -175,12 +177,14 @@ function _wp_connectors_resolve_ai_provider_logo_url( string $path ): ?string {
175177

176178
$mu_plugin_dir = wp_normalize_path( WPMU_PLUGIN_DIR );
177179
if ( str_starts_with( $path, $mu_plugin_dir . '/' ) ) {
178-
return plugins_url( substr( $path, strlen( $mu_plugin_dir ) ), WPMU_PLUGIN_DIR . '/.' );
180+
$logo_url = plugins_url( substr( $path, strlen( $mu_plugin_dir ) ), WPMU_PLUGIN_DIR . '/.' );
181+
return $logo_url ? $logo_url : null;
179182
}
180183

181184
$plugin_dir = wp_normalize_path( WP_PLUGIN_DIR );
182185
if ( str_starts_with( $path, $plugin_dir . '/' ) ) {
183-
return plugins_url( substr( $path, strlen( $plugin_dir ) ) );
186+
$logo_url = plugins_url( substr( $path, strlen( $plugin_dir ) ) );
187+
return $logo_url ? $logo_url : null;
184188
}
185189

186190
_doing_it_wrong(
@@ -295,7 +299,7 @@ function _wp_connectors_register_default_ai_providers( WP_Connector_Registry $re
295299
// Registry values (from provider plugins) take precedence over hardcoded fallbacks.
296300
$ai_registry = AiClient::defaultRegistry();
297301

298-
foreach ( $ai_registry->getRegisteredProviderIds() as $connector_id ) {
302+
foreach ( array_filter( $ai_registry->getRegisteredProviderIds() ) as $connector_id ) {
299303
$provider_class_name = $ai_registry->getProviderClassName( $connector_id );
300304
$provider_metadata = $provider_class_name::metadata();
301305

@@ -305,9 +309,11 @@ function _wp_connectors_register_default_ai_providers( WP_Connector_Registry $re
305309
if ( $is_api_key ) {
306310
$credentials_url = $provider_metadata->getCredentialsUrl();
307311
$authentication = array(
308-
'method' => 'api_key',
309-
'credentials_url' => $credentials_url ? $credentials_url : null,
312+
'method' => 'api_key',
310313
);
314+
if ( $credentials_url ) {
315+
$authentication['credentials_url'] = $credentials_url;
316+
}
311317
} else {
312318
$authentication = array( 'method' => 'none' );
313319
}
@@ -340,8 +346,10 @@ function _wp_connectors_register_default_ai_providers( WP_Connector_Registry $re
340346
'description' => $description ? $description : '',
341347
'type' => 'ai_provider',
342348
'authentication' => $authentication,
343-
'logo_url' => $logo_url,
344349
);
350+
if ( $logo_url ) {
351+
$defaults[ $connector_id ]['logo_url'] = $logo_url;
352+
}
345353
}
346354
}
347355

@@ -350,33 +358,22 @@ function _wp_connectors_register_default_ai_providers( WP_Connector_Registry $re
350358
if ( 'api_key' === $args['authentication']['method'] ) {
351359
$sanitized_id = str_replace( '-', '_', $id );
352360

353-
if ( ! isset( $args['authentication']['setting_name'] ) ) {
354-
$args['authentication']['setting_name'] = "connectors_ai_{$sanitized_id}_api_key";
355-
}
361+
$args['authentication']['setting_name'] = "connectors_ai_{$sanitized_id}_api_key";
356362

357363
// All AI providers use the {CONSTANT_CASE_ID}_API_KEY naming convention.
358-
if ( ! isset( $args['authentication']['constant_name'] ) || ! isset( $args['authentication']['env_var_name'] ) ) {
359-
$constant_case_key = strtoupper( preg_replace( '/([a-z])([A-Z])/', '$1_$2', $sanitized_id ) ) . '_API_KEY';
360-
361-
if ( ! isset( $args['authentication']['constant_name'] ) ) {
362-
$args['authentication']['constant_name'] = $constant_case_key;
363-
}
364+
$constant_case_key = strtoupper( (string) preg_replace( '/([a-z])([A-Z])/', '$1_$2', $sanitized_id ) ) . '_API_KEY';
364365

365-
if ( ! isset( $args['authentication']['env_var_name'] ) ) {
366-
$args['authentication']['env_var_name'] = $constant_case_key;
367-
}
368-
}
366+
$args['authentication']['constant_name'] = $constant_case_key;
367+
$args['authentication']['env_var_name'] = $constant_case_key;
369368
}
370369

371-
if ( ! isset( $args['plugin']['is_active'] ) ) {
372-
$args['plugin']['is_active'] = static function () use ( $ai_registry, $id ): bool {
373-
try {
374-
return $ai_registry->hasProvider( $id );
375-
} catch ( Exception $e ) {
376-
return false;
377-
}
378-
};
379-
}
370+
$args['plugin']['is_active'] = static function () use ( $ai_registry, $id ): bool {
371+
try {
372+
return $ai_registry->hasProvider( $id );
373+
} catch ( Exception $e ) {
374+
return false;
375+
}
376+
};
380377

381378
$registry->register( $id, $args );
382379
}
@@ -624,7 +621,7 @@ function _wp_connectors_pass_default_keys_to_ai_client(): void {
624621
}
625622

626623
$api_key = get_option( $auth['setting_name'], '' );
627-
if ( '' === $api_key ) {
624+
if ( ! is_string( $api_key ) || '' === $api_key ) {
628625
continue;
629626
}
630627

0 commit comments

Comments
 (0)