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 |
||
| 7 | trait ManagesPortfolio |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * Add a project to the portfolio. |
||
| 11 | * |
||
| 12 | * @param array $data Array of data to save. |
||
| 13 | * |
||
| 14 | * @return Project |
||
| 15 | */ |
||
| 16 | public function addProject(array $data) |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Update a project in the portfolio. |
||
| 35 | * |
||
| 36 | * @param Project $project Project to update. |
||
| 37 | * @param array $data Array of data to save. |
||
| 38 | * |
||
| 39 | * @return Project |
||
| 40 | */ |
||
| 41 | public function updateProject(Project $project, array $data) |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Remove a project from the portfolio. |
||
| 54 | * |
||
| 55 | * @param Project $project Project to remove. |
||
| 56 | * |
||
| 57 | * @return bool|null |
||
| 58 | */ |
||
| 59 | public function removeProject(Project $project) |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Restore a soft deleted project. |
||
| 66 | * |
||
| 67 | * @param Project $project Project to restore. |
||
| 68 | * |
||
| 69 | * @return bool|null |
||
| 70 | */ |
||
| 71 | public function restoreProject(Project $project) |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Hard delete a project from the portfolio. |
||
| 80 | * |
||
| 81 | * @param Project $project Project to purge. |
||
| 82 | * |
||
| 83 | * @return bool|null |
||
| 84 | */ |
||
| 85 | public function purgeProject(Project $project) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Update the order of projects in the portfolio. |
||
| 98 | * |
||
| 99 | * @param array $data Array of data for projects. |
||
| 100 | */ |
||
| 101 | public function updateProjectOrder(array $data) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Set order of data based on order value. |
||
| 114 | * |
||
| 115 | * @param array $data Data array containing 'order' index. |
||
| 116 | * |
||
| 117 | * @return \Illuminate\Support\Collection |
||
| 118 | */ |
||
| 119 | protected function setOrder(array $data) |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Add a text block to a project. |
||
| 131 | * |
||
| 132 | * @param Project $project Project to add text block to. |
||
| 133 | * @param array $blockData Array of text block data. |
||
| 134 | * |
||
| 135 | * @return \Illuminate\Database\Eloquent\Model |
||
| 136 | */ |
||
| 137 | public function addBlockToProject(Project $project, array $blockData) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Update a text block. |
||
| 144 | * |
||
| 145 | * @param TextBlock $textBlock Text block to update. |
||
| 146 | * @param array $blockData Array of text block data. |
||
| 147 | * |
||
| 148 | * @return TextBlock |
||
| 149 | */ |
||
| 150 | public function updateTextBlock(TextBlock $textBlock, array $blockData) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Update project text blocks by adding new ones and updating existing ones. |
||
| 159 | * |
||
| 160 | * @param Project $project Project that blocks belong to. |
||
| 161 | * @param array $data Array of project information. |
||
| 162 | */ |
||
| 163 | View Code Duplication | public function updateProjectTextBlocks(Project $project, array $data) |
|
| 179 | |||
| 180 | /** |
||
| 181 | * Remove a text block from a project. |
||
| 182 | * |
||
| 183 | * @param TextBlock $textBlock The text block to delete. |
||
| 184 | * |
||
| 185 | * @return bool|null |
||
| 186 | */ |
||
| 187 | public function removeTextBlock(TextBlock $textBlock) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Add image to a project. |
||
| 194 | * |
||
| 195 | * @param Project $project Project to add image to. |
||
| 196 | * @param array $imageData Array of image infomation. |
||
| 197 | * |
||
| 198 | * @return \Illuminate\Database\Eloquent\Model |
||
| 199 | */ |
||
| 200 | public function addImageToProject(Project $project, array $imageData) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Update image name and caption. |
||
| 207 | * |
||
| 208 | * @param Image $image Image to update. |
||
| 209 | * @param array $imageData Array of inmage information. |
||
| 210 | * |
||
| 211 | * @return Image |
||
| 212 | */ |
||
| 213 | public function updateImageInfo(Image $image, array $imageData) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Remove image from storage and delete database info. |
||
| 222 | * |
||
| 223 | * @param Image $image Image to remove. |
||
| 224 | * |
||
| 225 | * @return bool|null |
||
| 226 | */ |
||
| 227 | public function removeImage(Image $image) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Add a link to a project. |
||
| 236 | * |
||
| 237 | * @param Project $project Project to add link to. |
||
| 238 | * @param array $linkData Array of link info. |
||
| 239 | */ |
||
| 240 | public function addLinkToProject(Project $project, array $linkData) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Update a link. |
||
| 247 | * |
||
| 248 | * @param Link $link Link to update. |
||
| 249 | * @param array $linkData Array of link data. |
||
| 250 | * |
||
| 251 | * @return Link |
||
| 252 | */ |
||
| 253 | public function updateLink(Link $link, array $linkData) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Update project links by adding new ones and updating existing ones. |
||
| 262 | * |
||
| 263 | * @param Project $project Project that links belong to. |
||
| 264 | * @param array $data Array of project information. |
||
| 265 | */ |
||
| 266 | View Code Duplication | public function updateProjectLinks(Project $project, array $data) |
|
| 282 | |||
| 283 | /** |
||
| 284 | * Remove link from a project. |
||
| 285 | * |
||
| 286 | * @param Link $link Link to remove. |
||
| 287 | * |
||
| 288 | * @return bool|null |
||
| 289 | */ |
||
| 290 | public function removeLink(Link $link) |
||
| 294 | } |
||
| 295 |
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.