Skip to content

Commit 1e34954

Browse files
committed
Maintenance: Continued work towards PHPstan level 2
Updated html description code to be behind a proper interface. Set new convention for mode traits/interfaces.
1 parent 5ea4e1e commit 1e34954

18 files changed

+94
-57
lines changed

app/Entities/Models/Book.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626
* @property ?Page $defaultTemplate
2727
* @property ?SortRule $sortRule
2828
*/
29-
class Book extends Entity implements HasCoverImage
29+
class Book extends Entity implements CoverImageInterface, HtmlDescriptionInterface
3030
{
3131
use HasFactory;
32-
use HasHtmlDescription;
32+
use HtmlDescriptionTrait;
3333

3434
public float $searchFactor = 1.2;
3535

@@ -111,6 +111,7 @@ public function directPages(): HasMany
111111

112112
/**
113113
* Get all chapters within this book.
114+
* @return HasMany<Chapter>
114115
*/
115116
public function chapters(): HasMany
116117
{

app/Entities/Models/Bookshelf.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
use Illuminate\Database\Eloquent\Relations\BelongsTo;
99
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
1010

11-
class Bookshelf extends Entity implements HasCoverImage
11+
class Bookshelf extends Entity implements CoverImageInterface, HtmlDescriptionInterface
1212
{
1313
use HasFactory;
14-
use HasHtmlDescription;
14+
use HtmlDescriptionTrait;
1515

1616
protected $table = 'bookshelves';
1717

app/Entities/Models/Chapter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
* @property ?int $default_template_id
1515
* @property ?Page $defaultTemplate
1616
*/
17-
class Chapter extends BookChild
17+
class Chapter extends BookChild implements HtmlDescriptionInterface
1818
{
1919
use HasFactory;
20-
use HasHtmlDescription;
20+
use HtmlDescriptionTrait;
2121

2222
public float $searchFactor = 1.2;
2323

app/Entities/Models/HasCoverImage.php renamed to app/Entities/Models/CoverImageInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Illuminate\Database\Eloquent\Relations\BelongsTo;
66

7-
interface HasCoverImage
7+
interface CoverImageInterface
88
{
99
/**
1010
* Get the cover image for this item.

app/Entities/Models/Deletable.php renamed to app/Entities/Models/DeletableInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* A model that can be deleted in a manner that deletions
99
* are tracked to be part of the recycle bin system.
1010
*/
11-
interface Deletable
11+
interface DeletableInterface
1212
{
1313
public function deletions(): MorphMany;
1414
}

app/Entities/Models/Deletion.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* @property int $deleted_by
1414
* @property string $deletable_type
1515
* @property int $deletable_id
16-
* @property Deletable $deletable
16+
* @property DeletableInterface $deletable
1717
*/
1818
class Deletion extends Model implements Loggable
1919
{

app/Entities/Models/Entity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
* @method static Builder withLastView()
5050
* @method static Builder withViewCount()
5151
*/
52-
abstract class Entity extends Model implements Sluggable, Favouritable, Viewable, Deletable, Loggable
52+
abstract class Entity extends Model implements Sluggable, Favouritable, Viewable, DeletableInterface, Loggable
5353
{
5454
use SoftDeletes;
5555
use HasCreatorAndUpdater;

app/Entities/Models/HasHtmlDescription.php

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace BookStack\Entities\Models;
4+
5+
interface HtmlDescriptionInterface
6+
{
7+
/**
8+
* Get the HTML-based description for this item.
9+
* By default, the content should be sanitised unless raw is set to true.
10+
*/
11+
public function descriptionHtml(bool $raw = false): string;
12+
13+
/**
14+
* Set the HTML-based description for this item.
15+
*/
16+
public function setDescriptionHtml(string $html, string|null $plaintext = null): void;
17+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace BookStack\Entities\Models;
4+
5+
use BookStack\Util\HtmlContentFilter;
6+
7+
/**
8+
* @property string $description
9+
* @property string $description_html
10+
*/
11+
trait HtmlDescriptionTrait
12+
{
13+
public function descriptionHtml(bool $raw = false): string
14+
{
15+
$html = $this->description_html ?: '<p>' . nl2br(e($this->description)) . '</p>';
16+
if ($raw) {
17+
return $html;
18+
}
19+
20+
return HtmlContentFilter::removeScriptsFromHtmlString($html);
21+
}
22+
23+
public function setDescriptionHtml(string $html, string|null $plaintext = null): void
24+
{
25+
$this->description_html = $html;
26+
27+
if ($plaintext !== null) {
28+
$this->description = $plaintext;
29+
}
30+
31+
if (empty($html) && !empty($plaintext)) {
32+
$this->description_html = $this->descriptionHtml();
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)