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 |
||
29 | class PDO extends BasePDO implements PDOInterface |
||
30 | { |
||
31 | const VERSION = '0.3.1'; |
||
32 | const DRIVER_NAME = 'crate'; |
||
33 | |||
34 | const DSN_REGEX = '/^(?:crate)?(?::([\w\d\.-]+:\d+))+\/?([\w]+)?$/'; |
||
35 | |||
36 | const CRATE_ATTR_HTTP_BASIC_AUTH = 1000; |
||
37 | /** |
||
38 | * @deprecated |
||
39 | */ |
||
40 | const ATTR_HTTP_BASIC_AUTH = CRATE_ATTR_HTTP_BASIC_AUTH; |
||
41 | |||
42 | const CRATE_ATTR_DEFAULT_SCHEMA = 1001; |
||
43 | |||
44 | const PARAM_FLOAT = 6; |
||
45 | const PARAM_DOUBLE = 7; |
||
46 | const PARAM_LONG = 8; |
||
47 | const PARAM_ARRAY = 9; |
||
48 | const PARAM_OBJECT = 10; |
||
49 | const PARAM_TIMESTAMP = 11; |
||
50 | const PARAM_IP = 12; |
||
51 | |||
52 | /** |
||
53 | * @var array |
||
54 | */ |
||
55 | private $attributes = [ |
||
56 | 'defaultFetchMode' => self::FETCH_BOTH, |
||
57 | 'errorMode' => self::ERRMODE_SILENT, |
||
58 | 'statementClass' => 'Crate\PDO\PDOStatement', |
||
59 | 'timeout' => 5.0, |
||
60 | 'auth' => [], |
||
61 | 'defaultSchema' => 'doc' |
||
62 | ]; |
||
63 | |||
64 | /** |
||
65 | * @var Http\ClientInterface |
||
66 | */ |
||
67 | private $client; |
||
68 | |||
69 | /** |
||
70 | * @var PDOStatement|null |
||
71 | */ |
||
72 | private $lastStatement; |
||
73 | |||
74 | /** |
||
75 | * @var callable |
||
76 | */ |
||
77 | private $request; |
||
78 | |||
79 | /** |
||
80 | * {@inheritDoc} |
||
81 | * |
||
82 | * @param string $dsn The HTTP endpoint to call |
||
83 | * @param null $username Unused |
||
84 | * @param null $passwd Unused |
||
85 | * @param null|array $options Attributes to set on the PDO |
||
86 | */ |
||
87 | public function __construct($dsn, $username, $passwd, $options) |
||
136 | |||
137 | /** |
||
138 | * Extract host:port pairs out of the DSN string |
||
139 | * |
||
140 | * @param string $dsn The DSN string |
||
141 | * |
||
142 | * @return array An array of host:port strings |
||
143 | */ |
||
144 | private static function parseDSN($dsn) |
||
154 | |||
155 | /** |
||
156 | * Compute a URI for usage with the HTTP client |
||
157 | * |
||
158 | * @param string $server A host:port string |
||
159 | * |
||
160 | * @return string An URI which can be used by the HTTP client |
||
161 | */ |
||
162 | private static function computeURI($server) |
||
166 | |||
167 | /** |
||
168 | * {@inheritDoc} |
||
169 | */ |
||
170 | public function prepare($statement, $options = null) |
||
181 | |||
182 | /** |
||
183 | * {@inheritDoc} |
||
184 | */ |
||
185 | public function beginTransaction() |
||
189 | |||
190 | /** |
||
191 | * {@inheritDoc} |
||
192 | */ |
||
193 | public function commit() |
||
197 | |||
198 | /** |
||
199 | * {@inheritDoc} |
||
200 | */ |
||
201 | public function rollBack() |
||
205 | |||
206 | /** |
||
207 | * {@inheritDoc} |
||
208 | */ |
||
209 | public function inTransaction() |
||
213 | |||
214 | /** |
||
215 | * {@inheritDoc} |
||
216 | */ |
||
217 | public function exec($statement) |
||
224 | |||
225 | /** |
||
226 | * {@inheritDoc} |
||
227 | */ |
||
228 | public function query($statement) |
||
235 | |||
236 | /** |
||
237 | * {@inheritDoc} |
||
238 | */ |
||
239 | public function lastInsertId($name = null) |
||
243 | |||
244 | /** |
||
245 | * {@inheritDoc} |
||
246 | */ |
||
247 | public function errorCode() |
||
251 | |||
252 | /** |
||
253 | * {@inheritDoc} |
||
254 | */ |
||
255 | public function errorInfo() |
||
259 | |||
260 | /** |
||
261 | * {@inheritDoc} |
||
262 | */ |
||
263 | public function setAttribute($attribute, $value) |
||
300 | |||
301 | /** |
||
302 | * {@inheritDoc} |
||
303 | */ |
||
304 | public function getAttribute($attribute) |
||
352 | |||
353 | /** |
||
354 | * {@inheritDoc} |
||
355 | */ |
||
356 | public function quote($string, $parameter_type = PDO::PARAM_STR) |
||
378 | |||
379 | /** |
||
380 | * {@inheritDoc} |
||
381 | */ |
||
382 | public static function getAvailableDrivers() |
||
386 | } |
||
387 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.