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 |
||
18 | class TransformableSubscriber extends MappedEventSubscriber |
||
19 | { |
||
20 | const TRANSFORMABLE = 'transformable'; |
||
21 | |||
22 | const FUNCTION_TRANSFORM = 'transform'; |
||
23 | const FUNCTION_REVERSE_TRANSFORM = 'reverseTransform'; |
||
24 | |||
25 | const TYPE_TRANSFORMED = 'transformed'; |
||
26 | const TYPE_PLAIN = 'plain'; |
||
27 | |||
28 | /** |
||
29 | * @var TransformerPool |
||
30 | */ |
||
31 | protected $transformerPool; |
||
32 | |||
33 | /** |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $entityFieldValues = []; |
||
37 | |||
38 | /** |
||
39 | * TransformableListener constructor. |
||
40 | * @param TransformerPool $transformerPool |
||
41 | */ |
||
42 | 3 | public function __construct(TransformerPool $transformerPool) |
|
47 | |||
48 | /** |
||
49 | * @return array |
||
50 | */ |
||
51 | 3 | public function getSubscribedEvents() |
|
61 | |||
62 | /** |
||
63 | * @param EventArgs $eventArgs |
||
64 | * @return void |
||
65 | */ |
||
66 | 2 | public function loadClassMetadata(EventArgs $eventArgs) |
|
71 | |||
72 | /** |
||
73 | * @param EventArgs $args |
||
74 | */ |
||
75 | 1 | public function onFlush(EventArgs $args) |
|
79 | |||
80 | /** |
||
81 | * @param EventArgs $args |
||
82 | */ |
||
83 | 1 | public function postPersist(EventArgs $args) |
|
87 | |||
88 | /** |
||
89 | * @param EventArgs $args |
||
90 | */ |
||
91 | public function postLoad(EventArgs $args) |
||
95 | |||
96 | /** |
||
97 | * @param EventArgs $args |
||
98 | */ |
||
99 | public function postUpdate(EventArgs $args) |
||
103 | |||
104 | /** |
||
105 | * @param EventArgs $args |
||
106 | */ |
||
107 | 1 | protected function transform(EventArgs $args) |
|
121 | |||
122 | /** |
||
123 | * @param EventArgs $args |
||
124 | */ |
||
125 | 1 | protected function reverseTransform(EventArgs $args) |
|
132 | |||
133 | /** |
||
134 | * @param AdapterInterface $ea |
||
135 | * @param ObjectManager $om |
||
136 | * @param UnitOfWork $uow |
||
137 | * @param object $entity |
||
138 | * @param string $method |
||
139 | */ |
||
140 | 1 | protected function handle(AdapterInterface $ea, ObjectManager $om, UnitOfWork $uow, $entity, $method) |
|
155 | |||
156 | /** |
||
157 | * @param $entity |
||
158 | * @param $method |
||
159 | * @param $column |
||
160 | * @param $meta |
||
161 | */ |
||
162 | 1 | protected function handleField($entity, $method, $column, $meta) |
|
176 | |||
177 | /** |
||
178 | * @param $oid |
||
179 | * @param $field |
||
180 | * @param $transformerName |
||
181 | * @param $method |
||
182 | * @param $value |
||
183 | * @return mixed |
||
184 | */ |
||
185 | 1 | protected function getNewValue($oid, $field, $transformerName, $method, $value) |
|
194 | |||
195 | /** |
||
196 | * @param $transformerName |
||
197 | * @param $method |
||
198 | * @param $oldValue |
||
199 | * @return mixed |
||
200 | */ |
||
201 | 1 | protected function performTransformerOperation($transformerName, $method, $oldValue) |
|
205 | |||
206 | /** |
||
207 | * @param $oid |
||
208 | * @param $field |
||
209 | * @return null |
||
210 | */ |
||
211 | 1 | View Code Duplication | protected function getOriginalPlainFieldValue($oid, $field) |
219 | |||
220 | /** |
||
221 | * @param $oid |
||
222 | * @param $field |
||
223 | * @return mixed |
||
224 | */ |
||
225 | View Code Duplication | protected function getOriginalTransformedFieldValue($oid, $field) |
|
233 | |||
234 | /** |
||
235 | * @param $oid |
||
236 | * @param $field |
||
237 | * @return array|null |
||
238 | */ |
||
239 | 1 | protected function getFieldData($oid, $field) |
|
246 | |||
247 | /** |
||
248 | * @param $oid |
||
249 | * @param $field |
||
250 | * @param $transformed |
||
251 | * @param $plain |
||
252 | */ |
||
253 | 1 | protected function storeOriginalFieldData($oid, $field, $transformed, $plain) |
|
260 | |||
261 | /** |
||
262 | * @param $name |
||
263 | * @return TransformerInterface |
||
264 | */ |
||
265 | 1 | protected function getTransformer($name) |
|
269 | |||
270 | /** |
||
271 | * {@inheritDoc} |
||
272 | */ |
||
273 | 3 | protected function getNamespace() |
|
277 | } |
||
278 |
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: