Issues (2)

src/HasUnusualRelationships.php (2 issues)

Labels
1
<?php
2
3
namespace Shomisha\UnusualRelationships;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Database\Eloquent\Model;
7
use Shomisha\UnusualRelationships\Relationships\BelongsToManyThrough;
8
9
trait HasUnusualRelationships
10
{
11
    protected function belongsToManyThrough(string $related, string $through, string $foreign = null, string $pivot = null, string $pivotRelatedForeign = null, $pivotThroughForeign = null)
12
    {
13
        /** @var \Illuminate\Database\Eloquent\Model $relatedInstance */
14
        $relatedInstance = new $related;
15
16
        /** @var \Illuminate\Database\Eloquent\Model $throughInstance */
17
        $throughInstance = new $through;
18
19
        return new BelongsToManyThrough(
20
            $relatedInstance->newQuery(),
21
            $this,
0 ignored issues
show
$this of type Shomisha\UnusualRelation...HasUnusualRelationships is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $parent of Shomisha\UnusualRelation...yThrough::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

21
            /** @scrutinizer ignore-type */ $this,
Loading history...
22
            $relatedInstance,
23
            $throughInstance,
24
            $foreign ?? $this->getForeignKey(),
0 ignored issues
show
It seems like getForeignKey() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

24
            $foreign ?? $this->/** @scrutinizer ignore-call */ getForeignKey(),
Loading history...
25
            $pivot ?? $this->guessPivot($relatedInstance, $throughInstance),
26
            $pivotRelatedForeign ?? $relatedInstance->getForeignKey(),
27
            $pivotThroughForeign ?? $throughInstance->getForeignKey()
28
        );
29
    }
30
31
    /**
32
     * Guess the pivot table name for the provided Models.
33
     *
34
     * @param Model[] ...$models
35
     * @return string
36
     */
37
    protected function guessPivot(...$models)
38
    {
39
        return collect($models)->map(function (Model $model) {
40
            return Str::singular($model->getTable());
41
        })->sort()->join('_');
42
    }
43
}
44