BelongsTo::getRelationExistenceQuery()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
nc 2
nop 3
dl 0
loc 12
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Staudenmeir\EloquentJsonRelations\Relations\Postgres;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Relations\BelongsTo as Base;
7
8
class BelongsTo extends Base
9
{
10
    use IsPostgresRelation;
11
12
    /**
13
     * Add the constraints for a relationship query.
14
     *
15
     * @param \Illuminate\Database\Eloquent\Builder $query
16
     * @param \Illuminate\Database\Eloquent\Builder $parentQuery
17
     * @param array|mixed $columns
18
     * @return \Illuminate\Database\Eloquent\Builder
19
     */
20
    public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*'])
21
    {
22
        if ($parentQuery->getQuery()->from == $query->getQuery()->from) {
23
            return $this->getRelationExistenceQueryForSelfRelation($query, $parentQuery, $columns);
24
        }
25
26
        $first = $this->jsonColumn($query, $this->related, $this->getQualifiedForeignKeyName(), $this->ownerKey);
27
28
        return $query->select($columns)->whereColumn(
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query->select($c...olumn($this->ownerKey)) also could return the type Illuminate\Database\Query\Builder which is incompatible with the documented return type Illuminate\Database\Eloquent\Builder.
Loading history...
29
            $first,
30
            '=',
31
            $query->qualifyColumn($this->ownerKey)
32
        );
33
    }
34
35
    /**
36
     * Add the constraints for a relationship query on the same table.
37
     *
38
     * @param \Illuminate\Database\Eloquent\Builder $query
39
     * @param \Illuminate\Database\Eloquent\Builder $parentQuery
40
     * @param array|mixed $columns
41
     * @return \Illuminate\Database\Eloquent\Builder
42
     */
43
    public function getRelationExistenceQueryForSelfRelation(Builder $query, Builder $parentQuery, $columns = ['*'])
44
    {
45
        $query->select($columns)->from(
46
            $query->getModel()->getTable().' as '.$hash = $this->getRelationCountHash()
0 ignored issues
show
Bug introduced by
$query->getModel()->getT...>getRelationCountHash() of type string is incompatible with the type Closure|Illuminate\Datab...\Database\Query\Builder expected by parameter $table of Illuminate\Database\Query\Builder::from(). ( Ignorable by Annotation )

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

46
            /** @scrutinizer ignore-type */ $query->getModel()->getTable().' as '.$hash = $this->getRelationCountHash()
Loading history...
47
        );
48
49
        $query->getModel()->setTable($hash);
50
51
        $first = $this->jsonColumn($query, $this->related, $this->getQualifiedForeignKeyName(), $this->ownerKey);
52
53
        return $query->whereColumn(
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query->whereColu... '.' . $this->ownerKey) also could return the type Illuminate\Database\Query\Builder which is incompatible with the documented return type Illuminate\Database\Eloquent\Builder.
Loading history...
54
            $first,
55
            $hash.'.'.$this->ownerKey
56
        );
57
    }
58
}
59