GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Eloquence   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 0
Metric Value
dl 0
loc 157
rs 10
c 0
b 0
f 0
wmc 18
lcom 2
cbo 7

11 Methods

Rating   Name   Duplication   Size   Complexity  
A bootEloquence() 0 4 1
A isWhereNull() 0 4 3
A isWhereNullByArgs() 0 5 3
A extractColumnAlias() 0 10 2
A parseMappedColumn() 0 10 1
A hasColumn() 0 6 1
A getSearchableColumns() 0 4 2
A getColumnListing() 0 6 1
A loadColumnListing() 0 10 2
A newEloquentBuilder() 0 4 1
A newBaseQueryBuilder() 0 8 1
1
<?php
2
3
namespace Sofa\Eloquence;
4
5
use Illuminate\Database\Connection;
6
use Sofa\Hookable\Hookable;
7
use Sofa\Hookable\Contracts\ArgumentBag;
8
use Sofa\Eloquence\Query\Builder as QueryBuilder;
9
use Sofa\Eloquence\AttributeCleaner\Observer as AttributeCleaner;
10
11
/**
12
 * This trait is an entry point for all the hooks that we want to apply
13
 * on the Eloquent Model and Builder in order to let the magic happen.
14
 *
15
 * It also provides hasColumn and getColumnListing helper methods
16
 * so you can easily list or check columns in the model's table.
17
 *
18
 * @version 5.5
19
 *
20
 * @method Connection getConnection()
21
 * @method string getTable()
22
 */
23
trait Eloquence
24
{
25
    use Hookable;
26
27
    /**
28
     * Model's table column listing.
29
     *
30
     * @var array
31
     */
32
    protected static $columnListing = [];
33
34
    /**
35
     * Boot the trait.
36
     *
37
     * @codeCoverageIgnore
38
     *
39
     * @return void
40
     */
41
    public static function bootEloquence()
42
    {
43
        static::observe(new AttributeCleaner);
44
    }
45
46
    /**
47
     * Determine whether where should be treated as whereNull.
48
     *
49
     * @param  string $method
50
     * @param  ArgumentBag $args
51
     * @return bool
52
     */
53
    protected function isWhereNull($method, ArgumentBag $args)
54
    {
55
        return $method === 'whereNull' || $method === 'where' && $this->isWhereNullByArgs($args);
56
    }
57
58
    /**
59
     * Determine whether where is a whereNull by the arguments passed to where method.
60
     *
61
     * @param  ArgumentBag $args
62
     * @return bool
63
     */
64
    protected function isWhereNullByArgs(ArgumentBag $args)
65
    {
66
        return is_null($args->get('operator'))
67
            || is_null($args->get('value')) && !in_array($args->get('operator'), ['<>', '!=']);
68
    }
69
70
    /**
71
     * Extract real name and alias from the sql select clause.
72
     *
73
     * @param  string $column
74
     * @return array
75
     */
76
    protected function extractColumnAlias($column)
77
    {
78
        $alias = $column;
79
80
        if (strpos($column, ' as ') !== false) {
81
            list($column, $alias) = explode(' as ', $column);
82
        }
83
84
        return [$column, $alias];
85
    }
86
87
    /**
88
     * Get the target relation and column from the mapping.
89
     *
90
     * @param  string $mapping
91
     * @return array
92
     */
93
    public function parseMappedColumn($mapping)
94
    {
95
        $segments = explode('.', $mapping);
96
97
        $column = array_pop($segments);
98
99
        $target = implode('.', $segments);
100
101
        return [$target, $column];
102
    }
103
104
    /**
105
     * Determine whether the key is meta attribute or actual table field.
106
     *
107
     * @param  string  $key
108
     * @return bool
109
     */
110
    public static function hasColumn($key)
111
    {
112
        static::loadColumnListing();
113
114
        return in_array((string) $key, static::$columnListing);
115
    }
116
117
    /**
118
     * Get searchable columns defined on the model.
119
     *
120
     * @return array
121
     */
122
    public function getSearchableColumns()
123
    {
124
        return (property_exists($this, 'searchableColumns')) ? $this->searchableColumns : [];
0 ignored issues
show
Bug introduced by
The property searchableColumns does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
125
    }
126
127
    /**
128
     * Get model table columns.
129
     *
130
     * @return array
131
     */
132
    public static function getColumnListing()
133
    {
134
        static::loadColumnListing();
135
136
        return static::$columnListing;
137
    }
138
139
    /**
140
     * Fetch model table columns.
141
     *
142
     * @return void
143
     */
144
    protected static function loadColumnListing()
145
    {
146
        if (empty(static::$columnListing)) {
147
            $instance = new static;
148
149
            static::$columnListing = $instance->getConnection()
150
                                        ->getSchemaBuilder()
151
                                        ->getColumnListing($instance->getTable());
152
        }
153
    }
154
155
    /**
156
     * Create new Eloquence query builder for the instance.
157
     *
158
     * @param QueryBuilder $query
159
     * @return Builder
160
     */
161
    public function newEloquentBuilder($query)
162
    {
163
        return new Builder($query);
164
    }
165
166
    /**
167
     * Get a new query builder instance for the connection.
168
     *
169
     * @return QueryBuilder
170
     */
171
    protected function newBaseQueryBuilder()
172
    {
173
        $conn = $this->getConnection();
174
175
        $grammar = $conn->getQueryGrammar();
176
177
        return new QueryBuilder($conn, $grammar, $conn->getPostProcessor());
178
    }
179
}
180