From 996edd67587c5d52dc6f772fffa0088ab4173397 Mon Sep 17 00:00:00 2001 From: mundschenk-at <6943905+mundschenk-at@users.noreply.github.com> Date: Wed, 25 Dec 2024 10:15:48 +0100 Subject: [PATCH] Disallow short ternary (in general) --- .../components/class-image-proxy.php | 6 +- .../class-buddypress-integration.php | 4 +- .../avatar-privacy/tools/class-multisite.php | 6 +- .../tools/network/class-gravatar-service.php | 4 +- phpcs.xml | 1 - .../components/class-image-proxy-test.php | 149 ++++++++++-------- 6 files changed, 97 insertions(+), 73 deletions(-) diff --git a/includes/avatar-privacy/components/class-image-proxy.php b/includes/avatar-privacy/components/class-image-proxy.php index 8f7de5aa..ee7acdf1 100644 --- a/includes/avatar-privacy/components/class-image-proxy.php +++ b/includes/avatar-privacy/components/class-image-proxy.php @@ -2,7 +2,7 @@ /** * This file is part of Avatar Privacy. * - * Copyright 2018-2023 Peter Putzer. + * Copyright 2018-2024 Peter Putzer. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -181,11 +181,11 @@ public function load_cached_avatar( \WP $wp ) { list(, $type, $subdir, $hash, $size, $extension ) = $parts; - $file = "{$this->file_cache->get_base_dir()}{$type}/" . ( $subdir ?: '' ) . $hash . ( empty( $size ) ? '' : "-{$size}" ) . ".{$extension}"; + $file = "{$this->file_cache->get_base_dir()}{$type}/{$subdir}{$hash}" . ( empty( $size ) ? '' : "-{$size}" ) . ".{$extension}"; if ( ! \file_exists( $file ) ) { // Default size (for SVGs mainly, which ignore it). - $size = (int) $size ?: 100; + $size = (int) $size ?: 100; // phpcs:ignore Universal.Operators.DisallowShortTernary.Found -- Here it's the right tool. if ( isset( $this->handlers[ $type ] ) ) { $success = $this->handlers[ $type ]->cache_image( $type, $hash, $size, $subdir, $extension ); diff --git a/includes/avatar-privacy/integrations/class-buddypress-integration.php b/includes/avatar-privacy/integrations/class-buddypress-integration.php index e66c1c76..6b5f57d2 100644 --- a/includes/avatar-privacy/integrations/class-buddypress-integration.php +++ b/includes/avatar-privacy/integrations/class-buddypress-integration.php @@ -2,7 +2,7 @@ /** * This file is part of Avatar Privacy. * - * Copyright 2019-2023 Peter Putzer. + * Copyright 2019-2024 Peter Putzer. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -233,7 +233,7 @@ public function add_default_avatars_to_buddypress( $original_default, array $par ]; $default_avatar = \get_avatar_url( $params['item_id'], $args ); - return $default_avatar ?: $original_default; + return $default_avatar ?: $original_default; // phpcs:ignore Universal.Operators.DisallowShortTernary.Found } /** diff --git a/includes/avatar-privacy/tools/class-multisite.php b/includes/avatar-privacy/tools/class-multisite.php index 24213a2a..d18d4db5 100644 --- a/includes/avatar-privacy/tools/class-multisite.php +++ b/includes/avatar-privacy/tools/class-multisite.php @@ -2,7 +2,7 @@ /** * This file is part of Avatar Privacy. * - * Copyright 2018-2023 Peter Putzer. + * Copyright 2018-2024 Peter Putzer. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -63,8 +63,8 @@ public function do_for_all_sites_in_network( callable $task, $network_id = null * @return int[] An array of site IDs. */ public function get_site_ids( $network_id = null ) { - $network_id = $network_id ?: \get_current_network_id(); - $query = [ + $network_id ??= \get_current_network_id(); + $query = [ 'fields' => 'ids', 'network_id' => $network_id, 'number' => 0, diff --git a/includes/avatar-privacy/tools/network/class-gravatar-service.php b/includes/avatar-privacy/tools/network/class-gravatar-service.php index 162b3515..98789710 100644 --- a/includes/avatar-privacy/tools/network/class-gravatar-service.php +++ b/includes/avatar-privacy/tools/network/class-gravatar-service.php @@ -2,7 +2,7 @@ /** * This file is part of Avatar Privacy. * - * Copyright 2018-2022 Peter Putzer. + * Copyright 2018-2024 Peter Putzer. * Copyright 2012-2013 Johannes Freudendahl. * * This program is free software; you can redistribute it and/or @@ -204,7 +204,7 @@ protected function validate_and_cache( Transients_Helper $transients, $email, $h $this->validation_cache[ $hash ] = $result; } - return $result ?: ''; + return (string) $result; } /** diff --git a/phpcs.xml b/phpcs.xml index c834fee6..20b4e3c3 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -16,7 +16,6 @@ - diff --git a/tests/avatar-privacy/components/class-image-proxy-test.php b/tests/avatar-privacy/components/class-image-proxy-test.php index 0032b9c7..5c8a5b88 100644 --- a/tests/avatar-privacy/components/class-image-proxy-test.php +++ b/tests/avatar-privacy/components/class-image-proxy-test.php @@ -228,60 +228,59 @@ public function test_add_cache_rewrite_rules() { } /** - * Tests ::load_cached_avatar. + * Provides data for testing ::load_cached_avatar. * - * @covers ::load_cached_avatar + * @return array */ - public function test_load_cached_avatar_default_icon() { - // Input parameters. - $basedir = '/base/dir/'; - $type = 'ring'; - $hash = '19b4a035996a6f641a10a02fac6d3c6be1dd2713dcc42914b3acc4128bbe9399'; - $subdir = '1/9/'; - $extension = 'svg'; - $size = 100; - $file = "{$type}/{$subdir}{$hash}.{$extension}"; - - // Mock WP global. - $wp = m::mock( 'WP' ); - $wp->query_vars = [ - 'avatar-privacy-file' => $file, + public function provide_load_cached_avatar_data(): array { + return [ + 'Default icon' => + [ 'ring', '1/9/', 'svg', Image_File::SVG_IMAGE, fn() => $this->default_icons ], + 'Gravatar' => + [ 'gravatar', '2/8/', 'png', Image_File::PNG_IMAGE, fn() => $this->gravatar, 99 ], + 'Gravatar without subdir' => + [ 'gravatar', '', 'png', Image_File::PNG_IMAGE, fn() => $this->gravatar, 99 ], + 'User avatar' => + [ 'user', '3/7/', 'png', Image_File::PNG_IMAGE, fn() => $this->user_avatar, 64 ], ]; - - $this->file_cache->shouldReceive( 'get_base_dir' )->once()->andReturn( $basedir ); - - $this->default_icons->shouldReceive( 'cache_image' )->once()->with( $type, $hash, $size, $subdir, $extension )->andReturn( true ); - $this->sut->shouldReceive( 'send_image' )->once()->with( "{$basedir}{$file}", \DAY_IN_SECONDS, Image_File::SVG_IMAGE ); - $this->sut->shouldReceive( 'exit_request' )->once(); - - $this->assertNull( $this->sut->load_cached_avatar( $wp ) ); } /** * Tests ::load_cached_avatar. * + * @dataProvider provide_load_cached_avatar_data + * * @covers ::load_cached_avatar + * + * @param string $type The image type. + * @param string $subdir The sub-directory. + * @param string $extension The file extension. + * @param string $mime_type The expected MIME type of the image. + * @param \Closure $handler The expected avatar handler mock encapsulated for late evaluation. + * @param ?int $size The size in pixels (if part of the filename). */ - public function test_load_cached_avatar_gravatar() { + public function test_load_cached_avatar( string $type, string $subdir, string $extension, string $mime_type, \Closure $handler, ?int $size = null ) { // Input parameters. - $basedir = '/base/dir/'; - $type = 'gravatar'; - $hash = '19b4a035996a6f641a10a02fac6d3c6be1dd2713dcc42914b3acc4128bbe9399'; - $subdir = '1/9/'; - $extension = 'png'; - $size = 100; - $file = "{$type}/{$subdir}{$hash}-{$size}.{$extension}"; - - // Mock WP global. - $wp = m::mock( 'WP' ); + $basedir = '/base/dir/'; + $hash = '19b4a035996a6f641a10a02fac6d3c6be1dd2713dcc42914b3acc4128bbe9399'; + $filename = $hash . ( $size ? "-$size" : '' ); + $file = "{$type}/{$subdir}{$filename}.{$extension}"; + $size ??= 100; + + /** + * Mock WP global. + * + * @var \WP&m\MockInterface $wp + */ + $wp = m::mock( \WP::class ); $wp->query_vars = [ 'avatar-privacy-file' => $file, ]; $this->file_cache->shouldReceive( 'get_base_dir' )->once()->andReturn( $basedir ); - $this->gravatar->shouldReceive( 'cache_image' )->once()->with( $type, $hash, $size, $subdir, $extension )->andReturn( true ); - $this->sut->shouldReceive( 'send_image' )->once()->with( "{$basedir}{$file}", \DAY_IN_SECONDS, Image_File::PNG_IMAGE ); + $handler->bindTo( $this )()->shouldReceive( 'cache_image' )->once()->with( $type, $hash, $size, $subdir, $extension )->andReturn( true ); + $this->sut->shouldReceive( 'send_image' )->once()->with( "{$basedir}{$file}", \DAY_IN_SECONDS, $mime_type ); $this->sut->shouldReceive( 'exit_request' )->once(); $this->assertNull( $this->sut->load_cached_avatar( $wp ) ); @@ -290,27 +289,38 @@ public function test_load_cached_avatar_gravatar() { /** * Tests ::load_cached_avatar. * + * @dataProvider provide_load_cached_avatar_data + * * @covers ::load_cached_avatar + * + * @param string $type The image type. + * @param string $subdir The sub-directory. + * @param string $extension The file extension. + * @param string $mime_type The expected MIME type of the image. + * @param \Closure $handler The expected avatar handler mock encapsulated for late evaluation. + * @param ?int $size The size in pixels (if part of the filename). */ - public function test_load_cached_avatar_user_avatar_unsuccessful() { + public function test_load_cached_avatar_unsucessful( string $type, string $subdir, string $extension, string $mime_type, \Closure $handler, ?int $size = null ) { // Input parameters. - $basedir = '/base/dir/'; - $type = 'user'; - $hash = '19b4a035996a6f641a10a02fac6d3c6be1dd2713dcc42914b3acc4128bbe9399'; - $subdir = '1/9/'; - $extension = 'png'; - $size = 100; - $file = "{$type}/{$subdir}{$hash}-{$size}.{$extension}"; - - // Mock WP global. - $wp = m::mock( 'WP' ); + $basedir = '/base/dir/'; + $hash = '19b4a035996a6f641a10a02fac6d3c6be1dd2713dcc42914b3acc4128bbe9399'; + $filename = $hash . ( $size ? "-$size" : '' ); + $file = "{$type}/{$subdir}{$filename}.{$extension}"; + $size ??= 100; + + /** + * Mock WP global. + * + * @var \WP&m\MockInterface $wp + */ + $wp = m::mock( \WP::class ); $wp->query_vars = [ 'avatar-privacy-file' => $file, ]; $this->file_cache->shouldReceive( 'get_base_dir' )->once()->andReturn( $basedir ); - $this->user_avatar->shouldReceive( 'cache_image' )->once()->with( $type, $hash, $size, $subdir, $extension )->andReturn( false ); + $handler->bindTo( $this )()->shouldReceive( 'cache_image' )->once()->with( $type, $hash, $size, $subdir, $extension )->andReturn( false ); $this->sut->shouldReceive( 'send_image' )->never(); $this->sut->shouldReceive( 'exit_request' )->never(); @@ -328,8 +338,12 @@ public function test_load_cached_avatar_user_avatar_unsuccessful() { * @covers ::load_cached_avatar */ public function test_load_cached_avatar_no_query_var() { - // Mock WP global. - $wp = m::mock( 'WP' ); + /** + * Mock WP global. + * + * @var \WP&m\MockInterface $wp + */ + $wp = m::mock( \WP::class ); $wp->query_vars = []; $this->file_cache->shouldReceive( 'get_base_dir' )->never(); @@ -345,25 +359,36 @@ public function test_load_cached_avatar_no_query_var() { /** * Tests ::load_cached_avatar. * + * @dataProvider provide_load_cached_avatar_data + * * @covers ::load_cached_avatar + * + * @param string $type The image type. + * @param string $subdir The sub-directory. + * @param string $extension The file extension. + * @param string $mime_type The expected MIME type of the image. + * @param \Closure $handler The expected avatar handler mock encapsulated for late evaluation. + * @param ?int $size The size in pixels (if part of the filename). */ - public function test_load_cached_avatar_invalid_query_var() { + public function test_load_cached_avatar_invalid_query_var( string $type, string $subdir, string $extension, string $mime_type, \Closure $handler, ?int $size = null ) { // Input parameters. - $type = 'user'; - $hash = '19b4a035996a6f641a10a02faZ6d3c6be1dd2713dcc42914b3acc4128bbe9399'; // Invalid! - $subdir = '1/9/'; - $extension = 'png'; - $size = 100; - $file = "{$type}/{$subdir}{$hash}-{$size}.{$extension}"; - - // Mock WP global. - $wp = m::mock( 'WP' ); + $hash = '19b4a035996a6f641a10a02faZ6d3c6be1dd2713dcc42914b3acc4128bbe9399'; // Invalid! + $filename = $hash . ( $size ? "-$size" : '' ); + $file = "{$type}/{$subdir}{$filename}.{$extension}"; + $size ??= 100; + + /** + * Mock WP global. + * + * @var \WP&m\MockInterface $wp + */ + $wp = m::mock( \WP::class ); $wp->query_vars = [ 'avatar-privacy-file' => $file, ]; $this->file_cache->shouldReceive( 'get_base_dir' )->never(); - $this->user_avatar->shouldReceive( 'cache_image' )->never(); + $handler->bindTo( $this )()->shouldReceive( 'cache_image' )->never(); $this->sut->shouldReceive( 'send_image' )->never(); $this->sut->shouldReceive( 'exit_request' )->never();