add Post persistence: model, migration, eloquent + fake repo

PostModel maps posts table (id, user_id fk, title, body text,
created_at indexed). EloquentPostRepository: create, find,
findByUserId (desc by created_at), findRecent (limit, desc),
delete - chain via ::query() to keep larastan happy.
FakePostRepository sorts on read (defensive copy each return).
cascade-on-delete on user_id so removing a user nukes their
posts.

phpstan.neon suppresses staticMethod.dynamicCall under
app/*/Eloquent*Repository.php - phpstan-strict-rules flags
Eloquent's fluent builder idiom (Model::query()->orderBy())
because the static methods become instance calls mid-chain.
suppression scoped to repo files only so the rule still
applies elsewhere.
This commit is contained in:
yisroel 2026-05-06 15:22:22 +03:00
parent 73a3acd39f
commit e3dddc60aa
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
5 changed files with 261 additions and 0 deletions

View file

@ -0,0 +1,83 @@
<?php
namespace App\Post;
use DateTimeImmutable;
use DateTimeZone;
class EloquentPostRepository implements PostRepository
{
public function create(CreatePostDto $dto): Post
{
$model = PostModel::create([
'user_id' => $dto->userId,
'title' => $dto->title,
'body' => $dto->body,
'created_at' => $dto->createdAt,
]);
return $this->toDomain($model);
}
public function find(int $id): ?Post
{
$model = PostModel::find($id);
return $model === null ? null : $this->toDomain($model);
}
/**
* @return Post[]
*/
public function findByUserId(int $userId): array
{
$models = PostModel::query()
->where('user_id', $userId)
->orderBy('created_at', 'desc')
->get();
return $models->map(
function (PostModel $model) {
return $this->toDomain($model);
},
)->all();
}
/**
* @return Post[]
*/
public function findRecent(int $limit): array
{
$models = PostModel::query()
->orderBy('created_at', 'desc')
->limit($limit)
->get();
return $models->map(
function (PostModel $model) {
return $this->toDomain($model);
},
)->all();
}
public function delete(int $id): void
{
PostModel::query()->where('id', $id)->delete();
}
private function toDomain(PostModel $model): Post
{
$utc = new DateTimeZone('UTC');
return new Post(
id: $model->id,
userId: $model->user_id,
title: $model->title,
body: $model->body,
createdAt: new DateTimeImmutable(
$model->created_at->toDateTimeString(),
$utc,
),
);
}
}

View file

@ -0,0 +1,40 @@
<?php
namespace App\Post;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;
/**
* @property int $id
* @property int $user_id
* @property string $title
* @property string $body
* @property Carbon $created_at
*
* @method static Builder<static>|PostModel newModelQuery()
* @method static Builder<static>|PostModel newQuery()
* @method static Builder<static>|PostModel query()
* @method static Builder<static>|PostModel whereId($value)
* @method static Builder<static>|PostModel whereUserId($value)
*
* @mixin \Eloquent
*/
class PostModel extends Model
{
protected $table = 'posts';
public $timestamps = false;
protected $fillable = [
'user_id',
'title',
'body',
'created_at',
];
protected $casts = [
'created_at' => 'datetime',
];
}

View file

@ -0,0 +1,26 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')
->constrained('users')
->cascadeOnDelete();
$table->string('title');
$table->text('body');
$table->dateTime('created_at')->index();
});
}
public function down(): void
{
Schema::dropIfExists('posts');
}
};

View file

@ -7,6 +7,15 @@ parameters:
treatPhpDocTypesAsCertain: false treatPhpDocTypesAsCertain: false
reportUnmatchedIgnoredErrors: false reportUnmatchedIgnoredErrors: false
ignoreErrors:
# Eloquent's fluent builder triggers staticMethod.dynamicCall in
# phpstan-strict-rules because Builder methods declared static on
# the model become instance calls after the first chain link. This
# is the documented Laravel idiom; suppress the false positive.
-
identifier: staticMethod.dynamicCall
path: app/*/Eloquent*Repository.php
paths: paths:
- app - app

View file

@ -0,0 +1,103 @@
<?php
namespace Tests\Fakes;
use App\Post\CreatePostDto;
use App\Post\Post;
use App\Post\PostRepository;
class FakePostRepository implements PostRepository
{
/**
* @var Post[]
*/
private array $existingPosts = [];
public function create(CreatePostDto $dto): Post
{
$id = $this->getNextId();
$post = new Post(
id: $id,
userId: $dto->userId,
title: $dto->title,
body: $dto->body,
createdAt: $dto->createdAt,
);
$this->existingPosts[$id] = $post;
return $post;
}
public function find(int $id): ?Post
{
$post = $this->existingPosts[$id] ?? null;
if ($post === null) {
return null;
}
return $this->copy($post);
}
/**
* @return Post[]
*/
public function findByUserId(int $userId): array
{
$matching = [];
foreach ($this->existingPosts as $post) {
if ($post->getUserId() === $userId) {
$matching[] = $this->copy($post);
}
}
usort(
$matching,
function (Post $left, Post $right) {
return $right->getCreatedAt() <=> $left->getCreatedAt();
},
);
return $matching;
}
/**
* @return Post[]
*/
public function findRecent(int $limit): array
{
$all = array_map(
function (Post $post) {
return $this->copy($post);
},
array_values($this->existingPosts),
);
usort(
$all,
function (Post $left, Post $right) {
return $right->getCreatedAt() <=> $left->getCreatedAt();
},
);
return array_slice($all, 0, $limit);
}
public function delete(int $id): void
{
unset($this->existingPosts[$id]);
}
private function copy(Post $post): Post
{
return new Post(
id: $post->getId(),
userId: $post->getUserId(),
title: $post->getTitle(),
body: $post->getBody(),
createdAt: $post->getCreatedAt(),
);
}
private function getNextId(): int
{
return count($this->existingPosts) + 1;
}
}