Passed
Push — master ( e90b8a...d5b6e7 )
by Jonas
02:19 queued 13s
created

HasExistenceQueries   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 34
c 2
b 0
f 0
dl 0
loc 90
ccs 38
cts 38
cp 1
rs 10
wmc 11

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 36
    public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*'])
21
    {
22 36
        $this->setRelationExistenceQueryAlias($parentQuery);
23
24 34
        if ($this->firstKey instanceof Closure || $this->localKey instanceof Closure) {
25 12
            $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 12
            $closureKey = $this->firstKey instanceof Closure ? $this->firstKey : $this->localKey;
28
29 12
            $closureKey($query, $parentQuery);
30
31 12
            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 22
        $query = parent::getRelationExistenceQuery($query, $parentQuery, $columns);
35
36 22
        if (is_array($this->foreignKeys[0])) {
37 4
            $column = $this->throughParent->qualifyColumn($this->foreignKeys[0][0]);
38
39 4
            $query->where($column, '=', $this->farParent->getMorphClass());
40 18
        } 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 2
            $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 22
        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 4
    public function getRelationExistenceQueryForSelfRelation(Builder $query, Builder $parentQuery, $columns = ['*'])
56
    {
57 4
        $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 4
        $query->from($query->getModel()->getTable().' as '.$hash);
60
61 4
        $this->performJoin($query);
62
63 4
        $query->getModel()->setTable($hash);
64
65 4
        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 4
            $parentQuery->getQuery()->from.'.'.$this->localKey,
67 4
            '=',
68 4
            $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 4
        );
70
    }
71
72
    /**
73
     * Set the table alias for a relation existence query if necessary.
74
     *
75
     * @param \Illuminate\Database\Eloquent\Builder $query
76
     * @param \Illuminate\Database\Eloquent\Builder $parentQuery
77
     * @return void
78
     */
79 36
    protected function setRelationExistenceQueryAlias(Builder $parentQuery): void
80
    {
81 36
        foreach ($this->throughParents as $throughParent) {
82 36
            if ($throughParent->getTable() === $parentQuery->getQuery()->from) {
83 9
                if (!in_array(HasTableAlias::class, class_uses_recursive($throughParent))) {
84 2
                    $traitClass = HasTableAlias::class;
85 2
                    $parentClass = get_class($throughParent);
86
87 2
                    throw new Exception(
88 2
                        <<<EOT
89 2
This query requires an additional trait. Please add the $traitClass trait to $parentClass.
90
See https://github.com/staudenmeir/eloquent-has-many-deep/issues/137 for details.
91 2
EOT
92 2
                    );
93
                }
94
95 7
                $table = $throughParent->getTable() . ' as ' . $this->getRelationCountHash();
96
97 7
                $throughParent->setTable($table);
98
99 7
                break;
100
            }
101
        }
102
    }
103
}
104