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 PrefixedTranslator implements ITranslator |
||
23 | { |
||
24 | |||
25 | use Kdyby\StrictObjects\Scream; |
||
26 | |||
27 | /** |
||
28 | * @var \Kdyby\Translation\ITranslator|\Kdyby\Translation\Translator|\Kdyby\Translation\PrefixedTranslator |
||
29 | */ |
||
30 | private $translator; |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | private $prefix; |
||
36 | |||
37 | |||
38 | |||
39 | /** |
||
40 | * @param string $prefix |
||
41 | * @param \Kdyby\Translation\ITranslator $translator |
||
42 | * @throws \Kdyby\Translation\InvalidArgumentException |
||
43 | */ |
||
44 | public function __construct($prefix, ITranslator $translator) |
||
60 | |||
61 | |||
62 | |||
63 | /** |
||
64 | * @param string|\Kdyby\Translation\Phrase $message |
||
65 | * @param int|array|NULL $count |
||
66 | * @param array|string|NULL $parameters |
||
67 | * @param string|NULL $domain |
||
68 | * @param string|NULL $locale |
||
69 | * @return string|\Nette\Utils\IHtmlString|\Latte\Runtime\IHtmlString |
||
70 | */ |
||
71 | public function translate($message, $count = NULL, $parameters = [], $domain = NULL, $locale = NULL) |
||
94 | |||
95 | |||
96 | |||
97 | /** |
||
98 | * @return \Kdyby\Translation\ITranslator |
||
99 | */ |
||
100 | public function unwrap() |
||
104 | |||
105 | |||
106 | |||
107 | /** |
||
108 | * @param Latte\Runtime\Template $template |
||
109 | * @return \Kdyby\Translation\ITranslator |
||
110 | */ |
||
111 | public function unregister(Latte\Runtime\Template $template) |
||
117 | |||
118 | |||
119 | |||
120 | /** |
||
121 | * @param Latte\Runtime\Template $template |
||
122 | * @param string $prefix |
||
123 | * @throws \Kdyby\Translation\InvalidArgumentException |
||
124 | * @return \Kdyby\Translation\ITranslator |
||
125 | */ |
||
126 | public static function register(Latte\Runtime\Template $template, $prefix) |
||
132 | |||
133 | |||
134 | |||
135 | private static function overrideTemplateTranslator(Latte\Runtime\Template $template, ITranslator $translator) |
||
139 | |||
140 | } |
||
141 |
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.