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 |
||
| 5 | class Card |
||
| 6 | { |
||
| 7 | /** |
||
| 8 | * An id that will help HipChat recognise the same card when it is sent multiple times. |
||
| 9 | * |
||
| 10 | * @var string |
||
| 11 | */ |
||
| 12 | public $id; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * The title of the card. |
||
| 16 | * Valid length range: 1 - 500. |
||
| 17 | * |
||
| 18 | * @var string |
||
| 19 | */ |
||
| 20 | public $title = ''; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Style of the card. |
||
| 24 | * Valid values: file, image, application, link, media. |
||
| 25 | * |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | public $style = CardStyles::APPLICATION; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * The description in the specific format. |
||
| 32 | * Valid length range: 1 - 1000. |
||
| 33 | * |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | public $content = ''; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * The format that can be html or text. |
||
| 40 | * |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | public $format = 'text'; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Application cards can be compact (1 to 2 lines) or medium (1 to 5 lines). |
||
| 47 | * |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | public $cardFormat; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * The url where the card will open. |
||
| 54 | * |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | public $url = ''; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * The thumbnail url. Valid length range: 1 - 250. |
||
| 61 | * |
||
| 62 | * @var string |
||
| 63 | */ |
||
| 64 | public $thumbnail; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * The thumbnail url in retina. Valid length range: 1 - 250. |
||
| 68 | * |
||
| 69 | * @var string |
||
| 70 | */ |
||
| 71 | public $thumbnail2; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * The original width of the image. |
||
| 75 | * |
||
| 76 | * @var int |
||
| 77 | */ |
||
| 78 | public $thumbnailWidth; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * The original height of the image. |
||
| 82 | * |
||
| 83 | * @var int |
||
| 84 | */ |
||
| 85 | public $thumbnailHeight; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Html for the activity to show in one line a summary of the action that happened. |
||
| 89 | * |
||
| 90 | * @var string |
||
| 91 | */ |
||
| 92 | public $activity; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * The activity icon url. |
||
| 96 | * |
||
| 97 | * @var string |
||
| 98 | */ |
||
| 99 | public $activityIcon; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * The activity icon url in retina. |
||
| 103 | * |
||
| 104 | * @var string |
||
| 105 | */ |
||
| 106 | public $activityIcon2; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * The icon url. |
||
| 110 | * |
||
| 111 | * @var string |
||
| 112 | */ |
||
| 113 | public $icon; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * The icon url in retina. |
||
| 117 | * |
||
| 118 | * @var string |
||
| 119 | */ |
||
| 120 | public $icon2; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * List of attributes to show below the card. Sample {label}:{value.icon} {value.label}. |
||
| 124 | * |
||
| 125 | * @var CardAttribute[] |
||
| 126 | */ |
||
| 127 | public $attributes = []; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Create a new Card instance. |
||
| 131 | * |
||
| 132 | * @param string $title |
||
| 133 | * @param string $id |
||
| 134 | */ |
||
| 135 | public function __construct($title = '', $id = '') |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Create a new Card instance. |
||
| 146 | * |
||
| 147 | * @param string $title |
||
| 148 | * @param string $id |
||
| 149 | * @return static |
||
| 150 | */ |
||
| 151 | public static function create($title = '', $id = '') |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Sets the title of the card. |
||
| 158 | * |
||
| 159 | * @param string $title |
||
| 160 | * @return $this |
||
| 161 | */ |
||
| 162 | public function title($title) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Sets the id of the card. |
||
| 170 | * |
||
| 171 | * @param $id |
||
| 172 | * @return $this |
||
| 173 | */ |
||
| 174 | public function id($id) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Sets the style of the card. |
||
| 182 | * |
||
| 183 | * @param $style |
||
| 184 | * @return $this |
||
| 185 | */ |
||
| 186 | public function style($style) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Sets the content of the card. |
||
| 194 | * |
||
| 195 | * @param $content |
||
| 196 | * @return $this |
||
| 197 | */ |
||
| 198 | public function content($content) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Sets the format to plain text and optionally the content. |
||
| 206 | * |
||
| 207 | * @param string $content |
||
| 208 | * @return $this |
||
| 209 | */ |
||
| 210 | public function text($content = '') |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Sets the format to html and optionally the content. |
||
| 223 | * |
||
| 224 | * @param string $content |
||
| 225 | * @return $this |
||
| 226 | */ |
||
| 227 | public function html($content = '') |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Sets the format of the card. |
||
| 240 | * |
||
| 241 | * @param $cardFormat |
||
| 242 | * @return $this |
||
| 243 | */ |
||
| 244 | public function cardFormat($cardFormat) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Sets the url of the card. |
||
| 252 | * |
||
| 253 | * @param $url |
||
| 254 | * @return $this |
||
| 255 | */ |
||
| 256 | public function url($url) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Sets the thumbnail of the card. |
||
| 264 | * |
||
| 265 | * @param string $icon |
||
| 266 | * @param string|null $icon2 |
||
| 267 | * @param string|null $width |
||
| 268 | * @param string|null $height |
||
| 269 | * @return $this |
||
| 270 | */ |
||
| 271 | public function thumbnail($icon, $icon2 = null, $width = null, $height = null) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Sets the activity of the card. |
||
| 292 | * |
||
| 293 | * @param string $html |
||
| 294 | * @param string|null $icon |
||
| 295 | * @param string|null $icon2 |
||
| 296 | * @return $this |
||
| 297 | */ |
||
| 298 | public function activity($html, $icon = null, $icon2 = null) |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Sets the icon of the card. |
||
| 315 | * |
||
| 316 | * @param string $icon |
||
| 317 | * @param string|null $icon2 |
||
| 318 | * @return $this |
||
| 319 | */ |
||
| 320 | View Code Duplication | public function icon($icon, $icon2 = null) |
|
| 330 | |||
| 331 | /** |
||
| 332 | * Adds a CardAttribute to the card. |
||
| 333 | * |
||
| 334 | * @param CardAttribute|\Closure $attribute |
||
| 335 | * @return $this |
||
| 336 | */ |
||
| 337 | View Code Duplication | public function addAttribute($attribute) |
|
| 352 | |||
| 353 | /** |
||
| 354 | * Get an array representation of the Card. |
||
| 355 | * |
||
| 356 | * @return array |
||
| 357 | */ |
||
| 358 | public function toArray() |
||
| 409 | } |
||
| 410 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.