Skip to content

Commit

Permalink
Disallow short ternary (in general)
Browse files Browse the repository at this point in the history
  • Loading branch information
mundschenk-at committed Dec 25, 2024
1 parent 1582d35 commit 996edd6
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 73 deletions.
6 changes: 3 additions & 3 deletions includes/avatar-privacy/components/class-image-proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

/**
Expand Down
6 changes: 3 additions & 3 deletions includes/avatar-privacy/tools/class-multisite.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -204,7 +204,7 @@ protected function validate_and_cache( Transients_Helper $transients, $email, $h
$this->validation_cache[ $hash ] = $result;
}

return $result ?: '';
return (string) $result;
}

/**
Expand Down
1 change: 0 additions & 1 deletion phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
<exclude name ="PEAR.Functions.FunctionCallSignature.MultipleArguments" />
<exclude name ="PEAR.Functions.FunctionCallSignature.ContentAfterOpenBracket" />
<exclude name="Universal.Arrays.DisallowShortArraySyntax.Found" />
<exclude name="Universal.Operators.DisallowShortTernary.Found" />
</rule>
<rule ref="WordPress-Docs" />
<rule ref="WordPress-Extra" />
Expand Down
149 changes: 87 additions & 62 deletions tests/avatar-privacy/components/class-image-proxy-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) );
Expand All @@ -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();

Expand All @@ -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();
Expand All @@ -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();

Expand Down

0 comments on commit 996edd6

Please sign in to comment.