Skip to content

Commit bd966ef

Browse files
committed
phpstan: Address a range of level 2 issues
1 parent a6b5733 commit bd966ef

File tree

8 files changed

+25
-16
lines changed

8 files changed

+25
-16
lines changed

app/Access/Guards/LdapSessionGuard.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,13 @@ public function validate(array $credentials = [])
5757
/**
5858
* Attempt to authenticate a user using the given credentials.
5959
*
60-
* @param array $credentials
6160
* @param bool $remember
6261
*
63-
* @throws LdapException*@throws \BookStack\Exceptions\JsonDebugException
62+
* @throws LdapException
6463
* @throws LoginAttemptException
6564
* @throws JsonDebugException
66-
*
67-
* @return bool
6865
*/
69-
public function attempt(array $credentials = [], $remember = false)
66+
public function attempt(array $credentials = [], $remember = false): bool
7067
{
7168
$username = $credentials['username'];
7269
$userDetails = $this->ldapService->getUserDetails($username);

app/Activity/ActivityQueries.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use BookStack\Permissions\PermissionApplicator;
1212
use BookStack\Users\Models\User;
1313
use Illuminate\Database\Eloquent\Builder;
14+
use Illuminate\Database\Eloquent\Relations\MorphTo;
1415
use Illuminate\Database\Eloquent\Relations\Relation;
1516

1617
class ActivityQueries
@@ -67,6 +68,7 @@ public function entityActivity(Entity $entity, int $count = 20, int $page = 1):
6768

6869
$activity = $query->orderBy('created_at', 'desc')
6970
->with(['loggable' => function (Relation $query) {
71+
/** @var MorphTo<Entity, Activity> $query */
7072
$query->withTrashed();
7173
}, 'user.avatar'])
7274
->skip($count * ($page - 1))

app/Activity/Notifications/Handlers/PageUpdateNotificationHandler.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ public function handle(Activity $activity, Loggable|string $detail, User $user):
2020
throw new \InvalidArgumentException("Detail for page update notifications must be a page");
2121
}
2222

23-
// Get last update from activity
23+
// Get the last update from activity
24+
/** @var ?Activity $lastUpdate */
2425
$lastUpdate = $detail->activity()
2526
->where('type', '=', ActivityType::PAGE_UPDATE)
2627
->where('id', '!=', $activity->id)

app/Activity/Tools/WebhookFormatter.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function format(): array
5050
}
5151

5252
if ($this->detail instanceof Model) {
53-
$data['related_item'] = $this->formatModel();
53+
$data['related_item'] = $this->formatModel($this->detail);
5454
}
5555

5656
return $data;
@@ -83,10 +83,8 @@ public function addDefaultModelFormatters(): void
8383
);
8484
}
8585

86-
protected function formatModel(): array
86+
protected function formatModel(Model $model): array
8787
{
88-
/** @var Model $model */
89-
$model = $this->detail;
9088
$model->unsetRelations();
9189

9290
foreach ($this->modelFormatters as $formatter) {

app/Entities/Controllers/ChapterApiController.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace BookStack\Entities\Controllers;
44

5+
use BookStack\Entities\Models\Book;
56
use BookStack\Entities\Models\Chapter;
67
use BookStack\Entities\Queries\ChapterQueries;
78
use BookStack\Entities\Queries\EntityQueries;
@@ -143,7 +144,10 @@ protected function forJsonDisplay(Chapter $chapter): Chapter
143144
$chapter->load(['tags']);
144145
$chapter->makeVisible('description_html');
145146
$chapter->setAttribute('description_html', $chapter->descriptionHtml());
146-
$chapter->setAttribute('book_slug', $chapter->book()->first()->slug);
147+
148+
/** @var Book $book */
149+
$book = $chapter->book()->first();
150+
$chapter->setAttribute('book_slug', $book->slug);
147151

148152
return $chapter;
149153
}

app/Entities/Models/Entity.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use Carbon\Carbon;
2727
use Illuminate\Database\Eloquent\Builder;
2828
use Illuminate\Database\Eloquent\Collection;
29+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
2930
use Illuminate\Database\Eloquent\Relations\MorphMany;
3031
use Illuminate\Database\Eloquent\SoftDeletes;
3132

@@ -283,10 +284,14 @@ abstract public function getUrl(string $path = '/'): string;
283284
public function getParent(): ?self
284285
{
285286
if ($this instanceof Page) {
286-
return $this->chapter_id ? $this->chapter()->withTrashed()->first() : $this->book()->withTrashed()->first();
287+
/** @var BelongsTo<Chapter|Book, Page> $builder */
288+
$builder = $this->chapter_id ? $this->chapter() : $this->book();
289+
return $builder->withTrashed()->first();
287290
}
288291
if ($this instanceof Chapter) {
289-
return $this->book()->withTrashed()->first();
292+
/** @var BelongsTo<Book, Page> $builder */
293+
$builder = $this->book();
294+
return $builder->withTrashed()->first();
290295
}
291296

292297
return null;
@@ -295,15 +300,15 @@ public function getParent(): ?self
295300
/**
296301
* Rebuild the permissions for this entity.
297302
*/
298-
public function rebuildPermissions()
303+
public function rebuildPermissions(): void
299304
{
300305
app()->make(JointPermissionBuilder::class)->rebuildForEntity(clone $this);
301306
}
302307

303308
/**
304309
* Index the current entity for search.
305310
*/
306-
public function indexForSearch()
311+
public function indexForSearch(): void
307312
{
308313
app()->make(SearchIndex::class)->indexEntity(clone $this);
309314
}

app/Exports/ZipExports/ZipExportReferences.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ protected function handleModelReference(Model $model, ZipExportModel $exportMode
134134

135135
// Find and include images if in visibility
136136
$page = $model->getPage();
137-
if ($page && userCan('view', $page)) {
137+
if ($page && userCan('view', $page) && $exportModel instanceof ZipExportPage) {
138138
if (!isset($this->images[$model->id])) {
139139
$exportImage = ZipExportImage::fromModel($model, $files);
140140
$this->images[$model->id] = $exportImage;

app/Users/Models/HasCreatorAndUpdater.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
/**
88
* @property int $created_by
99
* @property int $updated_by
10+
* @property ?User $createdBy
11+
* @property ?User $updatedBy
1012
*/
1113
trait HasCreatorAndUpdater
1214
{

0 commit comments

Comments
 (0)