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 |
||
25 | class TagcommanderExtension extends \Twig_Extension |
||
26 | { |
||
27 | /** |
||
28 | * @var ParameterBagInterface |
||
29 | */ |
||
30 | protected $datalayer; |
||
31 | |||
32 | /** |
||
33 | * @var EventDispatcherInterface |
||
34 | */ |
||
35 | protected $dispatcher; |
||
36 | |||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $tcVars; |
||
41 | |||
42 | /** |
||
43 | * @var array |
||
44 | */ |
||
45 | protected $events = array(); |
||
46 | |||
47 | /** |
||
48 | * @var array |
||
49 | */ |
||
50 | protected $containers = array(); |
||
51 | |||
52 | /** |
||
53 | * @var string |
||
54 | */ |
||
55 | protected $defaultEvent = null; |
||
56 | |||
57 | /** |
||
58 | * @var Serializer |
||
59 | */ |
||
60 | protected $serializer; |
||
61 | |||
62 | /** |
||
63 | * @var array |
||
64 | */ |
||
65 | static protected $twigFunctions = array( |
||
66 | 'tc_vars' => 'tcVars', |
||
67 | 'tc_container' => 'tcContainer', |
||
68 | 'tc_event' => 'tcEvent', |
||
69 | ); |
||
70 | |||
71 | /** |
||
72 | * @param ParameterBagInterface $datalayer |
||
73 | * @param EventDispatcherInterface $dispatcher |
||
74 | * @param string $tcVars |
||
75 | */ |
||
76 | 1 | public function __construct( |
|
89 | |||
90 | /** |
||
91 | * @return string|boolean|integer|double|null |
||
92 | */ |
||
93 | 1 | protected function serializeWithValues($values = array()) |
|
108 | |||
109 | /** |
||
110 | * @param Array $values |
||
111 | * @return string |
||
112 | */ |
||
113 | 1 | public function tcVars($values = array()) |
|
121 | |||
122 | /** |
||
123 | * @param array $event |
||
124 | * @param boolean $setAsDefault |
||
125 | * @return self |
||
126 | */ |
||
127 | 1 | public function addEvent($event, $setAsDefault = false) |
|
137 | |||
138 | /** |
||
139 | * @param string $eventName |
||
140 | * @param array $values |
||
141 | * @param string|null $tracker |
||
142 | * @return string |
||
143 | */ |
||
144 | 1 | public function tcEvent($eventName, $values = array(), $tracker = null) |
|
162 | |||
163 | /** |
||
164 | * @param array $container |
||
165 | * @return self |
||
166 | */ |
||
167 | 1 | public function addContainer($container) |
|
173 | |||
174 | 1 | private function containerHas($container, $parameterName) |
|
178 | |||
179 | 1 | View Code Duplication | private function containerSrc($containerName) |
188 | |||
189 | 1 | View Code Duplication | private function containerAlternative($containerName) |
201 | |||
202 | /** |
||
203 | * @param string $containerName |
||
204 | * @return string |
||
205 | */ |
||
206 | 1 | public function tcContainer($containerName) |
|
225 | |||
226 | /** |
||
227 | * @param string $twigName |
||
228 | * @param string $methodName |
||
229 | * @return \Twig_SimpleFunction |
||
230 | */ |
||
231 | 1 | private function makeFunction($twigName, $methodName) |
|
241 | |||
242 | /** |
||
243 | * @return \Twig_SimpleFunction[] |
||
244 | */ |
||
245 | 1 | public function getFunctions() |
|
253 | |||
254 | /** |
||
255 | * @return string |
||
256 | */ |
||
257 | 1 | public function getName() |
|
261 | } |
||
262 |
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.