Skip to content

Commit 7fd03a9

Browse files
authored
Merge pull request #5 from mr-punyapal/refactor
Refactor
2 parents bb82551 + ddf63f7 commit 7fd03a9

12 files changed

+122
-156
lines changed

config/extended-relationships.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
// config for Mrpunyapal/LaravelExtendedRelationships
46
return [
57

debug.log

Lines changed: 0 additions & 1 deletion
This file was deleted.

pint.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"preset": "laravel",
3+
"rules": {
4+
"declare_strict_types": true,
5+
"strict_param":true,
6+
"strict_comparison":true
7+
}
8+
}

src/HasExtendedRelationships.php

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,39 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Mrpunyapal\LaravelExtendedRelationships;
46

7+
use Illuminate\Contracts\Database\Query\Builder;
58
use Mrpunyapal\LaravelExtendedRelationships\Relations\BelongsToArrayColumn;
69
use Mrpunyapal\LaravelExtendedRelationships\Relations\BelongsToManyKeys;
710
use Mrpunyapal\LaravelExtendedRelationships\Relations\HasManyArrayColumn;
811
use Mrpunyapal\LaravelExtendedRelationships\Relations\HasManyKeys;
912

1013
trait HasExtendedRelationships
1114
{
12-
/**
13-
* @param string|null $foreignKey
14-
* @param string[]|null $relations
15-
*/
16-
public function belongsToManyKeys(string $related, string $foreignKey, array $relations): BelongsToManyKeys
15+
public function belongsToManyKeys(string $related, ?string $foreignKey, ?array $relations): BelongsToManyKeys
1716
{
18-
$instance = new $related();
19-
20-
return new BelongsToManyKeys($instance->newQuery(), $this, $foreignKey, $relations);
17+
return new BelongsToManyKeys($this->relatedNewQuery($related), $this, $foreignKey, $relations);
2118
}
2219

23-
/**
24-
* @param string[]|null $relations
25-
*/
26-
public function hasManyKeys(string $related, ?array $relations = null, ?string $localKey = null): HasManyKeys
20+
public function hasManyKeys(string $related, ?array $relations, ?string $localKey): HasManyKeys
2721
{
28-
$instance = new $related();
29-
30-
return new HasManyKeys($instance->newQuery(), $this, $relations, $localKey);
22+
return new HasManyKeys($this->relatedNewQuery($related), $this, $relations, $localKey);
3123
}
3224

3325
public function hasManyArrayColumn(string $related, ?string $foreignKey, ?string $localKey): HasManyArrayColumn
3426
{
35-
$instance = new $related();
36-
37-
return new HasManyArrayColumn($instance->newQuery(), $this, $foreignKey, $localKey);
27+
return new HasManyArrayColumn($this->relatedNewQuery($related), $this, $foreignKey, $localKey);
3828
}
3929

40-
public function belongsToArrayColumn(string $related, ?string $foreignKey, ?string $localKey, $isString = false): BelongsToArrayColumn
30+
public function belongsToArrayColumn(string $related, ?string $foreignKey, ?string $localKey, bool $isString = false): BelongsToArrayColumn
4131
{
42-
$instance = new $related();
32+
return new BelongsToArrayColumn($this->relatedNewQuery($related), $this, $foreignKey, $localKey, null, $isString);
33+
}
4334

44-
return new BelongsToArrayColumn($instance->newQuery(), $this, $foreignKey, $localKey, null, $isString);
35+
protected function relatedNewQuery($related): Builder
36+
{
37+
return (new $related())->newQuery();
4538
}
4639
}

src/Relations/BelongsToArrayColumn.php

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,47 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Mrpunyapal\LaravelExtendedRelationships\Relations;
46

57
use Illuminate\Database\Eloquent\Builder;
8+
use Illuminate\Database\Eloquent\Collection;
69
use Illuminate\Database\Eloquent\Model;
710
use Illuminate\Database\Eloquent\Relations\BelongsTo;
811

912
class BelongsToArrayColumn extends BelongsTo
1013
{
1114
/**
12-
* Indicates whether the value is a string.
13-
*
14-
* @var bool
15-
*/
16-
protected $isString;
17-
18-
/**
19-
* Create a new belongs to relationship instance.
20-
*
21-
* @param string $foreignKey
22-
* @param string $ownerKey
23-
* @param string $relationName
24-
* @param bool $isString
25-
* @return void
15+
* Create a new belongs to array Column relationship instance.
2616
*/
27-
public function __construct(Builder $query, Model $child, $foreignKey, $ownerKey, $relationName, $isString = false)
17+
public function __construct(Builder $query, Model $child, string $foreignKey, string $ownerKey, ?string $relationName, protected bool $isString = false)
2818
{
29-
$this->isString = $isString;
3019
parent::__construct($query, $child, $foreignKey, $ownerKey, $relationName);
3120
}
3221

3322
/**
3423
* Set the base constraints on the relation query.
35-
*
36-
* @return void
3724
*/
38-
public function addConstraints()
25+
public function addConstraints(): void
3926
{
40-
if (static::$constraints) {
41-
$query = $this->getBaseQuery();
27+
if (! static::$constraints) {
28+
return;
29+
}
30+
$query = $this->getBaseQuery();
4231

43-
$query->when($this->isString, function ($q) {
44-
$q->whereJsonContains($this->ownerKey, (string) $this->getParentKey());
45-
}, function ($q) {
46-
$q->whereJsonContains($this->ownerKey, $this->getParentKey());
47-
});
32+
$query->when($this->isString, function ($q) {
33+
$q->whereJsonContains($this->ownerKey, (string) $this->getParentKey());
34+
}, function ($q) {
35+
$q->whereJsonContains($this->ownerKey, $this->getParentKey());
36+
});
4837

49-
$query->whereNotNull($this->ownerKey);
50-
}
38+
$query->whereNotNull($this->ownerKey);
5139
}
5240

5341
/**
5442
* Set the constraints for an eager load of the relation.
55-
*
56-
* @return void
5743
*/
58-
public function addEagerConstraints(array $models)
44+
public function addEagerConstraints(array $models): void
5945
{
6046
$ids = $this->getEagerModelKeys($models);
6147
$this->query->where(function ($q) use ($ids) {
@@ -72,18 +58,16 @@ public function addEagerConstraints(array $models)
7258
/**
7359
* Match the eagerly loaded results to their many parents.
7460
*
75-
* @param \Illuminate\Database\Eloquent\Collection $results
7661
* @param string $relation
77-
* @return array
7862
*/
79-
public function match(array $models, $results, $relation)
63+
public function match(array $models, Collection $results, $relation): array
8064
{
8165
$owner = $this->getOwnerKeyName();
8266
foreach ($models as $model) {
8367
$id = $model->getAttribute($this->foreignKey);
8468
$collection = collect();
8569
foreach ($results as $data) {
86-
if (in_array($id, $data->{$owner})) {
70+
if (in_array($id, $data->{$owner}, true)) {
8771
$collection->push($data);
8872
}
8973
}
@@ -95,10 +79,8 @@ public function match(array $models, $results, $relation)
9579

9680
/**
9781
* Get the results of the relationship.
98-
*
99-
* @return mixed
10082
*/
101-
public function getResults()
83+
public function getResults(): mixed
10284
{
10385
return $this->query->get();
10486
}

src/Relations/BelongsToManyKeys.php

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Mrpunyapal\LaravelExtendedRelationships\Relations;
46

57
use Illuminate\Database\Eloquent\Builder;
@@ -12,63 +14,51 @@ class BelongsToManyKeys extends Relation
1214
/**
1315
* The local keys of the parent model.
1416
*
15-
* @var string[]
17+
* @var array<string>
1618
*/
17-
protected $localKeys;
19+
protected array $localKeys;
1820

1921
/**
2022
* The local keys of the parent model.
2123
*
22-
* @var string[]
23-
*/
24-
protected $relations;
25-
26-
/**
27-
* The foreign key of the related model.
28-
*
29-
* @var string
24+
* @var array<string>
3025
*/
31-
protected $foreignKey;
26+
protected array $relations;
3227

3328
/**
3429
* Create a new has one or many relationship instance.
35-
*
36-
* @param array $localKeys
37-
* @return void
3830
*/
39-
public function __construct(Builder $query, Model $parent, string $foreignKey, array $relations)
31+
public function __construct(Builder $query, Model $parent, protected string $foreignKey, array $relations)
4032
{
4133
$this->localKeys = array_keys($relations);
42-
$this->foreignKey = $foreignKey;
4334
$this->relations = $relations;
4435
parent::__construct($query, $parent);
4536
}
4637

4738
/**
4839
* Set the base constraints on the relation query.
4940
* Note: Used to load relations of one model.
50-
*
51-
* @return void
5241
*/
53-
public function addConstraints()
42+
public function addConstraints(): void
5443
{
55-
if (static::$constraints) {
56-
$this->query->where(function ($query) {
57-
foreach ($this->localKeys as $localKey) {
58-
$query->orWhere(function ($query) use ($localKey) {
59-
$query->where($this->foreignKey, '=', $this->getParentKey($localKey))
60-
->whereNotNull($this->foreignKey);
61-
});
62-
}
63-
});
44+
if (! static::$constraints) {
45+
return;
6446
}
47+
$this->query->where(function ($query) {
48+
foreach ($this->localKeys as $localKey) {
49+
$query->orWhere(function ($query) use ($localKey) {
50+
$query->where($this->foreignKey, '=', $this->getParentKey($localKey))
51+
->whereNotNull($this->foreignKey);
52+
});
53+
}
54+
});
6555
}
6656

6757
/**
6858
* Set the constraints for an eager load of the relation.
6959
* Note: Used to load relations of multiple models at once.
7060
*/
71-
public function addEagerConstraints(array $models)
61+
public function addEagerConstraints(array $models): void
7262
{
7363
$localKeys = $this->localKeys;
7464
$foreignKey = $this->foreignKey;
@@ -83,9 +73,8 @@ public function addEagerConstraints(array $models)
8373
* Initialize the relation on a set of models.
8474
*
8575
* @param string $relation
86-
* @return array
8776
*/
88-
public function initRelation(array $models, $relation)
77+
public function initRelation(array $models, $relation): array|Collection
8978
{
9079
foreach ($models as $model) {
9180
$model->setRelation($relation, $this->related->newCollection());
@@ -94,7 +83,10 @@ public function initRelation(array $models, $relation)
9483
return $models;
9584
}
9685

97-
public function match(array $models, Collection $results, $relation)
86+
/**
87+
* Match the related models with the given models based on the local keys.
88+
*/
89+
public function match(array $models, Collection $results, $relation): array
9890
{
9991
$dictionary = $this->buildDictionary($results);
10092

@@ -112,7 +104,10 @@ public function match(array $models, Collection $results, $relation)
112104
return $models;
113105
}
114106

115-
public function buildDictionary(Collection $models)
107+
/**
108+
* Build a dictionary using the given models.
109+
*/
110+
public function buildDictionary(Collection $models): array|Collection
116111
{
117112
$dictionary = [];
118113
foreach ($models as $model) {
@@ -122,17 +117,18 @@ public function buildDictionary(Collection $models)
122117
return $dictionary;
123118
}
124119

125-
public function getParentKey($localKey)
120+
/**
121+
* Get the parent key value for the given local key.
122+
*/
123+
public function getParentKey(string $localKey): mixed
126124
{
127125
return $this->parent->getAttribute($localKey);
128126
}
129127

130128
/**
131129
* Get the results of the relationship.
132-
*
133-
* @return mixed
134130
*/
135-
public function getResults()
131+
public function getResults(): mixed
136132
{
137133
if (! static::$constraints) {
138134
return $this->query->get();

0 commit comments

Comments
 (0)