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 |
||
14 | class TwigStakxBridge implements TemplateBridgeInterface |
||
15 | { |
||
16 | private $twig; |
||
17 | private $logger; |
||
18 | |||
19 | /** @var \Twig_Profiler_Profile */ |
||
20 | private $profiler; |
||
21 | |||
22 | 76 | public function __construct(\Twig_Environment $twig) |
|
26 | |||
27 | /** |
||
28 | * {@inheritdoc} |
||
29 | */ |
||
30 | public function setLogger(LoggerInterface $logger) |
||
34 | |||
35 | /** |
||
36 | * {@inheritdoc} |
||
37 | */ |
||
38 | 12 | public function setGlobalVariable($key, $value) |
|
42 | |||
43 | /** |
||
44 | * {@inheritdoc} |
||
45 | */ |
||
46 | 12 | public function createTemplate($templateContent) |
|
47 | { |
||
48 | try |
||
49 | { |
||
50 | 12 | $template = $this->twig->createTemplate($templateContent); |
|
51 | } |
||
52 | 12 | catch (\Twig_Error $e) |
|
53 | { |
||
54 | throw new TwigError($e); |
||
55 | } |
||
56 | |||
57 | 12 | return new TwigTemplate($template); |
|
58 | } |
||
59 | |||
60 | /** |
||
61 | * {@inheritdoc} |
||
62 | */ |
||
63 | 21 | View Code Duplication | public function getAssortmentDependencies($namespace, $bodyContent) |
75 | |||
76 | /** |
||
77 | * {@inheritdoc} |
||
78 | */ |
||
79 | 7 | View Code Duplication | public function getTemplateImportDependencies($bodyContent) |
80 | { |
||
81 | 7 | $regex = "/{%\s?(?:import|from|include)\s?['\"](.+)['\"].+/"; |
|
82 | 7 | $results = []; |
|
83 | |||
84 | 7 | preg_match_all($regex, $bodyContent, $results); |
|
85 | |||
86 | 7 | if (empty($results[1])) |
|
87 | 7 | { |
|
88 | return []; |
||
89 | } |
||
90 | |||
91 | 7 | return array_unique($results[1]); |
|
92 | } |
||
93 | |||
94 | /** |
||
95 | * {@inheritdoc} |
||
96 | */ |
||
97 | public function hasProfiler() |
||
101 | |||
102 | /** |
||
103 | * {@inheritdoc} |
||
104 | */ |
||
105 | 48 | public function setProfiler($profiler) |
|
109 | |||
110 | /** |
||
111 | * {@inheritdoc} |
||
112 | */ |
||
113 | public function getProfilerOutput(Compiler $compiler) |
||
120 | } |
||
121 |
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.