diff --git a/src/Database/Concerns/HasRelationships.php b/src/Database/Concerns/HasRelationships.php index c79e63e1e..069c8e943 100644 --- a/src/Database/Concerns/HasRelationships.php +++ b/src/Database/Concerns/HasRelationships.php @@ -1,19 +1,20 @@ getRelationDefinition($name) !== null; } @@ -151,9 +151,8 @@ public function hasRelation($name) /** * Returns relationship details from a supplied name. * @param string $name Relation name - * @return array|null */ - public function getRelationDefinition($name) + public function getRelationDefinition($name): ?array { if (($type = $this->getRelationType($name)) !== null) { return (array) $this->getRelationTypeDefinition($type, $name) + $this->getRelationDefaults($type); @@ -195,9 +194,8 @@ public function getRelationTypeDefinition($type, $name) /** * Returns relationship details for all relations defined on this model. - * @return array */ - public function getRelationDefinitions() + public function getRelationDefinitions(): array { $result = []; @@ -219,10 +217,8 @@ public function getRelationDefinitions() /** * Returns a relationship type based on a supplied name. - * @param string $name Relation name - * @return string|null */ - public function getRelationType($name) + public function getRelationType(string $name): ?string { foreach (static::$relationTypes as $type) { if ($this->getRelationTypeDefinition($type, $name) !== null) { @@ -234,11 +230,9 @@ public function getRelationType($name) } /** - * Returns a relation class object - * @param string $name Relation name - * @return \Winter\Storm\Database\Relations\Relation|null + * Returns a new instance of a related model */ - public function makeRelation($name) + public function makeRelation(string $name): ?Model { $relationType = $this->getRelationType($name); $relation = $this->getRelationDefinition($name); @@ -248,7 +242,7 @@ public function makeRelation($name) } $relationClass = $relation[0]; - return new $relationClass(); + return $this->newRelatedInstance($relationClass); } /** diff --git a/src/Database/Models/DeferredBinding.php b/src/Database/Models/DeferredBinding.php index 5f2e1f113..ad7c37b0c 100644 --- a/src/Database/Models/DeferredBinding.php +++ b/src/Database/Models/DeferredBinding.php @@ -1,7 +1,7 @@ where('session_key', $sessionKey) @@ -88,7 +88,7 @@ public static function cancelDeferredActions($masterType, $sessionKey) /** * Delete this binding and cancel is actions */ - public function deleteCancel() + public function deleteCancel(): void { $this->deleteSlaveRecord(); $this->delete(); @@ -97,7 +97,7 @@ public function deleteCancel() /** * Clean up orphan bindings. */ - public static function cleanUp($days = 5) + public static function cleanUp(int $days = 5): void { $records = self::where('created_at', '<', Carbon::now()->subDays($days)->toDateTimeString())->get(); @@ -109,7 +109,7 @@ public static function cleanUp($days = 5) /** * Logic to cancel a bindings action. */ - protected function deleteSlaveRecord() + protected function deleteSlaveRecord(): void { /* * Try to delete unbound hasOne/hasMany records from the details table @@ -144,8 +144,7 @@ protected function deleteSlaveRecord() if (!$relatedObj->$foreignKey) { $relatedObj->delete(); } - } - catch (Exception $ex) { + } catch (Exception $ex) { // Do nothing } } diff --git a/src/Database/Relations/BelongsTo.php b/src/Database/Relations/BelongsTo.php index 1a133f6b1..f708d56a2 100644 --- a/src/Database/Relations/BelongsTo.php +++ b/src/Database/Relations/BelongsTo.php @@ -35,8 +35,7 @@ public function add(Model $model, $sessionKey = null) { if ($sessionKey === null) { $this->associate($model); - } - else { + } else { $this->child->bindDeferred($this->relationName, $model, $sessionKey); } } @@ -48,8 +47,7 @@ public function remove(Model $model, $sessionKey = null) { if ($sessionKey === null) { $this->dissociate(); - } - else { + } else { $this->child->unbindDeferred($this->relationName, $model, $sessionKey); } } @@ -78,8 +76,7 @@ public function setSimpleValue($value) $this->associate($value); $this->child->setRelation($this->relationName, $value); - } - else { + } else { $this->child->setAttribute($this->getForeignKeyName(), $value); $this->child->reloadRelations($this->relationName); } diff --git a/src/Database/Relations/Concerns/AttachOneOrMany.php b/src/Database/Relations/Concerns/AttachOneOrMany.php index 22e761e44..760cd1d6b 100644 --- a/src/Database/Relations/Concerns/AttachOneOrMany.php +++ b/src/Database/Relations/Concerns/AttachOneOrMany.php @@ -216,8 +216,7 @@ public function add(Model $model, $sessionKey = null) * */ $this->parent->fireEvent('model.relation.afterAdd', [$this->relationName, $model]); - } - else { + } else { $this->parent->bindDeferred($this->relationName, $model, $sessionKey); } } diff --git a/src/Database/Relations/Concerns/BelongsOrMorphsToMany.php b/src/Database/Relations/Concerns/BelongsOrMorphsToMany.php index 6b3e50698..e07d38281 100644 --- a/src/Database/Relations/Concerns/BelongsOrMorphsToMany.php +++ b/src/Database/Relations/Concerns/BelongsOrMorphsToMany.php @@ -243,8 +243,7 @@ public function add(Model $model, $sessionKey = null, $pivotData = []) if ($sessionKey === null || $sessionKey === false) { $this->attach($model, $pivotData); $this->parent->reloadRelations($this->relationName); - } - else { + } else { $this->parent->bindDeferred($this->relationName, $model, $sessionKey, $pivotData); } } diff --git a/src/Database/Relations/Concerns/HasOneOrMany.php b/src/Database/Relations/Concerns/HasOneOrMany.php index 918604cb9..b6fe905b1 100644 --- a/src/Database/Relations/Concerns/HasOneOrMany.php +++ b/src/Database/Relations/Concerns/HasOneOrMany.php @@ -102,8 +102,7 @@ public function add(Model $model, $sessionKey = null) * */ $this->parent->fireEvent('model.relation.afterAdd', [$this->relationName, $model]); - } - else { + } else { $this->parent->bindDeferred($this->relationName, $model, $sessionKey); } } diff --git a/src/Database/Relations/Concerns/MorphOneOrMany.php b/src/Database/Relations/Concerns/MorphOneOrMany.php index 6add0dcb4..2bb7c49a1 100644 --- a/src/Database/Relations/Concerns/MorphOneOrMany.php +++ b/src/Database/Relations/Concerns/MorphOneOrMany.php @@ -88,8 +88,7 @@ public function add(Model $model, $sessionKey = null) * */ $this->parent->fireEvent('model.relation.afterAdd', [$this->relationName, $model]); - } - else { + } else { $this->parent->bindDeferred($this->relationName, $model, $sessionKey); } } diff --git a/src/Database/Traits/DeferredBinding.php b/src/Database/Traits/DeferredBinding.php index e49750de2..af916f60a 100644 --- a/src/Database/Traits/DeferredBinding.php +++ b/src/Database/Traits/DeferredBinding.php @@ -1,5 +1,7 @@ hasRelation($relationName)) { return false; @@ -27,7 +29,7 @@ public function isDeferrable($relationName) /** * Bind a deferred relationship to the supplied record. */ - public function bindDeferred($relation, $record, $sessionKey, $pivotData = []) + public function bindDeferred(string $relation, Model $record, string $sessionKey, array $pivotData = []): DeferredBindingModel { $binding = new DeferredBindingModel; $binding->setConnection($this->getConnectionName()); @@ -45,7 +47,7 @@ public function bindDeferred($relation, $record, $sessionKey, $pivotData = []) /** * Unbind a deferred relationship to the supplied record. */ - public function unbindDeferred($relation, $record, $sessionKey) + public function unbindDeferred(string $relation, Model $record, string $sessionKey): DeferredBindingModel { $binding = new DeferredBindingModel; $binding->setConnection($this->getConnectionName()); @@ -62,7 +64,7 @@ public function unbindDeferred($relation, $record, $sessionKey) /** * Cancel all deferred bindings to this model. */ - public function cancelDeferred($sessionKey) + public function cancelDeferred(string $sessionKey): void { DeferredBindingModel::cancelDeferredActions(get_class($this), $sessionKey); } @@ -70,7 +72,7 @@ public function cancelDeferred($sessionKey) /** * Commit all deferred bindings to this model. */ - public function commitDeferred($sessionKey) + public function commitDeferred(string $sessionKey): void { $this->commitDeferredOfType($sessionKey); DeferredBindingModel::flushDuplicateCache(); @@ -81,7 +83,7 @@ public function commitDeferred($sessionKey) * It is a rare need to have to call this, since it only applies to the * "belongs to" relationship which generally does not need deferring. */ - protected function commitDeferredBefore($sessionKey) + protected function commitDeferredBefore(string $sessionKey): void { $this->commitDeferredOfType($sessionKey, 'belongsTo'); } @@ -89,7 +91,7 @@ protected function commitDeferredBefore($sessionKey) /** * Internally used method to commit all deferred bindings after saving. */ - protected function commitDeferredAfter($sessionKey) + protected function commitDeferredAfter(string $sessionKey): void { $this->commitDeferredOfType($sessionKey, null, 'belongsTo'); DeferredBindingModel::flushDuplicateCache(); @@ -98,7 +100,7 @@ protected function commitDeferredAfter($sessionKey) /** * Internal method for committing deferred relations. */ - protected function commitDeferredOfType($sessionKey, $include = null, $exclude = null) + protected function commitDeferredOfType(string $sessionKey, string|array|null $include = null, string|array|null $exclude = null): void { if (!strlen($sessionKey)) { return; @@ -120,8 +122,7 @@ protected function commitDeferredOfType($sessionKey, $include = null, $exclude = if ($include) { $allowedTypes = array_intersect($allowedTypes, (array) $include); - } - elseif ($exclude) { + } elseif ($exclude) { $allowedTypes = array_diff($allowedTypes, (array) $exclude); } @@ -132,8 +133,7 @@ protected function commitDeferredOfType($sessionKey, $include = null, $exclude = /* * Find the slave model */ - $slaveClass = $binding->slave_type; - $slaveModel = new $slaveClass; + $slaveModel = $this->makeRelation($relationName) ?: new $binding->slave_type; $slaveModel = $slaveModel->find($binding->slave_id); if (!$slaveModel) { @@ -152,8 +152,7 @@ protected function commitDeferredOfType($sessionKey, $include = null, $exclude = } else { $relationObj->add($slaveModel); } - } - else { + } else { $relationObj->remove($slaveModel); } @@ -163,9 +162,8 @@ protected function commitDeferredOfType($sessionKey, $include = null, $exclude = /** * Returns any outstanding binding records for this model. - * @return \Winter\Storm\Database\Collection */ - protected function getDeferredBindingRecords($sessionKey) + protected function getDeferredBindingRecords(string $sessionKey): Collection { $binding = new DeferredBindingModel; @@ -180,9 +178,8 @@ protected function getDeferredBindingRecords($sessionKey) /** * Returns all possible relation types that can be deferred. - * @return array */ - protected function getDeferrableRelationTypes() + protected function getDeferrableRelationTypes(): array { return [ 'hasMany',