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 |
||
13 | final class Configuration |
||
14 | { |
||
15 | const OPT_TRANSACTIONAL = 'transactional'; |
||
16 | const OPT_OPEN_TRACKING = 'open_tracking'; |
||
17 | const OPT_CLICK_TRACKING = 'click_tracking'; |
||
18 | const OPT_SANDBOX = 'sandbox'; |
||
19 | const OPT_SKIP_SUPPRESSION = 'skip_suppression'; |
||
20 | const OPT_INLINE_CSS = 'inline_css'; |
||
21 | const OPT_IP_POOL = 'ip_pool'; |
||
22 | |||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | private $recipientOverride; |
||
27 | |||
28 | /** |
||
29 | * @var bool |
||
30 | */ |
||
31 | private $overrideGmailStyle; |
||
32 | |||
33 | /** |
||
34 | * @var array |
||
35 | */ |
||
36 | private $options; |
||
37 | |||
38 | /** |
||
39 | * @var float |
||
40 | */ |
||
41 | private $ipPoolProbability; |
||
42 | |||
43 | /** |
||
44 | * @param array $options |
||
45 | * |
||
46 | * @throws Exception |
||
47 | */ |
||
48 | 33 | public static function guardOptionValidity(array $options) |
|
66 | |||
67 | /** |
||
68 | * @return Configuration |
||
69 | */ |
||
70 | 57 | public static function newInstance() |
|
74 | |||
75 | 93 | public function __construct() |
|
82 | |||
83 | 60 | public function overrideRecipients() |
|
87 | |||
88 | /** |
||
89 | * @return bool |
||
90 | */ |
||
91 | 18 | public function overrideGmailStyle() |
|
95 | |||
96 | /** |
||
97 | * @param bool $overrideGmailStyle |
||
98 | * |
||
99 | * @return Configuration |
||
100 | */ |
||
101 | 9 | public function setOverrideGmailStyle($overrideGmailStyle) |
|
107 | |||
108 | /** |
||
109 | * @return string |
||
110 | */ |
||
111 | 18 | public function getRecipientOverride() |
|
115 | |||
116 | /** |
||
117 | * @param string $recipientOverride |
||
118 | * |
||
119 | * @return Configuration |
||
120 | * @throws Exception |
||
121 | */ |
||
122 | 21 | public function setRecipientOverride($recipientOverride) |
|
132 | |||
133 | /** |
||
134 | * @return array |
||
135 | */ |
||
136 | 60 | public function getOptions() |
|
140 | |||
141 | /** |
||
142 | * @param array $options |
||
143 | * |
||
144 | * @return Configuration |
||
145 | * @throws Exception |
||
146 | */ |
||
147 | 24 | View Code Duplication | public function setOptions(array $options) |
148 | { |
||
149 | 24 | self::guardOptionValidity($options); |
|
150 | |||
151 | 21 | foreach ($options as $option => $value) { |
|
152 | 21 | if ($option === self::OPT_IP_POOL) { |
|
153 | 18 | $this->options[$option] = (string) $value; |
|
154 | 18 | continue; |
|
155 | } |
||
156 | |||
157 | 12 | $this->options[$option] = (bool) $value; |
|
158 | 7 | } |
|
159 | |||
160 | 21 | return $this; |
|
161 | } |
||
162 | |||
163 | /** |
||
164 | * @return float |
||
165 | */ |
||
166 | 60 | public function getIpPoolProbability() |
|
170 | |||
171 | /** |
||
172 | * @param float $ipPoolProbability |
||
173 | * |
||
174 | * @return Configuration |
||
175 | * @throws Exception |
||
176 | */ |
||
177 | 18 | public function setIpPoolProbability($ipPoolProbability) |
|
187 | } |
||
188 |