Passed
Push — master ( 92ce5e...e90b8a )
by Jonas
02:07 queued 13s
created

HasExistenceQueries   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
c 1
b 0
f 0
dl 0
loc 79
ccs 35
cts 35
cp 1
rs 10
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getRelationExistenceQueryForSelfRelation() 0 14 1
B getRelationExistenceQuery() 0 45 9
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
        foreach ($this->throughParents as $throughParent) {
23 36
            if ($throughParent->getTable() === $parentQuery->getQuery()->from) {
24 9
                if (!in_array(HasTableAlias::class, class_uses_recursive($throughParent))) {
25 2
                    $traitClass = HasTableAlias::class;
26 2
                    $parentClass = get_class($throughParent);
27
28 2
                    throw new Exception(
29 2
                        <<<EOT
30 2
This query requires an additional trait. Please add the $traitClass trait to $parentClass.
31
See https://github.com/staudenmeir/eloquent-has-many-deep/issues/137 for details.
32 2
EOT
33 2
                    );
34
                }
35
36 7
                $table = $throughParent->getTable() . ' as ' . $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

36
                $table = $throughParent->getTable() . ' as ' . $this->/** @scrutinizer ignore-call */ getRelationCountHash();
Loading history...
37
38 7
                $throughParent->setTable($table);
39
40 7
                break;
41
            }
42
        }
43
44 34
        if ($this->firstKey instanceof Closure || $this->localKey instanceof Closure) {
45 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

45
            $this->/** @scrutinizer ignore-call */ 
46
                   performJoin($query);
Loading history...
46
47 12
            $closureKey = $this->firstKey instanceof Closure ? $this->firstKey : $this->localKey;
48
49 12
            $closureKey($query, $parentQuery);
50
51 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...
52
        }
53
54 22
        $query = parent::getRelationExistenceQuery($query, $parentQuery, $columns);
55
56 22
        if (is_array($this->foreignKeys[0])) {
57 4
            $column = $this->throughParent->qualifyColumn($this->foreignKeys[0][0]);
58
59 4
            $query->where($column, '=', $this->farParent->getMorphClass());
60 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

60
        } elseif ($this->/** @scrutinizer ignore-call */ hasLeadingCompositeKey()) {
Loading history...
61 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

61
            $this->/** @scrutinizer ignore-call */ 
62
                   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...
62
        }
63
64 22
        return $query;
65
    }
66
67
    /**
68
     * Add the constraints for a relationship query on the same table.
69
     *
70
     * @param \Illuminate\Database\Eloquent\Builder $query
71
     * @param \Illuminate\Database\Eloquent\Builder $parentQuery
72
     * @param array|mixed $columns
73
     * @return \Illuminate\Database\Eloquent\Builder
74
     */
75 4
    public function getRelationExistenceQueryForSelfRelation(Builder $query, Builder $parentQuery, $columns = ['*'])
76
    {
77 4
        $hash = $this->getRelationCountHash();
78
79 4
        $query->from($query->getModel()->getTable().' as '.$hash);
80
81 4
        $this->performJoin($query);
82
83 4
        $query->getModel()->setTable($hash);
84
85 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...
86 4
            $parentQuery->getQuery()->from.'.'.$this->localKey,
87 4
            '=',
88 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

88
            $this->/** @scrutinizer ignore-call */ 
89
                   getQualifiedFirstKeyName()
Loading history...
89 4
        );
90
    }
91
}
92