Maintenance: Further PHPStan level 5 type fixes

This commit is contained in:
Dan Brown
2026-05-31 11:12:23 +01:00
parent 5611c8c0da
commit 03c9ddd14f
10 changed files with 59 additions and 29 deletions

View File

@@ -80,6 +80,7 @@ class OidcService
$provider->setPkceCode($pkceCode);
// Try to exchange authorization code for access token
/** @var OidcAccessToken $accessToken */
$accessToken = $provider->getAccessToken('authorization_code', [
'code' => $authorizationCode,
]);

View 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\Collection;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Relations\Relation;
@@ -27,6 +28,7 @@ class ActivityQueries
*/
public function latest(int $count = 20, int $page = 0): array
{
/** @var Collection<int, Activity> $activityList */
$activityList = $this->permissions
->restrictEntityRelationQuery(Activity::query(), 'activities', 'loggable_id', 'loggable_type')
->orderBy('created_at', 'desc')
@@ -83,6 +85,7 @@ class ActivityQueries
*/
public function userActivity(User $user, int $count = 20, int $page = 0): array
{
/** @var Collection<int, Activity> $activityList */
$activityList = $this->permissions
->restrictEntityRelationQuery(Activity::query(), 'activities', 'loggable_id', 'loggable_type')
->orderBy('created_at', 'desc')

View File

@@ -73,13 +73,17 @@ class WebhookFormatter
// Load entity owner, creator, updater details
$this->addModelFormatter(
fn ($event, $model) => ($model instanceof Entity),
fn ($model) => $model->load(['ownedBy', 'createdBy', 'updatedBy'])
function ($model) {
$model->load(['ownedBy', 'createdBy', 'updatedBy']);
}
);
// Load revision detail for page update and create events
$this->addModelFormatter(
fn ($event, $model) => ($model instanceof Page && ($event === ActivityType::PAGE_CREATE || $event === ActivityType::PAGE_UPDATE)),
fn ($model) => $model->load('currentRevision')
function ($model) {
$model->load('currentRevision');
}
);
}

View File

@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace BookStack\Entities\Controllers;
use BookStack\Activity\Tools\CommentTree;
@@ -10,7 +12,9 @@ use BookStack\Exceptions\PermissionsException;
use BookStack\Http\ApiController;
use BookStack\Permissions\Permission;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
class PageApiController extends ApiController
{
@@ -45,7 +49,7 @@ class PageApiController extends ApiController
/**
* Get a listing of pages visible to the user.
*/
public function list()
public function list(): JsonResponse
{
$pages = $this->queries->visibleForList()
->addSelect(['created_by', 'updated_by', 'revision_count', 'editor']);
@@ -69,7 +73,7 @@ class PageApiController extends ApiController
* Any images included via base64 data URIs will be extracted and saved as gallery
* images against the page during upload.
*/
public function create(Request $request)
public function create(Request $request): JsonResponse
{
$this->validate($request, $this->rules['create']);
@@ -102,9 +106,9 @@ class PageApiController extends ApiController
* Comments for the page are provided in a tree-structure representing the hierarchy of top-level
* comments and replies, for both archived and active comments.
*/
public function read(string $id)
public function read(string $id): JsonResponse
{
$page = $this->queries->findVisibleByIdOrFail($id);
$page = $this->queries->findVisibleByIdOrFail(intval($id));
$page = $page->forJsonDisplay();
$commentTree = (new CommentTree($page));
@@ -124,11 +128,11 @@ class PageApiController extends ApiController
* Providing a 'book_id' or 'chapter_id' property will essentially move
* the page into that parent element if you have permissions to do so.
*/
public function update(Request $request, string $id)
public function update(Request $request, string $id): JsonResponse
{
$requestData = $this->validate($request, $this->rules['update']);
$page = $this->queries->findVisibleByIdOrFail($id);
$page = $this->queries->findVisibleByIdOrFail(intval($id));
$this->checkOwnablePermission(Permission::PageUpdate, $page);
$parent = null;
@@ -161,9 +165,9 @@ class PageApiController extends ApiController
* Delete a page.
* This will typically send the page to the recycle bin.
*/
public function delete(string $id)
public function delete(string $id): Response
{
$page = $this->queries->findVisibleByIdOrFail($id);
$page = $this->queries->findVisibleByIdOrFail(intval($id));
$this->checkOwnablePermission(Permission::PageDelete, $page);
$this->pageRepo->destroy($page);

View File

@@ -25,6 +25,7 @@ use BookStack\Util\HtmlContentFilter;
use BookStack\Util\HtmlContentFilterConfig;
use Exception;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Throwable;
@@ -358,8 +359,8 @@ class PageController extends Controller
*/
public function showRecentlyUpdated()
{
$visibleBelongsScope = function (BelongsTo $query) {
$query->scopes('visible');
$visibleBelongsScope = function (Relation $relation): void {
$relation->scopes('visible');
};
$pages = $this->queries->visibleForList()

View File

@@ -177,7 +177,7 @@ class PageRevisionController extends Controller
*/
public function destroyUserDraft(string $pageId)
{
$page = $this->pageQueries->findVisibleByIdOrFail($pageId);
$page = $this->pageQueries->findVisibleByIdOrFail(intval($pageId));
$this->revisionRepo->deleteDraftsForCurrentUser($page);
return response('', 200);

View File

@@ -25,9 +25,13 @@ class CustomListItemRenderer implements NodeRendererInterface
*/
public function render(Node $node, ChildNodeRendererInterface $childRenderer)
{
if (!($node instanceof ListItem)) {
return null;
}
$listItem = $this->baseRenderer->render($node, $childRenderer);
if ($node instanceof ListItem && $this->startsTaskListItem($node) && $listItem instanceof HtmlElement) {
if ($this->startsTaskListItem($node) && $listItem instanceof HtmlElement) {
$listItem->setAttribute('class', 'task-list-item');
}

View File

@@ -421,28 +421,41 @@ class PageContent
*/
protected function headerNodesToLevelList(DOMNodeList $nodeList): array
{
$tree = collect($nodeList)->map(function (DOMElement $header) {
$minLevel = 6;
$headerDetails = array_map(function (DOMNode $header) use (&$minLevel) {
if (!$header instanceof DOMElement) {
return null;
}
$text = trim(str_replace("\xc2\xa0", ' ', $header->nodeValue));
$text = mb_substr($text, 0, 100);
if (empty($text)) {
return null;
}
$level = intval(str_replace('h', '', $header->nodeName));
if ($level < $minLevel) {
$minLevel = $level;
}
return [
'nodeName' => strtolower($header->nodeName),
'level' => intval(str_replace('h', '', $header->nodeName)),
'level' => $level,
'link' => '#' . $header->getAttribute('id'),
'text' => $text,
];
})->filter(function ($header) {
return mb_strlen($header['text']) > 0;
});
}, [...$nodeList]);
$filtered = array_values(array_filter($headerDetails));
// Shift headers if only smaller headers have been used
$levelChange = ($tree->pluck('level')->min() - 1);
$tree = $tree->map(function ($header) use ($levelChange) {
$header['level'] -= ($levelChange);
$levelChange = ($minLevel - 1);
foreach ($filtered as $index => $header) {
$filtered[$index]['level'] -= $levelChange;
}
return $header;
});
return $tree->toArray();
return $filtered;
}
}

View File

@@ -192,10 +192,10 @@ class PageIncludeParser
/**
* Get the parent paragraph of the given node, if existing.
*/
protected function getParentParagraph(DOMNode $parent): ?DOMNode
protected function getParentParagraph(DOMNode $parent): ?DOMElement
{
do {
if (strtolower($parent->nodeName) === 'p') {
if (strtolower($parent->nodeName) === 'p' && $parent instanceof DOMElement) {
return $parent;
}

View File

@@ -52,7 +52,7 @@ class SlugGenerator
{
$slug = Str::slug($name);
if ($slug === '') {
$slug = substr(md5(rand(1, 500)), 0, 5);
$slug = substr(md5(strval(rand(1, 500))), 0, 5);
}
return $slug;