HasExistenceQueries   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 11
eloc 34
dl 0
loc 89
rs 10
c 2
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setRelationExistenceQueryAlias() 0 21 4
A getRelationExistenceQueryForSelfRelation() 0 14 1
A getRelationExistenceQuery() 0 25 6
1
<?php
2
3
namespace Staudenmeir\EloquentHasManyDeep\Eloquent\Relations\Traits;
4
5
use Closure;
6
use Exception;
7
use Illuminate\Database\Eloquent\Builder;
8
use Staudenmeir\EloquentHasManyDeep\HasTableAlias;
9
10
trait HasExistenceQueries
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
        $this->setRelationExistenceQueryAlias($parentQuery);
23
24
        if ($this->firstKey instanceof Closure || $this->localKey instanceof Closure) {
25
            $this->performJoin($query);
0 ignored issues
show
Bug introduced by
It seems like performJoin() 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

25
            $this->/** @scrutinizer ignore-call */ 
26
                   performJoin($query);
Loading history...
26
27
            $closureKey = $this->firstKey instanceof Closure ? $this->firstKey : $this->localKey;
28
29
            $closureKey($query, $parentQuery);
30
31
            return $query->select($columns);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query->select($columns) also could return the type Illuminate\Database\Query\Builder which is incompatible with the documented return type Illuminate\Database\Eloquent\Builder.
Loading history...
32
        }
33
34
        $query = parent::getRelationExistenceQuery($query, $parentQuery, $columns);
35
36
        if (is_array($this->foreignKeys[0])) {
37
            $column = $this->throughParent->qualifyColumn($this->foreignKeys[0][0]);
38
39
            $query->where($column, '=', $this->farParent->getMorphClass());
40
        } elseif ($this->hasLeadingCompositeKey()) {
0 ignored issues
show
Bug introduced by
It seems like hasLeadingCompositeKey() 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

40
        } elseif ($this->/** @scrutinizer ignore-call */ hasLeadingCompositeKey()) {
Loading history...
41
            $this->getRelationExistenceQueryWithCompositeKey($query);
0 ignored issues
show
Bug introduced by
The method getRelationExistenceQueryWithCompositeKey() does not exist on Staudenmeir\EloquentHasM...its\HasExistenceQueries. Did you maybe mean getRelationExistenceQuery()? ( Ignorable by Annotation )

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

41
            $this->/** @scrutinizer ignore-call */ 
42
                   getRelationExistenceQueryWithCompositeKey($query);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
42
        }
43
44
        return $query;
45
    }
46
47
    /**
48
     * Add the constraints for a relationship query on the same table.
49
     *
50
     * @param \Illuminate\Database\Eloquent\Builder $query
51
     * @param \Illuminate\Database\Eloquent\Builder $parentQuery
52
     * @param array|mixed $columns
53
     * @return \Illuminate\Database\Eloquent\Builder
54
     */
55
    public function getRelationExistenceQueryForSelfRelation(Builder $query, Builder $parentQuery, $columns = ['*'])
56
    {
57
        $hash = $this->getRelationCountHash();
0 ignored issues
show
Bug introduced by
It seems like getRelationCountHash() 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

57
        /** @scrutinizer ignore-call */ 
58
        $hash = $this->getRelationCountHash();
Loading history...
58
59
        $query->from($query->getModel()->getTable().' as '.$hash);
60
61
        $this->performJoin($query);
62
63
        $query->getModel()->setTable($hash);
64
65
        return $query->select($columns)->whereColumn(
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query->select($c...ualifiedFirstKeyName()) also could return the type Illuminate\Database\Query\Builder which is incompatible with the documented return type Illuminate\Database\Eloquent\Builder.
Loading history...
66
            $parentQuery->getQuery()->from.'.'.$this->localKey,
0 ignored issues
show
Bug introduced by
Are you sure $parentQuery->getQuery()->from of type Illuminate\Database\Query\Expression|string can be used in concatenation? ( Ignorable by Annotation )

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

66
            /** @scrutinizer ignore-type */ $parentQuery->getQuery()->from.'.'.$this->localKey,
Loading history...
67
            '=',
68
            $this->getQualifiedFirstKeyName()
0 ignored issues
show
Bug introduced by
It seems like getQualifiedFirstKeyName() 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

68
            $this->/** @scrutinizer ignore-call */ 
69
                   getQualifiedFirstKeyName()
Loading history...
69
        );
70
    }
71
72
    /**
73
     * Set the table alias for a relation existence query if necessary.
74
     *
75
     * @param \Illuminate\Database\Eloquent\Builder $parentQuery
76
     * @return void
77
     */
78
    protected function setRelationExistenceQueryAlias(Builder $parentQuery): void
79
    {
80
        foreach ($this->throughParents as $throughParent) {
81
            if ($throughParent->getTable() === $parentQuery->getQuery()->from) {
82
                if (!in_array(HasTableAlias::class, class_uses_recursive($throughParent))) {
83
                    $traitClass = HasTableAlias::class;
84
                    $parentClass = get_class($throughParent);
85
86
                    throw new Exception(
87
                        <<<EOT
88
This query requires an additional trait. Please add the $traitClass trait to $parentClass.
89
See https://github.com/staudenmeir/eloquent-has-many-deep/issues/137 for details.
90
EOT
91
                    );
92
                }
93
94
                $table = $throughParent->getTable() . ' as ' . $this->getRelationCountHash();
95
96
                $throughParent->setTable($table);
97
98
                break;
99
            }
100
        }
101
    }
102
}
103