Skip to content

feat: add rule for Laravel Timestamps property to attribute#491

Open
yoannblot wants to merge 1 commit into
driftingly:mainfrom
yoannblot:feat/laravel-timestamps-property-to-attribute
Open

feat: add rule for Laravel Timestamps property to attribute#491
yoannblot wants to merge 1 commit into
driftingly:mainfrom
yoannblot:feat/laravel-timestamps-property-to-attribute

Conversation

@yoannblot
Copy link
Copy Markdown

Context

Laravel 13 introduces the WithoutTimestamps attributes (source).

Description

This new rule replaces the old property $timestamps by the new #[WithoutTimestamps] attribute :

 use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\Attributes\WithoutTimestamps;

+#[WithoutTimestamps]
 final class User extends Model
 {
-    public $timestamps = false;
 }

or, removes the property when the value is true, because that's the default behavior :

 use Illuminate\Database\Eloquent\Model;

 final class User extends Model
 {
-    public $timestamps = true;
 }

@yoannblot yoannblot marked this pull request as ready for review April 1, 2026 06:25
Comment on lines +88 to +95
private function isAnEloquentModelClass(Class_ $class): bool
{
if ($class->extends === null) {
return false;
}

return $this->isName($class->extends, 'Illuminate\Database\Eloquent\Model') || $this->isName($class->extends, 'Model');
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be replaced by a simple $this->isObjectType($class, new \PHPStan\Type\ObjectType('Illuminate\Database\Eloquent\Model')) call.

}

$this->removePropertyFromClass($node, $timestampsProperty);
if($this->isFalseProperty($timestampsProperty)){
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Equally if you have the property you should be able to do $this->getType($timestampProperty)->isFalse()->yes() instead of needing this method implemented.

return null;
}

$this->removePropertyFromClass($node, $timestampsProperty);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems wrong and should only happen if the attribute is added because the value is false. It just causes weird edge cases and extends the purepose of the rule beyond it's description.

$rectorConfig->rule(QueuePropertyToQueueAttributeRector::class);
$rectorConfig->rule(TablePropertyToTableAttributeRector::class);
$rectorConfig->rule(TimeoutPropertyToTimeoutAttributeRector::class);
$rectorConfig->rule(TimestampsPropertyToTimestampsAttributeRector::class);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also changed these to live in their own config instead of laravel130.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants