Skip to content

Commit 7e76c91

Browse files
committed
fix: resolve psalm errors
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
1 parent 655ef10 commit 7e76c91

File tree

8 files changed

+44
-33
lines changed

8 files changed

+44
-33
lines changed

lib/private/Avatar/Avatar.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,17 @@
1010
namespace OC\Avatar;
1111

1212
use Imagick;
13+
use OC\User\User;
1314
use OCP\Color;
1415
use OCP\Files\NotFoundException;
1516
use OCP\IAvatar;
16-
use Psr\Log\LoggerInterface;
17-
use OC\User\User;
1817
use OCP\IConfig;
18+
use Psr\Log\LoggerInterface;
1919

2020
/**
2121
* This class gets and sets users avatars.
2222
*/
2323
abstract class Avatar implements IAvatar {
24-
protected LoggerInterface $logger;
25-
2624
/**
2725
* https://github.com/sebdesign/cap-height -- for 500px height
2826
* Automated check: https://codepen.io/skjnldsv/pen/PydLBK/
@@ -37,8 +35,10 @@ abstract class Avatar implements IAvatar {
3735
<text x="50%" y="350" style="font-weight:normal;font-size:280px;font-family:\'Noto Sans\';text-anchor:middle;fill:#{fgFill}">{letter}</text>
3836
</svg>';
3937

40-
public function __construct(LoggerInterface $logger) {
41-
$this->logger = $logger;
38+
public function __construct(
39+
protected IConfig $config,
40+
protected LoggerInterface $logger,
41+
) {
4242
}
4343

4444
/**
@@ -97,12 +97,11 @@ protected function getAvatarVector(string $userDisplayName, int $size, bool $dar
9797
}
9898

9999
/**
100-
* Select the rendering font based on the user's display name and language
100+
* Select the rendering font based on the user's display name and language
101101
*/
102102
private function getFont(string $userDisplayName): string {
103103
if (preg_match('/\p{Han}/u', $userDisplayName) === 1) {
104-
$userlang = $this->config->getUserValue($this->user->getUID(), 'core', 'lang', '');
105-
switch ($userlang) {
104+
switch ($this->getAvatarLanguage()) {
106105
case 'zh_TW':
107106
return __DIR__ . '/../../../core/fonts/NotoSansTC-Regular.ttf';
108107
case 'zh_HK':
@@ -129,7 +128,7 @@ protected function generateAvatarFromSvg(string $userDisplayName, int $size, boo
129128
// Avatar generation breaks if RSVG format is enabled. Fall back to gd in that case
130129
if (in_array('RSVG', $formats, true)) {
131130
return null;
132-
}
131+
}
133132
$text = $this->getAvatarText();
134133
try {
135134
$font = $this->getFont($text);
@@ -282,4 +281,12 @@ public function avatarBackgroundColor(string $hash): Color {
282281

283282
return $finalPalette[$this->hashToInt($hash, $steps * 3)];
284283
}
284+
285+
/**
286+
* Get the language to be used for avatar generation.
287+
* This is used to determine the font to use for the avatar text (e.g. CJK characters).
288+
*/
289+
protected function getAvatarLanguage(): string {
290+
return $this->config->getSystemValueString('default_language', 'en');
291+
}
285292
}

lib/private/Avatar/AvatarManager.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,10 @@ public function getAvatar(string $userId): IAvatar {
9292
return new UserAvatar($folder, $this->l, $user, $this->logger, $this->config);
9393
default:
9494
// use a placeholder avatar which caches the generated images
95-
return new PlaceholderAvatar($folder, $user, $this->logger);
95+
return new PlaceholderAvatar($folder, $user, $this->config, $this->logger);
9696
}
9797

98-
return new PlaceholderAvatar($folder, $user, $this->logger);
98+
return new PlaceholderAvatar($folder, $user, $this->config, $this->logger);
9999
}
100100

101101
/**
@@ -129,6 +129,6 @@ public function deleteUserAvatar(string $userId): void {
129129
* @param string $name The guest name, e.g. "Albert".
130130
*/
131131
public function getGuestAvatar(string $name): IAvatar {
132-
return new GuestAvatar($name, $this->logger);
132+
return new GuestAvatar($name, $this->config, $this->logger);
133133
}
134134
}

lib/private/Avatar/GuestAvatar.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use OCP\Files\SimpleFS\InMemoryFile;
1212
use OCP\Files\SimpleFS\ISimpleFile;
13+
use OCP\IConfig;
1314
use Psr\Log\LoggerInterface;
1415

1516
/**
@@ -23,9 +24,10 @@ class GuestAvatar extends Avatar {
2324
*/
2425
public function __construct(
2526
private string $userDisplayName,
27+
IConfig $config,
2628
LoggerInterface $logger,
2729
) {
28-
parent::__construct($logger);
30+
parent::__construct($config, $logger);
2931
}
3032

3133
/**

lib/private/Avatar/PlaceholderAvatar.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use OCP\Files\NotPermittedException;
1515
use OCP\Files\SimpleFS\ISimpleFile;
1616
use OCP\Files\SimpleFS\ISimpleFolder;
17+
use OCP\IConfig;
1718
use OCP\IImage;
1819
use Psr\Log\LoggerInterface;
1920

@@ -27,9 +28,10 @@ class PlaceholderAvatar extends Avatar {
2728
public function __construct(
2829
private ISimpleFolder $folder,
2930
private User $user,
31+
IConfig $config,
3032
LoggerInterface $logger,
3133
) {
32-
parent::__construct($logger);
34+
parent::__construct($config, $logger);
3335
}
3436

3537
/**

lib/private/Avatar/UserAvatar.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ public function __construct(
2828
private IL10N $l,
2929
protected User $user,
3030
LoggerInterface $logger,
31-
protected IConfig $config,
31+
IConfig $config,
3232
) {
33-
parent::__construct($logger);
33+
parent::__construct($config, $logger);
3434
}
3535

3636
/**
@@ -295,4 +295,9 @@ public function userChanged(string $feature, $oldValue, $newValue): void {
295295
public function isCustomAvatar(): bool {
296296
return $this->config->getUserValue($this->user->getUID(), 'avatar', 'generated', 'false') !== 'true';
297297
}
298+
299+
#[\Override]
300+
protected function getAvatarLanguage(): string {
301+
return $this->config->getUserValue($this->user->getUID(), 'core', 'lang', parent::getAvatarLanguage());
302+
}
298303
}

tests/lib/Avatar/AvatarManagerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ public function testGetAvatarScopes($avatarScope, $isPublicCall, $isKnownUser, $
269269
}
270270

271271
if ($expectedPlaceholder) {
272-
$expected = new PlaceholderAvatar($folder, $user, $this->createMock(LoggerInterface::class));
272+
$expected = new PlaceholderAvatar($folder, $user, $this->config, $this->logger);
273273
} else {
274274
$expected = new UserAvatar($folder, $this->l10n, $user, $this->logger, $this->config);
275275
}

tests/lib/Avatar/GuestAvatarTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ class GuestAvatarTest extends TestCase {
3434
*/
3535
public function setupGuestAvatar() {
3636
/* @var MockObject|LoggerInterface $logger */
37-
$logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
38-
$this->guestAvatar = new GuestAvatar('einstein', $logger);
37+
$logger = $this->createMock(LoggerInterface::class);
38+
$config = $this->createMock(\OCP\IConfig::class);
39+
$this->guestAvatar = new GuestAvatar('einstein', $config, $logger);
3940
}
4041

4142
/**

tests/lib/Avatar/UserAvatarTest.php

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,16 @@
1818
use OCP\IConfig;
1919
use OCP\IL10N;
2020
use OCP\Image;
21+
use PHPUnit\Framework\MockObject\MockObject;
2122
use Psr\Log\LoggerInterface;
2223

2324
class UserAvatarTest extends \Test\TestCase {
24-
/** @var SimpleFolder | \PHPUnit\Framework\MockObject\MockObject */
25-
private $folder;
26-
27-
/** @var \OC\Avatar\UserAvatar */
28-
private $avatar;
29-
30-
/** @var User|\PHPUnit\Framework\MockObject\MockObject $user */
31-
private $user;
32-
33-
/** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
34-
private $config;
3525

26+
private UserAvatar $avatar;
27+
private SimpleFolder&MockObject $folder;
28+
private IConfig&MockObject $config;
29+
private User&MockObject $user;
30+
3631
protected function setUp(): void {
3732
parent::setUp();
3833

@@ -236,7 +231,7 @@ public function testSetAvatar(): void {
236231
}
237232

238233
public function testGenerateSvgAvatar(): void {
239-
$avatar = $this->invokePrivate($this->avatar, 'getAvatarVector', [64, false]);
234+
$avatar = $this->invokePrivate($this->avatar, 'getAvatarVector', [$this->user->getDisplayName(), 64, false]);
240235

241236
$svg = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>
242237
<svg width="64" height="64" version="1.1" viewBox="0 0 500 500" xmlns="http://www.w3.org/2000/svg">
@@ -246,7 +241,6 @@ public function testGenerateSvgAvatar(): void {
246241
$this->assertEquals($avatar, $svg);
247242
}
248243

249-
250244
#[\PHPUnit\Framework\Attributes\DataProvider('avatarTextData')]
251245
public function testGetAvatarText($displayName, $expectedAvatarText): void {
252246
$user = $this->getUserWithDisplayName($displayName);

0 commit comments

Comments
 (0)