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 | trait Publishable |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * Boot the has-drafts trait for a model. |
||
| 14 | * |
||
| 15 | * @return void |
||
| 16 | */ |
||
| 17 | 3 | public static function bootPublishable() |
|
| 21 | |||
| 22 | /** |
||
| 23 | * Initialize this trait for an instance. |
||
| 24 | * |
||
| 25 | * @return void |
||
| 26 | */ |
||
| 27 | 3 | public function initializePublishable() |
|
| 31 | |||
| 32 | /** |
||
| 33 | * Save instance of this model as published. |
||
| 34 | * |
||
| 35 | * @param array $options |
||
| 36 | * |
||
| 37 | * @return bool |
||
| 38 | */ |
||
| 39 | 1 | View Code Duplication | public function publish(array $options = []) |
| 62 | |||
| 63 | /** |
||
| 64 | * Toogle model instance state to non-published. |
||
| 65 | * |
||
| 66 | * @param array $options |
||
| 67 | * |
||
| 68 | * @return bool |
||
| 69 | */ |
||
| 70 | 1 | View Code Duplication | public function unpublish(array $options = []) |
| 86 | |||
| 87 | /** |
||
| 88 | * Save instance of this model as a draft. |
||
| 89 | * |
||
| 90 | * @param array $options |
||
| 91 | * |
||
| 92 | * @return bool |
||
| 93 | */ |
||
| 94 | public function draft(array $options = []) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Register a "publishing" model event callback with the dispatcher. |
||
| 101 | * |
||
| 102 | * @param \Closure|string $callback |
||
| 103 | * |
||
| 104 | * @return void |
||
| 105 | */ |
||
| 106 | public static function publishing($callback) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Register a "published" model event callback with the dispatcher. |
||
| 113 | * |
||
| 114 | * @param \Closure|string $callback |
||
| 115 | * |
||
| 116 | * @return void |
||
| 117 | */ |
||
| 118 | public static function published($callback) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Register a "unpublishing" model event callback with the dispatcher. |
||
| 125 | * |
||
| 126 | * @param \Closure|string $callback |
||
| 127 | * |
||
| 128 | * @return void |
||
| 129 | */ |
||
| 130 | public static function unpublishing($callback) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Register a "unpublished" model event callback with the dispatcher. |
||
| 137 | * |
||
| 138 | * @param \Closure|string $callback |
||
| 139 | * |
||
| 140 | * @return void |
||
| 141 | */ |
||
| 142 | public static function unpublished($callback) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Determine if the model instance is published. |
||
| 149 | * |
||
| 150 | * @return bool |
||
| 151 | */ |
||
| 152 | 1 | public function isPublished() |
|
| 156 | |||
| 157 | /** |
||
| 158 | * Get the name of the "published at" column. |
||
| 159 | * |
||
| 160 | * @return string |
||
| 161 | */ |
||
| 162 | 3 | public function getPublishedAtColumn() |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Get the fully qualified "published at" column. |
||
| 169 | * |
||
| 170 | * @return string |
||
| 171 | */ |
||
| 172 | 1 | public function getQualifiedPublishedAtColumn() |
|
| 176 | } |
||
| 177 |
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: