Skip to content

Commit c9347aa

Browse files
committed
Maintenance: Started work for PHPStan Level 4
1 parent e033578 commit c9347aa

15 files changed

+47
-59
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/node_modules
33
/.vscode
44
/composer
5+
/composer.phar
56
/coverage
67
Homestead.yaml
78
.env

app/Access/EmailConfirmationService.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use BookStack\Access\Notifications\ConfirmEmailNotification;
66
use BookStack\Exceptions\ConfirmationEmailException;
77
use BookStack\Users\Models\User;
8+
use Exception;
89

910
class EmailConfirmationService extends UserTokenService
1011
{
@@ -16,6 +17,7 @@ class EmailConfirmationService extends UserTokenService
1617
* Also removes any existing old ones.
1718
*
1819
* @throws ConfirmationEmailException
20+
* @throws Exception
1921
*/
2022
public function sendConfirmation(User $user): void
2123
{

app/Access/LoginService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function reattemptLoginFor(User $user): void
7171
}
7272

7373
$lastLoginDetails = $this->getLastLoginAttemptDetails();
74-
$this->login($user, $lastLoginDetails['method'], $lastLoginDetails['remember'] ?? false);
74+
$this->login($user, $lastLoginDetails['method'], $lastLoginDetails['remember']);
7575
}
7676

7777
/**

app/Access/Mfa/MfaValue.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,16 @@ public static function upsertWithValue(User $user, string $method, string $value
4848
}
4949

5050
/**
51-
* Easily get the decrypted MFA value for the given user and method.
51+
* Get the decrypted MFA value for the given user and method.
5252
*/
5353
public static function getValueForUser(User $user, string $method): ?string
5454
{
55-
/** @var MfaValue $mfaVal */
5655
$mfaVal = static::query()
5756
->where('user_id', '=', $user->id)
5857
->where('method', '=', $method)
5958
->first();
6059

61-
return $mfaVal ? $mfaVal->getValue() : null;
60+
return $mfaVal?->getValue();
6261
}
6362

6463
/**

app/Access/Oidc/OidcJwtSigningKey.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,21 @@
99

1010
class OidcJwtSigningKey
1111
{
12-
/**
13-
* @var PublicKey
14-
*/
15-
protected $key;
12+
protected PublicKey $key;
1613

1714
/**
1815
* Can be created either from a JWK parameter array or local file path to load a certificate from.
1916
* Examples:
2017
* 'file:///var/www/cert.pem'
2118
* ['kty' => 'RSA', 'alg' => 'RS256', 'n' => 'abc123...'].
2219
*
23-
* @param array|string $jwkOrKeyPath
24-
*
2520
* @throws OidcInvalidKeyException
2621
*/
27-
public function __construct($jwkOrKeyPath)
22+
public function __construct(array|string $jwkOrKeyPath)
2823
{
2924
if (is_array($jwkOrKeyPath)) {
3025
$this->loadFromJwkArray($jwkOrKeyPath);
31-
} elseif (is_string($jwkOrKeyPath) && strpos($jwkOrKeyPath, 'file://') === 0) {
26+
} elseif (str_starts_with($jwkOrKeyPath, 'file://')) {
3227
$this->loadFromPath($jwkOrKeyPath);
3328
} else {
3429
throw new OidcInvalidKeyException('Unexpected type of key value provided');
@@ -38,7 +33,7 @@ public function __construct($jwkOrKeyPath)
3833
/**
3934
* @throws OidcInvalidKeyException
4035
*/
41-
protected function loadFromPath(string $path)
36+
protected function loadFromPath(string $path): void
4237
{
4338
try {
4439
$key = PublicKeyLoader::load(
@@ -58,7 +53,7 @@ protected function loadFromPath(string $path)
5853
/**
5954
* @throws OidcInvalidKeyException
6055
*/
61-
protected function loadFromJwkArray(array $jwk)
56+
protected function loadFromJwkArray(array $jwk): void
6257
{
6358
// 'alg' is optional for a JWK, but we will still attempt to validate if
6459
// it exists otherwise presume it will be compatible.
@@ -82,7 +77,7 @@ protected function loadFromJwkArray(array $jwk)
8277
throw new OidcInvalidKeyException('A "n" parameter on the provided key is expected');
8378
}
8479

85-
$n = strtr($jwk['n'] ?? '', '-_', '+/');
80+
$n = strtr($jwk['n'], '-_', '+/');
8681

8782
try {
8883
$key = PublicKeyLoader::load([

app/Access/Oidc/OidcJwtWithClaims.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,12 @@ public function replaceClaims(array $claims): void
102102
protected function validateTokenStructure(): void
103103
{
104104
foreach (['header', 'payload'] as $prop) {
105-
if (empty($this->$prop) || !is_array($this->$prop)) {
105+
if (empty($this->$prop)) {
106106
throw new OidcInvalidTokenException("Could not parse out a valid {$prop} within the provided token");
107107
}
108108
}
109109

110-
if (empty($this->signature) || !is_string($this->signature)) {
110+
if (empty($this->signature)) {
111111
throw new OidcInvalidTokenException('Could not parse out a valid signature within the provided token');
112112
}
113113
}

app/Access/Oidc/OidcUserDetails.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function populate(
3939
): void {
4040
$this->externalId = $claims->getClaim($idClaim) ?? $this->externalId;
4141
$this->email = $claims->getClaim('email') ?? $this->email;
42-
$this->name = static::getUserDisplayName($displayNameClaims, $claims) ?? $this->name;
42+
$this->name = static::getUserDisplayName($displayNameClaims, $claims) ?: $this->name;
4343
$this->groups = static::getUserGroups($groupsClaim, $claims) ?? $this->groups;
4444
$this->picture = static::getPicture($claims) ?: $this->picture;
4545
}

app/Access/Saml2Service.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ protected function getExternalId(array $samlAttributes, string $defaultValue)
266266
/**
267267
* Extract the details of a user from a SAML response.
268268
*
269-
* @return array{external_id: string, name: string, email: string, saml_id: string}
269+
* @return array{external_id: string, name: string, email: string|null, saml_id: string}
270270
*/
271271
protected function getUserDetails(string $samlID, $samlAttributes): array
272272
{
@@ -357,7 +357,7 @@ public function processLoginCallback(string $samlID, array $samlAttributes): Use
357357
]);
358358
}
359359

360-
if ($userDetails['email'] === null) {
360+
if (empty($userDetails['email'])) {
361361
throw new SamlException(trans('errors.saml_no_email_address'));
362362
}
363363

app/Access/SocialAuthService.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,14 @@ public function handleLoginCallback(string $socialDriver, SocialUser $socialUser
117117
}
118118

119119
// When a user is logged in and the social account exists and is already linked to the current user.
120-
if ($isLoggedIn && $socialAccount !== null && $socialAccount->user->id === $currentUser->id) {
120+
if ($isLoggedIn && $socialAccount->user->id === $currentUser->id) {
121121
session()->flash('error', trans('errors.social_account_existing', ['socialAccount' => $titleCaseDriver]));
122122

123123
return redirect('/my-account/auth#social_accounts');
124124
}
125125

126126
// When a user is logged in, A social account exists but the users do not match.
127-
if ($isLoggedIn && $socialAccount !== null && $socialAccount->user->id != $currentUser->id) {
127+
if ($isLoggedIn && $socialAccount->user->id != $currentUser->id) {
128128
session()->flash('error', trans('errors.social_account_already_used_existing', ['socialAccount' => $titleCaseDriver]));
129129

130130
return redirect('/my-account/auth#social_accounts');

app/Activity/Notifications/NotificationManager.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
class NotificationManager
1616
{
1717
/**
18-
* @var class-string<NotificationHandler>[]
18+
* @var array<string, class-string<NotificationHandler>[]>
1919
*/
20-
protected array $handlers = [];
20+
protected array $handlersByActivity = [];
2121

2222
public function handle(Activity $activity, string|Loggable $detail, User $user): void
2323
{
2424
$activityType = $activity->type;
25-
$handlersToRun = $this->handlers[$activityType] ?? [];
25+
$handlersToRun = $this->handlersByActivity[$activityType] ?? [];
2626
foreach ($handlersToRun as $handlerClass) {
2727
/** @var NotificationHandler $handler */
2828
$handler = new $handlerClass();
@@ -35,12 +35,12 @@ public function handle(Activity $activity, string|Loggable $detail, User $user):
3535
*/
3636
public function registerHandler(string $activityType, string $handlerClass): void
3737
{
38-
if (!isset($this->handlers[$activityType])) {
39-
$this->handlers[$activityType] = [];
38+
if (!isset($this->handlersByActivity[$activityType])) {
39+
$this->handlersByActivity[$activityType] = [];
4040
}
4141

42-
if (!in_array($handlerClass, $this->handlers[$activityType])) {
43-
$this->handlers[$activityType][] = $handlerClass;
42+
if (!in_array($handlerClass, $this->handlersByActivity[$activityType])) {
43+
$this->handlersByActivity[$activityType][] = $handlerClass;
4444
}
4545
}
4646

0 commit comments

Comments
 (0)