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 |
||
33 | class Driver extends DriverAbstract |
||
34 | { |
||
35 | /** |
||
36 | * the connection link |
||
37 | * |
||
38 | * @var \PDO |
||
39 | * @access protected |
||
40 | */ |
||
41 | protected $link; |
||
42 | |||
43 | /** |
||
44 | * Default PDO attributes |
||
45 | * |
||
46 | * @var array |
||
47 | * @access protected |
||
48 | */ |
||
49 | protected $attributes = [ |
||
50 | 'PDO::ATTR_ERRMODE' => \PDO::ERRMODE_SILENT, |
||
51 | 'PDO::ATTR_CASE' => \PDO::CASE_NATURAL, |
||
52 | 'PDO::ATTR_ORACLE_NULLS' => \PDO::NULL_NATURAL, |
||
53 | 'PDO::ATTR_DEFAULT_FETCH_MODE' => \PDO::FETCH_ASSOC, |
||
54 | 'PDO::ATTR_EMULATE_PREPARES' => false, |
||
55 | ]; |
||
56 | |||
57 | /** |
||
58 | * Driver constructor |
||
59 | * |
||
60 | * @param array $connectInfo |
||
61 | * @param StatementInterface $statementPrototype |
||
62 | * @throws InvalidArgumentException if link type not right |
||
63 | * @throws LogicException driver specific extension not loaded |
||
64 | * @access public |
||
65 | */ |
||
66 | public function __construct( |
||
75 | |||
76 | /** |
||
77 | * {@inheritDoc} |
||
78 | */ |
||
79 | protected function extensionLoaded()/*# : bool */ |
||
83 | |||
84 | /** |
||
85 | * {@inheritDoc} |
||
86 | */ |
||
87 | protected function realLastId($name) |
||
91 | |||
92 | /** |
||
93 | * {@inheritDoc} |
||
94 | */ |
||
95 | protected function realConnect(array $parameters) |
||
113 | |||
114 | /** |
||
115 | * Disconnect the \PDO link |
||
116 | * |
||
117 | * {@inheritDoc} |
||
118 | */ |
||
119 | protected function realDisconnect() |
||
123 | |||
124 | /** |
||
125 | * {@inheritDoc} |
||
126 | */ |
||
127 | protected function realPing()/*# : bool */ |
||
135 | |||
136 | /** |
||
137 | * {@inheritDoc} |
||
138 | */ |
||
139 | View Code Duplication | protected function realSetAttribute(/*# string */ $attribute, $value) |
|
155 | |||
156 | /** |
||
157 | * {@inheritDoc} |
||
158 | */ |
||
159 | View Code Duplication | protected function realGetAttribute(/*# string */ $attribute) |
|
174 | |||
175 | /** |
||
176 | * {@inheritDoc} |
||
177 | */ |
||
178 | protected function realBegin() |
||
183 | |||
184 | /** |
||
185 | * {@inheritDoc} |
||
186 | */ |
||
187 | protected function realCommit() |
||
192 | |||
193 | /** |
||
194 | * {@inheritDoc} |
||
195 | */ |
||
196 | protected function realRollback() |
||
201 | } |
||
202 |
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.