Skip to content

Commit

Permalink
Fix phpstan issues
Browse files Browse the repository at this point in the history
  • Loading branch information
carstingaxion committed Aug 29, 2023
1 parent 3c07941 commit a6cc6c5
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
13 changes: 10 additions & 3 deletions inc/auto-featured-image/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,16 @@ function load() :void {
/**
* Automatically add first image in content as featured image if none is set.
*
* @param object|null $post Post Object or null if is called during trashing.
* @param WP_Post|null $post Post Object or null if is called during trashing.
*
* @link https://wordpress.org/plugins/easy-add-thumbnail/
*/
function auto_featured_image( WP_Post|null $post ) : void {

if ( is_null( $post ) ) {
return;
}

// Do nothing if the post has already a featured image set.
if ( has_post_thumbnail( $post ) ) {
return;
Expand All @@ -113,8 +117,11 @@ function auto_featured_image( WP_Post|null $post ) : void {
if ( $attached_image ) {

$attachment_values = array_values( $attached_image );
set_post_thumbnail( $post->ID, $attachment_values[0]->ID );
return;

if ( isset( $attachment_values[0] ) && $attachment_values[0] instanceof WP_Post ) {
set_post_thumbnail( $post->ID, $attachment_values[0]->ID );
return;
}
}

// Use regex to find image blocks in the post content.
Expand Down
18 changes: 14 additions & 4 deletions inc/image-optimization/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function bootstrap() :void {
* @since 2.9.0 as 'wp_handle_upload_prefilter'.
* @since 4.0.0 Converted to a dynamic hook with `$action`.
*
* @param array $file {
* @param array<string, int|string> $file {
* Reference to a single element from `$_FILES`.
*
* @type string $name The original name of the file on the client machine.
Expand All @@ -47,13 +47,15 @@ function bootstrap() :void {
* @type int $size The size, in bytes, of the uploaded file.
* @type int $error The error code associated with this file upload.
* }
*
* @return array<string, int|string>
*/
function compress( array $file ) : array {
if ( isset( $file['error'] ) && 0 !== $file['error'] ) {
return $file;
}

$mime = explode( '/', $file['type'] );
$mime = explode( '/', (string) $file['type'] );
if (
'image' !== $mime[0]
||
Expand All @@ -62,7 +64,7 @@ function compress( array $file ) : array {
return $file;
}

$file_put_contents = replace( $file['tmp_name'] );
$file_put_contents = replace( (string) $file['tmp_name'] );

if ( is_int( $file_put_contents ) && ! empty( $file_put_contents ) ) {
$file['size'] = $file_put_contents;
Expand All @@ -85,7 +87,7 @@ function replace( string $path ) : int|false {

// If somethin went wrong
// return, unchanged.
if ( ! is_string( $compressed_raw ) || empty( $compressed_raw ) ) {
if ( empty( $compressed_raw ) ) {
return false;
}

Expand Down Expand Up @@ -118,6 +120,10 @@ function optimize( string $file_path ) : string {

$raw_image = file_get_contents( $file_path );

Check warning on line 121 in inc/image-optimization/namespace.php

View workflow job for this annotation

GitHub Actions / call-workflow-build-test-measure / Lint: PHP

`file_get_contents()` is highly discouraged for remote requests, please use `wpcom_vip_file_get_contents()` or `vip_safe_wp_remote_get()` instead. If it's for a local file please use WP_Filesystem instead.

if ( ! is_string( $raw_image ) ) {
return '';
}

$imagick->readImageBlob( $raw_image );
$imagick->stripImage();

Expand All @@ -133,6 +139,10 @@ function optimize( string $file_path ) : string {
// Get thumbnail image.
$imagick->thumbnailImage( $width, $height );

if ( ! isset( $image_types[2] ) ) {
return '';
}

// Set image as based its own type.
if ( $image_types[2] === IMAGETYPE_JPEG ) {
$imagick->setImageFormat( 'jpeg' );
Expand Down
4 changes: 2 additions & 2 deletions inc/modern-images-wp/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ function filter_options() :void {
* @version version
* @author Carsten Bach
*
* @return [type] [description]
* @return void
*/
function wp_ajax_upload_attachment() {
function wp_ajax_upload_attachment() : void {
/**
* If we're not performing our AJAX request, return early.
*/
Expand Down
1 change: 0 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,3 @@ parameters:
ignoreErrors:
# Find a nicer way instead of ignoring this Error on every ft-module
- '#Function Altis\\register_module not found\.#'
- '#Function Figuren_Theater\\get_config not found\.#'

0 comments on commit a6cc6c5

Please sign in to comment.