Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,3 @@ parameters:
-
identifier: staticMethod.dynamicCall
path: */tests/*
-
# TODO: Remove this to fix https://github.com/WordPress/performance/issues/1219
identifier: empty.notAllowed
paths:
- */tests/*
- plugins/dominant-color-images
- plugins/performance-lab
- plugins/webp-uploads
13 changes: 7 additions & 6 deletions plugins/dominant-color-images/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,17 @@
$attr['data-has-transparency'] = $image_meta['has_transparency'] ? 'true' : 'false';

$class = $image_meta['has_transparency'] ? 'has-transparency' : 'not-transparent';
if ( empty( $attr['class'] ) ) {
$attr['class'] = $class;
} else {

if ( isset( $attr['class'] ) && is_string( $attr['class'] ) && '' !== $attr['class'] ) {
$attr['class'] .= ' ' . $class;
} else {
$attr['class'] = $class;

Check warning on line 72 in plugins/dominant-color-images/hooks.php

View check run for this annotation

Codecov / codecov/patch

plugins/dominant-color-images/hooks.php#L72

Added line #L72 was not covered by tests
}
}

if ( ! empty( $image_meta['dominant_color'] ) ) {
if ( isset( $image_meta['dominant_color'] ) && is_string( $image_meta['dominant_color'] ) && '' !== $image_meta['dominant_color'] ) {
$attr['data-dominant-color'] = esc_attr( $image_meta['dominant_color'] );
$style_attribute = empty( $attr['style'] ) ? '' : $attr['style'];
$style_attribute = isset( $attr['style'] ) && is_string( $attr['style'] ) ? $attr['style'] : '';
$attr['style'] = '--dominant-color: #' . esc_attr( $image_meta['dominant_color'] ) . ';' . $style_attribute;
}

Expand Down Expand Up @@ -140,7 +141,7 @@
return $filtered_image;
}

if ( ! empty( $image_meta['dominant_color'] ) ) {
if ( isset( $image_meta['dominant_color'] ) && is_string( $image_meta['dominant_color'] ) && '' !== $image_meta['dominant_color'] ) {
$processor->set_attribute( 'data-dominant-color', $image_meta['dominant_color'] );

$style_attribute = '--dominant-color: #' . $image_meta['dominant_color'] . '; ';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ static function ( $value ) {
*/
public function use_output_buffer(): bool {
$options = (array) get_option( PERFLAB_SERVER_TIMING_SETTING, array() );
$enabled = ! empty( $options['output_buffering'] );
$enabled = isset( $options['output_buffering'] ) && (bool) $options['output_buffering'];

/**
* Filters whether an output buffer should be used to be able to gather additional Server-Timing metrics.
Expand All @@ -220,7 +220,7 @@ public function use_output_buffer(): bool {
*
* @since 1.8.0
*
* @param bool $use_output_buffer Whether to use an output buffer.
* @param bool $enabled Whether to use an output buffer.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch.

*/
return (bool) apply_filters( 'perflab_server_timing_use_output_buffer', $enabled );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function perflab_aao_handle_update_autoload(): void {
wp_die( esc_html__( 'Permission denied.', 'performance-lab' ) );
}

if ( empty( $option_name ) ) {
if ( '' === $option_name ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If someone passed an option_name of '0' then this will pass through the condition since empty( '0' ) === true: https://3v4l.org/LurUe

I don't think this is a problem here though 😄

wp_die( esc_html__( 'Invalid option name.', 'performance-lab' ) );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ function perflab_aea_audit_enqueued_scripts(): void {

// Add any extra data (inlined) that was passed with the script.
$inline_size = 0;
if ( ! empty( $script->extra ) && ! empty( $script->extra['after'] ) ) {
if (
isset( $script->extra['after'] ) &&
is_array( $script->extra['after'] )
) {
foreach ( $script->extra['after'] as $extra ) {
$inline_size += ( is_string( $extra ) ) ? mb_strlen( $extra, '8bit' ) : 0;
}
Expand Down Expand Up @@ -78,7 +81,11 @@ function perflab_aea_audit_enqueued_styles(): void {
}

// Check if we already have the style's path ( part of a refactor for block styles from 5.9 ).
if ( ! empty( $style->extra ) && ! empty( $style->extra['path'] ) ) {
if (
isset( $style->extra['path'] ) &&
is_string( $style->extra['path'] ) &&
'' !== $style->extra['path']
) {
$path = $style->extra['path'];
} else { // Fallback to getting the path from the style's src.
$path = perflab_aea_get_path_from_resource_url( $style->src );
Expand All @@ -89,7 +96,10 @@ function perflab_aea_audit_enqueued_styles(): void {

// Add any extra data (inlined) that was passed with the style.
$inline_size = 0;
if ( ! empty( $style->extra ) && ! empty( $style->extra['after'] ) ) {
if (
isset( $style->extra['after'] ) &&
is_array( $style->extra['after'] )
) {
foreach ( $style->extra['after'] as $extra ) {
$inline_size += ( is_string( $extra ) ) ? mb_strlen( $extra, '8bit' ) : 0;
}
Expand Down
9 changes: 7 additions & 2 deletions plugins/webp-uploads/deprecated.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,17 @@
$metadata = wp_get_attachment_metadata( $attachment_id );

// Return full image size sources.
if ( 'full' === $size && ! empty( $metadata['sources'] ) ) {
if (
'full' === $size &&
isset( $metadata['sources'] ) &&
is_array( $metadata['sources'] ) &&
count( $metadata['sources'] ) > 0

Check warning on line 37 in plugins/webp-uploads/deprecated.php

View check run for this annotation

Codecov / codecov/patch

plugins/webp-uploads/deprecated.php#L34-L37

Added lines #L34 - L37 were not covered by tests
) {
return $metadata['sources'];
}

// Return the resized image sources.
if ( ! empty( $metadata['sizes'][ $size ]['sources'] ) ) {
if ( isset( $metadata['sizes'][ $size ]['sources'] ) && is_array( $metadata['sizes'][ $size ]['sources'] ) ) {

Check warning on line 43 in plugins/webp-uploads/deprecated.php

View check run for this annotation

Codecov / codecov/patch

plugins/webp-uploads/deprecated.php#L43

Added line #L43 was not covered by tests
return $metadata['sizes'][ $size ]['sources'];
}

Expand Down
6 changes: 3 additions & 3 deletions plugins/webp-uploads/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function webp_uploads_get_upload_image_mime_transforms(): array {
// Ensure that all mime types have correct transforms. If a mime type has invalid transforms array,
// then fallback to the original mime type to make sure that the correct subsizes are created.
foreach ( $transforms as $mime_type => $transform_types ) {
if ( ! is_array( $transform_types ) || empty( $transform_types ) ) {
if ( ! is_array( $transform_types ) || 0 === count( $transform_types ) ) {
$transforms[ $mime_type ] = array( $mime_type );
}
}
Expand Down Expand Up @@ -163,7 +163,7 @@ function webp_uploads_generate_additional_image_source( int $attachment_id, stri

$image_meta = wp_get_attachment_metadata( $attachment_id );
// If stored EXIF data exists, rotate the source image before creating sub-sizes.
if ( ! empty( $image_meta['image_meta'] ) ) {
if ( isset( $image_meta['image_meta'] ) && is_array( $image_meta['image_meta'] ) && count( $image_meta['image_meta'] ) > 0 ) {
$editor->maybe_exif_rotate();
}

Expand All @@ -185,7 +185,7 @@ function webp_uploads_generate_additional_image_source( int $attachment_id, stri
return $image;
}

if ( empty( $image['file'] ) ) {
if ( ! isset( $image['file'] ) || ! is_string( $image['file'] ) || '' === $image['file'] ) {
return new WP_Error( 'image_file_not_present', __( 'The file key is not present on the image data', 'webp-uploads' ) );
}

Expand Down
76 changes: 52 additions & 24 deletions plugins/webp-uploads/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
$metadata['sources'] = array();
}

if ( empty( $metadata['sources'][ $mime_type ] ) ) {
if ( ! isset( $metadata['sources'][ $mime_type ]['file'] ) ) {
$metadata['sources'][ $mime_type ] = array(
'file' => wp_basename( $file ),
'filesize' => wp_filesize( $file ),
Expand All @@ -99,12 +99,12 @@
// Create the sources for the full sized image.
foreach ( $valid_mime_transforms[ $mime_type ] as $targeted_mime ) {
// If this property exists no need to create the image again.
if ( ! empty( $metadata['sources'][ $targeted_mime ] ) ) {
if ( isset( $metadata['sources'][ $targeted_mime ]['file'] ) ) {
continue;
}

// The targeted mime is not allowed in the current installation.
if ( empty( $allowed_mimes[ $targeted_mime ] ) ) {
if ( ! isset( $allowed_mimes[ $targeted_mime ] ) ) {
continue;
}

Expand Down Expand Up @@ -150,7 +150,7 @@
$original_image = wp_get_original_image_path( $attachment_id );

// If WordPress already modified the original itself, keep the original and discard WordPress's generated version.
if ( ! empty( $metadata['original_image'] ) ) {
if ( isset( $metadata['original_image'] ) && is_string( $metadata['original_image'] ) && '' !== $metadata['original_image'] ) {

Check warning on line 153 in plugins/webp-uploads/hooks.php

View check run for this annotation

Codecov / codecov/patch

plugins/webp-uploads/hooks.php#L153

Added line #L153 was not covered by tests
$uploadpath = wp_get_upload_dir();
$attached_file = get_attached_file( $attachment_id );
if ( false !== $attached_file ) {
Expand All @@ -171,15 +171,23 @@
}

// Make sure we have some sizes to work with, otherwise avoid any work.
if ( empty( $metadata['sizes'] ) || ! is_array( $metadata['sizes'] ) ) {
if (
! isset( $metadata['sizes'] ) ||
! is_array( $metadata['sizes'] ) ||
0 === count( $metadata['sizes'] )
) {
return $metadata;
}

$sizes_with_mime_type_support = webp_uploads_get_image_sizes_additional_mime_type_support();

foreach ( $metadata['sizes'] as $size_name => $properties ) {
// Do nothing if this image size is not an array or is not allowed to have additional mime types.
if ( ! is_array( $properties ) || empty( $sizes_with_mime_type_support[ $size_name ] ) ) {
if (
! is_array( $properties ) ||
! isset( $sizes_with_mime_type_support[ $size_name ] ) ||
false === $sizes_with_mime_type_support[ $size_name ]
) {
continue;
}

Expand All @@ -192,16 +200,16 @@
}

// The mime type can't be determined.
if ( empty( $current_mime ) ) {
if ( ! is_string( $current_mime ) || '' === $current_mime ) {
continue;
}

// Ensure a `sources` property exists on the existing size.
if ( empty( $properties['sources'] ) || ! is_array( $properties['sources'] ) ) {
if ( ! isset( $properties['sources'] ) || ! is_array( $properties['sources'] ) ) {
$properties['sources'] = array();
}

if ( empty( $properties['sources'][ $current_mime ] ) && isset( $properties['file'] ) ) {
if ( ! isset( $properties['sources'][ $current_mime ]['file'] ) && isset( $properties['file'] ) ) {
$properties['sources'][ $current_mime ] = array(
'file' => $properties['file'],
'filesize' => 0,
Expand All @@ -217,7 +225,7 @@

foreach ( $valid_mime_transforms[ $mime_type ] as $mime ) {
// If this property exists no need to create the image again.
if ( ! empty( $properties['sources'][ $mime ] ) ) {
if ( isset( $properties['sources'][ $mime ]['file'] ) ) {
continue;
}

Expand Down Expand Up @@ -275,7 +283,7 @@
}

// Only setup the trace array if we no longer have more sizes.
if ( ! empty( $missing_sizes ) ) {
if ( count( $missing_sizes ) > 0 ) {
return $missing_sizes;
}

Expand Down Expand Up @@ -362,7 +370,7 @@
function webp_uploads_remove_sources_files( int $attachment_id ): void {
$file = get_attached_file( $attachment_id );

if ( empty( $file ) ) {
if ( false === (bool) $file ) {
return;
}

Expand All @@ -371,7 +379,11 @@
$sizes = ! isset( $metadata['sizes'] ) || ! is_array( $metadata['sizes'] ) ? array() : $metadata['sizes'];

$upload_path = wp_get_upload_dir();
if ( empty( $upload_path['basedir'] ) ) {
if (
! isset( $upload_path['basedir'] ) ||
! is_string( $upload_path['basedir'] ) ||
'' === $upload_path['basedir']
) {
return;
}

Expand All @@ -383,7 +395,7 @@
continue;
}

$original_size_mime = empty( $size['mime-type'] ) ? '' : $size['mime-type'];
$original_size_mime = isset( $size['mime-type'] ) && is_string( $size['mime-type'] ) ? $size['mime-type'] : '';

foreach ( $size['sources'] as $mime => $properties ) {
/**
Expand All @@ -397,7 +409,11 @@
continue;
}

if ( ! is_array( $properties ) || empty( $properties['file'] ) ) {
if (
! isset( $properties['file'] ) ||
! is_string( $properties['file'] ) ||
'' === $properties['file']
) {
continue;
}

Expand Down Expand Up @@ -429,7 +445,11 @@
continue;
}

if ( ! is_array( $properties ) || empty( $properties['file'] ) ) {
if (
! isset( $properties['file'] ) ||
! is_string( $properties['file'] ) ||
'' === $properties['file']
) {
continue;
}

Expand All @@ -453,7 +473,7 @@
continue;
}

$original_backup_size_mime = empty( $backup_size['mime-type'] ) ? '' : $backup_size['mime-type'];
$original_backup_size_mime = isset( $backup_size['mime-type'] ) && is_string( $backup_size['mime-type'] ) ? $backup_size['mime-type'] : '';

Check warning on line 476 in plugins/webp-uploads/hooks.php

View check run for this annotation

Codecov / codecov/patch

plugins/webp-uploads/hooks.php#L476

Added line #L476 was not covered by tests

foreach ( $backup_size['sources'] as $backup_mime => $backup_properties ) {
/**
Expand All @@ -467,12 +487,16 @@
continue;
}

if ( ! is_array( $backup_properties ) || empty( $backup_properties['file'] ) ) {
if (
! isset( $backup_properties['file'] ) ||
! is_string( $backup_properties['file'] ) ||
'' === $backup_properties['file']

Check warning on line 493 in plugins/webp-uploads/hooks.php

View check run for this annotation

Codecov / codecov/patch

plugins/webp-uploads/hooks.php#L491-L493

Added lines #L491 - L493 were not covered by tests
) {
continue;
}

$backup_intermediate_file = str_replace( $basename, $backup_properties['file'], $file );
if ( empty( $backup_intermediate_file ) ) {
if ( '' === $backup_intermediate_file ) {

Check warning on line 499 in plugins/webp-uploads/hooks.php

View check run for this annotation

Codecov / codecov/patch

plugins/webp-uploads/hooks.php#L499

Added line #L499 was not covered by tests
continue;
}

Expand All @@ -492,12 +516,16 @@
foreach ( $backup_sources as $backup_mimes ) {

foreach ( $backup_mimes as $backup_mime_properties ) {
if ( ! is_array( $backup_mime_properties ) || empty( $backup_mime_properties['file'] ) ) {
if (
! isset( $backup_mime_properties['file'] ) ||
! is_string( $backup_mime_properties['file'] ) ||
'' === $backup_mime_properties['file']

Check warning on line 522 in plugins/webp-uploads/hooks.php

View check run for this annotation

Codecov / codecov/patch

plugins/webp-uploads/hooks.php#L520-L522

Added lines #L520 - L522 were not covered by tests
) {
continue;
}

$full_size = str_replace( $basename, $backup_mime_properties['file'], $file );
if ( empty( $full_size ) ) {
if ( '' === $full_size ) {

Check warning on line 528 in plugins/webp-uploads/hooks.php

View check run for this annotation

Codecov / codecov/patch

plugins/webp-uploads/hooks.php#L528

Added line #L528 was not covered by tests
continue;
}

Expand Down Expand Up @@ -547,7 +575,7 @@
$image = $original_image;
$metadata = wp_get_attachment_metadata( $attachment_id );

if ( empty( $metadata['file'] ) ) {
if ( ! isset( $metadata['file'] ) || ! is_string( $metadata['file'] ) || '' === $metadata['file'] ) {
return $image;
}

Expand Down Expand Up @@ -596,7 +624,7 @@
// Replace sub sizes for the image if present.
foreach ( $metadata['sizes'] as $size => $size_data ) {

if ( empty( $size_data['file'] ) ) {
if ( ! isset( $size_data['file'] ) || ! is_string( $size_data['file'] ) || '' === $size_data['file'] ) {
continue;
}

Expand Down Expand Up @@ -671,7 +699,7 @@
);

foreach ( $additional_sizes as $size => $size_details ) {
$allowed_sizes[ $size ] = ! empty( $size_details['provide_additional_mime_types'] );
$allowed_sizes[ $size ] = isset( $size_details['provide_additional_mime_types'] ) && true === (bool) $size_details['provide_additional_mime_types'];
}

/**
Expand Down
Loading