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 |
||
26 | class Extension extends BaseExtension |
||
27 | { |
||
28 | /** |
||
29 | * {@inheritdoc} |
||
30 | */ |
||
31 | 1 | public function getAlias() |
|
35 | |||
36 | /** |
||
37 | * {@inheritdoc} |
||
38 | */ |
||
39 | 1 | public function getNamespace() |
|
43 | |||
44 | /** |
||
45 | * {@inheritdoc} |
||
46 | */ |
||
47 | public function getXsdValidationBasePath() |
||
51 | |||
52 | /** |
||
53 | * {@inheritdoc} |
||
54 | */ |
||
55 | public function load(array $config, ContainerBuilder $container) |
||
86 | |||
87 | /** |
||
88 | * Configure base currency. |
||
89 | * |
||
90 | * @param array $config Configuration parameters. |
||
91 | * @param ContainerBuilder $container Service container. |
||
92 | * @return Extension $this Fluent interface. |
||
93 | */ |
||
94 | protected function configureBaseCurrency(array $config, ContainerBuilder $container) |
||
101 | |||
102 | /** |
||
103 | * Configure rates. |
||
104 | * |
||
105 | * @param array $config Configuration parameters. |
||
106 | * @param ContainerBuilder $container Service container. |
||
107 | * @return Extension $this Fluent interface. |
||
108 | */ |
||
109 | protected function configureRates(array $config, ContainerBuilder $container) |
||
118 | |||
119 | /** |
||
120 | * Configure required processors. |
||
121 | * |
||
122 | * @param array $config Configuration parameters. |
||
123 | * @param ContainerBuilder $container Service container. |
||
124 | * @return Extension $this Fluent interface. |
||
125 | */ |
||
126 | protected function configureRepository(array $config, ContainerBuilder $container) |
||
131 | |||
132 | /** |
||
133 | * Configure file repository, if used. |
||
134 | * |
||
135 | * @param array $config Configuration parameters. |
||
136 | * @param ContainerBuilder $container Service container. |
||
137 | * @return Extension $this Fluent interface. |
||
138 | */ |
||
139 | protected function configureFileRepository(array $config, ContainerBuilder $container) |
||
144 | |||
145 | /** |
||
146 | * Configure controller. |
||
147 | * |
||
148 | * @param array $config Configuration parameters. |
||
149 | * @param ContainerBuilder $container Service container. |
||
150 | * @return Extension $this Fluent interface. |
||
151 | */ |
||
152 | protected function configureController(array $config, ContainerBuilder $container) |
||
166 | |||
167 | /** |
||
168 | * Configure simple sources which does not have to be explicitly added to service container. |
||
169 | * |
||
170 | * @param array $config Configuration parameters. |
||
171 | * @param ContainerBuilder $container Service container. |
||
172 | * @return Extension $this Fluent interface. |
||
173 | */ |
||
174 | protected function configureSimpleSources(array $config, ContainerBuilder $container) |
||
182 | |||
183 | /** |
||
184 | * Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\SourceType" default settings. |
||
185 | * |
||
186 | * @param array $config Configuration parameters. |
||
187 | * @param ContainerBuilder $container Service container. |
||
188 | * @return Extension $this Fluent interface. |
||
189 | */ |
||
190 | View Code Duplication | protected function configureSourceType(array $config, ContainerBuilder $container) |
|
202 | |||
203 | /** |
||
204 | * Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\RateTypeType" default settings. |
||
205 | * |
||
206 | * @param array $config Configuration parameters. |
||
207 | * @param ContainerBuilder $container Service container. |
||
208 | * @return Extension $this Fluent interface. |
||
209 | */ |
||
210 | protected function configureRateTypeType(array $config, ContainerBuilder $container) |
||
224 | |||
225 | /** |
||
226 | * Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\CurrencyCodeType" default settings. |
||
227 | * |
||
228 | * @param array $config Configuration parameters. |
||
229 | * @param ContainerBuilder $container Service container. |
||
230 | * @return Extension $this Fluent interface. |
||
231 | */ |
||
232 | protected function configureCurrencyCodeType(array $config, ContainerBuilder $container) |
||
248 | |||
249 | /** |
||
250 | * Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\ForeignCurrencyCodeType" default settings. |
||
251 | * |
||
252 | * @param array $config Configuration parameters. |
||
253 | * @param ContainerBuilder $container Service container. |
||
254 | * @return Extension $this Fluent interface. |
||
255 | */ |
||
256 | View Code Duplication | protected function configureForeignCurrencyCodeType(array $config, ContainerBuilder $container) |
|
270 | |||
271 | /** |
||
272 | * Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\RateType" default settings. |
||
273 | * |
||
274 | * @param array $config Configuration parameters. |
||
275 | * @param ContainerBuilder $container Service container. |
||
276 | * @return Extension $this Fluent interface. |
||
277 | */ |
||
278 | protected function configureRateType(array $config, ContainerBuilder $container) |
||
284 | |||
285 | /** |
||
286 | * Configure internal notifications. |
||
287 | * |
||
288 | * @param array $config Configuration parameters. |
||
289 | * @param ContainerBuilder $container Service container. |
||
290 | * @return Extension $this Fluent interface. |
||
291 | */ |
||
292 | protected function configureNotifications(array $config, ContainerBuilder $container) |
||
300 | } |
||
301 |
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.