From 0391cf80de24434bf7227c08b4cf284109dcf99f Mon Sep 17 00:00:00 2001 From: Jonny Barnes Date: Thu, 29 Apr 2021 20:02:20 +0100 Subject: [PATCH] Redo Notes a little, improve tests --- app/Models/Note.php | 53 +++++++++++++++++---------- app/Models/Tag.php | 10 +---- database/seeders/NotesTableSeeder.php | 6 +++ tests/Unit/NotesTest.php | 44 ++++++++++++++++++++++ tests/Unit/TagsTest.php | 21 +++++++++++ 5 files changed, 107 insertions(+), 27 deletions(-) diff --git a/app/Models/Note.php b/app/Models/Note.php index d86b8eac..6e4c2f8e 100644 --- a/app/Models/Note.php +++ b/app/Models/Note.php @@ -5,14 +5,15 @@ declare(strict_types=1); namespace App\Models; use App\Exceptions\TwitterContentException; +use Barryvdh\LaravelIdeHelper\Eloquent; use Codebird\Codebird; -use Eloquent; use Exception; use GuzzleHttp\Client; use Illuminate\Database\Eloquent\Relations\{BelongsTo, BelongsToMany, HasMany, MorphMany}; use Illuminate\Database\Eloquent\{Builder, Collection, Factories\HasFactory, Model, SoftDeletes}; use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Cache; +use JetBrains\PhpStorm\ArrayShape; use Jonnybarnes\IndieWeb\Numbers; use Laravel\Scout\Searchable; use League\CommonMark\Block\Element\{FencedCode, IndentedCode}; @@ -20,6 +21,11 @@ use League\CommonMark\Extension\Autolink\AutolinkExtension; use League\CommonMark\{CommonMarkConverter, Environment}; use Normalizer; use Spatie\CommonMarkHighlighter\{FencedCodeRenderer, IndentedCodeRenderer}; +use App\Models\Tag; +use App\Models\MicropubClient; +use App\Models\WebMention; +use App\Models\Place; +use App\Models\Media; /** * App\Models\Note. @@ -102,12 +108,12 @@ class Note extends Model /** * This variable is used to keep track of contacts in a note. */ - protected $contacts; + protected ?array $contacts; /** * Set our contacts variable to null. * - * @param array $attributes + * @param array $attributes */ public function __construct(array $attributes = []) { @@ -145,9 +151,9 @@ class Note extends Model * * @return BelongsToMany */ - public function tags() + public function tags(): BelongsToMany { - return $this->belongsToMany('App\Models\Tag'); + return $this->belongsToMany(Tag::class); } /** @@ -155,9 +161,9 @@ class Note extends Model * * @return BelongsTo */ - public function client() + public function client(): BelongsTo { - return $this->belongsTo('App\Models\MicropubClient', 'client_id', 'client_url'); + return $this->belongsTo(MicropubClient::class, 'client_id', 'client_url'); } /** @@ -165,9 +171,9 @@ class Note extends Model * * @return MorphMany */ - public function webmentions() + public function webmentions(): MorphMany { - return $this->morphMany('App\Models\WebMention', 'commentable'); + return $this->morphMany(WebMention::class, 'commentable'); } /** @@ -175,9 +181,9 @@ class Note extends Model * * @return BelongsTo */ - public function place() + public function place(): BelongsTo { - return $this->belongsTo('App\Models\Place'); + return $this->belongsTo(Place::class); } /** @@ -185,9 +191,9 @@ class Note extends Model * * @return HasMany */ - public function media() + public function media(): HasMany { - return $this->hasMany('App\Models\Media'); + return $this->hasMany(Media::class); } /** @@ -195,6 +201,7 @@ class Note extends Model * * @return array */ + #[ArrayShape(['note' => "null|string"])] public function toSearchableArray(): array { return [ @@ -207,7 +214,7 @@ class Note extends Model * * @param string|null $value */ - public function setNoteAttribute(?string $value) + public function setNoteAttribute(?string $value): void { if ($value !== null) { $normalized = normalizer_normalize($value, Normalizer::FORM_C); @@ -253,13 +260,13 @@ class Note extends Model $note = $this->note; foreach ($this->media as $media) { - if ($media->type == 'image') { + if ($media->type === 'image') { $note .= ''; } - if ($media->type == 'audio') { + if ($media->type === 'audio') { $note .= '