From 531ac374263085492848d455e5f6f007d0416493 Mon Sep 17 00:00:00 2001 From: Jannik Malken Date: Mon, 9 Aug 2021 08:57:16 +0000 Subject: [PATCH] Replace trait init with fill + add type hints --- src/Traits/SingleTableInheritance.php | 31 ++++++++++--------- .../Traits/SingleTableInheritanceTest.php | 11 +++++-- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/Traits/SingleTableInheritance.php b/src/Traits/SingleTableInheritance.php index a53a642..67fbba1 100644 --- a/src/Traits/SingleTableInheritance.php +++ b/src/Traits/SingleTableInheritance.php @@ -10,7 +10,6 @@ trait SingleTableInheritance { public static function bootSingleTableInheritance() { - if (static::isSubClass()) { static::addGlobalScope('sti', function (Builder $builder) { return $builder->sti(); @@ -18,8 +17,10 @@ public static function bootSingleTableInheritance() } } - public function initializeSingleTableInheritance($attributes = []) + public function fill(array $attributes) { + parent::fill($attributes); + if ($this->resolveTypeViaAttributes($attributes)) { return; } @@ -37,7 +38,7 @@ public static function getStiMap(): array })->toArray(); } - public static function getStiSubTypes() + public static function getStiSubTypes(): array { return array_keys(static::getStiMap()); } @@ -45,7 +46,7 @@ public static function getStiSubTypes() /** * Recursively traverse all sti sub classes */ - public static function getStiSubClasses() + public static function getStiSubClasses(): array { $classes = collect(static::getDirectStiSubClasses()); foreach (static::getDirectStiSubClasses() as $subClass) { @@ -72,12 +73,12 @@ public static function getDirectStiSubClasses(): array return static::$stiSubClasses; } - public static function getTypesForScope() + public static function getTypesForScope(): array { return array_merge([static::resolveTypeViaClass()], static::getStiSubTypes()); } - public function resolveTypeViaAttributes($attributes = null) + public function resolveTypeViaAttributes($attributes = null): ?string { $attributes = $attributes ?: $this->attributes; return ($attribute = $this->getTypeColumn()) @@ -85,7 +86,7 @@ public function resolveTypeViaAttributes($attributes = null) : null; } - public static function resolveTypeViaClass() + public static function resolveTypeViaClass(): ?string { return static::isSubClass() ? static::class : null; } @@ -98,12 +99,12 @@ public function applyTypeCharacteristics($type) $this->attributes[$this->getTypeColumn()] = $type; } - public static function isSubClass() + public static function isSubClass(): bool { return is_subclass_of(static::class, static::getBaseModelClass()); } - public static function getBaseModelClass() + public static function getBaseModelClass(): string { return self::class; } @@ -126,12 +127,12 @@ public function handleSaved() { } - public function getMorphClass() + public function getMorphClass(): string { return self::class; } - public function getTypeColumn($qualified = false) + public function getTypeColumn($qualified = false): string { $typeColumn = isset($this->typeColumn) ? $this->typeColumn @@ -147,24 +148,24 @@ public function getTypeColumn($qualified = false) * * @return string */ - public function getForeignKey() + public function getForeignKey(): string { return Str::snake(class_basename(self::class)) . '_' . $this->getKeyName(); } - public function getModelClassViaAttributes($attributes = []) + public function getModelClassViaAttributes($attributes = []): ?string { return $this->resolveTypeViaAttributes($attributes); } - public function getTypeAttribute() + public function getTypeAttribute(): ?string { $type = $this->resolveTypeViaAttributes($this->attributes); $type = Str::kebab(class_basename($type)); return $type ?: null; } - public function getTable() + public function getTable(): string { if (!isset($this->table)) { return str_replace( diff --git a/tests/Unit/Traits/SingleTableInheritanceTest.php b/tests/Unit/Traits/SingleTableInheritanceTest.php index b983741..3969256 100644 --- a/tests/Unit/Traits/SingleTableInheritanceTest.php +++ b/tests/Unit/Traits/SingleTableInheritanceTest.php @@ -152,8 +152,8 @@ public function queries_are_scoped_correctly() $this->assertCount(10, Car::all()); } - /** @test */ - public function _scope_sti_with_types_param() + /** @test */ + public function scope_sti_with_types_param() { factory(Vehicle::class, 10)->create(); factory(Car::class, 10)->create(); @@ -161,4 +161,11 @@ public function _scope_sti_with_types_param() $this->assertEquals(10, Vehicle::sti([Car::class])->count()); $this->assertCount(10, Car::all()); } + + /** @test */ + public function attribute_is_set_when_subclass_is_created_via_constructor() + { + $car = new Car(); + $this->assertEquals('car', $car->type); + } }