Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php namespace Anomaly\Streams\Platform\Model; |
||
20 | class EloquentCriteria |
||
21 | { |
||
22 | |||
23 | use Hookable; |
||
24 | use DispatchesJobs; |
||
25 | |||
26 | /** |
||
27 | * Safe builder methods. |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | private $disabled = [ |
||
32 | 'delete', |
||
33 | 'update', |
||
34 | ]; |
||
35 | |||
36 | /** |
||
37 | * The query builder. |
||
38 | * |
||
39 | * @var Builder|\Illuminate\Database\Query\Builder |
||
40 | */ |
||
41 | protected $query; |
||
42 | |||
43 | /** |
||
44 | * Set the get method. |
||
45 | * |
||
46 | * @var string |
||
47 | */ |
||
48 | protected $method; |
||
49 | |||
50 | /** |
||
51 | * Create a new EntryCriteria instance. |
||
52 | * |
||
53 | * @param Builder $query |
||
54 | * @param string $method |
||
55 | */ |
||
56 | public function __construct(Builder $query, $method = 'get') |
||
61 | |||
62 | /** |
||
63 | * Get the paginated entries. |
||
64 | * |
||
65 | * @param array $columns |
||
66 | * @return Collection|Presenter|EntryPresenter |
||
67 | */ |
||
68 | public function paginate($perPage = 15, array $columns = ['*']) |
||
72 | |||
73 | /** |
||
74 | * Get the entries. |
||
75 | * |
||
76 | * @param array $columns |
||
77 | * @return Collection|Presenter|EntryPresenter |
||
78 | */ |
||
79 | public function get(array $columns = ['*']) |
||
83 | |||
84 | /** |
||
85 | * Find an entry. |
||
86 | * |
||
87 | * @param $identifier |
||
88 | * @param array $columns |
||
89 | * @return Presenter|EntryPresenter |
||
90 | */ |
||
91 | public function find($identifier, array $columns = ['*']) |
||
95 | |||
96 | /** |
||
97 | * Find an entry by column value. |
||
98 | * |
||
99 | * @param $column |
||
100 | * @param $value |
||
101 | * @param array $columns |
||
102 | * @return Presenter|EntryPresenter |
||
103 | */ |
||
104 | public function findBy($column, $value, array $columns = ['*']) |
||
110 | |||
111 | /** |
||
112 | * Return the first entry. |
||
113 | * |
||
114 | * @param array $columns |
||
115 | * @return EloquentModel|EntryInterface |
||
116 | */ |
||
117 | public function first(array $columns = ['*']) |
||
121 | |||
122 | /** |
||
123 | * Return whether the method is safe or not. |
||
124 | * |
||
125 | * @param $name |
||
126 | * @return bool |
||
127 | */ |
||
128 | protected function methodIsSafe($name) |
||
132 | |||
133 | /** |
||
134 | * Route through __call. |
||
135 | * |
||
136 | * @param $name |
||
137 | * @return Builder|null |
||
138 | */ |
||
139 | function __get($name) |
||
143 | |||
144 | /** |
||
145 | * Call the method on the query. |
||
146 | * |
||
147 | * @param $name |
||
148 | * @param $arguments |
||
149 | * @return Builder|null |
||
150 | */ |
||
151 | function __call($name, $arguments) |
||
179 | |||
180 | /** |
||
181 | * Return the string. |
||
182 | * |
||
183 | * @return string |
||
184 | */ |
||
185 | public function __toString() |
||
189 | } |
||
190 |
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:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.