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 |
||
22 | class Audio extends OpenElement implements FlowContent, PhrasingContent, EmbeddedContent, InteractiveContent, TransparentElement, MediaElement |
||
23 | { |
||
24 | 1 | protected function getAllowedAttributes() |
|
25 | { |
||
26 | $audioAllowedAttributes = array( |
||
27 | 1 | '/^src$/i' => Attribute::URI, |
|
28 | '/^crossorigin$/i' => Attribute::CS_STRING, |
||
29 | '/^preload$/i' => Attribute::CI_ENUM . '("","none","metadata","auto"|"")', |
||
30 | '/^autoplay$/i' => Attribute::BOOL, |
||
31 | '/^mediagroup$/i' => Attribute::CS_STRING, |
||
32 | '/^loop$/i' => Attribute::BOOL, |
||
33 | '/^muted$/i' => Attribute::BOOL, |
||
34 | '/^controls$/i' => Attribute::BOOL |
||
35 | ); |
||
36 | |||
37 | 1 | return array_merge( |
|
38 | 1 | $audioAllowedAttributes, |
|
39 | 1 | parent::getAllowedAttributes() |
|
40 | ); |
||
41 | } |
||
42 | |||
43 | 3 | View Code Duplication | protected function removeInvalidChildren(LoggerInterface $logger) |
|
|||
44 | { |
||
45 | 3 | $hasSrc = $this->hasAttribute('src'); |
|
46 | 3 | foreach ($this->children as $child) { |
|
47 | 3 | if ($child instanceof NonParticipating || |
|
48 | 3 | $child instanceof Text) { |
|
49 | 1 | continue; |
|
50 | } |
||
51 | |||
52 | 3 | if (!$hasSrc && $child instanceof Source) { |
|
53 | 1 | continue; |
|
54 | } |
||
55 | |||
56 | 2 | if ($child instanceof Track) { |
|
57 | 1 | continue; |
|
58 | } |
||
59 | |||
60 | 2 | if ($child instanceof TransparentElement && |
|
61 | 2 | $child->isTransparentElement()) { |
|
62 | 1 | continue; |
|
63 | } |
||
64 | |||
65 | 1 | $logger->debug('Removing ' . $child . '. Only text, "source", "track", and transparent elements allowed as children of "audio" element.'); |
|
66 | 1 | $this->removeChild($child); |
|
67 | } |
||
68 | 3 | } |
|
69 | |||
70 | 1 | public function isInteractiveContent() : bool |
|
74 | |||
75 | 1 | public function isTransparentElement() : bool |
|
80 | } |
||
81 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.