22
33namespace Parental ;
44
5+ use Closure ;
6+ use Illuminate \Database \Eloquent \Relations \BelongsTo ;
7+ use Illuminate \Database \Eloquent \Relations \BelongsToMany ;
8+ use Illuminate \Database \Eloquent \Relations \HasMany ;
59use Illuminate \Support \Str ;
610
711trait HasChildren
812{
13+ /**
14+ * @var bool
15+ */
916 protected static $ parentBootMethods ;
1017
18+ /**
19+ * @var bool
20+ */
1121 protected $ hasChildren = true ;
1222
13- protected static function registerModelEvent ($ event , $ callback )
23+ /**
24+ * Register a model event with the dispatcher.
25+ *
26+ * @param string $event
27+ * @param Closure|string $callback
28+ * @return void
29+ */
30+ protected static function registerModelEvent ($ event , $ callback ): void
1431 {
1532 parent ::registerModelEvent ($ event , $ callback );
1633
@@ -29,7 +46,10 @@ protected static function registerModelEvent($event, $callback)
2946 }
3047 }
3148
32- protected static function parentIsBooting ()
49+ /**
50+ * @return bool
51+ */
52+ protected static function parentIsBooting (): bool
3353 {
3454 if (! isset (self ::$ parentBootMethods )) {
3555 self ::$ parentBootMethods [] = 'boot ' ;
@@ -54,11 +74,13 @@ protected static function parentIsBooting()
5474 }
5575
5676 /**
57- * @param array $attributes
58- * @param bool $exists
59- * @return $this
77+ * Create a new instance of the given model.
78+ *
79+ * @param array $attributes
80+ * @param bool $exists
81+ * @return static
6082 */
61- public function newInstance ($ attributes = [], $ exists = false )
83+ public function newInstance ($ attributes = [], $ exists = false ): self
6284 {
6385 $ model = isset ($ attributes [$ this ->getInheritanceColumn ()])
6486 ? $ this ->getChildModel ($ attributes )
@@ -74,11 +96,13 @@ public function newInstance($attributes = [], $exists = false)
7496 }
7597
7698 /**
77- * @param array $attributes
78- * @param null $connection
79- * @return $this
99+ * Create a new model instance that is existing.
100+ *
101+ * @param array $attributes
102+ * @param string|null $connection
103+ * @return static
80104 */
81- public function newFromBuilder ($ attributes = [], $ connection = null )
105+ public function newFromBuilder ($ attributes = [], $ connection = null ): self
82106 {
83107 $ attributes = (array ) $ attributes ;
84108
@@ -104,12 +128,12 @@ public function newFromBuilder($attributes = [], $connection = null)
104128 * Define an inverse one-to-one or many relationship.
105129 *
106130 * @param string $related
107- * @param string $foreignKey
108- * @param string $ownerKey
109- * @param string $relation
110- * @return \Illuminate\Database\Eloquent\Relations\ BelongsTo
131+ * @param string|null $foreignKey
132+ * @param string|null $ownerKey
133+ * @param string|null $relation
134+ * @return BelongsTo
111135 */
112- public function belongsTo ($ related , $ foreignKey = null , $ ownerKey = null , $ relation = null )
136+ public function belongsTo ($ related , $ foreignKey = null , $ ownerKey = null , $ relation = null ): BelongsTo
113137 {
114138 $ instance = $ this ->newRelatedInstance ($ related );
115139
@@ -128,11 +152,11 @@ public function belongsTo($related, $foreignKey = null, $ownerKey = null, $relat
128152 * Define a one-to-many relationship.
129153 *
130154 * @param string $related
131- * @param string $foreignKey
132- * @param string $localKey
133- * @return \Illuminate\Database\Eloquent\Relations\ HasMany
155+ * @param string|null $foreignKey
156+ * @param string|null $localKey
157+ * @return HasMany
134158 */
135- public function hasMany ($ related , $ foreignKey = null , $ localKey = null )
159+ public function hasMany ($ related , $ foreignKey = null , $ localKey = null ): HasMany
136160 {
137161 return parent ::hasMany ($ related , $ foreignKey , $ localKey );
138162 }
@@ -141,37 +165,51 @@ public function hasMany($related, $foreignKey = null, $localKey = null)
141165 * Define a many-to-many relationship.
142166 *
143167 * @param string $related
144- * @param string $table
145- * @param string $foreignPivotKey
146- * @param string $relatedPivotKey
147- * @param string $parentKey
148- * @param string $relatedKey
149- * @param string $relation
150- * @return \Illuminate\Database\Eloquent\Relations\ BelongsToMany
168+ * @param string|null $table
169+ * @param string|null $foreignPivotKey
170+ * @param string|null $relatedPivotKey
171+ * @param string|null $parentKey
172+ * @param string|null $relatedKey
173+ * @param string|null $relation
174+ * @return BelongsToMany
151175 */
152- public function belongsToMany ($ related , $ table = null , $ foreignPivotKey = null , $ relatedPivotKey = null , $ parentKey = null , $ relatedKey = null , $ relation = null )
153- {
176+ public function belongsToMany (
177+ $ related , $ table = null ,
178+ $ foreignPivotKey = null ,
179+ $ relatedPivotKey = null ,
180+ $ parentKey = null ,
181+ $ relatedKey = null ,
182+ $ relation = null
183+ ): BelongsToMany {
154184 $ instance = $ this ->newRelatedInstance ($ related );
155185
156186 if (is_null ($ table ) && $ instance ->hasParent ) {
157187 $ table = $ this ->joiningTable ($ instance ->getClassNameForRelationships ());
158188 }
159189
160- return parent ::belongsToMany ($ related , $ table , $ foreignPivotKey , $ relatedPivotKey , $ parentKey , $ relatedKey , $ relation );
190+ return parent ::belongsToMany (
191+ $ related ,
192+ $ table ,
193+ $ foreignPivotKey ,
194+ $ relatedPivotKey ,
195+ $ parentKey ,
196+ $ relatedKey ,
197+ $ relation ,
198+ );
161199 }
162200
163201 /**
164202 * @return string
165203 */
166- public function getClassNameForRelationships ()
204+ public function getClassNameForRelationships (): string
167205 {
168206 return class_basename ($ this );
169207 }
170208
171209 /**
172210 * @return string
173211 */
174- public function getInheritanceColumn ()
212+ public function getInheritanceColumn (): string
175213 {
176214 return property_exists ($ this , 'childColumn ' ) ? $ this ->childColumn : 'type ' ;
177215 }
@@ -186,14 +224,14 @@ protected function getChildModel(array $attributes)
186224 $ attributes [$ this ->getInheritanceColumn ()]
187225 );
188226
189- return new $ className ((array )$ attributes );
227+ return new $ className ((array ) $ attributes );
190228 }
191229
192230 /**
193- * @param $aliasOrClass
231+ * @param mixed $aliasOrClass
194232 * @return string
195233 */
196- public function classFromAlias ($ aliasOrClass )
234+ public function classFromAlias ($ aliasOrClass ): string
197235 {
198236 $ childTypes = $ this ->getChildTypes ();
199237
@@ -205,10 +243,10 @@ public function classFromAlias($aliasOrClass)
205243 }
206244
207245 /**
208- * @param $className
246+ * @param string $className
209247 * @return string
210248 */
211- public function classToAlias ($ className )
249+ public function classToAlias (string $ className ): string
212250 {
213251 $ childTypes = $ this ->getChildTypes ();
214252
@@ -222,7 +260,7 @@ public function classToAlias($className)
222260 /**
223261 * @return array
224262 */
225- public function getChildTypes ()
263+ public function getChildTypes (): array
226264 {
227265 return property_exists ($ this , 'childTypes ' ) ? $ this ->childTypes : [];
228266 }
0 commit comments