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 |
||
| 9 | trait ReceivesApplications |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * updates application record in db. |
||
| 13 | * |
||
| 14 | * @param array|object $model |
||
| 15 | * @param array $criteria |
||
| 16 | * @return bool |
||
| 17 | */ |
||
| 18 | public function processApplicationFrom($model, $criteria = [], $newStatus = 'processed') |
||
| 47 | |||
| 48 | /** |
||
| 49 | * check if current model has received application from other model or not. |
||
| 50 | * |
||
| 51 | * @param array|object $model |
||
| 52 | * @param array $criteria |
||
| 53 | * @return bool |
||
| 54 | */ |
||
| 55 | public function hasReceivedApplicationFrom($model, $criteria = []) |
||
| 59 | |||
| 60 | /** |
||
| 61 | * get applications that applied on this model and this model was the receiver. |
||
| 62 | * |
||
| 63 | * @param array|object $model |
||
| 64 | * @param array $criteria |
||
| 65 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 66 | */ |
||
| 67 | public function receivedApplicationsFrom($model, $criteria = []) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * sets receiverCriteria attribute and returns $this to allow fluent api. |
||
| 94 | * |
||
| 95 | * @param array $criteria |
||
| 96 | * @return self |
||
| 97 | */ |
||
| 98 | public function setReceiverCriteria($criteria = []) |
||
| 104 | |||
| 105 | /** |
||
| 106 | * returns received applications for that model. |
||
| 107 | * |
||
| 108 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany |
||
| 109 | */ |
||
| 110 | public function receivedApplications() |
||
| 114 | } |
||
| 115 |
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: