Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
bastien-phi committed Jun 5, 2024
1 parent 1ddf2b7 commit 5551971
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
3 changes: 2 additions & 1 deletion config/next-ide-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@
'larastan_friendly' => false,

/**
* Do not use nullable for timestamps defined in the model. For example: created_at, updated_at.
* For convenience, model timestamps are by default considered as non-nullable even if they are nullable in the database.
* Setting this to true will force the timestamps to be documented as nullable.
*/
'nullable_timestamps' => false,
],
Expand Down
18 changes: 13 additions & 5 deletions src/Domain/Models/Actions/ResolveModelAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function execute(Model $model): void
foreach ($columns as $column) {
$attribute = new Attribute($column['name'], $column['type_name'], $column['comment']);
$attribute->inDatabase = true;
if ($column['nullable'] && !$this->isLaravelTimestamp($model, $attribute)) {
if ($column['nullable'] && !$this->forceNullableColumnAsNonNullable($model, $attribute)) {
$attribute->nullable = true;
$attribute->nullableInDatabase = true;
}
Expand All @@ -42,24 +42,32 @@ private function resolveColumns(Model $model): array
->getColumns($model->instance()->getTable());
}

private function forceNullableColumnAsNonNullable(Model $model, Attribute $attribute): bool
{
if (!$this->isLaravelTimestamp($model, $attribute)) {
return false;
}

return !$this->modelTimestampsAreNonNullable();
}

private function isLaravelTimestamp(Model $model, Attribute $attribute): bool
{
if (!$model->instance()->usesTimestamps()) {
return false;
}

if (
!$this->isNullableLaravelTimestamp()
&& ($attribute->name === $model->instance()->getCreatedAtColumn()
|| $attribute->name === $model->instance()->getUpdatedAtColumn())
$attribute->name === $model->instance()->getCreatedAtColumn()
|| $attribute->name === $model->instance()->getUpdatedAtColumn()
) {
return true;
}

return false;
}

private function isNullableLaravelTimestamp(): bool
private function modelTimestampsAreNonNullable(): bool
{
return (bool) config('next-ide-helper.models.nullable_timestamps', false);
}
Expand Down
19 changes: 19 additions & 0 deletions tests/Unit/Domain/Models/Actions/ResolveModelAttributeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,23 @@ public function timestampsAreCorrectlyResolved(): void
$this->assertEquals('\\' . Date::now()::class, $createdAt->type);
$this->assertFalse($createdAt->nullable);
}

/**
* @test
*/
public function timestampsNullabilityCanBeConfigured(): void
{
config(['next-ide-helper.models.nullable_timestamps' => true]);

$model = new Model(User::class, $this->fixturePath('User.php'));

$resolveAttributes = new ResolveModelAttributes();

$resolveAttributes->execute($model);

$createdAt = $model->attributes->findByName('created_at');
$this->assertNotNull($createdAt);
$this->assertEquals('\\' . Date::now()::class, $createdAt->type);
$this->assertTrue($createdAt->nullable);
}
}

0 comments on commit 5551971

Please sign in to comment.