Skip to content

Commit c4f458f

Browse files
Merge pull request #97 from JustSteveKing/feature/improvements
Improvements
2 parents 381d8bc + edc126c commit c4f458f

File tree

4 files changed

+117
-56
lines changed

4 files changed

+117
-56
lines changed

composer.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,16 @@
44
"license": "MIT",
55
"authors": [
66
{
7+
"role": "author",
78
"name": "Caleb Porzio",
8-
"email": "calebporzio@gmail.com"
9+
"email": "calebporzio@gmail.com",
10+
"homepage": "https://calebporzio.com/"
11+
},
12+
{
13+
"role": "developer",
14+
"name": "Steve McDougall",
15+
"email": "juststevemcd@gmail.com",
16+
"homepage": "https://www.juststeveking.uk/"
917
}
1018
],
1119
"require": {

src/HasChildren.php

Lines changed: 75 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,32 @@
22

33
namespace 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;
59
use Illuminate\Support\Str;
610

711
trait 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
}

src/HasParent.php

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,24 @@
22

33
namespace Parental;
44

5+
use Illuminate\Database\Eloquent\Model;
56
use ReflectionClass;
67
use Illuminate\Support\Str;
78
use Illuminate\Events\Dispatcher;
9+
use ReflectionException;
810

911
trait HasParent
1012
{
13+
/**
14+
* @var bool
15+
*/
1116
public $hasParent = true;
1217

13-
public static function bootHasParent()
18+
/**
19+
* @return void
20+
* @throws ReflectionException
21+
*/
22+
public static function bootHasParent(): void
1423
{
1524
// This adds support for using Parental with standalone Eloquent, outside a normal Laravel app.
1625
if (static::getEventDispatcher() === null) {
@@ -37,16 +46,16 @@ public static function bootHasParent()
3746
/**
3847
* @return bool
3948
*/
40-
public function parentHasHasChildrenTrait()
49+
public function parentHasHasChildrenTrait(): bool
4150
{
4251
return $this->hasChildren ?? false;
4352
}
4453

4554
/**
4655
* @return string
47-
* @throws \ReflectionException
56+
* @throws ReflectionException
4857
*/
49-
public function getTable()
58+
public function getTable(): string
5059
{
5160
if (! isset($this->table)) {
5261
return str_replace('\\', '', Str::snake(Str::plural(class_basename($this->getParentClass()))));
@@ -57,20 +66,20 @@ public function getTable()
5766

5867
/**
5968
* @return string
60-
* @throws \ReflectionException
69+
* @throws ReflectionException
6170
*/
62-
public function getForeignKey()
71+
public function getForeignKey(): string
6372
{
6473
return Str::snake(class_basename($this->getParentClass())).'_'.$this->primaryKey;
6574
}
6675

6776
/**
68-
* @param $related
69-
* @param null $instance
77+
* @param string $related
78+
* @param null|Model $instance
7079
* @return string
71-
* @throws \ReflectionException
80+
* @throws ReflectionException
7281
*/
73-
public function joiningTable($related, $instance = null)
82+
public function joiningTable($related, $instance = null): string
7483
{
7584
$relatedClassName = method_exists((new $related), 'getClassNameForRelationships')
7685
? (new $related)->getClassNameForRelationships()
@@ -88,9 +97,9 @@ public function joiningTable($related, $instance = null)
8897

8998
/**
9099
* @return string
91-
* @throws \ReflectionException
100+
* @throws ReflectionException
92101
*/
93-
public function getClassNameForRelationships()
102+
public function getClassNameForRelationships(): string
94103
{
95104
return class_basename($this->getParentClass());
96105
}
@@ -99,9 +108,9 @@ public function getClassNameForRelationships()
99108
* Get the class name for polymorphic relations.
100109
*
101110
* @return string
102-
* @throws \ReflectionException
111+
* @throws ReflectionException
103112
*/
104-
public function getMorphClass()
113+
public function getMorphClass(): string
105114
{
106115
$parentClass = $this->getParentClass();
107116

@@ -112,9 +121,9 @@ public function getMorphClass()
112121
* Get the class name for Parent Class.
113122
*
114123
* @return string
115-
* @throws \ReflectionException
124+
* @throws ReflectionException
116125
*/
117-
protected function getParentClass()
126+
protected function getParentClass(): string
118127
{
119128
static $parentClassName;
120129

src/Providers/NovaResourceProvider.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99

1010
class NovaResourceProvider extends ServiceProvider
1111
{
12-
public function boot()
12+
/**
13+
* @return void
14+
*/
15+
public function boot(): void
1316
{
1417
if (class_exists(Nova::class)) {
1518
Nova::serving(function () {
@@ -18,7 +21,10 @@ public function boot()
1821
}
1922
}
2023

21-
protected function setNovaResources()
24+
/**
25+
* @return void
26+
*/
27+
protected function setNovaResources(): void
2228
{
2329
$map = [];
2430
foreach (Nova::$resources as $resource) {

0 commit comments

Comments
 (0)