Completed
Pull Request — devel (#146)
by Litera
22:56 queued 29s
created

PersonRepository::getPersonModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Repositories;
4
5
use App\Entities\PersonEntity;
6
use App\Models\PersonModel;
7
8
class PersonRepository
9
{
10
11
	/**
12
	 * @var PersonModel
13
	 */
14
	protected $personModel;
15
16
	/**
17
	 * @param PersonModel $personModel
18
	 */
19
	public function __construct(PersonModel $personModel)
20
	{
21
		$this->setPersonModel($personModel);
22
	}
23
24
	/**
25
	 * @return array
26
	 */
27
	public function all(): array
28
	{
29
		return $this->getPersonModel()->all();
30
	}
31
32
	/**
33
	 * @param  int $id
34
	 * @return PersonEntity
35
	 */
36
	public function find(int $id): PersonEntity
37
	{
38
		return $this->getPersonModel()->find($id);
39
	}
40
41
	/**
42
	 * @param  $person
43
	 * @return \Nette\Database\Table\ActiveRow
44
	 */
45
	public function create($person)
46
	{
47
		unset($person->id);
48
49
		return $this->getPersonModel()
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getPersonM...is->populate($person)); (Nette\Database\Table\IRow|integer|boolean|null) is incompatible with the return type documented by App\Repositories\PersonRepository::create of type Nette\Database\Table\ActiveRow|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...
50
			->save(
51
				$this->populate($person)
52
			);
53
	}
54
55
	/**
56
	 * @param  $id
57
	 * @param  $person
58
	 * @return \Nette\Database\Table\ActiveRow
59
	 */
60
	public function update($id, $person)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
61
	{
62
		return $this->getPersonModel()
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getPersonM...is->populate($person)); (Nette\Database\Table\IRow|integer|boolean|null) is incompatible with the return type documented by App\Repositories\PersonRepository::update of type Nette\Database\Table\ActiveRow|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
			->save(
64
				$this->populate($person)
65
			);
66
	}
67
68
	/**
69
	 * @param  $id
70
	 * @param  $person
71
	 * @return \Nette\Database\Table\ActiveRow
72
	 */
73
	public function save($person)
74
	{
75
		return $this->getPersonModel()
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getPersonM...is->populate($person)); (Nette\Database\Table\IRow|integer|boolean|null) is incompatible with the return type documented by App\Repositories\PersonRepository::save of type Nette\Database\Table\ActiveRow|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...
76
			->save(
77
				$this->populate($person)
78
			);
79
	}
80
81
	/**
82
	 * @param  $id
83
	 * @return bool
84
	 */
85
	public function delete($id): bool
86
	{
87
		return $this->getPersonModel()->delete($id);
88
	}
89
90
	/**
91
	 * @param  array $values
92
	 * @return PersonEntity
93
	 */
94
	protected function populate($values): PersonEntity
95
	{
96
		$model = $this->getPersonModel();
97
		$person = $model->hydrate($values);
98
99
		return $person;
100
	}
101
102
	/**
103
	 * @return PersonModel
104
	 */
105
	protected function getPersonModel(): PersonModel
106
	{
107
		return $this->personModel;
108
	}
109
110
	/**
111
	 * @param  PersonModel $model
112
	 * @return $this
113
	 */
114
	protected function setPersonModel(PersonModel $model): self
115
	{
116
		$this->personModel = $model;
117
118
		return $this;
119
	}
120
121
}
122