Skip to content

Commit 242b7df

Browse files
authored
Merge pull request #5785 from BookStackApp/phpstan_level2
PHPstan level 3
2 parents 5ea4e1e + 7d1c316 commit 242b7df

78 files changed

Lines changed: 341 additions & 345 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

app/Access/ExternalBaseUserProvider.php

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,18 @@
22

33
namespace BookStack\Access;
44

5+
use BookStack\Users\Models\User;
56
use Illuminate\Contracts\Auth\Authenticatable;
67
use Illuminate\Contracts\Auth\UserProvider;
7-
use Illuminate\Database\Eloquent\Model;
88

99
class ExternalBaseUserProvider implements UserProvider
1010
{
11-
public function __construct(
12-
protected string $model
13-
) {
14-
}
15-
16-
/**
17-
* Create a new instance of the model.
18-
*/
19-
public function createModel(): Model
20-
{
21-
$class = '\\' . ltrim($this->model, '\\');
22-
23-
return new $class();
24-
}
25-
2611
/**
2712
* Retrieve a user by their unique identifier.
2813
*/
2914
public function retrieveById(mixed $identifier): ?Authenticatable
3015
{
31-
return $this->createModel()->newQuery()->find($identifier);
16+
return User::query()->find($identifier);
3217
}
3318

3419
/**
@@ -59,10 +44,7 @@ public function updateRememberToken(Authenticatable $user, $token)
5944
*/
6045
public function retrieveByCredentials(array $credentials): ?Authenticatable
6146
{
62-
// Search current user base by looking up a uid
63-
$model = $this->createModel();
64-
65-
return $model->newQuery()
47+
return User::query()
6648
->where('external_auth_id', $credentials['external_auth_id'])
6749
->first();
6850
}

app/Access/Guards/AsyncExternalBaseSessionGuard.php

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,28 @@
33
namespace BookStack\Access\Guards;
44

55
/**
6-
* Saml2 Session Guard.
6+
* External Auth Session Guard.
77
*
8-
* The saml2 login process is async in nature meaning it does not fit very well
9-
* into the default laravel 'Guard' auth flow. Instead most of the logic is done
10-
* via the Saml2 controller & Saml2Service. This class provides a safer, thin
11-
* version of SessionGuard.
8+
* The login process for external auth (SAML2/OIDC) is async in nature, meaning it does not fit very well
9+
* into the default laravel 'Guard' auth flow. Instead, most of the logic is done via the relevant
10+
* controller and services. This class provides a safer, thin version of SessionGuard.
1211
*/
1312
class AsyncExternalBaseSessionGuard extends ExternalBaseSessionGuard
1413
{
1514
/**
1615
* Validate a user's credentials.
17-
*
18-
* @param array $credentials
19-
*
20-
* @return bool
2116
*/
22-
public function validate(array $credentials = [])
17+
public function validate(array $credentials = []): bool
2318
{
2419
return false;
2520
}
2621

2722
/**
2823
* Attempt to authenticate a user using the given credentials.
2924
*
30-
* @param array $credentials
3125
* @param bool $remember
32-
*
33-
* @return bool
3426
*/
35-
public function attempt(array $credentials = [], $remember = false)
27+
public function attempt(array $credentials = [], $remember = false): bool
3628
{
3729
return false;
3830
}

app/Access/Guards/ExternalBaseSessionGuard.php

Lines changed: 26 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use BookStack\Access\RegistrationService;
66
use Illuminate\Auth\GuardHelpers;
7-
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
7+
use Illuminate\Contracts\Auth\Authenticatable;
88
use Illuminate\Contracts\Auth\StatefulGuard;
99
use Illuminate\Contracts\Auth\UserProvider;
1010
use Illuminate\Contracts\Session\Session;
@@ -24,43 +24,31 @@ class ExternalBaseSessionGuard implements StatefulGuard
2424
* The name of the Guard. Typically "session".
2525
*
2626
* Corresponds to guard name in authentication configuration.
27-
*
28-
* @var string
2927
*/
30-
protected $name;
28+
protected readonly string $name;
3129

3230
/**
3331
* The user we last attempted to retrieve.
34-
*
35-
* @var \Illuminate\Contracts\Auth\Authenticatable
3632
*/
37-
protected $lastAttempted;
33+
protected Authenticatable|null $lastAttempted;
3834

3935
/**
4036
* The session used by the guard.
41-
*
42-
* @var \Illuminate\Contracts\Session\Session
4337
*/
44-
protected $session;
38+
protected Session $session;
4539

4640
/**
4741
* Indicates if the logout method has been called.
48-
*
49-
* @var bool
5042
*/
51-
protected $loggedOut = false;
43+
protected bool $loggedOut = false;
5244

5345
/**
5446
* Service to handle common registration actions.
55-
*
56-
* @var RegistrationService
5747
*/
58-
protected $registrationService;
48+
protected RegistrationService $registrationService;
5949

6050
/**
6151
* Create a new authentication guard.
62-
*
63-
* @return void
6452
*/
6553
public function __construct(string $name, UserProvider $provider, Session $session, RegistrationService $registrationService)
6654
{
@@ -72,13 +60,11 @@ public function __construct(string $name, UserProvider $provider, Session $sessi
7260

7361
/**
7462
* Get the currently authenticated user.
75-
*
76-
* @return \Illuminate\Contracts\Auth\Authenticatable|null
7763
*/
78-
public function user()
64+
public function user(): Authenticatable|null
7965
{
8066
if ($this->loggedOut) {
81-
return;
67+
return null;
8268
}
8369

8470
// If we've already retrieved the user for the current request we can just
@@ -101,13 +87,11 @@ public function user()
10187

10288
/**
10389
* Get the ID for the currently authenticated user.
104-
*
105-
* @return int|null
10690
*/
107-
public function id()
91+
public function id(): int|null
10892
{
10993
if ($this->loggedOut) {
110-
return;
94+
return null;
11195
}
11296

11397
return $this->user()
@@ -117,12 +101,8 @@ public function id()
117101

118102
/**
119103
* Log a user into the application without sessions or cookies.
120-
*
121-
* @param array $credentials
122-
*
123-
* @return bool
124104
*/
125-
public function once(array $credentials = [])
105+
public function once(array $credentials = []): bool
126106
{
127107
if ($this->validate($credentials)) {
128108
$this->setUser($this->lastAttempted);
@@ -135,12 +115,8 @@ public function once(array $credentials = [])
135115

136116
/**
137117
* Log the given user ID into the application without sessions or cookies.
138-
*
139-
* @param mixed $id
140-
*
141-
* @return \Illuminate\Contracts\Auth\Authenticatable|false
142118
*/
143-
public function onceUsingId($id)
119+
public function onceUsingId($id): Authenticatable|false
144120
{
145121
if (!is_null($user = $this->provider->retrieveById($id))) {
146122
$this->setUser($user);
@@ -153,38 +129,26 @@ public function onceUsingId($id)
153129

154130
/**
155131
* Validate a user's credentials.
156-
*
157-
* @param array $credentials
158-
*
159-
* @return bool
160132
*/
161-
public function validate(array $credentials = [])
133+
public function validate(array $credentials = []): bool
162134
{
163135
return false;
164136
}
165137

166138
/**
167139
* Attempt to authenticate a user using the given credentials.
168-
*
169-
* @param array $credentials
170-
* @param bool $remember
171-
*
172-
* @return bool
140+
* @param bool $remember
173141
*/
174-
public function attempt(array $credentials = [], $remember = false)
142+
public function attempt(array $credentials = [], $remember = false): bool
175143
{
176144
return false;
177145
}
178146

179147
/**
180148
* Log the given user ID into the application.
181-
*
182-
* @param mixed $id
183149
* @param bool $remember
184-
*
185-
* @return \Illuminate\Contracts\Auth\Authenticatable|false
186150
*/
187-
public function loginUsingId($id, $remember = false)
151+
public function loginUsingId(mixed $id, $remember = false): Authenticatable|false
188152
{
189153
// Always return false as to disable this method,
190154
// Logins should route through LoginService.
@@ -194,12 +158,9 @@ public function loginUsingId($id, $remember = false)
194158
/**
195159
* Log a user into the application.
196160
*
197-
* @param \Illuminate\Contracts\Auth\Authenticatable $user
198-
* @param bool $remember
199-
*
200-
* @return void
161+
* @param bool $remember
201162
*/
202-
public function login(AuthenticatableContract $user, $remember = false)
163+
public function login(Authenticatable $user, $remember = false): void
203164
{
204165
$this->updateSession($user->getAuthIdentifier());
205166

@@ -208,12 +169,8 @@ public function login(AuthenticatableContract $user, $remember = false)
208169

209170
/**
210171
* Update the session with the given ID.
211-
*
212-
* @param string $id
213-
*
214-
* @return void
215172
*/
216-
protected function updateSession($id)
173+
protected function updateSession(string|int $id): void
217174
{
218175
$this->session->put($this->getName(), $id);
219176

@@ -222,10 +179,8 @@ protected function updateSession($id)
222179

223180
/**
224181
* Log the user out of the application.
225-
*
226-
* @return void
227182
*/
228-
public function logout()
183+
public function logout(): void
229184
{
230185
$this->clearUserDataFromStorage();
231186

@@ -239,62 +194,48 @@ public function logout()
239194

240195
/**
241196
* Remove the user data from the session and cookies.
242-
*
243-
* @return void
244197
*/
245-
protected function clearUserDataFromStorage()
198+
protected function clearUserDataFromStorage(): void
246199
{
247200
$this->session->remove($this->getName());
248201
}
249202

250203
/**
251204
* Get the last user we attempted to authenticate.
252-
*
253-
* @return \Illuminate\Contracts\Auth\Authenticatable
254205
*/
255-
public function getLastAttempted()
206+
public function getLastAttempted(): Authenticatable
256207
{
257208
return $this->lastAttempted;
258209
}
259210

260211
/**
261212
* Get a unique identifier for the auth session value.
262-
*
263-
* @return string
264213
*/
265-
public function getName()
214+
public function getName(): string
266215
{
267216
return 'login_' . $this->name . '_' . sha1(static::class);
268217
}
269218

270219
/**
271220
* Determine if the user was authenticated via "remember me" cookie.
272-
*
273-
* @return bool
274221
*/
275-
public function viaRemember()
222+
public function viaRemember(): bool
276223
{
277224
return false;
278225
}
279226

280227
/**
281228
* Return the currently cached user.
282-
*
283-
* @return \Illuminate\Contracts\Auth\Authenticatable|null
284229
*/
285-
public function getUser()
230+
public function getUser(): Authenticatable|null
286231
{
287232
return $this->user;
288233
}
289234

290235
/**
291236
* Set the current user.
292-
*
293-
* @param \Illuminate\Contracts\Auth\Authenticatable $user
294-
*
295-
* @return $this
296237
*/
297-
public function setUser(AuthenticatableContract $user)
238+
public function setUser(Authenticatable $user): self
298239
{
299240
$this->user = $user;
300241

app/Access/Guards/LdapSessionGuard.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,9 @@ public function __construct(
3535
/**
3636
* Validate a user's credentials.
3737
*
38-
* @param array $credentials
39-
*
4038
* @throws LdapException
41-
*
42-
* @return bool
4339
*/
44-
public function validate(array $credentials = [])
40+
public function validate(array $credentials = []): bool
4541
{
4642
$userDetails = $this->ldapService->getUserDetails($credentials['username']);
4743

0 commit comments

Comments
 (0)