summaryrefslogtreecommitdiffstats
path: root/lib/private/Avatar
diff options
context:
space:
mode:
Diffstat (limited to 'lib/private/Avatar')
-rw-r--r--lib/private/Avatar/Avatar.php46
-rw-r--r--lib/private/Avatar/GuestAvatar.php28
-rw-r--r--lib/private/Avatar/PlaceholderAvatar.php25
-rw-r--r--lib/private/Avatar/UserAvatar.php48
4 files changed, 44 insertions, 103 deletions
diff --git a/lib/private/Avatar/Avatar.php b/lib/private/Avatar/Avatar.php
index f0e14ad8b2f..188e7378aff 100644
--- a/lib/private/Avatar/Avatar.php
+++ b/lib/private/Avatar/Avatar.php
@@ -46,9 +46,7 @@ use Psr\Log\LoggerInterface;
* This class gets and sets users avatars.
*/
abstract class Avatar implements IAvatar {
-
- /** @var LoggerInterface */
- protected $logger;
+ protected LoggerInterface $logger;
/**
* https://github.com/sebdesign/cap-height -- for 500px height
@@ -57,10 +55,8 @@ abstract class Avatar implements IAvatar {
* (0.4 letter-to-total-height ratio, 500*0.4=200), so: 200/0.715 = 280px.
* Since we start from the baseline (text-anchor) we need to
* shift the y axis by 100px (half the caps height): 500/2+100=350
- *
- * @var string
*/
- private $svgTemplate = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+ private string $svgTemplate = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg width="{size}" height="{size}" version="1.1" viewBox="0 0 500 500" xmlns="http://www.w3.org/2000/svg">
<rect width="100%" height="100%" fill="#{fill}"></rect>
<text x="50%" y="350" style="font-weight:normal;font-size:280px;font-family:\'Noto Sans\';text-anchor:middle;fill:#fff">{letter}</text>
@@ -72,15 +68,11 @@ abstract class Avatar implements IAvatar {
/**
* Returns the user display name.
- *
- * @return string
*/
abstract public function getDisplayName(): string;
/**
* Returns the first letter of the display name, or "?" if no name given.
- *
- * @return string
*/
private function getAvatarText(): string {
$displayName = $this->getDisplayName();
@@ -96,9 +88,7 @@ abstract class Avatar implements IAvatar {
/**
* @inheritdoc
*/
- public function get($size = 64) {
- $size = (int) $size;
-
+ public function get(int $size = 64) {
try {
$file = $this->getFile($size);
} catch (NotFoundException $e) {
@@ -158,12 +148,8 @@ abstract class Avatar implements IAvatar {
/**
* Generate png avatar with GD
- *
- * @param string $userDisplayName
- * @param int $size
- * @return string
*/
- protected function generateAvatar($userDisplayName, $size) {
+ protected function generateAvatar(string $userDisplayName, int $size): string {
$text = $this->getAvatarText();
$backgroundColor = $this->avatarBackgroundColor($userDisplayName);
@@ -209,7 +195,7 @@ abstract class Avatar implements IAvatar {
string $text,
string $font,
int $size,
- $angle = 0
+ int $angle = 0
): array {
// Image width & height
$xi = imagesx($image);
@@ -231,11 +217,11 @@ abstract class Avatar implements IAvatar {
/**
* Calculate steps between two Colors
- * @param object Color $steps start color
- * @param object Color $ends end color
- * @return array [r,g,b] steps for each color to go from $steps to $ends
+ * @param int $steps start color
+ * @param Color[] $ends end color
+ * @return array{0: int, 1: int, 2: int} [r,g,b] steps for each color to go from $steps to $ends
*/
- private function stepCalc($steps, $ends) {
+ private function stepCalc(int $steps, array $ends): array {
$step = [];
$step[0] = ($ends[1]->r - $ends[0]->r) / $steps;
$step[1] = ($ends[1]->g - $ends[0]->g) / $steps;
@@ -244,12 +230,11 @@ abstract class Avatar implements IAvatar {
}
/**
- * Convert a string to an integer evenly
- * @param string $hash the text to parse
- * @param int $maximum the maximum range
- * @return int[] between 0 and $maximum
+ * Mix two colors
+ *
+ * @return Color[]
*/
- private function mixPalette($steps, $color1, $color2) {
+ private function mixPalette($steps, Color $color1, Color $color2) {
$palette = [$color1];
$step = $this->stepCalc($steps, [$color1, $color2]);
for ($i = 1; $i < $steps; $i++) {
@@ -267,7 +252,7 @@ abstract class Avatar implements IAvatar {
* @param int $maximum the maximum range
* @return int between 0 and $maximum
*/
- private function hashToInt($hash, $maximum) {
+ private function hashToInt(string $hash, int $maximum): int {
$final = 0;
$result = [];
@@ -285,10 +270,9 @@ abstract class Avatar implements IAvatar {
}
/**
- * @param string $hash
* @return Color Object containting r g b int in the range [0, 255]
*/
- public function avatarBackgroundColor(string $hash) {
+ public function avatarBackgroundColor(string $hash): Color {
// Normalize hash
$hash = strtolower($hash);
diff --git a/lib/private/Avatar/GuestAvatar.php b/lib/private/Avatar/GuestAvatar.php
index f0297b3a93c..f3dde0840e9 100644
--- a/lib/private/Avatar/GuestAvatar.php
+++ b/lib/private/Avatar/GuestAvatar.php
@@ -26,6 +26,7 @@ declare(strict_types=1);
*/
namespace OC\Avatar;
+use OCP\Files\SimpleFS\ISimpleFile;
use OCP\Files\SimpleFS\InMemoryFile;
use Psr\Log\LoggerInterface;
@@ -35,16 +36,13 @@ use Psr\Log\LoggerInterface;
class GuestAvatar extends Avatar {
/**
* Holds the guest user display name.
- *
- * @var string
*/
- private $userDisplayName;
+ private string $userDisplayName;
/**
* GuestAvatar constructor.
*
* @param string $userDisplayName The guest user display name
- * @param LoggerInterface $logger The logger
*/
public function __construct(string $userDisplayName, LoggerInterface $logger) {
parent::__construct($logger);
@@ -53,17 +51,13 @@ class GuestAvatar extends Avatar {
/**
* Tests if the user has an avatar.
- *
- * @return true Guests always have an avatar.
*/
- public function exists() {
- return true;
+ public function exists(): bool {
+ return true; // Guests always have an avatar.
}
/**
* Returns the guest user display name.
- *
- * @return string
*/
public function getDisplayName(): string {
return $this->userDisplayName;
@@ -75,24 +69,21 @@ class GuestAvatar extends Avatar {
* @param \OCP\IImage|resource|string $data
* @return void
*/
- public function set($data) {
+ public function set($data): void {
// unimplemented for guest user avatars
}
/**
* Removing avatars isn't implemented for guests.
*/
- public function remove() {
+ public function remove(bool $silent = false): void {
// unimplemented for guest user avatars
}
/**
* Generates an avatar for the guest.
- *
- * @param int $size The desired image size.
- * @return InMemoryFile
*/
- public function getFile($size) {
+ public function getFile(int $size): ISimpleFile {
$avatar = $this->generateAvatar($this->userDisplayName, $size);
return new InMemoryFile('avatar.png', $avatar);
}
@@ -103,9 +94,8 @@ class GuestAvatar extends Avatar {
* @param string $feature The changed feature
* @param mixed $oldValue The previous value
* @param mixed $newValue The new value
- * @return void
*/
- public function userChanged($feature, $oldValue, $newValue) {
+ public function userChanged(string $feature, $oldValue, $newValue): void {
if ($feature === 'displayName') {
$this->userDisplayName = $newValue;
}
@@ -113,8 +103,6 @@ class GuestAvatar extends Avatar {
/**
* Guests don't have custom avatars.
- *
- * @return bool
*/
public function isCustomAvatar(): bool {
return false;
diff --git a/lib/private/Avatar/PlaceholderAvatar.php b/lib/private/Avatar/PlaceholderAvatar.php
index df7a490cbe4..504e5c1457d 100644
--- a/lib/private/Avatar/PlaceholderAvatar.php
+++ b/lib/private/Avatar/PlaceholderAvatar.php
@@ -44,11 +44,8 @@ use Psr\Log\LoggerInterface;
* for faster retrieval, unlike the GuestAvatar.
*/
class PlaceholderAvatar extends Avatar {
- /** @var ISimpleFolder */
- private $folder;
-
- /** @var User */
- private $user;
+ private ISimpleFolder $folder;
+ private User $user;
/**
* UserAvatar constructor.
@@ -71,10 +68,8 @@ class PlaceholderAvatar extends Avatar {
/**
* Check if an avatar exists for the user
- *
- * @return bool
*/
- public function exists() {
+ public function exists(): bool {
return true;
}
@@ -87,14 +82,14 @@ class PlaceholderAvatar extends Avatar {
* @throws NotSquareException if the image is not square
* @return void
*/
- public function set($data) {
+ public function set($data): void {
// unimplemented for placeholder avatars
}
/**
* Removes the users avatar.
*/
- public function remove(bool $silent = false) {
+ public function remove(bool $silent = false): void {
$avatars = $this->folder->getDirectoryListing();
foreach ($avatars as $avatar) {
@@ -113,9 +108,7 @@ class PlaceholderAvatar extends Avatar {
* @throws \OCP\Files\NotPermittedException
* @throws \OCP\PreConditionNotMetException
*/
- public function getFile($size) {
- $size = (int) $size;
-
+ public function getFile(int $size): ISimpleFile {
$ext = 'png';
if ($size === -1) {
@@ -149,8 +142,6 @@ class PlaceholderAvatar extends Avatar {
/**
* Returns the user display name.
- *
- * @return string
*/
public function getDisplayName(): string {
return $this->user->getDisplayName();
@@ -165,14 +156,12 @@ class PlaceholderAvatar extends Avatar {
* @throws NotPermittedException
* @throws \OCP\PreConditionNotMetException
*/
- public function userChanged($feature, $oldValue, $newValue) {
+ public function userChanged(string $feature, $oldValue, $newValue): void {
$this->remove();
}
/**
* Check if the avatar of a user is a custom uploaded one
- *
- * @return bool
*/
public function isCustomAvatar(): bool {
return false;
diff --git a/lib/private/Avatar/UserAvatar.php b/lib/private/Avatar/UserAvatar.php
index 6cca5a1c355..191a6ebfff4 100644
--- a/lib/private/Avatar/UserAvatar.php
+++ b/lib/private/Avatar/UserAvatar.php
@@ -44,17 +44,10 @@ use Psr\Log\LoggerInterface;
* This class represents a registered user's avatar.
*/
class UserAvatar extends Avatar {
- /** @var IConfig */
- private $config;
-
- /** @var ISimpleFolder */
- private $folder;
-
- /** @var IL10N */
- private $l;
-
- /** @var User */
- private $user;
+ private IConfig $config;
+ private ISimpleFolder $folder;
+ private IL10N $l;
+ private User $user;
/**
* UserAvatar constructor.
@@ -68,7 +61,7 @@ class UserAvatar extends Avatar {
public function __construct(
ISimpleFolder $folder,
IL10N $l,
- $user,
+ User $user,
LoggerInterface $logger,
IConfig $config) {
parent::__construct($logger);
@@ -80,10 +73,8 @@ class UserAvatar extends Avatar {
/**
* Check if an avatar exists for the user
- *
- * @return bool
*/
- public function exists() {
+ public function exists(): bool {
return $this->folder->fileExists('avatar.jpg') || $this->folder->fileExists('avatar.png');
}
@@ -96,7 +87,7 @@ class UserAvatar extends Avatar {
* @throws NotSquareException if the image is not square
* @return void
*/
- public function set($data) {
+ public function set($data): void {
$img = $this->getAvatarImage($data);
$data = $img->data();
@@ -124,7 +115,7 @@ class UserAvatar extends Avatar {
* @param IImage|resource|string|\GdImage $data An image object, imagedata or path to the avatar
* @return IImage
*/
- private function getAvatarImage($data) {
+ private function getAvatarImage($data): IImage {
if ($data instanceof IImage) {
return $data;
}
@@ -156,11 +147,8 @@ class UserAvatar extends Avatar {
/**
* Returns the avatar image type.
- *
- * @param IImage $avatar
- * @return string
*/
- private function getAvatarImageType(IImage $avatar) {
+ private function getAvatarImageType(IImage $avatar): string {
$type = substr($avatar->mimeType(), -3);
if ($type === 'peg') {
$type = 'jpg';
@@ -179,7 +167,7 @@ class UserAvatar extends Avatar {
* @throws \Exception if the provided image is not valid
* @throws NotSquareException if the image is not square
*/
- private function validateAvatar(IImage $avatar) {
+ private function validateAvatar(IImage $avatar): void {
$type = $this->getAvatarImageType($avatar);
if ($type !== 'jpg' && $type !== 'png') {
@@ -197,11 +185,10 @@ class UserAvatar extends Avatar {
/**
* Removes the users avatar.
- * @return void
* @throws \OCP\Files\NotPermittedException
* @throws \OCP\PreConditionNotMetException
*/
- public function remove(bool $silent = false) {
+ public function remove(bool $silent = false): void {
$avatars = $this->folder->getDirectoryListing();
$this->config->setUserValue($this->user->getUID(), 'avatar', 'version',
@@ -219,10 +206,9 @@ class UserAvatar extends Avatar {
/**
* Get the extension of the avatar. If there is no avatar throw Exception
*
- * @return string
* @throws NotFoundException
*/
- private function getExtension() {
+ private function getExtension(): string {
if ($this->folder->fileExists('avatar.jpg')) {
return 'jpg';
} elseif ($this->folder->fileExists('avatar.png')) {
@@ -242,9 +228,7 @@ class UserAvatar extends Avatar {
* @throws \OCP\Files\NotPermittedException
* @throws \OCP\PreConditionNotMetException
*/
- public function getFile($size) {
- $size = (int) $size;
-
+ public function getFile(int $size): ISimpleFile {
try {
$ext = $this->getExtension();
} catch (NotFoundException $e) {
@@ -304,8 +288,6 @@ class UserAvatar extends Avatar {
/**
* Returns the user display name.
- *
- * @return string
*/
public function getDisplayName(): string {
return $this->user->getDisplayName();
@@ -320,7 +302,7 @@ class UserAvatar extends Avatar {
* @throws NotPermittedException
* @throws \OCP\PreConditionNotMetException
*/
- public function userChanged($feature, $oldValue, $newValue) {
+ public function userChanged(string $feature, $oldValue, $newValue): void {
// If the avatar is not generated (so an uploaded image) we skip this
if (!$this->folder->fileExists('generated')) {
return;
@@ -331,8 +313,6 @@ class UserAvatar extends Avatar {
/**
* Check if the avatar of a user is a custom uploaded one
- *
- * @return bool
*/
public function isCustomAvatar(): bool {
return $this->config->getUserValue($this->user->getUID(), 'avatar', 'generated', 'false') !== 'true';