EloquentCriteria::findBy()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 3
1
<?php namespace Anomaly\Streams\Platform\Model;
2
3
use Anomaly\Streams\Platform\Entry\Contract\EntryInterface;
4
use Anomaly\Streams\Platform\Entry\EntryPresenter;
5
use Anomaly\Streams\Platform\Search\SearchCriteria;
6
use Anomaly\Streams\Platform\Support\Collection;
7
use Anomaly\Streams\Platform\Support\Decorator;
8
use Anomaly\Streams\Platform\Support\Presenter;
9
use Anomaly\Streams\Platform\Traits\Hookable;
10
use Illuminate\Database\Eloquent\Builder;
11
use Illuminate\Foundation\Bus\DispatchesJobs;
12
13
/**
14
 * Class EloquentCriteria
15
 *
16
 * @link   http://pyrocms.com/
17
 * @author PyroCMS, Inc. <[email protected]>
18
 * @author Ryan Thompson <[email protected]>
19
 */
20
class EloquentCriteria
21
{
22
23
    use Hookable;
24
    use DispatchesJobs;
25
26
    /**
27
     * Additional available methods.
28
     *
29
     * @var array
30
     */
31
    protected $available = [
32
        'whereBetween',
33
        'whereNotBetween',
34
        'whereIn',
35
        'whereNotIn',
36
        'whereNull',
37
        'whereNotNull',
38
        'whereDate',
39
        'whereMonth',
40
        'whereDay',
41
        'whereYear',
42
        'whereColumn',
43
    ];
44
45
    /**
46
     * Safe builder methods.
47
     *
48
     * @var array
49
     */
50
    private $disabled = [
51
        'delete',
52
        'update',
53
    ];
54
55
    /**
56
     * The query builder.
57
     *
58
     * @var Builder|\Illuminate\Database\Query\Builder
59
     */
60
    protected $query;
61
62
    /**
63
     * Set the get method.
64
     *
65
     * @var string
66
     */
67
    protected $method;
68
69
    /**
70
     * Create a new EntryCriteria instance.
71
     *
72
     * @param Builder $query
73
     * @param string  $method
74
     */
75
    public function __construct(Builder $query, $method = 'get')
76
    {
77
        $this->query  = $query;
78
        $this->method = $method;
79
    }
80
81
    /**
82
     * Get the paginated entries.
83
     *
84
     * @param int    $perPage
85
     * @param array  $columns
86
     * @param string $pageName
87
     * @return array|\ArrayAccess|\IteratorAggregate|Presenter
88
     */
89
    public function paginate($perPage = 15, array $columns = ['*'], $pageName = 'page')
90
    {
91
        return (new Decorator())->decorate($this->query->paginate($perPage, $columns, $pageName));
92
    }
93
94
    /**
95
     * Return a new search criteria.
96
     *
97
     * @param  string $term
98
     * @return SearchCriteria
99
     */
100
    public function search($term)
101
    {
102
        return new SearchCriteria(
103
            $this->query->getModel()->search($term),
0 ignored issues
show
Bug introduced by
The method getModel does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Query\Builder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
104
            $this->query->getModel()
105
        );
106
    }
107
108
    /**
109
     * Get the entries.
110
     *
111
     * @param  array $columns
112
     * @return Collection|Presenter|EntryPresenter
113
     */
114
    public function get(array $columns = ['*'])
115
    {
116
        return (new Decorator())->decorate($this->query->{$this->method}($columns));
117
    }
118
119
    /**
120
     * Get the entry count.
121
     *
122
     * @param  array $columns
123
     * @return int
124
     */
125
    public function count(array $columns = ['*'])
126
    {
127
        return (new Decorator())->decorate($this->query->count($columns));
0 ignored issues
show
Documentation introduced by
$columns is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Bug introduced by
The method count does only exist in Illuminate\Database\Query\Builder, but not in Illuminate\Database\Eloquent\Builder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
128
    }
129
130
    /**
131
     * Get the aggregate sum.
132
     *
133
     * @param  $column
134
     * @return int
135
     */
136
    public function sum($column)
137
    {
138
        return (new Decorator())->decorate($this->query->sum($column));
0 ignored issues
show
Bug introduced by
The method sum does only exist in Illuminate\Database\Query\Builder, but not in Illuminate\Database\Eloquent\Builder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
139
    }
140
141
    /**
142
     * Get the aggregate max.
143
     *
144
     * @param  $column
145
     * @return int
146
     */
147
    public function max($column)
148
    {
149
        return (new Decorator())->decorate($this->query->max($column));
0 ignored issues
show
Bug introduced by
The method max does only exist in Illuminate\Database\Query\Builder, but not in Illuminate\Database\Eloquent\Builder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
150
    }
151
152
    /**
153
     * Get the aggregate min.
154
     *
155
     * @param  $column
156
     * @return int
157
     */
158
    public function min($column)
159
    {
160
        return (new Decorator())->decorate($this->query->min($column));
0 ignored issues
show
Bug introduced by
The method min does only exist in Illuminate\Database\Query\Builder, but not in Illuminate\Database\Eloquent\Builder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
161
    }
162
163
    /**
164
     * Get the aggregate avg.
165
     *
166
     * @param  $column
167
     * @return int
168
     */
169
    public function avg($column)
170
    {
171
        return (new Decorator())->decorate($this->query->avg($column));
0 ignored issues
show
Bug introduced by
The method avg does only exist in Illuminate\Database\Query\Builder, but not in Illuminate\Database\Eloquent\Builder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
172
    }
173
174
    /**
175
     * Find an entry.
176
     *
177
     * @param                           $identifier
178
     * @param  array                    $columns
179
     * @return Presenter|EntryPresenter
180
     */
181
    public function find($identifier, array $columns = ['*'])
182
    {
183
        return (new Decorator())->decorate($this->query->find($identifier, $columns));
0 ignored issues
show
Bug Best Practice introduced by
The return type of return (new \Anomaly\Str...identifier, $columns)); (object|integer|double|string|null|boolean|array) is incompatible with the return type documented by Anomaly\Streams\Platform...\EloquentCriteria::find of type Anomaly\Streams\Platform\Support\Presenter|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
184
    }
185
186
    /**
187
     * Find an entry by column value.
188
     *
189
     * @param                           $column
190
     * @param                           $value
191
     * @param  array                    $columns
192
     * @return Presenter|EntryPresenter
193
     */
194
    public function findBy($column, $value, array $columns = ['*'])
195
    {
196
        $this->query->where($column, $value);
197
198
        return (new Decorator())->decorate($this->query->first($columns));
0 ignored issues
show
Bug Best Practice introduced by
The return type of return (new \Anomaly\Str...uery->first($columns)); (object|integer|double|string|null|boolean|array) is incompatible with the return type documented by Anomaly\Streams\Platform...loquentCriteria::findBy of type Anomaly\Streams\Platform\Support\Presenter|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
199
    }
200
201
    /**
202
     * Return the first entry.
203
     *
204
     * @param  array $columns
205
     * @return EloquentModel|EntryInterface
206
     */
207
    public function first(array $columns = ['*'])
208
    {
209
        return (new Decorator())->decorate($this->query->first($columns));
210
    }
211
212
    /**
213
     * Return whether the method is safe or not.
214
     *
215
     * @param $name
216
     * @return bool
217
     */
218
    protected function methodIsSafe($name)
219
    {
220
        return (!in_array($name, $this->disabled));
221
    }
222
223
    /**
224
     * Return whether the method
225
     * exists on the query or not.
226
     *
227
     * @param $name
228
     * @return bool
229
     */
230
    protected function methodExists($name)
231
    {
232
        return method_exists($this->query->getQuery(), $name) || method_exists($this->query, $name);
0 ignored issues
show
Bug introduced by
The method getQuery does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Query\Builder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
233
    }
234
235
    /**
236
     * Route through __call.
237
     *
238
     * @param $name
239
     * @return Builder|null
240
     */
241
    public function __get($name)
242
    {
243
        return $this->__call($name, []);
244
    }
245
246
    /**
247
     * Call the method on the query.
248
     *
249
     * @param $name
250
     * @param $arguments
251
     * @return Builder|null
252
     */
253
    public function __call($name, $arguments)
254
    {
255
        if ($this->hasHook($name)) {
256
            return $this->call($name, $arguments);
257
        }
258
259
        if ($this->methodExists($name) && $this->methodIsSafe($name)) {
260
261
            call_user_func_array([$this->query, $name], $arguments);
262
263
            return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Anomaly\Streams\Platform\Model\EloquentCriteria) is incompatible with the return type documented by Anomaly\Streams\Platform...loquentCriteria::__call of type Illuminate\Database\Eloquent\Builder|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
264
        }
265
266 View Code Duplication
        if (starts_with($name, 'findBy') && $column = snake_case(substr($name, 6))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
267
268
            call_user_func_array([$this->query, 'where'], array_merge([$column], $arguments));
269
270
            return $this->first();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->first(); (object|integer|double|string|null|boolean|array) is incompatible with the return type documented by Anomaly\Streams\Platform...loquentCriteria::__call of type Illuminate\Database\Eloquent\Builder|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
271
        }
272
273 View Code Duplication
        if (starts_with($name, 'where') && $column = snake_case(substr($name, 5))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
274
275
            call_user_func_array([$this->query, 'where'], array_merge([$column], $arguments));
276
277
            return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Anomaly\Streams\Platform\Model\EloquentCriteria) is incompatible with the return type documented by Anomaly\Streams\Platform...loquentCriteria::__call of type Illuminate\Database\Eloquent\Builder|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
278
        }
279
280
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Anomaly\Streams\Platform\Model\EloquentCriteria) is incompatible with the return type documented by Anomaly\Streams\Platform...loquentCriteria::__call of type Illuminate\Database\Eloquent\Builder|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
281
    }
282
283
    /**
284
     * Return the string.
285
     *
286
     * @return string
287
     */
288
    public function __toString()
289
    {
290
        return '';
291
    }
292
}
293