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:
Complex classes like Pdo often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Pdo, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class Pdo implements DbInterface |
||
25 | { |
||
26 | /** |
||
27 | * The primary table we're working with |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $table = null; |
||
31 | |||
32 | /** |
||
33 | * Any filtering for a WHERE SQL clause |
||
34 | * @var mixed |
||
35 | */ |
||
36 | protected $where = false; |
||
37 | |||
38 | /** |
||
39 | * The database connection credentials |
||
40 | * @var array |
||
41 | */ |
||
42 | protected $credentials = array(); |
||
43 | |||
44 | /** |
||
45 | * The database object we're piggybacking on |
||
46 | * @var \Aura\Sql\ExtendedPdo |
||
47 | */ |
||
48 | protected $db = null; |
||
49 | |||
50 | /** |
||
51 | * A matched array of "bad" characters our string manipulation hates |
||
52 | * @var array |
||
53 | */ |
||
54 | protected $escape_chars = array( |
||
55 | 'search' => array("\\", "\0", "\n", "\r", "\x1a", "'", '"'), |
||
56 | 'replace' => array("\\\\", "\\0", "\\n", "\\r", "\Z", "\'", '\"') |
||
57 | ); |
||
58 | |||
59 | /** |
||
60 | * (non-PHPdoc) |
||
61 | * @see \JaegerApp\Db\DbInterface::select() |
||
62 | */ |
||
63 | public function select($table, $where) |
||
64 | { |
||
65 | $this->table = $table; |
||
66 | $this->where = $where; |
||
67 | return $this; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * (non-PHPdoc) |
||
72 | * @see \JaegerApp\Db\DbInterface::insert() |
||
73 | */ |
||
74 | public function insert($table, array $data = array()) |
||
75 | { |
||
76 | $query_factory = new QueryFactory('mysql'); |
||
77 | $insert = $query_factory->newInsert(); |
||
78 | $insert->into($table); |
||
79 | $cols = $bind = array(); |
||
80 | foreach($data AS $key => $value) |
||
81 | { |
||
82 | $cols[] = $key; |
||
83 | $bind[$key] = $value; |
||
84 | } |
||
85 | |||
86 | $insert->cols($cols)->bindValues($bind); |
||
87 | $sth = $this->getDb()->prepare($insert->getStatement()); |
||
88 | $sth->execute($insert->getBindValues()); |
||
89 | |||
90 | $name = $insert->getLastInsertIdName('id'); |
||
91 | return $this->getDb()->lastInsertId($name); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * (non-PHPdoc) |
||
96 | * @see \JaegerApp\Db\DbInterface::update() |
||
97 | */ |
||
98 | public function update($table, $data, $where) |
||
121 | |||
122 | /** |
||
123 | * (non-PHPdoc) |
||
124 | * @see \Aura\Sql\ExtendedPdo::query() |
||
125 | */ |
||
126 | public function query($sql = '', $return = false) |
||
134 | |||
135 | /** |
||
136 | * (non-PHPdoc) |
||
137 | * @see \JaegerApp\Db\DbInterface::escape() |
||
138 | */ |
||
139 | public function escape($string) |
||
143 | |||
144 | /** |
||
145 | * (non-PHPdoc) |
||
146 | * @see \JaegerApp\Db\DbInterface::getAllTables() |
||
147 | */ |
||
148 | public function getAllTables() |
||
153 | |||
154 | /** |
||
155 | * (non-PHPdoc) |
||
156 | * @see \JaegerApp\Db\DbInterface::getTableStatus() |
||
157 | */ |
||
158 | public function getTableStatus() |
||
163 | |||
164 | /** |
||
165 | * (non-PHPdoc) |
||
166 | * @see \JaegerApp\Db\DbInterface::getCreateTable() |
||
167 | */ |
||
168 | public function getCreateTable($table, $if_not_exists = false) |
||
186 | |||
187 | /** |
||
188 | * (non-PHPdoc) |
||
189 | * @see \JaegerApp\Db\DbInterface::clear() |
||
190 | */ |
||
191 | public function clear() |
||
197 | |||
198 | /** |
||
199 | * (non-PHPdoc) |
||
200 | * @see \JaegerApp\Db\DbInterface::totalRows() |
||
201 | */ |
||
202 | public function totalRows($table) |
||
214 | |||
215 | /** |
||
216 | * (non-PHPdoc) |
||
217 | * @see \JaegerApp\Db\DbInterface::getColumns() |
||
218 | */ |
||
219 | View Code Duplication | public function getColumns($table) |
|
228 | |||
229 | /** |
||
230 | * (non-PHPdoc) |
||
231 | * @see \JaegerApp\Db\DbInterface::get() |
||
232 | */ |
||
233 | public function get() |
||
258 | |||
259 | /** |
||
260 | * |
||
261 | * @param array $credentials |
||
262 | * @return \JaegerApp\Db\Mysqli |
||
263 | */ |
||
264 | public function setCredentials(array $credentials) |
||
269 | |||
270 | /** |
||
271 | * (non-PHPdoc) |
||
272 | * @see \JaegerApp\Db\DbInterface::getDb() |
||
273 | */ |
||
274 | public function getDb($force = false) |
||
291 | |||
292 | /** |
||
293 | * |
||
294 | * @param unknown $db_name |
||
295 | */ |
||
296 | public function setDbName($db_name) |
||
302 | |||
303 | /** |
||
304 | * Takes the WHERE array clause and prepairs it for use |
||
305 | * @param array $arrayPair |
||
306 | * @param string $glue |
||
307 | */ |
||
308 | protected function parseArrayPair($arrayPair, $glue = ',') |
||
417 | } |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: