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 |
||
23 | class SymfonyPort |
||
24 | { |
||
25 | /** |
||
26 | * @param \DOMDocument $dom |
||
27 | * @param MessageCatalogue $catalogue |
||
28 | * @param string $domain |
||
29 | */ |
||
30 | 3 | public function extractXliff2(\DOMDocument $dom, MessageCatalogue $catalogue, $domain) |
|
70 | |||
71 | /** |
||
72 | * Convert a UTF8 string to the specified encoding. |
||
73 | * |
||
74 | * @param string $content String to decode |
||
75 | * @param string $encoding Target encoding |
||
76 | * |
||
77 | * @return string |
||
78 | */ |
||
79 | 3 | private function utf8ToCharset($content, $encoding = null) |
|
87 | |||
88 | /** |
||
89 | * Gets xliff file version based on the root "version" attribute. |
||
90 | * Defaults to 1.2 for backwards compatibility. |
||
91 | * |
||
92 | * @param \DOMDocument $dom |
||
93 | * |
||
94 | * @throws InvalidArgumentException |
||
95 | * |
||
96 | * @return string |
||
97 | * |
||
98 | * @deprecated Will be removed when we drop support for SF2.7 |
||
99 | */ |
||
100 | 10 | public function getVersionNumber(\DOMDocument $dom) |
|
101 | { |
||
102 | /** @var \DOMNode $xliff */ |
||
103 | 10 | foreach ($dom->getElementsByTagName('xliff') as $xliff) { |
|
104 | 10 | $version = $xliff->attributes->getNamedItem('version'); |
|
105 | 10 | if ($version) { |
|
106 | 10 | return $version->nodeValue; |
|
107 | } |
||
108 | |||
109 | $namespace = $xliff->attributes->getNamedItem('xmlns'); |
||
110 | if ($namespace) { |
||
111 | if (0 !== substr_compare('urn:oasis:names:tc:xliff:document:', $namespace->nodeValue, 0, 34)) { |
||
112 | throw new InvalidArgumentException(sprintf('Not a valid XLIFF namespace "%s"', $namespace)); |
||
113 | } |
||
114 | |||
115 | return substr($namespace, 34); |
||
116 | } |
||
117 | } |
||
118 | |||
119 | // Falls back to v1.2 |
||
120 | return '1.2'; |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Extract messages and metadata from DOMDocument into a MessageCatalogue. |
||
125 | * |
||
126 | * @param \DOMDocument $dom Source to extract messages and metadata |
||
127 | * @param MessageCatalogue $catalogue Catalogue where we'll collect messages and metadata |
||
128 | * @param string $domain The domain |
||
129 | * |
||
130 | * @deprecated Will be removed when we drop support for SF2.7 |
||
131 | */ |
||
132 | public function extractXliff1(\DOMDocument $dom, MessageCatalogue $catalogue, $domain) |
||
171 | |||
172 | /** |
||
173 | * @param \SimpleXMLElement|null $noteElement |
||
174 | * @param string|null $encoding |
||
175 | * |
||
176 | * @return array |
||
177 | * |
||
178 | * @deprecated Will be removed when we drop support for SF2.7 |
||
179 | */ |
||
180 | private function parseNotesMetadata(\SimpleXMLElement $noteElement = null, $encoding = null) |
||
205 | } |
||
206 |
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.