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 |
||
8 | class Extension extends \Twig_Extension |
||
9 | { |
||
10 | /** |
||
11 | * The token generator. |
||
12 | * |
||
13 | * @var callable |
||
14 | */ |
||
15 | protected $tokenGenerator; |
||
16 | |||
17 | /** |
||
18 | * The token name. |
||
19 | * |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $tokenName; |
||
23 | |||
24 | /** |
||
25 | * Create a new Extension. |
||
26 | * |
||
27 | * @param callable $tokenGenerator the token generator |
||
28 | * @param string $tokenName the token name |
||
29 | */ |
||
30 | public function __construct(callable $tokenGenerator, $tokenName = 'X-XSRF-TOKEN') |
||
35 | |||
36 | /** |
||
37 | * Returns the name of the extension. |
||
38 | * |
||
39 | * @return string The extension name |
||
40 | */ |
||
41 | public function getName() |
||
45 | |||
46 | /** |
||
47 | * Returns a list of functions to add to the existing list. |
||
48 | * |
||
49 | * @return array An array of functions |
||
50 | */ |
||
51 | public function getFunctions() |
||
68 | |||
69 | /** |
||
70 | * Returns the token name. |
||
71 | * |
||
72 | * @return string |
||
73 | */ |
||
74 | public function getTokenName() |
||
78 | |||
79 | /** |
||
80 | * Generate a new token. |
||
81 | * |
||
82 | * @return mixed |
||
83 | */ |
||
84 | public function generateCsrfToken() |
||
88 | |||
89 | /** |
||
90 | * Generate a new csrf input widget. |
||
91 | * |
||
92 | * @param \Twig_Environment $env twig environment needed for escaping |
||
93 | * |
||
94 | * @return string |
||
95 | */ |
||
96 | View Code Duplication | public function generateInputWidget(\Twig_Environment $env) |
|
103 | |||
104 | /** |
||
105 | * Generate a new csrf meta widget. |
||
106 | * |
||
107 | * @param \Twig_Environment $env twig environment needed for escaping |
||
108 | * |
||
109 | * @return string |
||
110 | */ |
||
111 | View Code Duplication | public function generateMetaWidget(\Twig_Environment $env) |
|
118 | } |
||
119 |