]> BookStack Code Mirror - bookstack/commitdiff
phpstan: Address a range of level 2 issues
authorDan Brown <redacted>
Sat, 9 Aug 2025 10:09:50 +0000 (11:09 +0100)
committerDan Brown <redacted>
Sat, 9 Aug 2025 10:09:50 +0000 (11:09 +0100)
app/Access/Guards/LdapSessionGuard.php
app/Activity/ActivityQueries.php
app/Activity/Notifications/Handlers/PageUpdateNotificationHandler.php
app/Activity/Tools/WebhookFormatter.php
app/Entities/Controllers/ChapterApiController.php
app/Entities/Models/Entity.php
app/Exports/ZipExports/ZipExportReferences.php
app/Users/Models/HasCreatorAndUpdater.php

index a417d3e61cb0aeb5e8be23172967c2f76898f633..bd020f70b9555e107ca4c5fa8ac7322b043b8850 100644 (file)
@@ -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);
index 86326fb80af5c93575d71e54dda143d95e7d8de0..d5b047937fbb24c3c531f319b2b322ce33eb6021 100644 (file)
@@ -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))
index 744aba18f631782e38b739874a3f8e5e44b3c34b..20dc0fc4d8e42cf36ef4a6eed1c423cf5c45b12b 100644 (file)
@@ -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)
index 6ccb084b8de769b2856020b101beffb527199ef1..cb4e9cb0a9c648b00f03c1756560584a1925aca8 100644 (file)
@@ -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) {
index 8ac0c7a60a22a41c4e96864553ba3c7088a88fcf..6ba2e9fd2bf5d3bed8daff73deae6df54666005c 100644 (file)
@@ -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;
     }
index 0de83c93869332906842a6ad31fa28ad35ce27a2..1ef4e618df9265fc2053e4524e0f5239698ccd9a 100644 (file)
@@ -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);
     }
index bf5e02133ef3d48939aa79af309ddb9e0a2de74a..b9f356a6a5ea9e87c78695d50bcf1342d0d4509a 100644 (file)
@@ -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;
index 5ff46bc3cb78747b4be997b6d33320e24164094a..9fb24ead9ec7d1d7e1bd8f28b982902481bace5a 100644 (file)
@@ -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
 {