Skip to content

Commit 3b0abcf

Browse files
committed
Maintenance: Continued work for phpstan level 4
1 parent c9347aa commit 3b0abcf

File tree

10 files changed

+40
-21
lines changed

10 files changed

+40
-21
lines changed

app/Console/Commands/AssignSortRuleCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class AssignSortRuleCommand extends Command
3232
*/
3333
public function handle(BookSorter $sorter): int
3434
{
35-
$sortRuleId = intval($this->argument('sort-rule')) ?? 0;
35+
$sortRuleId = intval($this->argument('sort-rule'));
3636
if ($sortRuleId === 0) {
3737
return $this->listSortRules();
3838
}

app/Console/Commands/CopyShelfPermissionsCommand.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public function handle(PermissionsUpdater $permissionsUpdater, BookshelfQueries
3232
{
3333
$shelfSlug = $this->option('slug');
3434
$cascadeAll = $this->option('all');
35+
$noInteraction = boolval($this->option('no-interaction'));
3536
$shelves = null;
3637

3738
if (!$cascadeAll && !$shelfSlug) {
@@ -41,14 +42,16 @@ public function handle(PermissionsUpdater $permissionsUpdater, BookshelfQueries
4142
}
4243

4344
if ($cascadeAll) {
44-
$continue = $this->confirm(
45-
'Permission settings for all shelves will be cascaded. ' .
46-
'Books assigned to multiple shelves will receive only the permissions of it\'s last processed shelf. ' .
47-
'Are you sure you want to proceed?'
48-
);
45+
if (!$noInteraction) {
46+
$continue = $this->confirm(
47+
'Permission settings for all shelves will be cascaded. ' .
48+
'Books assigned to multiple shelves will receive only the permissions of it\'s last processed shelf. ' .
49+
'Are you sure you want to proceed?',
50+
);
4951

50-
if (!$continue && !$this->hasOption('no-interaction')) {
51-
return 0;
52+
if (!$continue) {
53+
return 0;
54+
}
5255
}
5356

5457
$shelves = $queries->start()->get(['id']);

app/Entities/Models/Entity.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@ public static function instanceFromType(string $type): self
479479
'chapter' => new Chapter(),
480480
'book' => new Book(),
481481
'bookshelf' => new Bookshelf(),
482+
default => throw new \InvalidArgumentException("Invalid entity type: {$type}"),
482483
};
483484
}
484485
}

app/Entities/Repos/PageRepo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function getNewDraftPage(Entity $parent): Page
6060
$page->book_id = $parent->id;
6161
}
6262

63-
$defaultTemplate = $page->chapter?->defaultTemplate()->get() ?? $page->book?->defaultTemplate()->get();
63+
$defaultTemplate = $page->chapter?->defaultTemplate()->get() ?? $page->book->defaultTemplate()->get();
6464
if ($defaultTemplate) {
6565
$page->forceFill([
6666
'html' => $defaultTemplate->html,

app/Http/Controller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ protected function showPermissionError(string $redirectLocation = '/'): never
6262
*/
6363
protected function checkPermission(string|Permission $permission): void
6464
{
65-
if (!user() || !user()->can($permission)) {
65+
if (!user()->can($permission)) {
6666
$this->showPermissionError();
6767
}
6868
}

app/Search/SearchOptions.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,11 @@ protected function addOptionsFromString(string $searchString): void
121121
foreach ($patterns as $termType => $pattern) {
122122
$matches = [];
123123
preg_match_all($pattern, $searchString, $matches);
124-
if (count($matches) > 0) {
125-
foreach ($matches[1] as $index => $value) {
126-
$negated = str_starts_with($matches[0][$index], '-');
127-
$terms[$termType][] = $constructors[$termType]($value, $negated);
128-
}
129-
$searchString = preg_replace($pattern, '', $searchString);
124+
foreach ($matches[1] as $index => $value) {
125+
$negated = str_starts_with($matches[0][$index], '-');
126+
$terms[$termType][] = $constructors[$termType]($value, $negated);
130127
}
128+
$searchString = preg_replace($pattern, '', $searchString);
131129
}
132130

133131
// Unescape exacts and backslash escapes
@@ -261,7 +259,7 @@ public function getAdditionalOptionsString(): string
261259
$userFilters = ['updated_by', 'created_by', 'owned_by'];
262260
$unsupportedFilters = ['is_template', 'sort_by'];
263261
foreach ($this->filters->all() as $filter) {
264-
if (in_array($filter->getKey(), $userFilters, true) && $filter->value !== null && $filter->value !== 'me') {
262+
if (in_array($filter->getKey(), $userFilters, true) && $filter->value && $filter->value !== 'me') {
265263
$options[] = $filter;
266264
} else if (in_array($filter->getKey(), $unsupportedFilters, true)) {
267265
$options[] = $filter;

app/Sorting/BookSorter.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,8 @@ public function sortUsingMap(BookSortMap $sortMap): array
125125
*/
126126
protected function applySortUpdates(BookSortMapItem $sortMapItem, array $modelMap): void
127127
{
128-
/** @var BookChild $model */
129128
$model = $modelMap[$sortMapItem->type . ':' . $sortMapItem->id] ?? null;
130-
if (!$model) {
129+
if (!($model instanceof BookChild)) {
131130
return;
132131
}
133132

app/Uploads/ImageRepo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function getEntityFiltered(
9191
$parentFilter = function (Builder $query) use ($filterType, $contextPage) {
9292
if ($filterType === 'page') {
9393
$query->where('uploaded_to', '=', $contextPage->id);
94-
} else if ($filterType === 'book') {
94+
} else {
9595
$validPageIds = $contextPage->book->pages()
9696
->scopes('visible')
9797
->pluck('id')

app/Uploads/UserAvatars.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ protected function getAvatarImageData(string $url): string
148148
$responseCount++;
149149
$isRedirect = ($response->getStatusCode() === 301 || $response->getStatusCode() === 302);
150150
$url = $response->getHeader('Location')[0] ?? '';
151-
} while ($responseCount < 3 && $isRedirect && is_string($url) && str_starts_with($url, 'http'));
151+
} while ($responseCount < 3 && $isRedirect && str_starts_with($url, 'http'));
152152

153153
if ($responseCount === 3) {
154154
throw new HttpFetchException("Failed to fetch image, max redirect limit of 3 tries reached. Last fetched URL: {$url}");

tests/Commands/CopyShelfPermissionsCommandTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Tests\Commands;
44

5+
use BookStack\Entities\Models\Book;
56
use BookStack\Entities\Models\Bookshelf;
67
use Tests\TestCase;
78

@@ -61,4 +62,21 @@ public function test_copy_shelf_permissions_command_using_all()
6162
'view' => true, 'update' => true, 'create' => false, 'delete' => false,
6263
]);
6364
}
65+
66+
public function test_copy_shelf_permissions_command_using_slug_without_interaction()
67+
{
68+
$shelf = $this->entities->shelfHasBooks();
69+
$editorRole = $this->users->editor()->roles()->first();
70+
/** @var Book $child */
71+
$child = $shelf->books()->first();
72+
$child->shelves()->where('id', '!=', $shelf->id)->delete();
73+
74+
$this->assertFalse($child->hasPermissions());
75+
76+
$this->permissions->setEntityPermissions($shelf, ['view', 'update'], [$editorRole]);
77+
$this->artisan('bookstack:copy-shelf-permissions --all --no-interaction');
78+
79+
$child->refresh();
80+
$this->assertTrue($child->hasPermissions(), 'Child book should now be restricted');
81+
}
6482
}

0 commit comments

Comments
 (0)