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 |
||
28 | class Repository extends DocumentRepository implements RepositoryInterface |
||
29 | { |
||
30 | /** |
||
31 | * @var ResourceInterface |
||
32 | */ |
||
33 | private $resource; |
||
34 | |||
35 | /** |
||
36 | 16 | * @param DocumentManager $dm |
|
37 | * @param UnitOfWork $uow |
||
38 | * @param ClassMetadata $class |
||
39 | * @param ResourceInterface $resource |
||
40 | */ |
||
41 | public function __construct( |
||
51 | |||
52 | /** |
||
53 | * {@inheritdoc} |
||
54 | */ |
||
55 | public function findForIndex(array $criteria, array $orderBy = []) |
||
59 | |||
60 | /** |
||
61 | * {@inheritdoc} |
||
62 | */ |
||
63 | public function findForShow(array $criteria, array $orderBy = []) |
||
67 | |||
68 | /** |
||
69 | * {@inheritdoc} |
||
70 | */ |
||
71 | public function findForUpdate(array $criteria, array $orderBy = []) |
||
75 | |||
76 | /** |
||
77 | * {@inheritdoc} |
||
78 | */ |
||
79 | public function findForDelete(array $criteria, array $orderBy = []) |
||
83 | |||
84 | /** |
||
85 | * {@inheritdoc} |
||
86 | */ |
||
87 | public function findOneBy(array $criteria, array $orderBy = []) |
||
91 | |||
92 | /** |
||
93 | * {@inheritdoc} |
||
94 | */ |
||
95 | View Code Duplication | public function findBy(array $criteria, array $orderBy = [], $limit = null, $offset = null) |
|
109 | |||
110 | /** |
||
111 | * {@inheritdoc} |
||
112 | */ |
||
113 | public function createQueryBuilderForCollection() |
||
117 | |||
118 | /** |
||
119 | * {@inheritdoc} |
||
120 | */ |
||
121 | public function createDataSourceBuilder(array $options = []) |
||
125 | |||
126 | /** |
||
127 | * {@inheritdoc} |
||
128 | */ |
||
129 | public function getProperty($property, $root = null) |
||
137 | |||
138 | /** |
||
139 | * @param mixed[] $criteria |
||
140 | * @param string[] $orderBy |
||
141 | * @param bool $collection |
||
142 | * |
||
143 | * @return Builder |
||
144 | */ |
||
145 | View Code Duplication | protected function buildQueryBuilder(array $criteria, array $orderBy, $collection = false) |
|
154 | |||
155 | /** |
||
156 | * @param Builder $queryBuilder |
||
157 | * @param mixed[] $criteria |
||
158 | */ |
||
159 | private function applyCriteria(Builder $queryBuilder, array $criteria = null) |
||
171 | |||
172 | /** |
||
173 | * @param Builder $queryBuilder |
||
174 | * @param string[] $orderBy |
||
175 | */ |
||
176 | View Code Duplication | private function applySorting(Builder $queryBuilder, array $orderBy = []) |
|
184 | } |
||
185 |
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.