Skip to content

Commit

Permalink
PHPStan: re-enable bleedingEdge option (#13509)
Browse files Browse the repository at this point in the history
  • Loading branch information
swissspidy authored Jun 26, 2024
1 parent 8b8c84d commit 4cd3b17
Show file tree
Hide file tree
Showing 30 changed files with 161 additions and 277 deletions.
4 changes: 3 additions & 1 deletion includes/Admin/Customizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,9 @@ private function parse_args( array $args, array $defaults = [] ): array {
*
* @var array<string, array<string, array<string, array<int,string>|bool|int|string>>> $def
*/
$def = $parsed_args[ $key ];
$def = $parsed_args[ $key ];

// @phpstan-ignore argument.type (TODO: improve type for recursion)
$parsed_args[ $key ] = $this->parse_args( $value, $def );
} else {
$parsed_args[ $key ] = $value;
Expand Down
15 changes: 1 addition & 14 deletions includes/Infrastructure/Injector/SimpleInjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@
* @template T
*/
final class SimpleInjector implements Injector {

/**
* Special-case index key for handling globally defined named arguments.
*/
public const GLOBAL_ARGUMENTS = '__global__';

/**
* Mappings.
*
Expand Down Expand Up @@ -70,9 +64,7 @@ final class SimpleInjector implements Injector {
*
* @var array<string, array<mixed>>
*/
private array $argument_mappings = [
self::GLOBAL_ARGUMENTS => [],
];
private array $argument_mappings = [];

/**
* Instantiator.
Expand Down Expand Up @@ -429,11 +421,6 @@ private function resolve_argument_by_name(
return $value;
}

// No argument found for the class, check if we have a global value.
if ( \array_key_exists( $name, $this->argument_mappings[ self::GLOBAL_ARGUMENTS ] ) ) {
return $this->argument_mappings[ self::GLOBAL_ARGUMENTS ][ $name ];
}

// No provided argument found, check if it has a default value.
try {
if ( $parameter->isDefaultValueAvailable() ) {
Expand Down
22 changes: 17 additions & 5 deletions includes/Infrastructure/ServiceBasedPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,13 @@ public function register_services(): void {
while ( null !== key( $services ) ) {
$id = $this->maybe_resolve( key( $services ) );

$class = $this->maybe_resolve( current( $services ) );
$curr = current( $services );

if ( ! $curr ) {
continue;
}

$class = $this->maybe_resolve( $curr );

// Delay registering the service until all requirements are met.
if (
Expand Down Expand Up @@ -395,7 +401,7 @@ protected function get_registration_action_priority( string $class_name, array &
/**
* Missing requirement.
*
* @phpstan-var class-string<S> $missing_requirement
* @phpstan-var class-string<D&S> $missing_requirement
*/
$requirement_priority = $this->get_registration_action_priority( $missing_requirement, $services );

Expand Down Expand Up @@ -547,7 +553,7 @@ protected function collect_missing_requirements( string $class_name, array $serv
* @since 1.6.0
*
* @param array<int|string, string|class-string> $services Services to validate.
* @return string[] Validated array of service mappings.
* @return array<string, class-string<S>> Validated array of service mappings.
*/
protected function validate_services( array $services ): array {
// Make a copy so we can safely mutate while iterating.
Expand All @@ -564,11 +570,16 @@ protected function validate_services( array $services ): array {

// Verify that the FQCN is valid and points to an existing class.
// If not, skip this service.
if ( empty( $fqcn ) || ! \is_string( $fqcn ) || ! class_exists( $fqcn ) ) {
if ( empty( $fqcn ) || ! class_exists( $fqcn ) ) {
unset( $services[ $identifier ] );
}
}

/**
* Validated services.
*
* @phpstan-var array<string, class-string<S>> $services
*/
return $services;
}

Expand Down Expand Up @@ -686,6 +697,7 @@ protected function instantiate_service( $class_name ): Service {
// The service needs to be registered, so instantiate right away.
$service = $this->injector->make( $class_name );

// @phpstan-ignore instanceof.alwaysTrue
if ( ! $service instanceof Service ) {
throw InvalidService::from_service( $service ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
}
Expand Down Expand Up @@ -910,7 +922,7 @@ protected function get_delegations(): array {
* @phpstan-return class-string<C&D&H&R&S> Resolved or unchanged value.
*/
protected function maybe_resolve( $value ): string {
if ( \is_callable( $value ) && ! ( \is_string( $value ) && \function_exists( $value ) ) ) {
if ( ! ( \is_string( $value ) && \function_exists( $value ) ) && \is_callable( $value ) ) {
$value = $value( $this->injector, $this->service_container );
}

Expand Down
2 changes: 1 addition & 1 deletion includes/Integrations/AMP.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public static function get_requirements(): array {
* @param array|mixed $options Options.
* @return array|mixed Filtered options.
*
* @phpstan-param AMPOptions $options
* @phpstan-param AMPOptions|mixed $options
*
* @template T
*
Expand Down
2 changes: 1 addition & 1 deletion includes/Integrations/Jetpack.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ public function filter_ajax_query_attachments_args( $args ) {
* @param WP_Post $attachment Attachment object.
* @return array|mixed
*
* @phpstan-param AttachmentData $data
* @phpstan-param AttachmentData|mixed $data
* @phpstan-return AttachmentData|mixed
*
* @template T
Expand Down
4 changes: 3 additions & 1 deletion includes/Integrations/Site_Kit.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,14 @@ function (): void {
* @param array|mixed $gtag_opt Array of gtag configuration options.
* @return array|mixed Modified configuration options.
*
* @phpstan-param GtagOpt $gtag_opt
* @phpstan-param GtagOpt|mixed $gtag_opt
*/
public function filter_site_kit_gtag_opt( $gtag_opt ) {
if (
! \is_array( $gtag_opt ) ||
! \is_array( $gtag_opt['vars'] ) ||
! isset( $gtag_opt['vars']['gtag_id'] ) ||
! \is_string( $gtag_opt['vars']['gtag_id'] ) ||
! $this->context->is_web_story()
) {
return $gtag_opt;
Expand Down
14 changes: 10 additions & 4 deletions includes/KSES.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ public static function get_requirements(): array {
* originally passed to wp_insert_post().
* @return array<string,mixed>|mixed Filtered post data.
*
* @phpstan-param PostData $data
* @phpstan-param PostData $unsanitized_postarr
* @phpstan-param PostData|mixed $data
* @phpstan-param PostData|mixed $unsanitized_postarr
*
* @template T
*
Expand All @@ -135,11 +135,17 @@ public function filter_insert_post_data( $data, $postarr, $unsanitized_postarr )
return $data;
}

if ( ! $this->is_allowed_post_type( $data['post_type'], $data['post_parent'] ) ) {
if (
! \is_string( $data['post_type'] ) ||
! $this->is_allowed_post_type( $data['post_type'], $data['post_parent'] )
) {
return $data;
}

if ( isset( $unsanitized_postarr['post_content_filtered'] ) ) {
if (
isset( $unsanitized_postarr['post_content_filtered'] ) &&
\is_string( $unsanitized_postarr['post_content_filtered'] )
) {
$data['post_content_filtered'] = $this->filter_story_data( $unsanitized_postarr['post_content_filtered'] );
}

Expand Down
5 changes: 4 additions & 1 deletion includes/Media/Media_Source_Taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public function rest_api_init(): void {
*
* @since 1.0.0
*
* @param array|mixed $response Array of prepared attachment data.
* @param array<string, mixed>|mixed $response Array of prepared attachment data.
* @return array|mixed $response Filtered attachment data.
*
* @template T
Expand All @@ -184,6 +184,7 @@ public function wp_prepare_attachment_for_js( $response ) {
return $response;
}

// @phpstan-ignore argument.type (TODO: improve type)
$response[ self::MEDIA_SOURCE_KEY ] = $this->get_callback_media_source( $response );

return $response;
Expand Down Expand Up @@ -249,6 +250,7 @@ public function filter_ajax_query_attachments_args( $args ) {
return $args;
}

// @phpstan-ignore argument.type (TODO: improve type)
$args['tax_query'] = $this->get_exclude_tax_query( $args ); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query

return $args;
Expand Down Expand Up @@ -290,6 +292,7 @@ public function filter_rest_generated_media_attachments( $args ) {
return $args;
}

// @phpstan-ignore argument.type (TODO: improve type)
$args['tax_query'] = $this->get_exclude_tax_query( $args ); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query

return $args;
Expand Down
2 changes: 1 addition & 1 deletion includes/Media/Types.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function get_allowed_mime_types(): array {
* @var string $media_type
*/
foreach ( array_keys( $default_allowed_mime_types ) as $media_type ) {
if ( ! \is_array( $allowed_mime_types[ $media_type ] ) || empty( $allowed_mime_types[ $media_type ] ) ) {
if ( ! isset( $allowed_mime_types[ $media_type ] ) || empty( $allowed_mime_types[ $media_type ] ) ) {
$allowed_mime_types[ $media_type ] = $default_allowed_mime_types[ $media_type ];
}

Expand Down
4 changes: 2 additions & 2 deletions includes/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ class Plugin extends ServiceBasedPlugin {
*
* @since 1.6.0
*
* @return array<string> Associative array of identifiers mapped to fully
* qualified class names.
* @return array<string, string> Associative array of identifiers mapped to fully
* qualified class names.
*/
protected function get_service_classes(): array {
return self::SERVICES;
Expand Down
7 changes: 2 additions & 5 deletions includes/REST_API/Embed_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,8 @@ public function get_proxy_item( $request ) {
* @param array<string, mixed>|false $embed Embed value, default to false is not set.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response|WP_Error Response object.
*
* @phpstan-param WP_REST_Request<array{context: string}> $request
*/
public function prepare_item_for_response( $embed, $request ) {
$fields = $this->get_fields_for_response( $request );
Expand All @@ -267,11 +269,6 @@ public function prepare_item_for_response( $embed, $request ) {
}
}

/**
* Request context.
*
* @var string $context
*/
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
Expand Down
36 changes: 14 additions & 22 deletions includes/REST_API/Font_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ public function register_routes(): void {
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*
* @phpstan-param WP_REST_Request<array{search?: string, service?: string, include?: string[]}> $request
*/
public function get_items( $request ) {
/**
Expand All @@ -145,13 +147,7 @@ public function get_items( $request ) {

// For custom fonts the searching will be done in WP_Query already.
if ( isset( $registered['search'], $request['search'] ) && ! empty( $request['search'] ) ) {
/**
* Requested URL.
*
* @var string $search
*/
$search = $request['search'];
$fonts = array_values(
$fonts = array_values(
array_filter(
$fonts,
/**
Expand All @@ -160,7 +156,7 @@ public function get_items( $request ) {
* @param array{family: string} $font
* @return bool
*/
static fn( array $font ) => false !== stripos( $font['family'], $search )
static fn( array $font ) => false !== stripos( $font['family'], $request['search'] )
)
);
}
Expand All @@ -172,13 +168,7 @@ public function get_items( $request ) {

// Filter before doing any sorting.
if ( isset( $registered['include'], $request['include'] ) && ! empty( $request['include'] ) ) {
/**
* Include list.
*
* @var array{string} $include_list
*/
$include_list = $request['include'];
$include_list = array_map( 'strtolower', $include_list );
$include_list = array_map( 'strtolower', $request['include'] );

$fonts = array_values(
array_filter(
Expand All @@ -202,8 +192,8 @@ public function get_items( $request ) {
/**
* Font A and Font B.
*
* @param Font $a
* @param Font $b
* @phpstan-param Font $a
* @phpstan-param Font $b
* @return int
*/
static fn( array $a, array $b ): int => strnatcasecmp( $a['family'], $b['family'] )
Expand Down Expand Up @@ -263,6 +253,8 @@ public function delete_item( $request ) {
* @param WP_Post $item Post object.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response Response object.
*
* @phpstan-param WP_REST_Request<array{context: string}> $request
*/
public function prepare_item_for_response( $item, $request ): WP_REST_Response {
// Restores the more descriptive, specific name for use within this method.
Expand Down Expand Up @@ -302,11 +294,6 @@ public function prepare_item_for_response( $item, $request ): WP_REST_Response {
}
}

/**
* Request context.
*
* @var string $context
*/
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
Expand Down Expand Up @@ -687,6 +674,11 @@ protected function prepare_item_for_database( $request ) {
* @return bool Whether a font with this exact name already exists.
*/
private function font_exists( string $font_family ): bool {
/**
* A custom request to perform the lookup.
*
* @phpstan-var WP_REST_Request<array{search?: string, service?: string, include?: array<string>}> $request
*/
$request = new WP_REST_Request(
WP_REST_Server::READABLE,
$this->namespace .
Expand Down
8 changes: 2 additions & 6 deletions includes/REST_API/Hotlinking_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,8 @@ public function proxy_url( WP_REST_Request $request ) {
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response|WP_Error Response object.
*
* @phpstan-param LinkData $link
* @phpstan-param LinkData $link
* @phpstan-param WP_REST_Request<array{context: string}> $request
*/
public function prepare_item_for_response( $link, $request ) {
$fields = $this->get_fields_for_response( $request );
Expand All @@ -440,11 +441,6 @@ public function prepare_item_for_response( $link, $request ) {
return $error;
}

/**
* Request context.
*
* @var string $context
*/
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
Expand Down
12 changes: 5 additions & 7 deletions includes/REST_API/Link_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,9 +354,12 @@ public function parse_link( $request ) {
*
* @since 1.10.0
*
* @param array{title: string, image: string, description: string} $link Link value, default to false is not set.
* @param WP_REST_Request $request Request object.
* @param array $link Link value, default to false is not set.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response|WP_Error Response object.
*
* @phpstan-param array{title: string, image: string, description: string} $link
* @phpstan-param WP_REST_Request<array{context: string}> $request
*/
public function prepare_item_for_response( $link, $request ) {
$fields = $this->get_fields_for_response( $request );
Expand All @@ -371,11 +374,6 @@ public function prepare_item_for_response( $link, $request ) {
}
}

/**
* Request context.
*
* @var string $context
*/
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
Expand Down
Loading

0 comments on commit 4cd3b17

Please sign in to comment.