Completed
Branch pivot-model (ba2c99)
by Tyler
06:15
created

Builder::find()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 0
cts 15
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 3
nop 2
crap 12
1
<?php
2
3
namespace Tylercd100\Database\Eloquent;
4
5
use Illuminate\Database\Eloquent\Builder as IlluminateBuilder;
6
use Illuminate\Support\Facades\Cache;
7
8
class Builder extends IlluminateBuilder
9
{
10
    /**
11
     * Find a model by its primary key.
12
     *
13
     * @param  mixed  $id
14
     * @param  array  $columns
15
     * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection|null
16
     */
17
    public function find($id, $columns = ['*'])
18
    {
19
        if (! $this->isBasicQuery()) {
20
            return parent::find($id, $columns);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return parent::find($id, $columns); (Illuminate\Database\Eloq...e\Eloquent\Builder|null) is incompatible with the return type documented by Tylercd100\Database\Eloquent\Builder::find of type Illuminate\Database\Eloq...loquent\Collection|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...
21
        }
22
23
        if (is_array($id)) {
24
            return $this->findMany($id, $columns);
25
        }
26
27
        return Cache::tags($this->model->getTable())->remember(
28
            $id,
29
            $this->model->cacheExpiry,
30
            function () use ($id, $columns) {
31
                return parent::find($id, $columns);
32
            }
33
        );
34
    }
35
36
    /**
37
     * Find a model by its primary key.
38
     *
39
     * @param  array  $ids
40
     * @param  array  $columns
41
     * @return \Illuminate\Database\Eloquent\Collection
42
     */
43
    public function findMany($ids, $columns = ['*'])
44
    {
45
        if (! $this->isBasicQuery()) {
46
            return parent::findMany($ids, $columns);
47
        }
48
49
        if (empty($ids)) {
50
            return $this->model->newCollection();
51
        }
52
53
        return $this->model->newCollection(
54
            Cache::tags($this->model->getTable())->rememberMany(
55
                $ids,
56
                $this->model->cacheExpiry,
57
                function ($ids) use ($columns) {
58
                    return parent::findMany($ids, $columns)->all();
59
                }
60
            )
61
        );
62
    }
63
64
    /**
65
     * Check if the current query is a basic query or has had some
66
     * conditions (where, joins, etc) attached to it.
67
     *
68
     * @return boolean
69
     */
70
    protected function isBasicQuery()
71
    {
72
        $freshQuery = $this->query
73
            ->newQuery()
74
            ->from($this->model->getTable());
75
76
        return $this->query == $freshQuery && empty($this->eagerLoad);
77
    }
78
}
79