Skip to content

Commit 58e2e0e

Browse files
committed
Maintenance: Finished getting to PHPStan level 4
1 parent 3b0abcf commit 58e2e0e

File tree

10 files changed

+19
-23
lines changed

10 files changed

+19
-23
lines changed

app/Api/ApiEntityListFormatter.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,21 @@ public function withTags(): self
7474

7575
/**
7676
* Include parent book/chapter info in the formatted data.
77+
* These functions are careful to not load the relation themselves, since they should
78+
* have already been loaded in a more efficient manner, with permissions applied, by the time
79+
* the parent fields are handled here.
7780
*/
7881
public function withParents(): self
7982
{
8083
$this->withField('book', function (Entity $entity) {
81-
if ($entity instanceof BookChild) {
84+
if ($entity instanceof BookChild && $entity->relationLoaded('book') && $entity->getRelationValue('book')) {
8285
return $entity->book->only(['id', 'name', 'slug']);
8386
}
8487
return null;
8588
});
8689

8790
$this->withField('chapter', function (Entity $entity) {
88-
if ($entity instanceof Page && $entity->chapter) {
91+
if ($entity instanceof Page && $entity->relationLoaded('chapter') && $entity->getRelationValue('chapter')) {
8992
return $entity->chapter->only(['id', 'name', 'slug']);
9093
}
9194
return null;

app/Entities/Models/Book.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*
1818
* @property string $description
1919
* @property string $description_html
20-
* @property int $image_id
20+
* @property ?int $image_id
2121
* @property ?int $default_template_id
2222
* @property ?int $sort_rule_id
2323
* @property \Illuminate\Database\Eloquent\Collection $chapters

app/Entities/Tools/PageContent.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ protected function getContentCacheKey(string $html): string
359359
{
360360
$contentHash = md5($html);
361361
$contentId = $this->page->id;
362-
$contentTime = $this->page->updated_at?->timestamp ?? time();
362+
$contentTime = $this->page->updated_at->timestamp ?? time();
363363
$appVersion = AppVersion::get();
364364
$filterConfig = config('app.content_filtering') ?? '';
365365
return "page-content-cache::{$filterConfig}::{$appVersion}::{$contentId}::{$contentTime}::{$contentHash}";

app/Entities/Tools/PermissionsUpdater.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function updateFromApiRequestData(Entity $entity, array $data): void
4747
{
4848
if (isset($data['role_permissions'])) {
4949
$entity->permissions()->where('role_id', '!=', 0)->delete();
50-
$rolePermissionData = $this->formatPermissionsFromApiRequestToEntityPermissions($data['role_permissions'] ?? [], false);
50+
$rolePermissionData = $this->formatPermissionsFromApiRequestToEntityPermissions($data['role_permissions'], false);
5151
$entity->permissions()->createMany($rolePermissionData);
5252
}
5353

app/Exports/ExportFormatter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ protected function containHtml(string $htmlContent): string
208208
preg_match_all("/\<img.*?src\=(\'|\")(.*?)(\'|\").*?\>/i", $htmlContent, $imageTagsOutput);
209209

210210
// Replace image src with base64 encoded image strings
211-
if (isset($imageTagsOutput[0]) && count($imageTagsOutput[0]) > 0) {
211+
if (count($imageTagsOutput[0]) > 0) {
212212
foreach ($imageTagsOutput[0] as $index => $imgMatch) {
213213
$oldImgTagString = $imgMatch;
214214
$srcString = $imageTagsOutput[2][$index];
@@ -225,7 +225,7 @@ protected function containHtml(string $htmlContent): string
225225
preg_match_all("/\<a.*href\=(\'|\")(.*?)(\'|\").*?\>/i", $htmlContent, $linksOutput);
226226

227227
// Update relative links to be absolute, with instance url
228-
if (isset($linksOutput[0]) && count($linksOutput[0]) > 0) {
228+
if (count($linksOutput[0]) > 0) {
229229
foreach ($linksOutput[0] as $index => $linkMatch) {
230230
$oldLinkString = $linkMatch;
231231
$srcString = $linksOutput[2][$index];

app/Exports/ZipExports/ZipImportRunner.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,8 @@ public function run(Import $import, ?Entity $parent = null): Entity
8282
$entity = $this->importBook($exportModel, $reader);
8383
} else if ($exportModel instanceof ZipExportChapter) {
8484
$entity = $this->importChapter($exportModel, $parent, $reader);
85-
} else if ($exportModel instanceof ZipExportPage) {
86-
$entity = $this->importPage($exportModel, $parent, $reader);
8785
} else {
88-
throw new ZipImportException(['No importable data found in import data.']);
86+
$entity = $this->importPage($exportModel, $parent, $reader);
8987
}
9088

9189
$this->references->replaceReferences();
@@ -132,7 +130,7 @@ protected function importBook(ZipExportBook $exportBook, ZipExportReader $reader
132130
'name' => $exportBook->name,
133131
'description_html' => $exportBook->description_html ?? '',
134132
'image' => $exportBook->cover ? $this->zipFileToUploadedFile($exportBook->cover, $reader) : null,
135-
'tags' => $this->exportTagsToInputArray($exportBook->tags ?? []),
133+
'tags' => $this->exportTagsToInputArray($exportBook->tags),
136134
]);
137135

138136
if ($book->coverInfo()->getImage()) {
@@ -151,7 +149,7 @@ protected function importBook(ZipExportBook $exportBook, ZipExportReader $reader
151149
foreach ($children as $child) {
152150
if ($child instanceof ZipExportChapter) {
153151
$this->importChapter($child, $book, $reader);
154-
} else if ($child instanceof ZipExportPage) {
152+
} else {
155153
$this->importPage($child, $book, $reader);
156154
}
157155
}
@@ -166,7 +164,7 @@ protected function importChapter(ZipExportChapter $exportChapter, Book $parent,
166164
$chapter = $this->chapterRepo->create([
167165
'name' => $exportChapter->name,
168166
'description_html' => $exportChapter->description_html ?? '',
169-
'tags' => $this->exportTagsToInputArray($exportChapter->tags ?? []),
167+
'tags' => $this->exportTagsToInputArray($exportChapter->tags),
170168
], $parent);
171169

172170
$exportPages = $exportChapter->pages;
@@ -199,7 +197,7 @@ protected function importPage(ZipExportPage $exportPage, Book|Chapter $parent, Z
199197
'name' => $exportPage->name,
200198
'markdown' => $exportPage->markdown ?? '',
201199
'html' => $exportPage->html ?? '',
202-
'tags' => $this->exportTagsToInputArray($exportPage->tags ?? []),
200+
'tags' => $this->exportTagsToInputArray($exportPage->tags),
203201
]);
204202

205203
$this->references->addPage($page, $exportPage);
@@ -302,7 +300,7 @@ protected function ensurePermissionsPermitImport(ZipExportPage|ZipExportChapter|
302300
array_push($chapters, ...$exportModel->chapters);
303301
} else if ($exportModel instanceof ZipExportChapter) {
304302
$chapters[] = $exportModel;
305-
} else if ($exportModel instanceof ZipExportPage) {
303+
} else {
306304
$pages[] = $exportModel;
307305
}
308306

app/Exports/ZipExports/ZipReferenceParser.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,6 @@ public function parseReferences(string $content, callable $handler): string
6868
$matches = [];
6969
preg_match_all($referenceRegex, $content, $matches);
7070

71-
if (count($matches) < 3) {
72-
return $content;
73-
}
74-
7571
for ($i = 0; $i < count($matches[0]); $i++) {
7672
$referenceText = $matches[0][$i];
7773
$type = strtolower($matches[1][$i]);

app/Permissions/JointPermissionBuilder.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ public function rebuildForEntity(Entity $entity): void
6161
return;
6262
}
6363

64-
/** @var BookChild $entity */
65-
if ($entity->book) {
64+
if ($entity instanceof BookChild) {
6665
$entities[] = $entity->book;
6766
}
6867

app/Users/Models/User.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,7 @@ public function hasSocialAccount(string $socialDriver = ''): bool
222222
public function getAvatar(int $size = 50): string
223223
{
224224
$default = url('/user_avatar.png');
225-
$imageId = $this->image_id;
226-
if ($imageId === 0 || $imageId === '0' || $imageId === null) {
225+
if ($this->image_id === 0) {
227226
return $default;
228227
}
229228

tests/Api/SearchApiTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ public function test_all_endpoint_includes_parent_details_where_visible()
106106
$this->permissions->setEntityPermissions($page, ['view'], [$editor->roles()->first()]);
107107

108108
$resp = $this->getJson($this->baseEndpoint . '?query=superextrauniquevalue');
109+
$resp->assertOk();
109110
$resp->assertJsonPath('data.0.id', $page->id);
110111
$resp->assertJsonPath('data.0.book.name', $book->name);
111112
$resp->assertJsonMissingPath('data.0.chapter');

0 commit comments

Comments
 (0)