Skip to content

Commit 20614ec

Browse files
committed
fix: Filament v4 imports and PHPStan level 6 compliance
Filament imports: - Use Filament\Actions\* namespace for table actions - Fixed imports in DocumentsRelationManager, DocumentResource, ViewDocumentVersions, ServersRelationManager PHPStan level 6 fixes: - Add @Property annotations to Document and DocumentVersion models - Add @method annotations for scopes on Document model - Fix return type annotations in DocumentService for DB::transaction - Replace unnecessary nullsafe operators with explicit null checks - Add type hints for array parameters - Use Laravel's Str::sanitizeHtml directly (requires Laravel 11+) - Fix record resolution typing in ViewDocumentVersions
1 parent 9c4c150 commit 20614ec

9 files changed

Lines changed: 76 additions & 44 deletions

File tree

server-documentation/src/Filament/Admin/RelationManagers/DocumentsRelationManager.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66

77
use Filament\Forms\Components\TextInput;
88
use Filament\Resources\RelationManagers\RelationManager;
9-
use Filament\Tables\Actions\AttachAction;
10-
use Filament\Tables\Actions\CreateAction;
11-
use Filament\Tables\Actions\DetachAction;
12-
use Filament\Tables\Actions\DetachBulkAction;
13-
use Filament\Tables\Actions\ViewAction;
9+
use Filament\Actions\AttachAction;
10+
use Filament\Actions\CreateAction;
11+
use Filament\Actions\DetachAction;
12+
use Filament\Actions\DetachBulkAction;
13+
use Filament\Actions\ViewAction;
1414
use Filament\Tables\Columns\TextColumn;
1515
use Filament\Tables\Table;
1616
use Starter\ServerDocumentation\Filament\Admin\Resources\DocumentResource;

server-documentation/src/Filament/Admin/Resources/DocumentResource.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
use Filament\Resources\Resource;
2121
use Filament\Schemas\Components\Section;
2222
use Filament\Schemas\Schema;
23-
use Filament\Tables\Actions\DeleteBulkAction;
24-
use Filament\Tables\Actions\EditAction;
23+
use Filament\Actions\DeleteBulkAction;
24+
use Filament\Actions\EditAction;
2525
use Filament\Tables\Columns\TextColumn;
2626
use Filament\Tables\Filters\TrashedFilter;
2727
use Filament\Tables\Table;

server-documentation/src/Filament/Admin/Resources/DocumentResource/Pages/ListDocuments.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ protected function getHeaderActions(): array
6363

6464
/**
6565
* Import a Markdown file and create a new document.
66+
*
67+
* @param array{markdown_file: TemporaryUploadedFile, use_frontmatter?: bool} $data
6668
*/
6769
protected function importMarkdownFile(array $data): void
6870
{
@@ -161,7 +163,7 @@ protected function normalizeDocumentType(?string $type): string
161163
if (DocumentType::isValid($type)) {
162164
$enumType = DocumentType::tryFromLegacy($type);
163165

164-
return $enumType?->value ?? DocumentType::Player->value;
166+
return $enumType !== null ? $enumType->value : DocumentType::Player->value;
165167
}
166168

167169
logger()->warning('Invalid document type in import', [

server-documentation/src/Filament/Admin/Resources/DocumentResource/Pages/ViewDocumentVersions.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Filament\Notifications\Notification;
99
use Filament\Resources\Pages\Concerns\InteractsWithRecord;
1010
use Filament\Resources\Pages\Page;
11-
use Filament\Tables\Actions\Action as TableAction;
1211
use Filament\Tables\Columns\TextColumn;
1312
use Filament\Tables\Concerns\InteractsWithTable;
1413
use Filament\Tables\Contracts\HasTable;
@@ -34,7 +33,9 @@ class ViewDocumentVersions extends Page implements HasTable
3433

3534
public function mount(int|string $record): void
3635
{
37-
$this->record = $this->resolveRecord($record);
36+
/** @var Document $resolved */
37+
$resolved = $this->resolveRecord($record);
38+
$this->record = $resolved;
3839
}
3940

4041
public function getTitle(): string|Htmlable
@@ -87,7 +88,7 @@ public function table(Table $table): Table
8788
])
8889
->defaultSort('version_number', 'desc')
8990
->actions([
90-
TableAction::make('preview')
91+
Action::make('preview')
9192
->label(trans('server-documentation::strings.versions.preview'))
9293
->icon('tabler-eye')
9394
->modalHeading(fn (DocumentVersion $record): string => 'v' . $record->version_number . ': ' . $record->title)
@@ -97,7 +98,7 @@ public function table(Table $table): Table
9798
->modalSubmitAction(false)
9899
->modalCancelActionLabel('Close'),
99100

100-
TableAction::make('restore')
101+
Action::make('restore')
101102
->label(trans('server-documentation::strings.versions.restore'))
102103
->icon('tabler-restore')
103104
->color('warning')

server-documentation/src/Filament/Admin/Resources/DocumentResource/RelationManagers/ServersRelationManager.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
namespace Starter\ServerDocumentation\Filament\Admin\Resources\DocumentResource\RelationManagers;
66

7+
use Filament\Actions\AttachAction;
8+
use Filament\Actions\DetachAction;
9+
use Filament\Actions\DetachBulkAction;
710
use Filament\Forms\Components\TextInput;
811
use Filament\Resources\RelationManagers\RelationManager;
9-
use Filament\Tables\Actions\AttachAction;
10-
use Filament\Tables\Actions\DetachAction;
11-
use Filament\Tables\Actions\DetachBulkAction;
1212
use Filament\Tables\Columns\TextColumn;
1313
use Filament\Tables\Table;
1414

server-documentation/src/Models/Document.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,33 @@
1818
use Starter\ServerDocumentation\Enums\DocumentType;
1919
use Starter\ServerDocumentation\Services\DocumentService;
2020

21+
/**
22+
* @property int $id
23+
* @property string $uuid
24+
* @property string $title
25+
* @property string $slug
26+
* @property string $content
27+
* @property string $type
28+
* @property bool $is_global
29+
* @property bool $is_published
30+
* @property int|null $author_id
31+
* @property int|null $last_edited_by
32+
* @property int $sort_order
33+
* @property \Carbon\Carbon|null $created_at
34+
* @property \Carbon\Carbon|null $updated_at
35+
* @property \Carbon\Carbon|null $deleted_at
36+
* @property-read User|null $author
37+
* @property-read User|null $lastEditor
38+
* @property-read \Illuminate\Database\Eloquent\Collection<int, Server> $servers
39+
* @property-read \Illuminate\Database\Eloquent\Collection<int, DocumentVersion> $versions
40+
*
41+
* @method static Builder|Document forServer(Server $server)
42+
* @method static Builder|Document forTypes(array $types)
43+
* @method static Builder|Document published()
44+
* @method static Builder|Document global()
45+
* @method static Builder|Document search(string $term)
46+
* @method static Builder|Document visibleTo(?User $user, Server $server)
47+
*/
2148
class Document extends Model
2249
{
2350
/** @use HasFactory<DocumentFactory> */
@@ -36,6 +63,8 @@ protected static function newFactory(): DocumentFactory
3663

3764
/**
3865
* Temporary storage for original values before update (for versioning).
66+
*
67+
* @var array{title?: string, content?: string, dirty_fields?: array<string>}
3968
*/
4069
protected array $originalValuesForVersion = [];
4170

@@ -110,9 +139,9 @@ protected static function booted(): void
110139
static::updated(function (Document $document) {
111140
if (!empty($document->originalValuesForVersion)) {
112141
$changeSummary = app(DocumentService::class)->generateChangeSummary(
113-
$document->originalValuesForVersion['dirty_fields'],
142+
$document->originalValuesForVersion['dirty_fields'] ?? [],
114143
$document->originalValuesForVersion['content'] ?? '',
115-
$document->content ?? ''
144+
$document->content
116145
);
117146

118147
app(DocumentService::class)->createVersionFromOriginal(

server-documentation/src/Models/DocumentVersion.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,20 @@
1010
use Illuminate\Database\Eloquent\Relations\BelongsTo;
1111
use Starter\ServerDocumentation\Database\Factories\DocumentVersionFactory;
1212

13+
/**
14+
* @property int $id
15+
* @property int $document_id
16+
* @property string $title
17+
* @property string $content
18+
* @property int $version_number
19+
* @property int|null $edited_by
20+
* @property string|null $change_summary
21+
* @property \Carbon\Carbon|null $created_at
22+
* @property \Carbon\Carbon|null $updated_at
23+
* @property-read Document $document
24+
* @property-read User|null $editor
25+
* @property-read string $formatted_version
26+
*/
1327
class DocumentVersion extends Model
1428
{
1529
/** @use HasFactory<DocumentVersionFactory> */

server-documentation/src/Services/DocumentService.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,15 +192,17 @@ public function createVersionFromOriginal(
192192
?string $changeSummary = null,
193193
?int $userId = null
194194
): ?DocumentVersion {
195-
return DB::transaction(function () use ($document, $originalTitle, $originalContent, $changeSummary, $userId) {
195+
/** @var DocumentVersion|null */
196+
return DB::transaction(function () use ($document, $originalTitle, $originalContent, $changeSummary, $userId): ?DocumentVersion {
197+
/** @var DocumentVersion|null $latestVersion */
196198
$latestVersion = $document->versions()
197199
->lockForUpdate()
198200
->latest()
199201
->first();
200202

201-
$latestVersionNumber = $latestVersion?->version_number ?? 0;
203+
$latestVersionNumber = $latestVersion !== null ? $latestVersion->version_number : 0;
202204

203-
if ($latestVersion && $latestVersion->created_at->diffInSeconds(now()) < self::VERSION_DEBOUNCE_SECONDS) {
205+
if ($latestVersion !== null && $latestVersion->created_at->diffInSeconds(now()) < self::VERSION_DEBOUNCE_SECONDS) {
204206
$latestVersion->update([
205207
'title' => $originalTitle ?? $document->title,
206208
'content' => $originalContent ?? $document->content,
@@ -216,6 +218,7 @@ public function createVersionFromOriginal(
216218
return $latestVersion;
217219
}
218220

221+
/** @var DocumentVersion $version */
219222
$version = $document->versions()->create([
220223
'title' => $originalTitle ?? $document->title,
221224
'content' => $originalContent ?? $document->content,
@@ -234,15 +237,18 @@ public function createVersionFromOriginal(
234237

235238
/**
236239
* Create a new version of a document within a transaction.
240+
*
237241
* @deprecated Use createVersionFromOriginal for model events
238242
*/
239243
public function createVersion(Document $document, ?string $changeSummary = null, ?int $userId = null): DocumentVersion
240244
{
241-
return DB::transaction(function () use ($document, $changeSummary, $userId) {
245+
/** @var DocumentVersion */
246+
return DB::transaction(function () use ($document, $changeSummary, $userId): DocumentVersion {
242247
$latestVersion = $document->versions()
243248
->lockForUpdate()
244249
->max('version_number') ?? 0;
245250

251+
/** @var DocumentVersion */
246252
return $document->versions()->create([
247253
'title' => $document->getOriginal('title') ?? $document->title,
248254
'content' => $document->getOriginal('content') ?? $document->content,
@@ -291,6 +297,7 @@ public function restoreVersion(Document $document, DocumentVersion $version, ?in
291297
*/
292298
public function clearDocumentCache(Document $document): void
293299
{
300+
/** @var Server $server */
294301
foreach ($document->servers as $server) {
295302
$this->clearServerDocumentsCache($server);
296303
}

server-documentation/src/Services/MarkdownConverter.php

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -57,32 +57,11 @@ public function toHtml(string $markdown, bool $sanitize = true): string
5757
}
5858

5959
/**
60-
* Sanitize HTML content to prevent XSS.
61-
* Uses Laravel's built-in sanitizer if available, otherwise strips dangerous tags.
60+
* Sanitize HTML content to prevent XSS using Laravel's built-in sanitizer.
6261
*/
6362
public function sanitizeHtml(string $html): string
6463
{
65-
if (method_exists(\Illuminate\Support\Str::class, 'sanitizeHtml')) {
66-
return (string) str($html)->sanitizeHtml();
67-
}
68-
69-
$dangerous = [
70-
'/<script\b[^>]*>.*?<\/script>/is',
71-
'/<style\b[^>]*>.*?<\/style>/is',
72-
'/<iframe\b[^>]*>.*?<\/iframe>/is',
73-
'/<object\b[^>]*>.*?<\/object>/is',
74-
'/<embed\b[^>]*>.*?<\/embed>/is',
75-
'/<form\b[^>]*>.*?<\/form>/is',
76-
'/on\w+\s*=\s*["\'][^"\']*["\']/i',
77-
'/javascript\s*:/i',
78-
'/(href|src)\s*=\s*["\']data\s*:/i',
79-
];
80-
81-
foreach ($dangerous as $pattern) {
82-
$html = preg_replace($pattern, '', $html) ?? $html;
83-
}
84-
85-
return $html;
64+
return (string) str($html)->sanitizeHtml();
8665
}
8766

8867
/**

0 commit comments

Comments
 (0)