EntryCriteria::sorted()   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 1
1
<?php namespace Anomaly\Streams\Platform\Entry;
2
3
use Anomaly\Streams\Platform\Model\EloquentCriteria;
4
use Anomaly\Streams\Platform\Stream\Contract\StreamInterface;
5
use Illuminate\Database\Eloquent\Builder;
6
7
/**
8
 * Class EntryCriteria
9
 *
10
 * @link   http://pyrocms.com/
11
 * @author PyroCMS, Inc. <[email protected]>
12
 * @author Ryan Thompson <[email protected]>
13
 */
14
class EntryCriteria extends EloquentCriteria
15
{
16
17
    /**
18
     * The stream instance.
19
     *
20
     * @var StreamInterface
21
     */
22
    protected $stream;
23
24
    /**
25
     * Create a new EntryCriteria instance.
26
     *
27
     * @param Builder         $query
28
     * @param StreamInterface $stream
29
     * @param string          $method
30
     */
31
    public function __construct(Builder $query, StreamInterface $stream, $method)
32
    {
33
        $this->stream = $stream;
34
35
        parent::__construct($query, $method);
36
    }
37
38
    /**
39
     * Return sorted entries.
40
     *
41
     * @param  string $direction
42
     * @return $this
43
     */
44
    public function sorted($direction = 'ASC')
45
    {
46
        $this->query->orderBy('sort_order', $direction);
0 ignored issues
show
Bug introduced by
The method orderBy 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...
47
48
        return $this;
49
    }
50
51
    /**
52
     * Route through __call.
53
     *
54
     * @param $name
55
     * @return Builder|null
56
     */
57
    public function __get($name)
58
    {
59
        if ($assignment = $this->stream->getAssignment(snake_case($name))) {
60
            $this->query->where($assignment->getColumnName(), null);
61
62
            return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Anomaly\Streams\Platform\Entry\EntryCriteria) is incompatible with the return type of the parent method Anomaly\Streams\Platform...EloquentCriteria::__get 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...
63
        }
64
65
        return parent::__get($name);
66
    }
67
68
    /**
69
     * Call the method on the query.
70
     *
71
     * @param $name
72
     * @param $arguments
73
     * @return Builder|null
74
     */
75
    public function __call($name, $arguments)
76
    {
77
        if ($assignment = $this->stream->getAssignment(snake_case($name))) {
78
            $this->query->where($assignment->getColumnName(), $arguments ? array_shift($arguments) : null);
79
80
            return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Anomaly\Streams\Platform\Entry\EntryCriteria) is incompatible with the return type of the parent method 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...
81
        }
82
83
        return parent::__call($name, $arguments);
84
    }
85
}
86