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 |
||
10 | abstract class File extends AbstractEntity implements EntityInterface |
||
11 | { |
||
12 | protected $extension; |
||
13 | |||
14 | /** |
||
15 | * Returns the base path. |
||
16 | * |
||
17 | * @return string |
||
18 | */ |
||
19 | abstract protected function getBasePath(); |
||
20 | |||
21 | protected function getIterator() |
||
25 | |||
26 | /** |
||
27 | * {@inheritdoc} |
||
28 | */ |
||
29 | public function search(SearchQuery $search) |
||
76 | |||
77 | /** |
||
78 | * {@inheritdoc} |
||
79 | */ |
||
80 | View Code Duplication | public function create(array $data) |
|
90 | |||
91 | /** |
||
92 | * {@inheritdoc} |
||
93 | */ |
||
94 | public function read($id) |
||
100 | |||
101 | /** |
||
102 | * {@inheritdoc} |
||
103 | */ |
||
104 | View Code Duplication | public function update($id, array $data) |
|
113 | |||
114 | /** |
||
115 | * {@inheritdoc} |
||
116 | */ |
||
117 | public function delete($id) |
||
123 | |||
124 | /** |
||
125 | * Calculate the id of a new row. |
||
126 | * |
||
127 | * @param array $data |
||
128 | * |
||
129 | * @return string |
||
130 | */ |
||
131 | protected function getId(array $data) |
||
137 | |||
138 | /** |
||
139 | * Returns the path of a file. |
||
140 | * |
||
141 | * @return string |
||
142 | */ |
||
143 | protected function getFilePath($filename) |
||
147 | |||
148 | /** |
||
149 | * Transform the data to a string. |
||
150 | * |
||
151 | * @param array $data |
||
152 | * |
||
153 | * @return string |
||
154 | */ |
||
155 | abstract protected function stringify(array $data); |
||
156 | |||
157 | /** |
||
158 | * Transform the string to an array. |
||
159 | * |
||
160 | * @param string $source |
||
161 | * |
||
162 | * @return array |
||
163 | */ |
||
164 | abstract protected function parse($source); |
||
165 | } |
||
166 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: