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 |
||
27 | class ConnectionConfigurator |
||
28 | { |
||
29 | protected $configuration; |
||
30 | |||
31 | /** |
||
32 | * __construct |
||
33 | * |
||
34 | * Initialize configuration. |
||
35 | * |
||
36 | * @access public |
||
37 | * @param array $dsn |
||
38 | */ |
||
39 | public function __construct($dsn) |
||
40 | { |
||
41 | $this->configuration = new ParameterHolder( |
||
42 | [ |
||
43 | 'dsn' => $dsn, |
||
44 | 'configuration' => $this->getDefaultConfiguration(), |
||
45 | ] |
||
46 | ); |
||
47 | $this->parseDsn(); |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * addConfiguration |
||
52 | * |
||
53 | * Add configuration settings. If settings exist, they are overridden. |
||
54 | * |
||
55 | * @access public |
||
56 | * @param array $configuration |
||
57 | * @return Connection $this |
||
58 | */ |
||
59 | public function addConfiguration(array $configuration) |
||
60 | { |
||
61 | $this ->configuration->setParameter( |
||
62 | 'configuration', |
||
63 | array_merge( |
||
64 | $this->configuration->getParameter('configuration'), |
||
65 | $configuration |
||
66 | ) |
||
67 | ); |
||
68 | |||
69 | return $this; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * set |
||
74 | * |
||
75 | * Set a new configuration setting. |
||
76 | * |
||
77 | * @access public |
||
78 | * @param string $name |
||
79 | * @param mixed $value |
||
80 | * @return ConnectionConfigurator $this |
||
81 | */ |
||
82 | public function set($name, $value) |
||
83 | { |
||
84 | $configuration = $this->configuration->getParameter('configuration'); |
||
85 | $configuration[$name] = $value; |
||
86 | $this |
||
87 | ->configuration |
||
88 | ->setParameter( |
||
89 | 'configuration', |
||
90 | $configuration |
||
91 | ); |
||
92 | |||
93 | return $this; |
||
94 | } |
||
95 | |||
96 | |||
97 | /** |
||
98 | * parseDsn() |
||
99 | * |
||
100 | * Sets the different parameters from the DSN. |
||
101 | * |
||
102 | * @access private |
||
103 | * @return Connection $this |
||
104 | * @throws ConnectionException |
||
105 | */ |
||
106 | private function parseDsn() |
||
172 | |||
173 | /** |
||
174 | * getConnectionString |
||
175 | * |
||
176 | * Return the connection string. |
||
177 | * |
||
178 | * @access public |
||
179 | * @return string |
||
180 | */ |
||
181 | public function getConnectionString() |
||
206 | |||
207 | /** |
||
208 | * getDefaultConfiguration |
||
209 | * |
||
210 | * Standalone, default configuration. |
||
211 | * |
||
212 | * @access protected |
||
213 | * @return array |
||
214 | */ |
||
215 | protected function getDefaultConfiguration() |
||
219 | |||
220 | /** |
||
221 | * getConfiguration |
||
222 | * |
||
223 | * Return current configuration settings. |
||
224 | * |
||
225 | * @access public |
||
226 | * @return array |
||
227 | * @throws ConnectionException |
||
228 | */ |
||
229 | public function getConfiguration() |
||
244 | } |
||
245 |