mirror of
https://github.com/nextcloud/server.git
synced 2026-02-03 20:41:22 -05:00
fix: resolve psalm errors
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
This commit is contained in:
parent
655ef1031b
commit
7e76c91677
8 changed files with 44 additions and 33 deletions
|
|
@ -10,19 +10,17 @@ declare(strict_types=1);
|
|||
namespace OC\Avatar;
|
||||
|
||||
use Imagick;
|
||||
use OC\User\User;
|
||||
use OCP\Color;
|
||||
use OCP\Files\NotFoundException;
|
||||
use OCP\IAvatar;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use OC\User\User;
|
||||
use OCP\IConfig;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
* This class gets and sets users avatars.
|
||||
*/
|
||||
abstract class Avatar implements IAvatar {
|
||||
protected LoggerInterface $logger;
|
||||
|
||||
/**
|
||||
* https://github.com/sebdesign/cap-height -- for 500px height
|
||||
* Automated check: https://codepen.io/skjnldsv/pen/PydLBK/
|
||||
|
|
@ -37,8 +35,10 @@ abstract class Avatar implements IAvatar {
|
|||
<text x="50%" y="350" style="font-weight:normal;font-size:280px;font-family:\'Noto Sans\';text-anchor:middle;fill:#{fgFill}">{letter}</text>
|
||||
</svg>';
|
||||
|
||||
public function __construct(LoggerInterface $logger) {
|
||||
$this->logger = $logger;
|
||||
public function __construct(
|
||||
protected IConfig $config,
|
||||
protected LoggerInterface $logger,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -97,12 +97,11 @@ abstract class Avatar implements IAvatar {
|
|||
}
|
||||
|
||||
/**
|
||||
* Select the rendering font based on the user's display name and language
|
||||
* Select the rendering font based on the user's display name and language
|
||||
*/
|
||||
private function getFont(string $userDisplayName): string {
|
||||
if (preg_match('/\p{Han}/u', $userDisplayName) === 1) {
|
||||
$userlang = $this->config->getUserValue($this->user->getUID(), 'core', 'lang', '');
|
||||
switch ($userlang) {
|
||||
switch ($this->getAvatarLanguage()) {
|
||||
case 'zh_TW':
|
||||
return __DIR__ . '/../../../core/fonts/NotoSansTC-Regular.ttf';
|
||||
case 'zh_HK':
|
||||
|
|
@ -129,7 +128,7 @@ abstract class Avatar implements IAvatar {
|
|||
// Avatar generation breaks if RSVG format is enabled. Fall back to gd in that case
|
||||
if (in_array('RSVG', $formats, true)) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
$text = $this->getAvatarText();
|
||||
try {
|
||||
$font = $this->getFont($text);
|
||||
|
|
@ -282,4 +281,12 @@ abstract class Avatar implements IAvatar {
|
|||
|
||||
return $finalPalette[$this->hashToInt($hash, $steps * 3)];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the language to be used for avatar generation.
|
||||
* This is used to determine the font to use for the avatar text (e.g. CJK characters).
|
||||
*/
|
||||
protected function getAvatarLanguage(): string {
|
||||
return $this->config->getSystemValueString('default_language', 'en');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -92,10 +92,10 @@ class AvatarManager implements IAvatarManager {
|
|||
return new UserAvatar($folder, $this->l, $user, $this->logger, $this->config);
|
||||
default:
|
||||
// use a placeholder avatar which caches the generated images
|
||||
return new PlaceholderAvatar($folder, $user, $this->logger);
|
||||
return new PlaceholderAvatar($folder, $user, $this->config, $this->logger);
|
||||
}
|
||||
|
||||
return new PlaceholderAvatar($folder, $user, $this->logger);
|
||||
return new PlaceholderAvatar($folder, $user, $this->config, $this->logger);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -129,6 +129,6 @@ class AvatarManager implements IAvatarManager {
|
|||
* @param string $name The guest name, e.g. "Albert".
|
||||
*/
|
||||
public function getGuestAvatar(string $name): IAvatar {
|
||||
return new GuestAvatar($name, $this->logger);
|
||||
return new GuestAvatar($name, $this->config, $this->logger);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ namespace OC\Avatar;
|
|||
|
||||
use OCP\Files\SimpleFS\InMemoryFile;
|
||||
use OCP\Files\SimpleFS\ISimpleFile;
|
||||
use OCP\IConfig;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
|
|
@ -23,9 +24,10 @@ class GuestAvatar extends Avatar {
|
|||
*/
|
||||
public function __construct(
|
||||
private string $userDisplayName,
|
||||
IConfig $config,
|
||||
LoggerInterface $logger,
|
||||
) {
|
||||
parent::__construct($logger);
|
||||
parent::__construct($config, $logger);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ use OCP\Files\NotFoundException;
|
|||
use OCP\Files\NotPermittedException;
|
||||
use OCP\Files\SimpleFS\ISimpleFile;
|
||||
use OCP\Files\SimpleFS\ISimpleFolder;
|
||||
use OCP\IConfig;
|
||||
use OCP\IImage;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
|
|
@ -27,9 +28,10 @@ class PlaceholderAvatar extends Avatar {
|
|||
public function __construct(
|
||||
private ISimpleFolder $folder,
|
||||
private User $user,
|
||||
IConfig $config,
|
||||
LoggerInterface $logger,
|
||||
) {
|
||||
parent::__construct($logger);
|
||||
parent::__construct($config, $logger);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -28,9 +28,9 @@ class UserAvatar extends Avatar {
|
|||
private IL10N $l,
|
||||
protected User $user,
|
||||
LoggerInterface $logger,
|
||||
protected IConfig $config,
|
||||
IConfig $config,
|
||||
) {
|
||||
parent::__construct($logger);
|
||||
parent::__construct($config, $logger);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -295,4 +295,9 @@ class UserAvatar extends Avatar {
|
|||
public function isCustomAvatar(): bool {
|
||||
return $this->config->getUserValue($this->user->getUID(), 'avatar', 'generated', 'false') !== 'true';
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
protected function getAvatarLanguage(): string {
|
||||
return $this->config->getUserValue($this->user->getUID(), 'core', 'lang', parent::getAvatarLanguage());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -269,7 +269,7 @@ class AvatarManagerTest extends \Test\TestCase {
|
|||
}
|
||||
|
||||
if ($expectedPlaceholder) {
|
||||
$expected = new PlaceholderAvatar($folder, $user, $this->createMock(LoggerInterface::class));
|
||||
$expected = new PlaceholderAvatar($folder, $user, $this->config, $this->logger);
|
||||
} else {
|
||||
$expected = new UserAvatar($folder, $this->l10n, $user, $this->logger, $this->config);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,8 +34,9 @@ class GuestAvatarTest extends TestCase {
|
|||
*/
|
||||
public function setupGuestAvatar() {
|
||||
/* @var MockObject|LoggerInterface $logger */
|
||||
$logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
|
||||
$this->guestAvatar = new GuestAvatar('einstein', $logger);
|
||||
$logger = $this->createMock(LoggerInterface::class);
|
||||
$config = $this->createMock(\OCP\IConfig::class);
|
||||
$this->guestAvatar = new GuestAvatar('einstein', $config, $logger);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -18,21 +18,16 @@ use OCP\Files\SimpleFS\ISimpleFile;
|
|||
use OCP\IConfig;
|
||||
use OCP\IL10N;
|
||||
use OCP\Image;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class UserAvatarTest extends \Test\TestCase {
|
||||
/** @var SimpleFolder | \PHPUnit\Framework\MockObject\MockObject */
|
||||
private $folder;
|
||||
|
||||
/** @var \OC\Avatar\UserAvatar */
|
||||
private $avatar;
|
||||
|
||||
/** @var User|\PHPUnit\Framework\MockObject\MockObject $user */
|
||||
private $user;
|
||||
|
||||
/** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
|
||||
private $config;
|
||||
|
||||
private UserAvatar $avatar;
|
||||
private SimpleFolder&MockObject $folder;
|
||||
private IConfig&MockObject $config;
|
||||
private User&MockObject $user;
|
||||
|
||||
protected function setUp(): void {
|
||||
parent::setUp();
|
||||
|
||||
|
|
@ -236,7 +231,7 @@ class UserAvatarTest extends \Test\TestCase {
|
|||
}
|
||||
|
||||
public function testGenerateSvgAvatar(): void {
|
||||
$avatar = $this->invokePrivate($this->avatar, 'getAvatarVector', [64, false]);
|
||||
$avatar = $this->invokePrivate($this->avatar, 'getAvatarVector', [$this->user->getDisplayName(), 64, false]);
|
||||
|
||||
$svg = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<svg width="64" height="64" version="1.1" viewBox="0 0 500 500" xmlns="http://www.w3.org/2000/svg">
|
||||
|
|
@ -246,7 +241,6 @@ class UserAvatarTest extends \Test\TestCase {
|
|||
$this->assertEquals($avatar, $svg);
|
||||
}
|
||||
|
||||
|
||||
#[\PHPUnit\Framework\Attributes\DataProvider('avatarTextData')]
|
||||
public function testGetAvatarText($displayName, $expectedAvatarText): void {
|
||||
$user = $this->getUserWithDisplayName($displayName);
|
||||
|
|
|
|||
Loading…
Reference in a new issue