diff --git a/composer.json b/composer.json index 468c24e..a4a2427 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": { diff --git a/src/HasChildren.php b/src/HasChildren.php index c720ad7..42bb7f0 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); @@ -29,7 +46,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'; @@ -54,11 +74,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) @@ -74,11 +96,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; @@ -104,12 +128,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); @@ -128,11 +152,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); } @@ -141,29 +165,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); } @@ -171,7 +209,7 @@ public function getClassNameForRelationships() /** * @return string */ - public function getInheritanceColumn() + public function getInheritanceColumn(): string { return property_exists($this, 'childColumn') ? $this->childColumn : 'type'; } @@ -186,14 +224,14 @@ protected function getChildModel(array $attributes) $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($aliasOrClass): string { $childTypes = $this->getChildTypes(); @@ -205,10 +243,10 @@ public function classFromAlias($aliasOrClass) } /** - * @param $className + * @param string $className * @return string */ - public function classToAlias($className) + public function classToAlias(string $className): string { $childTypes = $this->getChildTypes(); @@ -222,7 +260,7 @@ public function classToAlias($className) /** * @return array */ - public function getChildTypes() + public function getChildTypes(): array { return property_exists($this, 'childTypes') ? $this->childTypes : []; } diff --git a/src/HasParent.php b/src/HasParent.php index 7205b5d..d0cd1c9 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 { $parentClass = $this->getParentClass(); @@ -112,9 +121,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; 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) {