Skip to content

NOW MANAGED ON CODEBERG

phpstan: Address a range of level 2 issues

Dan Brownbd966ef99e1b
diff --git a/app/Access/Guards/LdapSessionGuard.php b/app/Access/Guards/LdapSessionGuard.php
index a417d3e61..bd020f70b 100644
--- a/app/Access/Guards/LdapSessionGuard.php
+++ b/app/Access/Guards/LdapSessionGuard.php
@@ -57,16 +57,13 @@ class LdapSessionGuard extends ExternalBaseSessionGuard
/**
* Attempt to authenticate a user using the given credentials.
*
- * @param array $credentials
* @param bool $remember
*
- * @throws LdapException*@throws \BookStack\Exceptions\JsonDebugException
+ * @throws LdapException
* @throws LoginAttemptException
* @throws JsonDebugException
- *
- * @return bool
*/
- public function attempt(array $credentials = [], $remember = false)
+ public function attempt(array $credentials = [], $remember = false): bool
{
$username = $credentials['username'];
$userDetails = $this->ldapService->getUserDetails($username);
diff --git a/app/Activity/ActivityQueries.php b/app/Activity/ActivityQueries.php
index 86326fb80..d5b047937 100644
--- a/app/Activity/ActivityQueries.php
+++ b/app/Activity/ActivityQueries.php
@@ -11,6 +11,7 @@ use BookStack\Entities\Tools\MixedEntityListLoader;
use BookStack\Permissions\PermissionApplicator;
use BookStack\Users\Models\User;
use Illuminate\Database\Eloquent\Builder;
+use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Relations\Relation;
class ActivityQueries
@@ -67,6 +68,7 @@ class ActivityQueries
$activity = $query->orderBy('created_at', 'desc')
->with(['loggable' => function (Relation $query) {
+ /** @var MorphTo<Entity, Activity> $query */
$query->withTrashed();
}, 'user.avatar'])
->skip($count * ($page - 1))
diff --git a/app/Activity/Notifications/Handlers/PageUpdateNotificationHandler.php b/app/Activity/Notifications/Handlers/PageUpdateNotificationHandler.php
index 744aba18f..20dc0fc4d 100644
--- a/app/Activity/Notifications/Handlers/PageUpdateNotificationHandler.php
+++ b/app/Activity/Notifications/Handlers/PageUpdateNotificationHandler.php
@@ -20,7 +20,8 @@ class PageUpdateNotificationHandler extends BaseNotificationHandler
throw new \InvalidArgumentException("Detail for page update notifications must be a page");
}
- // Get last update from activity
+ // Get the last update from activity
+ /** @var ?Activity $lastUpdate */
$lastUpdate = $detail->activity()
->where('type', '=', ActivityType::PAGE_UPDATE)
->where('id', '!=', $activity->id)
diff --git a/app/Activity/Tools/WebhookFormatter.php b/app/Activity/Tools/WebhookFormatter.php
index 6ccb084b8..cb4e9cb0a 100644
--- a/app/Activity/Tools/WebhookFormatter.php
+++ b/app/Activity/Tools/WebhookFormatter.php
@@ -50,7 +50,7 @@ class WebhookFormatter
}
if ($this->detail instanceof Model) {
- $data['related_item'] = $this->formatModel();
+ $data['related_item'] = $this->formatModel($this->detail);
}
return $data;
@@ -83,10 +83,8 @@ class WebhookFormatter
);
}
- protected function formatModel(): array
+ protected function formatModel(Model $model): array
{
- /** @var Model $model */
- $model = $this->detail;
$model->unsetRelations();
foreach ($this->modelFormatters as $formatter) {
diff --git a/app/Entities/Controllers/ChapterApiController.php b/app/Entities/Controllers/ChapterApiController.php
index 8ac0c7a60..6ba2e9fd2 100644
--- a/app/Entities/Controllers/ChapterApiController.php
+++ b/app/Entities/Controllers/ChapterApiController.php
@@ -2,6 +2,7 @@
namespace BookStack\Entities\Controllers;
+use BookStack\Entities\Models\Book;
use BookStack\Entities\Models\Chapter;
use BookStack\Entities\Queries\ChapterQueries;
use BookStack\Entities\Queries\EntityQueries;
@@ -143,7 +144,10 @@ class ChapterApiController extends ApiController
$chapter->load(['tags']);
$chapter->makeVisible('description_html');
$chapter->setAttribute('description_html', $chapter->descriptionHtml());
- $chapter->setAttribute('book_slug', $chapter->book()->first()->slug);
+
+ /** @var Book $book */
+ $book = $chapter->book()->first();
+ $chapter->setAttribute('book_slug', $book->slug);
return $chapter;
}
diff --git a/app/Entities/Models/Entity.php b/app/Entities/Models/Entity.php
index 0de83c938..1ef4e618d 100644
--- a/app/Entities/Models/Entity.php
+++ b/app/Entities/Models/Entity.php
@@ -26,6 +26,7 @@ use BookStack\Users\Models\HasOwner;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
+use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\SoftDeletes;
@@ -283,10 +284,14 @@ abstract class Entity extends Model implements Sluggable, Favouritable, Viewable
public function getParent(): ?self
{
if ($this instanceof Page) {
- return $this->chapter_id ? $this->chapter()->withTrashed()->first() : $this->book()->withTrashed()->first();
+ /** @var BelongsTo<Chapter|Book, Page> $builder */
+ $builder = $this->chapter_id ? $this->chapter() : $this->book();
+ return $builder->withTrashed()->first();
}
if ($this instanceof Chapter) {
- return $this->book()->withTrashed()->first();
+ /** @var BelongsTo<Book, Page> $builder */
+ $builder = $this->book();
+ return $builder->withTrashed()->first();
}
return null;
@@ -295,7 +300,7 @@ abstract class Entity extends Model implements Sluggable, Favouritable, Viewable
/**
* Rebuild the permissions for this entity.
*/
- public function rebuildPermissions()
+ public function rebuildPermissions(): void
{
app()->make(JointPermissionBuilder::class)->rebuildForEntity(clone $this);
}
@@ -303,7 +308,7 @@ abstract class Entity extends Model implements Sluggable, Favouritable, Viewable
/**
* Index the current entity for search.
*/
- public function indexForSearch()
+ public function indexForSearch(): void
{
app()->make(SearchIndex::class)->indexEntity(clone $this);
}
diff --git a/app/Exports/ZipExports/ZipExportReferences.php b/app/Exports/ZipExports/ZipExportReferences.php
index bf5e02133..b9f356a6a 100644
--- a/app/Exports/ZipExports/ZipExportReferences.php
+++ b/app/Exports/ZipExports/ZipExportReferences.php
@@ -134,7 +134,7 @@ class ZipExportReferences
// Find and include images if in visibility
$page = $model->getPage();
- if ($page && userCan('view', $page)) {
+ if ($page && userCan('view', $page) && $exportModel instanceof ZipExportPage) {
if (!isset($this->images[$model->id])) {
$exportImage = ZipExportImage::fromModel($model, $files);
$this->images[$model->id] = $exportImage;
diff --git a/app/Users/Models/HasCreatorAndUpdater.php b/app/Users/Models/HasCreatorAndUpdater.php
index 5ff46bc3c..9fb24ead9 100644
--- a/app/Users/Models/HasCreatorAndUpdater.php
+++ b/app/Users/Models/HasCreatorAndUpdater.php
@@ -7,6 +7,8 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
/**
* @property int $created_by
* @property int $updated_by
+ * @property ?User $createdBy
+ * @property ?User $updatedBy
*/
trait HasCreatorAndUpdater
{