Skip to content

Commit 812016d

Browse files
committed
Cleanup avatar related code
- Move event listener to new event handling - Add typing almost everywhere - Fix inconsistent interface parameter Signed-off-by: Carl Schwan <carl@carlschwan.eu>
1 parent b282fe1 commit 812016d

File tree

14 files changed

+215
-190
lines changed

14 files changed

+215
-190
lines changed

build/psalm-baseline.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2497,12 +2497,6 @@
24972497
<ImplementedReturnTypeMismatch occurrences="1">
24982498
<code>Color</code>
24992499
</ImplementedReturnTypeMismatch>
2500-
<InvalidReturnStatement occurrences="1">
2501-
<code>$finalPalette[$this-&gt;hashToInt($hash, $steps * 3)]</code>
2502-
</InvalidReturnStatement>
2503-
<InvalidReturnType occurrences="1">
2504-
<code>Color</code>
2505-
</InvalidReturnType>
25062500
<ParamNameMismatch occurrences="1">
25072501
<code>$hash</code>
25082502
</ParamNameMismatch>

lib/composer/composer/autoload_classmap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1548,6 +1548,8 @@
15481548
'OC\\User\\Database' => $baseDir . '/lib/private/User/Database.php',
15491549
'OC\\User\\DisplayNameCache' => $baseDir . '/lib/private/User/DisplayNameCache.php',
15501550
'OC\\User\\LazyUser' => $baseDir . '/lib/private/User/LazyUser.php',
1551+
'OC\\User\\Listeners\\UserChangedListener' => $baseDir . '/lib/private/User/Listeners/UserChangedListener.php',
1552+
'OC\\User\\Listeners\\UserDeletedListener' => $baseDir . '/lib/private/User/Listeners/UserDeletedListener.php',
15511553
'OC\\User\\LoginException' => $baseDir . '/lib/private/User/LoginException.php',
15521554
'OC\\User\\Manager' => $baseDir . '/lib/private/User/Manager.php',
15531555
'OC\\User\\NoUserException' => $baseDir . '/lib/private/User/NoUserException.php',

lib/composer/composer/autoload_static.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,6 +1581,8 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
15811581
'OC\\User\\Database' => __DIR__ . '/../../..' . '/lib/private/User/Database.php',
15821582
'OC\\User\\DisplayNameCache' => __DIR__ . '/../../..' . '/lib/private/User/DisplayNameCache.php',
15831583
'OC\\User\\LazyUser' => __DIR__ . '/../../..' . '/lib/private/User/LazyUser.php',
1584+
'OC\\User\\Listeners\\UserChangedListener' => __DIR__ . '/../../..' . '/lib/private/User/Listeners/UserChangedListener.php',
1585+
'OC\\User\\Listeners\\UserDeletedListener' => __DIR__ . '/../../..' . '/lib/private/User/Listeners/UserDeletedListener.php',
15841586
'OC\\User\\LoginException' => __DIR__ . '/../../..' . '/lib/private/User/LoginException.php',
15851587
'OC\\User\\Manager' => __DIR__ . '/../../..' . '/lib/private/User/Manager.php',
15861588
'OC\\User\\NoUserException' => __DIR__ . '/../../..' . '/lib/private/User/NoUserException.php',

lib/private/Avatar/Avatar.php

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@
4646
* This class gets and sets users avatars.
4747
*/
4848
abstract class Avatar implements IAvatar {
49-
50-
/** @var LoggerInterface */
51-
protected $logger;
49+
protected LoggerInterface $logger;
5250

5351
/**
5452
* https://github.com/sebdesign/cap-height -- for 500px height
@@ -57,10 +55,8 @@ abstract class Avatar implements IAvatar {
5755
* (0.4 letter-to-total-height ratio, 500*0.4=200), so: 200/0.715 = 280px.
5856
* Since we start from the baseline (text-anchor) we need to
5957
* shift the y axis by 100px (half the caps height): 500/2+100=350
60-
*
61-
* @var string
6258
*/
63-
private $svgTemplate = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>
59+
private string $svgTemplate = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>
6460
<svg width="{size}" height="{size}" version="1.1" viewBox="0 0 500 500" xmlns="http://www.w3.org/2000/svg">
6561
<rect width="100%" height="100%" fill="#{fill}"></rect>
6662
<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 @@ public function __construct(LoggerInterface $logger) {
7268

7369
/**
7470
* Returns the user display name.
75-
*
76-
* @return string
7771
*/
7872
abstract public function getDisplayName(): string;
7973

8074
/**
8175
* Returns the first letter of the display name, or "?" if no name given.
82-
*
83-
* @return string
8476
*/
8577
private function getAvatarText(): string {
8678
$displayName = $this->getDisplayName();
@@ -96,9 +88,7 @@ private function getAvatarText(): string {
9688
/**
9789
* @inheritdoc
9890
*/
99-
public function get($size = 64) {
100-
$size = (int) $size;
101-
91+
public function get(int $size = 64) {
10292
try {
10393
$file = $this->getFile($size);
10494
} catch (NotFoundException $e) {
@@ -158,12 +148,8 @@ protected function generateAvatarFromSvg(int $size) {
158148

159149
/**
160150
* Generate png avatar with GD
161-
*
162-
* @param string $userDisplayName
163-
* @param int $size
164-
* @return string
165151
*/
166-
protected function generateAvatar($userDisplayName, $size) {
152+
protected function generateAvatar(string $userDisplayName, int $size): string {
167153
$text = $this->getAvatarText();
168154
$backgroundColor = $this->avatarBackgroundColor($userDisplayName);
169155

@@ -209,7 +195,7 @@ protected function imageTTFCenter(
209195
string $text,
210196
string $font,
211197
int $size,
212-
$angle = 0
198+
int $angle = 0
213199
): array {
214200
// Image width & height
215201
$xi = imagesx($image);
@@ -231,11 +217,11 @@ protected function imageTTFCenter(
231217

232218
/**
233219
* Calculate steps between two Colors
234-
* @param object Color $steps start color
235-
* @param object Color $ends end color
236-
* @return array [r,g,b] steps for each color to go from $steps to $ends
220+
* @param int $steps start color
221+
* @param Color[] $ends end color
222+
* @return array{0: int, 1: int, 2: int} [r,g,b] steps for each color to go from $steps to $ends
237223
*/
238-
private function stepCalc($steps, $ends) {
224+
private function stepCalc(int $steps, array $ends): array {
239225
$step = [];
240226
$step[0] = ($ends[1]->r - $ends[0]->r) / $steps;
241227
$step[1] = ($ends[1]->g - $ends[0]->g) / $steps;
@@ -244,12 +230,11 @@ private function stepCalc($steps, $ends) {
244230
}
245231

246232
/**
247-
* Convert a string to an integer evenly
248-
* @param string $hash the text to parse
249-
* @param int $maximum the maximum range
250-
* @return int[] between 0 and $maximum
233+
* Mix two colors
234+
*
235+
* @return Color[]
251236
*/
252-
private function mixPalette($steps, $color1, $color2) {
237+
private function mixPalette($steps, Color $color1, Color $color2) {
253238
$palette = [$color1];
254239
$step = $this->stepCalc($steps, [$color1, $color2]);
255240
for ($i = 1; $i < $steps; $i++) {
@@ -267,7 +252,7 @@ private function mixPalette($steps, $color1, $color2) {
267252
* @param int $maximum the maximum range
268253
* @return int between 0 and $maximum
269254
*/
270-
private function hashToInt($hash, $maximum) {
255+
private function hashToInt(string $hash, int $maximum): int {
271256
$final = 0;
272257
$result = [];
273258

@@ -285,10 +270,9 @@ private function hashToInt($hash, $maximum) {
285270
}
286271

287272
/**
288-
* @param string $hash
289273
* @return Color Object containting r g b int in the range [0, 255]
290274
*/
291-
public function avatarBackgroundColor(string $hash) {
275+
public function avatarBackgroundColor(string $hash): Color {
292276
// Normalize hash
293277
$hash = strtolower($hash);
294278

lib/private/Avatar/GuestAvatar.php

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
*/
2727
namespace OC\Avatar;
2828

29+
use OCP\Files\SimpleFS\ISimpleFile;
2930
use OCP\Files\SimpleFS\InMemoryFile;
3031
use Psr\Log\LoggerInterface;
3132

@@ -35,16 +36,13 @@
3536
class GuestAvatar extends Avatar {
3637
/**
3738
* Holds the guest user display name.
38-
*
39-
* @var string
4039
*/
41-
private $userDisplayName;
40+
private string $userDisplayName;
4241

4342
/**
4443
* GuestAvatar constructor.
4544
*
4645
* @param string $userDisplayName The guest user display name
47-
* @param LoggerInterface $logger The logger
4846
*/
4947
public function __construct(string $userDisplayName, LoggerInterface $logger) {
5048
parent::__construct($logger);
@@ -53,17 +51,13 @@ public function __construct(string $userDisplayName, LoggerInterface $logger) {
5351

5452
/**
5553
* Tests if the user has an avatar.
56-
*
57-
* @return true Guests always have an avatar.
5854
*/
59-
public function exists() {
60-
return true;
55+
public function exists(): bool {
56+
return true; // Guests always have an avatar.
6157
}
6258

6359
/**
6460
* Returns the guest user display name.
65-
*
66-
* @return string
6761
*/
6862
public function getDisplayName(): string {
6963
return $this->userDisplayName;
@@ -75,24 +69,21 @@ public function getDisplayName(): string {
7569
* @param \OCP\IImage|resource|string $data
7670
* @return void
7771
*/
78-
public function set($data) {
72+
public function set($data): void {
7973
// unimplemented for guest user avatars
8074
}
8175

8276
/**
8377
* Removing avatars isn't implemented for guests.
8478
*/
85-
public function remove() {
79+
public function remove(bool $silent = false): void {
8680
// unimplemented for guest user avatars
8781
}
8882

8983
/**
9084
* Generates an avatar for the guest.
91-
*
92-
* @param int $size The desired image size.
93-
* @return InMemoryFile
9485
*/
95-
public function getFile($size) {
86+
public function getFile(int $size): ISimpleFile {
9687
$avatar = $this->generateAvatar($this->userDisplayName, $size);
9788
return new InMemoryFile('avatar.png', $avatar);
9889
}
@@ -103,18 +94,15 @@ public function getFile($size) {
10394
* @param string $feature The changed feature
10495
* @param mixed $oldValue The previous value
10596
* @param mixed $newValue The new value
106-
* @return void
10797
*/
108-
public function userChanged($feature, $oldValue, $newValue) {
98+
public function userChanged(string $feature, $oldValue, $newValue): void {
10999
if ($feature === 'displayName') {
110100
$this->userDisplayName = $newValue;
111101
}
112102
}
113103

114104
/**
115105
* Guests don't have custom avatars.
116-
*
117-
* @return bool
118106
*/
119107
public function isCustomAvatar(): bool {
120108
return false;

lib/private/Avatar/PlaceholderAvatar.php

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,8 @@
4444
* for faster retrieval, unlike the GuestAvatar.
4545
*/
4646
class PlaceholderAvatar extends Avatar {
47-
/** @var ISimpleFolder */
48-
private $folder;
49-
50-
/** @var User */
51-
private $user;
47+
private ISimpleFolder $folder;
48+
private User $user;
5249

5350
/**
5451
* UserAvatar constructor.
@@ -71,10 +68,8 @@ public function __construct(
7168

7269
/**
7370
* Check if an avatar exists for the user
74-
*
75-
* @return bool
7671
*/
77-
public function exists() {
72+
public function exists(): bool {
7873
return true;
7974
}
8075

@@ -87,14 +82,14 @@ public function exists() {
8782
* @throws NotSquareException if the image is not square
8883
* @return void
8984
*/
90-
public function set($data) {
85+
public function set($data): void {
9186
// unimplemented for placeholder avatars
9287
}
9388

9489
/**
9590
* Removes the users avatar.
9691
*/
97-
public function remove(bool $silent = false) {
92+
public function remove(bool $silent = false): void {
9893
$avatars = $this->folder->getDirectoryListing();
9994

10095
foreach ($avatars as $avatar) {
@@ -113,9 +108,7 @@ public function remove(bool $silent = false) {
113108
* @throws \OCP\Files\NotPermittedException
114109
* @throws \OCP\PreConditionNotMetException
115110
*/
116-
public function getFile($size) {
117-
$size = (int) $size;
118-
111+
public function getFile(int $size): ISimpleFile {
119112
$ext = 'png';
120113

121114
if ($size === -1) {
@@ -149,8 +142,6 @@ public function getFile($size) {
149142

150143
/**
151144
* Returns the user display name.
152-
*
153-
* @return string
154145
*/
155146
public function getDisplayName(): string {
156147
return $this->user->getDisplayName();
@@ -165,14 +156,12 @@ public function getDisplayName(): string {
165156
* @throws NotPermittedException
166157
* @throws \OCP\PreConditionNotMetException
167158
*/
168-
public function userChanged($feature, $oldValue, $newValue) {
159+
public function userChanged(string $feature, $oldValue, $newValue): void {
169160
$this->remove();
170161
}
171162

172163
/**
173164
* Check if the avatar of a user is a custom uploaded one
174-
*
175-
* @return bool
176165
*/
177166
public function isCustomAvatar(): bool {
178167
return false;

0 commit comments

Comments
 (0)