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 Page extends HasContent |
||
| 9 | { |
||
| 10 | use Sluggable, SoftDeletes; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * The table associated with the model. |
||
| 14 | * |
||
| 15 | * @var string |
||
| 16 | */ |
||
| 17 | protected $table = 'pages'; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * The attributes that are mass assignable. |
||
| 21 | * |
||
| 22 | * @var array |
||
| 23 | */ |
||
| 24 | protected $fillable = [ |
||
| 25 | 'name', 'slug', '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 | * Return all visible pages. |
||
| 63 | * |
||
| 64 | * @param bool $order If true, order pages by 'order'. |
||
| 65 | * |
||
| 66 | * @return \Illuminate\Support\Collection |
||
| 67 | */ |
||
| 68 | public static function allVisible($order = true) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Return all hidden pages. |
||
| 77 | * |
||
| 78 | * @param bool $order If true, order pages by 'order'. |
||
| 79 | * |
||
| 80 | * @return \Illuminate\Support\Collection |
||
| 81 | */ |
||
| 82 | public static function allHidden($order = true) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Return all pages ordered by 'order'. |
||
| 91 | * |
||
| 92 | * @return \Illuminate\Support\Collection |
||
| 93 | */ |
||
| 94 | public static function allOrdered() |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Get all pages with given block name. |
||
| 103 | * |
||
| 104 | * @param string $blockName Name of block. |
||
| 105 | * |
||
| 106 | * @return \Illuminate\Support\Collection |
||
| 107 | */ |
||
| 108 | public static function hasBlockNamed($blockName) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Get all pages with given image name. |
||
| 115 | * |
||
| 116 | * @param string $imageName Name of image. |
||
| 117 | * |
||
| 118 | * @return \Illuminate\Support\Collection |
||
| 119 | */ |
||
| 120 | public static function hasImageNamed($imageName) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Get all pages with given link name. |
||
| 127 | * |
||
| 128 | * @param string $linkName Name of link. |
||
| 129 | * |
||
| 130 | * @return \Illuminate\Support\Collection |
||
| 131 | */ |
||
| 132 | public static function hasLinkNamed($linkName) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Get all pages with relationship on table that has given name. |
||
| 139 | * |
||
| 140 | * @param string $table Name of table relationship is on. |
||
| 141 | * @param string $name Relationship name. |
||
| 142 | * |
||
| 143 | * @return \Illuminate\Support\Collection |
||
| 144 | */ |
||
| 145 | protected static function hasRelationshipNamed($table, $name) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Return the page id. |
||
| 156 | * |
||
| 157 | * @return int |
||
| 158 | */ |
||
| 159 | public function id() |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Return the page name. |
||
| 166 | * |
||
| 167 | * @return string |
||
| 168 | */ |
||
| 169 | public function name() |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Return the page slug. |
||
| 176 | * |
||
| 177 | * @return string |
||
| 178 | */ |
||
| 179 | public function slug() |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Return the page order value. |
||
| 186 | * |
||
| 187 | * @return int |
||
| 188 | */ |
||
| 189 | public function order() |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Return page properties to be passed to js. |
||
| 196 | * |
||
| 197 | * @return array |
||
| 198 | */ |
||
| 199 | View Code Duplication | public function generateProps() |
|
| 208 | } |
||
| 209 |
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: