From 4a166de0e2e9a499f48f4bbe2ee95267e6007aab Mon Sep 17 00:00:00 2001 From: Steve McDougall Date: Fri, 8 Oct 2021 09:23:34 +0100 Subject: [PATCH 1/5] Adding some type hints --- src/HasParent.php | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/src/HasParent.php b/src/HasParent.php index e4251c3..7c48cd1 100644 --- a/src/HasParent.php +++ b/src/HasParent.php @@ -2,15 +2,24 @@ namespace Parental; +use Illuminate\Database\Eloquent\Model; use ReflectionClass; use Illuminate\Support\Str; use Illuminate\Events\Dispatcher; +use ReflectionException; trait HasParent { + /** + * @var bool + */ public $hasParent = true; - public static function bootHasParent() + /** + * @return void + * @throws ReflectionException + */ + public static function bootHasParent(): void { // This adds support for using Parental with standalone Eloquent, outside a normal Laravel app. if (static::getEventDispatcher() === null) { @@ -37,16 +46,16 @@ public static function bootHasParent() /** * @return bool */ - public function parentHasHasChildrenTrait() + public function parentHasHasChildrenTrait(): bool { return $this->hasChildren ?? false; } /** * @return string - * @throws \ReflectionException + * @throws ReflectionException */ - public function getTable() + public function getTable(): string { if (! isset($this->table)) { return str_replace('\\', '', Str::snake(Str::plural(class_basename($this->getParentClass())))); @@ -57,20 +66,20 @@ public function getTable() /** * @return string - * @throws \ReflectionException + * @throws ReflectionException */ - public function getForeignKey() + public function getForeignKey(): string { return Str::snake(class_basename($this->getParentClass())).'_'.$this->primaryKey; } /** - * @param $related - * @param null $instance + * @param string $related + * @param null|Model $instance * @return string - * @throws \ReflectionException + * @throws ReflectionException */ - public function joiningTable($related, $instance = null) + public function joiningTable($related, $instance = null): string { $relatedClassName = method_exists((new $related), 'getClassNameForRelationships') ? (new $related)->getClassNameForRelationships() @@ -88,9 +97,9 @@ public function joiningTable($related, $instance = null) /** * @return string - * @throws \ReflectionException + * @throws ReflectionException */ - public function getClassNameForRelationships() + public function getClassNameForRelationships(): string { return class_basename($this->getParentClass()); } @@ -99,9 +108,9 @@ public function getClassNameForRelationships() * Get the class name for polymorphic relations. * * @return string - * @throws \ReflectionException + * @throws ReflectionException */ - public function getMorphClass() + public function getMorphClass(): string { if ($this->parentHasHasChildrenTrait()) { $parentClass = $this->getParentClass(); @@ -115,9 +124,9 @@ public function getMorphClass() * Get the class name for Parent Class. * * @return string - * @throws \ReflectionException + * @throws ReflectionException */ - protected function getParentClass() + protected function getParentClass(): string { static $parentClassName; From e6612b298c8d40c1b5330ac10826dad5141a0682 Mon Sep 17 00:00:00 2001 From: Steve McDougall Date: Fri, 8 Oct 2021 09:23:47 +0100 Subject: [PATCH 2/5] Updating composer file --- composer.json | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 2ad0991..900b978 100644 --- a/composer.json +++ b/composer.json @@ -4,8 +4,16 @@ "license": "MIT", "authors": [ { + "role": "author", "name": "Caleb Porzio", - "email": "calebporzio@gmail.com" + "email": "calebporzio@gmail.com", + "homepage": "https://calebporzio.com/" + }, + { + "role": "developer", + "name": "Steve McDougall", + "email": "juststevemcd@gmail.com", + "homepage": "https://www.juststeveking.uk/" } ], "require": { From 266a7f8136f875e6b36872ff4931f7c4d4b0112a Mon Sep 17 00:00:00 2001 From: Steve McDougall Date: Fri, 8 Oct 2021 09:32:41 +0100 Subject: [PATCH 3/5] Type hinting magic --- src/HasChildren.php | 114 +++++++++++++++++++++++++++++--------------- 1 file changed, 76 insertions(+), 38 deletions(-) diff --git a/src/HasChildren.php b/src/HasChildren.php index 97d977d..74e67e0 100644 --- a/src/HasChildren.php +++ b/src/HasChildren.php @@ -2,15 +2,32 @@ namespace Parental; +use Closure; +use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Relations\BelongsToMany; +use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Support\Str; trait HasChildren { + /** + * @var bool + */ protected static $parentBootMethods; + /** + * @var bool + */ protected $hasChildren = true; - protected static function registerModelEvent($event, $callback) + /** + * Register a model event with the dispatcher. + * + * @param string $event + * @param Closure|string $callback + * @return void + */ + protected static function registerModelEvent($event, $callback): void { parent::registerModelEvent($event, $callback); @@ -27,7 +44,10 @@ protected static function registerModelEvent($event, $callback) } } - protected static function parentIsBooting() + /** + * @return bool + */ + protected static function parentIsBooting(): bool { if (! isset(self::$parentBootMethods)) { self::$parentBootMethods[] = 'boot'; @@ -52,11 +72,13 @@ protected static function parentIsBooting() } /** - * @param array $attributes - * @param bool $exists - * @return $this + * Create a new instance of the given model. + * + * @param array $attributes + * @param bool $exists + * @return static */ - public function newInstance($attributes = [], $exists = false) + public function newInstance($attributes = [], $exists = false): self { $model = isset($attributes[$this->getInheritanceColumn()]) ? $this->getChildModel($attributes) @@ -72,11 +94,13 @@ public function newInstance($attributes = [], $exists = false) } /** - * @param array $attributes - * @param null $connection - * @return $this + * Create a new model instance that is existing. + * + * @param array $attributes + * @param string|null $connection + * @return static */ - public function newFromBuilder($attributes = [], $connection = null) + public function newFromBuilder($attributes = [], $connection = null): self { $attributes = (array) $attributes; @@ -102,12 +126,12 @@ public function newFromBuilder($attributes = [], $connection = null) * Define an inverse one-to-one or many relationship. * * @param string $related - * @param string $foreignKey - * @param string $ownerKey - * @param string $relation - * @return \Illuminate\Database\Eloquent\Relations\BelongsTo + * @param string|null $foreignKey + * @param string|null $ownerKey + * @param string|null $relation + * @return BelongsTo */ - public function belongsTo($related, $foreignKey = null, $ownerKey = null, $relation = null) + public function belongsTo($related, $foreignKey = null, $ownerKey = null, $relation = null): BelongsTo { $instance = $this->newRelatedInstance($related); @@ -126,11 +150,11 @@ public function belongsTo($related, $foreignKey = null, $ownerKey = null, $relat * Define a one-to-many relationship. * * @param string $related - * @param string $foreignKey - * @param string $localKey - * @return \Illuminate\Database\Eloquent\Relations\HasMany + * @param string|null $foreignKey + * @param string|null $localKey + * @return HasMany */ - public function hasMany($related, $foreignKey = null, $localKey = null) + public function hasMany($related, $foreignKey = null, $localKey = null): HasMany { return parent::hasMany($related, $foreignKey, $localKey); } @@ -139,29 +163,43 @@ public function hasMany($related, $foreignKey = null, $localKey = null) * Define a many-to-many relationship. * * @param string $related - * @param string $table - * @param string $foreignPivotKey - * @param string $relatedPivotKey - * @param string $parentKey - * @param string $relatedKey - * @param string $relation - * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany + * @param string|null $table + * @param string|null $foreignPivotKey + * @param string|null $relatedPivotKey + * @param string|null $parentKey + * @param string|null $relatedKey + * @param string|null $relation + * @return BelongsToMany */ - public function belongsToMany($related, $table = null, $foreignPivotKey = null, $relatedPivotKey = null, $parentKey = null, $relatedKey = null, $relation = null) - { + public function belongsToMany( + $related, $table = null, + $foreignPivotKey = null, + $relatedPivotKey = null, + $parentKey = null, + $relatedKey = null, + $relation = null + ): BelongsToMany { $instance = $this->newRelatedInstance($related); if (is_null($table) && $instance->hasParent) { $table = $this->joiningTable($instance->getClassNameForRelationships()); } - return parent::belongsToMany($related, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relation); + return parent::belongsToMany( + $related, + $table, + $foreignPivotKey, + $relatedPivotKey, + $parentKey, + $relatedKey, + $relation, + ); } /** * @return string */ - public function getClassNameForRelationships() + public function getClassNameForRelationships(): string { return class_basename($this); } @@ -169,7 +207,7 @@ public function getClassNameForRelationships() /** * @return string */ - public function getInheritanceColumn() + public function getInheritanceColumn(): string { return property_exists($this, 'childColumn') ? $this->childColumn : 'type'; } @@ -178,20 +216,20 @@ public function getInheritanceColumn() * @param array $attributes * @return mixed */ - protected function getChildModel(array $attributes) + protected function getChildModel(array $attributes): mixed { $className = $this->classFromAlias( $attributes[$this->getInheritanceColumn()] ); - return new $className((array)$attributes); + return new $className((array) $attributes); } /** - * @param $aliasOrClass + * @param mixed $aliasOrClass * @return string */ - public function classFromAlias($aliasOrClass) + public function classFromAlias(mixed $aliasOrClass): string { if (property_exists($this, 'childTypes')) { if (isset($this->childTypes[$aliasOrClass])) { @@ -203,10 +241,10 @@ public function classFromAlias($aliasOrClass) } /** - * @param $className + * @param string $className * @return string */ - public function classToAlias($className) + public function classToAlias(string $className): string { if (property_exists($this, 'childTypes')) { if (in_array($className, $this->childTypes)) { @@ -220,7 +258,7 @@ public function classToAlias($className) /** * @return array */ - public function getChildTypes() + public function getChildTypes(): array { return property_exists($this, 'childTypes') ? $this->childTypes : []; } From 46466229ef4608b5c744f9524decb6b3d6332d5d Mon Sep 17 00:00:00 2001 From: Steve McDougall Date: Fri, 8 Oct 2021 09:34:30 +0100 Subject: [PATCH 4/5] Type hint returns --- src/Providers/NovaResourceProvider.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Providers/NovaResourceProvider.php b/src/Providers/NovaResourceProvider.php index 7cfed0c..87f9381 100644 --- a/src/Providers/NovaResourceProvider.php +++ b/src/Providers/NovaResourceProvider.php @@ -9,7 +9,10 @@ class NovaResourceProvider extends ServiceProvider { - public function boot() + /** + * @return void + */ + public function boot(): void { if (class_exists(Nova::class)) { Nova::serving(function () { @@ -18,7 +21,10 @@ public function boot() } } - protected function setNovaResources() + /** + * @return void + */ + protected function setNovaResources(): void { $map = []; foreach (Nova::$resources as $resource) { From edc126cdd49f033d718ca3af133d2d67e7628038 Mon Sep 17 00:00:00 2001 From: Steve McDougall Date: Fri, 8 Oct 2021 09:37:29 +0100 Subject: [PATCH 5/5] Type hint returns fixing --- src/HasChildren.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/HasChildren.php b/src/HasChildren.php index 74e67e0..759bcfc 100644 --- a/src/HasChildren.php +++ b/src/HasChildren.php @@ -216,7 +216,7 @@ public function getInheritanceColumn(): string * @param array $attributes * @return mixed */ - protected function getChildModel(array $attributes): mixed + protected function getChildModel(array $attributes) { $className = $this->classFromAlias( $attributes[$this->getInheritanceColumn()] @@ -229,7 +229,7 @@ protected function getChildModel(array $attributes): mixed * @param mixed $aliasOrClass * @return string */ - public function classFromAlias(mixed $aliasOrClass): string + public function classFromAlias($aliasOrClass): string { if (property_exists($this, 'childTypes')) { if (isset($this->childTypes[$aliasOrClass])) {