Passed
Push — master ( 5dcb31...b3f3ee )
by Michael
02:32
created

HelperMethodTrait   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 128
rs 10
c 0
b 0
f 0
wmc 16

7 Methods

Rating   Name   Duplication   Size   Complexity  
A parseRelationNames() 0 16 3
A getTableWithAlias() 0 7 3
A columnWithTableName() 0 3 2
A removeGlobalScope() 0 8 2
A toSqlWithBindings() 0 17 3
A getTableFromColumn() 0 5 2
A getRelationTables() 0 9 1
1
<?php
2
3
namespace Blasttech\EloquentRelatedPlus;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Relations\Relation;
7
8
/**
9
 * Trait HelperMethodTrait
10
 *
11
 * @property array order_fields
12
 * @property array order_defaults
13
 * @property array order_relations
14
 * @property array order_with
15
 * @property array search_fields
16
 * @property string connection
17
 */
18
trait HelperMethodTrait
19
{
20
    /**
21
     * Get relation table name and alias
22
     *
23
     * @param Relation $relation
24
     * @return \stdClass
25
     */
26
    protected function getRelationTables($relation)
27
    {
28
        $table = new \stdClass();
29
        $table->name = $relation->getRelated()->getTable();
30
        // if using a 'table' AS 'tableAlias' in a from statement, otherwise alias will be the table name
31
        $from = explode(' ', $relation->getQuery()->getQuery()->from);
32
        $table->alias = array_pop($from);
33
34
        return $table;
35
    }
36
37
    /**
38
     * Get the relations from a relation name
39
     * $relationName can be a single relation
40
     * Usage for User model:
41
     * parseRelationNames('customer') returns [$user->customer()]
42
     * parseRelationNames('customer.contact') returns [$user->customer(), $user->customer->contact()]
43
     *
44
     * @param string $relationName
45
     * @return Relation[]
46
     */
47
    protected function parseRelationNames($relationName)
48
    {
49
        $relationNames = explode('.', $relationName);
50
        $parentRelationName = null;
51
        $relations = [];
52
53
        foreach ($relationNames as $relationName) {
0 ignored issues
show
introduced by
$relationName is overwriting one of the parameters of this function.
Loading history...
54
            if (is_null($parentRelationName)) {
55
                $relations[] = $this->$relationName();
56
                $parentRelationName = $this->$relationName()->getRelated();
57
            } else {
58
                $relations[] = $parentRelationName->$relationName();
59
            }
60
        }
61
62
        return $relations;
63
    }
64
65
    /**
66
     * Return the sql for a query with the bindings replaced with the binding values
67
     *
68
     * @param Builder $builder
69
     * @return string
70
     */
71
    protected function toSqlWithBindings($builder)
72
    {
73
        $replacements = array_map('addslashes', $builder->getBindings());
0 ignored issues
show
Bug introduced by
It seems like $builder->getBindings() can also be of type Illuminate\Database\Eloquent\Builder; however, parameter $arr1 of array_map() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

73
        $replacements = array_map('addslashes', /** @scrutinizer ignore-type */ $builder->getBindings());
Loading history...
74
        $sql = $builder->toSql();
75
76
        foreach ($replacements as &$replacement) {
77
            if (!is_numeric($replacement)) {
78
                $replacement = '"' . $replacement . '"';
79
            }
80
        }
81
82
        return preg_replace_callback(
83
            '/(\?)(?=(?:[^\'"]|["\'][^\'"]*["\'])*$)/',
84
            function () use (&$replacements) {
85
                return array_shift($replacements);
86
            },
87
            $sql
88
        );
89
    }
90
91
    /**
92
     * Add table name to column name if table name not already included in column name
93
     *
94
     * @param string $table
95
     * @param string $column
96
     * @return string
97
     */
98
    protected function columnWithTableName($table, $column)
99
    {
100
        return (preg_match('/(' . $table . '\.|`' . $table . '`)/i', $column) > 0 ? '' : $table . '.') . $column;
101
    }
102
103
    /**
104
     * Get table name from column name
105
     *
106
     * @param string $column
107
     * @return string
108
     */
109
    protected function getTableFromColumn($column)
110
    {
111
        $periodPos = strpos($column, '.');
112
113
        return ($periodPos !== false ? substr($column, 0, $periodPos) : $column);
0 ignored issues
show
introduced by
The condition $periodPos !== false can never be false.
Loading history...
114
    }
115
116
    /**
117
     * Get table name with alias if different to table name
118
     *
119
     * @param \stdClass $table
120
     * @return string
121
     */
122
    protected function getTableWithAlias($table)
123
    {
124
        if ($table->alias !== '' && $table->name !== $table->alias) {
125
            return $table->name . ' AS ' . $table->alias;
126
        }
127
128
        return $table->name;
129
    }
130
131
    /**
132
     * Remove a global scope if it exists
133
     *
134
     * @param Builder $query
135
     * @param string $scopeName
136
     * @return Builder
137
     */
138
    protected function removeGlobalScope($query, $scopeName)
139
    {
140
        $globalScopes = $this->getGlobalScopes();
0 ignored issues
show
Bug introduced by
It seems like getGlobalScopes() 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

140
        /** @scrutinizer ignore-call */ 
141
        $globalScopes = $this->getGlobalScopes();
Loading history...
141
        if (isset($globalScopes[$scopeName])) {
142
            $query->withoutGlobalScope($scopeName);
143
        }
144
145
        return $query;
146
    }
147
}
148