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 |
||
| 8 | class Project extends HasContent |
||
| 9 | { |
||
| 10 | use Sluggable, SoftDeletes; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * The table associated with the model. |
||
| 14 | * |
||
| 15 | * @var string |
||
| 16 | */ |
||
| 17 | protected $table = 'projects'; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * The attributes that are mass assignable. |
||
| 21 | * |
||
| 22 | * @var array |
||
| 23 | */ |
||
| 24 | protected $fillable = [ |
||
| 25 | 'name', 'slug', 'type', 'visible', 'order', |
||
| 26 | ]; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Properties to always eager load. |
||
| 30 | * |
||
| 31 | * @var array |
||
| 32 | */ |
||
| 33 | protected $with = ['blocks', 'images', 'links']; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * The attributes that should be casted to native types. |
||
| 37 | * |
||
| 38 | * @var array |
||
| 39 | */ |
||
| 40 | protected $casts = [ |
||
| 41 | 'visible' => 'boolean', |
||
| 42 | ]; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Fields that are dates. |
||
| 46 | * |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Get the route key for the model. |
||
| 53 | * |
||
| 54 | * @return string |
||
| 55 | */ |
||
| 56 | public function getRouteKeyName() |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Bootstrap model. |
||
| 63 | */ |
||
| 64 | public static function boot() |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Return all visible projects. |
||
| 79 | * |
||
| 80 | * @param bool $group If true, group projects by 'type'. |
||
| 81 | * @param bool $order If true, order projects by 'order'. |
||
| 82 | * |
||
| 83 | * @return \Illuminate\Support\Collection |
||
| 84 | */ |
||
| 85 | public static function allVisible($group = true, $order = true) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Return all hidden projects. |
||
| 94 | * |
||
| 95 | * @param bool $group If true, group projects by 'type'. |
||
| 96 | * @param bool $order If true, order projects by 'order'. |
||
| 97 | * |
||
| 98 | * @return \Illuminate\Support\Collection |
||
| 99 | */ |
||
| 100 | public static function allHidden($group = true, $order = true) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Return all projects grouped by 'type'. |
||
| 109 | * |
||
| 110 | * @param bool $order If true, order projects by 'order'. |
||
| 111 | * |
||
| 112 | * @return \Illuminate\Support\Collection |
||
| 113 | */ |
||
| 114 | public static function allGrouped($order = true) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Return all projects ordered by 'order'. |
||
| 123 | * |
||
| 124 | * @return \Illuminate\Support\Collection |
||
| 125 | */ |
||
| 126 | public static function allOrdered() |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Get all projects with given block name. |
||
| 135 | * |
||
| 136 | * @param string $blockName Name of block. |
||
| 137 | * |
||
| 138 | * @return \Illuminate\Support\Collection |
||
| 139 | */ |
||
| 140 | public static function hasBlockNamed($blockName) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Get all projects with given image name. |
||
| 147 | * |
||
| 148 | * @param string $imageName Name of image. |
||
| 149 | * |
||
| 150 | * @return \Illuminate\Support\Collection |
||
| 151 | */ |
||
| 152 | public static function hasImageNamed($imageName) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Get all projects with given link name. |
||
| 159 | * |
||
| 160 | * @param string $linkName Name of link. |
||
| 161 | * |
||
| 162 | * @return \Illuminate\Support\Collection |
||
| 163 | */ |
||
| 164 | public static function hasLinkNamed($linkName) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Get all projects with relationship on table that has given name. |
||
| 171 | * |
||
| 172 | * @param string $table Name of table relationship is on. |
||
| 173 | * @param string $name Relationship name. |
||
| 174 | * |
||
| 175 | * @return \Illuminate\Support\Collection |
||
| 176 | */ |
||
| 177 | protected static function hasRelationshipNamed($table, $name) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Return the project id. |
||
| 188 | * |
||
| 189 | * @return int |
||
| 190 | */ |
||
| 191 | public function id() |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Return the project name. |
||
| 198 | * |
||
| 199 | * @return string |
||
| 200 | */ |
||
| 201 | public function name() |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Return the project type. |
||
| 208 | * |
||
| 209 | * @return string |
||
| 210 | */ |
||
| 211 | public function type() |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Return the project slug. |
||
| 218 | * |
||
| 219 | * @return string |
||
| 220 | */ |
||
| 221 | public function slug() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Return the project order value. |
||
| 228 | * |
||
| 229 | * @return int |
||
| 230 | */ |
||
| 231 | public function order() |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Get formatted text of block with project name or first block. |
||
| 238 | * |
||
| 239 | * @return Larafolio\Models\TextBlock |
||
| 240 | */ |
||
| 241 | public function getProjectBlock() |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Get formatted text of block with project name or first block. |
||
| 254 | * |
||
| 255 | * @param bool $formatted If true, return formatted text. |
||
| 256 | * |
||
| 257 | * @return string |
||
| 258 | */ |
||
| 259 | public function getProjectBlockText($formatted = true) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Get url of small image with project name or first image in collection. |
||
| 274 | * |
||
| 275 | * @return Larafolio\Models\Image |
||
| 276 | */ |
||
| 277 | public function getProjectImage() |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Get url of small image with project name or first image in collection. |
||
| 290 | * |
||
| 291 | * @param string $size The size of the image, name of image cache filter. |
||
| 292 | * |
||
| 293 | * @return string |
||
| 294 | */ |
||
| 295 | public function getProjectImageUrl($size = 'small') |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Get blocks sorted by order. |
||
| 308 | * |
||
| 309 | * @param \Builder $query Query builder. |
||
| 310 | * @param string $slug Project slug. |
||
| 311 | * |
||
| 312 | * @return \Builder |
||
| 313 | */ |
||
| 314 | public function scopeWithBlocks($query, $slug) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Get full project info (blocks and links sorted by order). |
||
| 322 | * |
||
| 323 | * @param \Builder $query Query builder. |
||
| 324 | * @param string $slug Project slug. |
||
| 325 | * |
||
| 326 | * @return \Builder |
||
| 327 | */ |
||
| 328 | public function scopeFull($query, $slug) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Order given relationship by order value. |
||
| 337 | * |
||
| 338 | * @param \Builder $query Query builder. |
||
| 339 | * @param string $relationship Name of relationship to order. |
||
| 340 | * |
||
| 341 | * @return \Builder |
||
| 342 | */ |
||
| 343 | public function scopeOrderRelationship($query, $relationship) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Return project properties to be passed to js. |
||
| 352 | * |
||
| 353 | * @return array |
||
| 354 | */ |
||
| 355 | View Code Duplication | public function generateProps() |
|
| 364 | } |
||
| 365 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: