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 |
||
10 | class Card implements \JsonSerializable |
||
11 | { |
||
12 | /** @var string Card Title */ |
||
13 | protected $title; |
||
14 | |||
15 | /** @var string Item Url */ |
||
16 | protected $image_url; |
||
17 | |||
18 | /** @var string Image Url */ |
||
19 | protected $item_url; |
||
20 | |||
21 | /** @var string Subtitle */ |
||
22 | protected $subtitle; |
||
23 | |||
24 | /** @var array Call to Action Buttons */ |
||
25 | public $buttons = []; |
||
26 | |||
27 | /** |
||
28 | * Create a Card |
||
29 | * |
||
30 | * @return static |
||
31 | */ |
||
32 | public static function create($title = '') |
||
36 | |||
37 | /** |
||
38 | * Create Card constructor |
||
39 | */ |
||
40 | public function __construct($title = '') |
||
44 | |||
45 | /** |
||
46 | * Set Button Title. |
||
47 | * |
||
48 | * @param $title |
||
49 | * @throws CouldNotCreateCard |
||
50 | * @return $this |
||
51 | */ |
||
52 | public function title($title) |
||
60 | |||
61 | /** |
||
62 | * Add up to 3 call to action buttons. |
||
63 | * |
||
64 | * @param array $buttons |
||
65 | * |
||
66 | * @return $this |
||
67 | * @throws CouldNotSendNotification |
||
68 | */ |
||
69 | View Code Duplication | public function buttons(array $buttons = []) |
|
77 | |||
78 | /** |
||
79 | * Set Card Item Url |
||
80 | * |
||
81 | * @param $item_url |
||
82 | * @return $this |
||
83 | */ |
||
84 | public function url($item_url) |
||
89 | |||
90 | /** |
||
91 | * Set Card Image Url |
||
92 | * |
||
93 | * @param $image_url |
||
94 | * Image ration should be 1.91:1 |
||
95 | * @return $this |
||
96 | */ |
||
97 | public function image($image_url) |
||
102 | |||
103 | /** |
||
104 | * Set Card Subtitle |
||
105 | * |
||
106 | * @param $subtitle |
||
107 | * @throws CouldNotCreateCard |
||
108 | * @return $this |
||
109 | */ |
||
110 | public function subtitle($subtitle) |
||
117 | |||
118 | |||
119 | /** |
||
120 | * Builds payload and returns an array |
||
121 | * |
||
122 | * @return array |
||
123 | * @throws CouldNotCreateCard |
||
124 | */ |
||
125 | public function toArray() |
||
147 | |||
148 | /** |
||
149 | * Convert the object into something JSON serializable. |
||
150 | * |
||
151 | * @return array |
||
152 | */ |
||
153 | public function jsonSerialize() |
||
157 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.