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 DSN { |
||
28 | |||
29 | const TYPE_MYSQL = 'mysql'; |
||
30 | const TYPE_PGSQL = 'pgsql'; |
||
31 | const TYPE_SQLITE = 'sqlite'; |
||
32 | |||
33 | protected $config; |
||
34 | |||
35 | public static function fromString( $config ) { |
||
67 | |||
68 | public static function fromJSON( $config ) { |
||
78 | |||
79 | /** |
||
80 | * Create a DSN from an array of parameters. |
||
81 | * scheme - type of database (mysql, pgsql, sqlite) required |
||
82 | * host - hostname of database server |
||
83 | * port - network port to connect on |
||
84 | * user - user to connect as |
||
85 | * pass - user's password |
||
86 | * db - name of the database schema to connect to |
||
87 | * options - an array of database specific options |
||
88 | * @param array $dsn |
||
|
|||
89 | */ |
||
90 | public function __construct( array $config ) { |
||
107 | |||
108 | public function isMySQL() { |
||
111 | |||
112 | public function isPgSQL() { |
||
115 | |||
116 | public function isSQLite() { |
||
119 | |||
120 | /** |
||
121 | * Dynamic property access. |
||
122 | * @param string $key |
||
123 | * @return mixed |
||
124 | */ |
||
125 | public function __get( $key ) { |
||
128 | |||
129 | /** |
||
130 | * Dynamic property access. |
||
131 | * @param string $key |
||
132 | * @return boolean |
||
133 | */ |
||
134 | public function __isset( $key ) { |
||
137 | |||
138 | /** |
||
139 | * Convert the DSN into a URI-type string. |
||
140 | * @return string |
||
141 | */ |
||
142 | public function toString() { |
||
172 | |||
173 | /** |
||
174 | * Return a connection string for use by PDO. |
||
175 | * @return string |
||
176 | */ |
||
177 | public function getConnectionString() { |
||
180 | |||
181 | /** |
||
182 | * Ensure the dsn configuration is valid. |
||
183 | * @param array $config |
||
184 | * @return void |
||
185 | */ |
||
186 | protected function configure( array $config ) { |
||
211 | |||
212 | /** |
||
213 | * Configure a MySQL DSN. |
||
214 | * @param array $config |
||
215 | * @return array |
||
216 | */ |
||
217 | View Code Duplication | protected function configureMySQL( array $config ) { |
|
233 | |||
234 | /** |
||
235 | * Configure a PostgreSQL DSN. |
||
236 | * @param array $config |
||
237 | * @return array |
||
238 | */ |
||
239 | View Code Duplication | protected function configurePgSQL( array $config ) { |
|
255 | |||
256 | /** |
||
257 | * Configure a SQLite DSN. |
||
258 | * @param array $config |
||
259 | * @return array |
||
260 | */ |
||
261 | protected function configureSQLite( array $config ) { |
||
278 | |||
279 | } |
||
280 | |||
281 | // EOF |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.