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 |
||
| 20 | trait CacheableRepository |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @var CacheRepository |
||
| 24 | */ |
||
| 25 | protected $cacheRepository = null; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Set Cache Repository. |
||
| 29 | * |
||
| 30 | * @param CacheRepository $repository |
||
| 31 | * |
||
| 32 | * @return $this |
||
| 33 | */ |
||
| 34 | public function setCacheRepository(CacheRepository $repository) |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Return instance of Cache Repository. |
||
| 43 | * |
||
| 44 | * @return CacheRepository |
||
| 45 | */ |
||
| 46 | public function getCacheRepository() |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Skip Cache. |
||
| 58 | * |
||
| 59 | * @param bool $status |
||
| 60 | * |
||
| 61 | * @return $this |
||
| 62 | */ |
||
| 63 | public function skipCache($status = true) |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @return bool |
||
| 72 | */ |
||
| 73 | public function isSkippedCache() |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @param $method |
||
| 88 | * |
||
| 89 | * @return bool |
||
| 90 | */ |
||
| 91 | protected function allowedCache($method) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Get Cache key for the method. |
||
| 119 | * |
||
| 120 | * @param $method |
||
| 121 | * @param $args |
||
| 122 | * |
||
| 123 | * @return string |
||
| 124 | */ |
||
| 125 | public function getCacheKey($method, $args = null) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Get cache minutes. |
||
| 143 | * |
||
| 144 | * @return int |
||
| 145 | */ |
||
| 146 | public function getCacheMinutes() |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Retrieve all data of repository. |
||
| 155 | * |
||
| 156 | * @param array $columns |
||
| 157 | * |
||
| 158 | * @return mixed |
||
| 159 | */ |
||
| 160 | public function all($columns = ['*']) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Retrieve all data of repository, paginated. |
||
| 178 | * |
||
| 179 | * @param null $limit |
||
| 180 | * @param array $columns |
||
| 181 | * |
||
| 182 | * @return mixed |
||
| 183 | */ |
||
| 184 | View Code Duplication | public function paginate($limit = null, $columns = ['*']) |
|
| 200 | |||
| 201 | /** |
||
| 202 | * Find data by id. |
||
| 203 | * |
||
| 204 | * @param $id |
||
| 205 | * @param array $columns |
||
| 206 | * |
||
| 207 | * @return mixed |
||
| 208 | */ |
||
| 209 | View Code Duplication | public function find($id, $columns = ['*']) |
|
| 224 | |||
| 225 | /** |
||
| 226 | * Find data by field and value. |
||
| 227 | * |
||
| 228 | * @param $field |
||
| 229 | * @param $value |
||
| 230 | * @param array $columns |
||
| 231 | * |
||
| 232 | * @return mixed |
||
| 233 | */ |
||
| 234 | View Code Duplication | public function findByField($field, $value = null, $columns = ['*']) |
|
| 249 | |||
| 250 | /** |
||
| 251 | * Find data by multiple fields. |
||
| 252 | * |
||
| 253 | * @param array $where |
||
| 254 | * @param array $columns |
||
| 255 | * |
||
| 256 | * @return mixed |
||
| 257 | */ |
||
| 258 | View Code Duplication | public function findWhere(array $where, $columns = ['*']) |
|
| 273 | |||
| 274 | /** |
||
| 275 | * Find data by Criteria. |
||
| 276 | * |
||
| 277 | * @param Criteria $criteria |
||
| 278 | * |
||
| 279 | * @return mixed |
||
| 280 | */ |
||
| 281 | public function getByCriteria(Criteria $criteria) |
||
| 296 | |||
| 297 | } |
||
| 298 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: