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 |
||
| 34 | trait ExtendDisplay |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * Displayers for grid column. |
||
| 38 | * |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | public static $displayers = [ |
||
| 42 | 'editable' => Displayers\Editable::class, |
||
| 43 | 'image' => Displayers\Image::class, |
||
| 44 | 'label' => Displayers\Label::class, |
||
| 45 | 'button' => Displayers\Button::class, |
||
| 46 | 'link' => Displayers\Link::class, |
||
| 47 | 'badge' => Displayers\Badge::class, |
||
| 48 | 'progressBar' => Displayers\ProgressBar::class, |
||
| 49 | 'progress' => Displayers\ProgressBar::class, |
||
| 50 | 'orderable' => Displayers\Orderable::class, |
||
| 51 | 'table' => Displayers\Table::class, |
||
| 52 | 'expand' => Displayers\Expand::class, |
||
| 53 | 'modal' => Displayers\Modal::class, |
||
| 54 | 'carousel' => Displayers\Carousel::class, |
||
| 55 | 'downloadable' => Displayers\Downloadable::class, |
||
| 56 | 'copyable' => Displayers\Copyable::class, |
||
| 57 | 'qrcode' => Displayers\QRCode::class, |
||
| 58 | 'prefix' => Displayers\Prefix::class, |
||
| 59 | 'suffix' => Displayers\Suffix::class, |
||
| 60 | 'secret' => Displayers\Secret::class, |
||
| 61 | 'limit' => Displayers\Limit::class, |
||
| 62 | ]; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var bool |
||
| 66 | */ |
||
| 67 | protected $searchable = false; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Extend column displayer. |
||
| 71 | * |
||
| 72 | * @param $name |
||
| 73 | * @param $displayer |
||
| 74 | */ |
||
| 75 | public static function extend($name, $displayer) |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Set column as searchable. |
||
| 82 | * |
||
| 83 | * @return $this |
||
| 84 | */ |
||
| 85 | public function searchable() |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Bind search query to grid model. |
||
| 105 | * |
||
| 106 | * @param Model $model |
||
| 107 | */ |
||
| 108 | public function bindSearchQuery(Model $model) |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Display column using array value map. |
||
| 117 | * |
||
| 118 | * @param array $values |
||
| 119 | * @param null $default |
||
| 120 | * |
||
| 121 | * @return $this |
||
| 122 | */ |
||
| 123 | View Code Duplication | public function using(array $values, $default = null) |
|
| 133 | |||
| 134 | /** |
||
| 135 | * Replace output value with giving map. |
||
| 136 | * |
||
| 137 | * @param array $replacements |
||
| 138 | * |
||
| 139 | * @return $this |
||
| 140 | */ |
||
| 141 | public function replace(array $replacements) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @param string|Closure $input |
||
| 154 | * @param string $seperator |
||
| 155 | * |
||
| 156 | * @return $this |
||
| 157 | */ |
||
| 158 | public function repeat($input, $seperator = '') |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Render this column with the given view. |
||
| 177 | * |
||
| 178 | * @param string $view |
||
| 179 | * |
||
| 180 | * @return $this |
||
| 181 | */ |
||
| 182 | public function view($view) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Convert file size to a human readable format like `100mb`. |
||
| 193 | * |
||
| 194 | * @return $this |
||
| 195 | */ |
||
| 196 | public function filesize() |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Display the fields in the email format as gavatar. |
||
| 205 | * |
||
| 206 | * @param int $size |
||
| 207 | * |
||
| 208 | * @return $this |
||
| 209 | */ |
||
| 210 | public function gravatar($size = 30) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Display field as a loading icon. |
||
| 225 | * |
||
| 226 | * @param array $values |
||
| 227 | * @param array $others |
||
| 228 | * |
||
| 229 | * @return $this |
||
| 230 | */ |
||
| 231 | public function loading($values = [], $others = []) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Display column as an font-awesome icon based on it's value. |
||
| 246 | * |
||
| 247 | * @param array $setting |
||
| 248 | * @param string $default |
||
| 249 | * |
||
| 250 | * @return $this |
||
| 251 | */ |
||
| 252 | public function icon(array $setting, $default = '') |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Return a human readable format time. |
||
| 269 | * |
||
| 270 | * @param null $locale |
||
| 271 | * |
||
| 272 | * @return $this |
||
| 273 | */ |
||
| 274 | public function diffForHumans($locale = null) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Display column as boolean , `✓` for true, and `✗` for false. |
||
| 287 | * |
||
| 288 | * @param array $map |
||
| 289 | * @param bool $default |
||
| 290 | * |
||
| 291 | * @return $this |
||
| 292 | */ |
||
| 293 | public function bool(array $map = [], $default = false) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Display column as a default value if empty. |
||
| 304 | * |
||
| 305 | * @param string $default |
||
| 306 | * @return $this |
||
| 307 | */ |
||
| 308 | public function default($default = '-') |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Add a `dot` before column text. |
||
| 317 | * |
||
| 318 | * @param array $options |
||
| 319 | * @param string $default |
||
| 320 | * |
||
| 321 | * @return $this |
||
| 322 | */ |
||
| 323 | public function dot($options = [], $default = '') |
||
| 335 | } |
||
| 336 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.