Completed
Push — master ( f6ecff...656ae7 )
by Oscar
02:19
created

SelectOne::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace SimpleCrud\Queries\Mysql;
4
5
use SimpleCrud\Queries\BaseQuery;
6
use SimpleCrud\Queries\WhereExtendedTrait;
7
use SimpleCrud\Queries\LimitTrait;
8
use SimpleCrud\RowCollection;
9
use SimpleCrud\Entity;
10
use SimpleCrud\SimpleCrudException;
11
use PDOStatement;
12
use PDO;
13
14
/**
15
 * Manages a database select query with just one result.
16
 */
17
class SelectOne extends SelectAll
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22
    public function __construct(Entity $entity)
23
    {
24
        parent::__construct($entity);
25
        $this->limit = 1;
26
    }
27
28
    /**
29
     * Prevent to change the limit clause
30
     */
31
    public function limit($limit)
32
    {
33
        throw new SimpleCrudException('SelectOne cannot modify the LIMIT clause');
34
    }
35
36
    /**
37
     * Run the query and return the first value.
38
     *
39
     * @return Row|null
40
     */
41
    public function get()
42
    {
43
        $row = $this->run()->fetch();
44
45
        if ($row !== false) {
46
            return $this->entity->create($this->entity->prepareDataFromDatabase($row));
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->entity->cr...ataFromDatabase($row)); (SimpleCrud\Row) is incompatible with the return type of the parent method SimpleCrud\Queries\Mysql\SelectAll::get of type SimpleCrud\RowCollection.

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...
47
        }
48
    }
49
}
50