Skip to content

Commit

Permalink
address phpstan errors
Browse files Browse the repository at this point in the history
  • Loading branch information
manewc committed Sep 26, 2024
1 parent 8d24cc1 commit 1cfbed8
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 26 deletions.
10 changes: 7 additions & 3 deletions src/adapters/class-jw-player.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Alley\WP\WP_Video_Sync\Interfaces\Adapter;
use Alley\WP\WP_Video_Sync\Last_Modified_Date;
use DateTimeImmutable;
use stdClass;

/**
* JW Player Adapter.
Expand Down Expand Up @@ -41,7 +40,7 @@ public function __construct( JW_Player_API $api ) {
* @param DateTimeImmutable $updated_after Return videos modified after this date.
* @param int $batch_size The number of videos to fetch in each batch.
*
* @return stdClass[] An array of video data.
* @return array<mixed> An array of video data objects.
*/
public function get_videos( DateTimeImmutable $updated_after, int $batch_size ): array {
// Set the request URL based on the arguments.
Expand All @@ -58,9 +57,14 @@ public function get_videos( DateTimeImmutable $updated_after, int $batch_size ):
return [];
}

// Validate the media property.
if ( ! is_array( $videos['media'] ) ) {
return [];
}

// Attempt to set the last modified date.
if (
! empty( $videos['media'] )
! empty( $videos['media'][ count( $videos['media'] ) - 1 ] )
&& isset( $videos['media'][ count( $videos['media'] ) - 1 ]->last_modified )
) {
$this->set_last_modified_date( $videos['media'][ count( $videos ) - 1 ]->last_modified );
Expand Down
19 changes: 11 additions & 8 deletions src/api/class-jw-player-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function __construct( string $api_key, string $api_secret ) {
* @param string $last_modified_date The date of the last modification to the last batch of videos.
* @param int $batch_size The number of videos to fetch in each batch.
*/
public function set_request_url( string $last_modified_date, int $batch_size ) {
public function set_request_url( string $last_modified_date, int $batch_size ): void {
$request_url = $this->api_url . '/' . $this->api_key . '/media/';

$this->request_url = add_query_arg(
Expand All @@ -85,7 +85,7 @@ public function get_request_url(): string {
/**
* Get the request arguments.
*
* @return array
* @return array<string, array<string, string>>
*/
public function get_request_args(): array {
return [
Expand All @@ -99,25 +99,28 @@ public function get_request_args(): array {
/**
* Parse the API error response.
*
* @param array $response_object The API response object.
* @param array<mixed> $response_object The API response object.
*
* @return array
* @return array<string, string>
*/
public function parse_error( array $response_object ): array {
return isset( $response_object['errors'][0]->description )
return ! empty( $response_object['errors'] )
&& is_array($response_object['errors'] )
&& isset( $response_object['errors'][0]->description )
? [ 'error' => $response_object['errors'][0]->description ]
: [];
}

/**
* Parse the API successful response.
*
* @param array $response_object The API response object.
* @param array<mixed> $response_object The API response object.
*
* @return array
* @return array<string, mixed>
*/
public function parse_success( array $response_object ): array {
return is_array( $response_object['media'] ) && ! empty( $response_object['media'] )
return ! empty( $response_object['media'] )
&& is_array( $response_object['media'] )
? [ 'media' => $response_object['media'] ]
: [];
}
Expand Down
14 changes: 10 additions & 4 deletions src/api/class-request.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function user_agent(): string {
/**
* Get the request arguments.
*
* @return array
* @return array<string, mixed>
*/
public function get_request_args(): array {
$requester_args = $this->api_requester->get_request_args();
Expand All @@ -53,18 +53,24 @@ public function get_request_args(): array {
$requester_args['user-agent'] = $this->user_agent();
}

/**
* Allow the request arguments to be filtered before the request is made.
*
* @param array<string, string|float|int|bool|array> $requester_args The request arguments.
*/
return apply_filters( 'wp_video_sync_request_args', $requester_args );
}

/**
* Parse the API response.
*
* @param mixed $response The API response.
* @return array
*
* @return array<string, mixed>
*/
private function parse_response( mixed $response ): array {
// Failed request expressed as a WP_Error.
if ( is_wp_error( $response ) || empty( $response ) ) {
if ( is_wp_error( $response ) || empty( $response ) || ! is_array( $response ) ) {
return [];
}

Expand All @@ -87,7 +93,7 @@ private function parse_response( mixed $response ): array {
/**
* Perform a GET request.
*
* @return array
* @return array<string, mixed>
*/
public function get(): array {
if ( function_exists( 'vip_safe_wp_remote_get' ) ) {
Expand Down
3 changes: 1 addition & 2 deletions src/interfaces/adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
namespace Alley\WP\WP_Video_Sync\Interfaces;

use DateTimeImmutable;
use stdClass;

/**
* Defines an interface that all adapters must implement.
Expand All @@ -21,7 +20,7 @@ interface Adapter {
* @param DateTimeImmutable $updated_after Return videos modified after this date.
* @param int $batch_size The number of videos to fetch in each batch.
*
* @return stdClass[] An array of video data. Specific shape will be determined by the adapter.
* @return array<mixed> An array of video data. Specific shape will be determined by the adapter.
*/
public function get_videos( DateTimeImmutable $updated_after, int $batch_size ): array;
}
21 changes: 12 additions & 9 deletions src/interfaces/api-requester.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,26 @@ public function get_request_url(): string;
/**
* Get the arguments for the API request.
*
* @return array
* @return array<string, array<string, string>>
*/
public function get_request_args(): array;

/**
* Parse a successful API response.
* Parse an error API response.
*
* @param array<mixed> $response_object The API response object.
*
* @param array $response_object The response object.
* @return array
* @return array<string, string>
*/
public function parse_success( array $response_object ): array;
public function parse_error( array $response_object ): array;

/**
* Parse an error API response.
* Parse a successful API response.
*
* @param array<mixed> $response_object The API response object.
*
* @param array $response_object The response object.
* @return array
* @return array<string, mixed>
*/
public function parse_error( array $response_object ): array;
public function parse_success( array $response_object ): array;

}

0 comments on commit 1cfbed8

Please sign in to comment.