Skip to content

Commit e05ec7d

Browse files
committed
Maintenance: Addressed a range of phpstan level 3 issues
1 parent cee23de commit e05ec7d

26 files changed

+84
-151
lines changed

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/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 $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/Activity/Models/Comment.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function entity(): MorphTo
3939

4040
/**
4141
* Get the parent comment this is in reply to (if existing).
42-
* @return BelongsTo<Comment, Comment>
42+
* @return BelongsTo<Comment, $this>
4343
*/
4444
public function parent(): BelongsTo
4545
{

app/App/Providers/AuthServiceProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ public function boot(): void
5959
*/
6060
public function register(): void
6161
{
62-
Auth::provider('external-users', function ($app, array $config) {
63-
return new ExternalBaseUserProvider($config['model']);
62+
Auth::provider('external-users', function () {
63+
return new ExternalBaseUserProvider();
6464
});
6565

6666
// Bind and provide the default system user as a singleton to the app instance when needed.

app/App/Providers/EventServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class EventServiceProvider extends ServiceProvider
1515
/**
1616
* The event listener mappings for the application.
1717
*
18-
* @var array<class-string, array<int, class-string>>
18+
* @var array<class-string, array<int, string>>
1919
*/
2020
protected $listen = [
2121
SocialiteWasCalled::class => [

app/Entities/Controllers/PageRevisionController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function changes(string $bookSlug, string $pageSlug, int $revisionId)
9898
throw new NotFoundException();
9999
}
100100

101-
$prev = $revision->getPrevious();
101+
$prev = $revision->getPreviousRevision();
102102
$prevContent = $prev->html ?? '';
103103
$diff = Diff::excecute($prevContent, $revision->html);
104104

app/Entities/Models/PageRevision.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function getUrl(string $path = ''): string
6060
/**
6161
* Get the previous revision for the same page if existing.
6262
*/
63-
public function getPrevious(): ?PageRevision
63+
public function getPreviousRevision(): ?PageRevision
6464
{
6565
$id = static::newQuery()->where('page_id', '=', $this->page_id)
6666
->where('id', '<', $this->id)

app/Exceptions/Handler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,15 @@ public function render($request, Throwable $e)
9494
* If the callable returns a response, this response will be returned
9595
* to the request upon error.
9696
*/
97-
public function prepareForOutOfMemory(callable $onOutOfMemory)
97+
public function prepareForOutOfMemory(callable $onOutOfMemory): void
9898
{
9999
$this->onOutOfMemory = $onOutOfMemory;
100100
}
101101

102102
/**
103103
* Forget the current out of memory handler, if existing.
104104
*/
105-
public function forgetOutOfMemoryHandler()
105+
public function forgetOutOfMemoryHandler(): void
106106
{
107107
$this->onOutOfMemory = null;
108108
}

app/Exports/ImportRepo.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ public function getVisibleImports(): Collection
3939
return $this->queryVisible()->get();
4040
}
4141

42+
/**
43+
* @return Builder<Import>
44+
*/
4245
public function queryVisible(): Builder
4346
{
4447
$query = Import::query();

0 commit comments

Comments
 (0)