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 TextExtension extends \Twig_Extension |
||
9 | { |
||
10 | /** |
||
11 | * Return extension name |
||
12 | * |
||
13 | * @return string |
||
14 | */ |
||
15 | public function getName() |
||
16 | { |
||
17 | return 'jasny/text'; |
||
18 | } |
||
19 | |||
20 | /** |
||
21 | * {@inheritdoc} |
||
22 | */ |
||
23 | 25 | public function getFilters() |
|
33 | |||
34 | /** |
||
35 | * Add paragraph and line breaks to text. |
||
36 | * |
||
37 | * @param string $value |
||
38 | * @return string |
||
39 | */ |
||
40 | 2 | public function paragraph($value) |
|
49 | |||
50 | /** |
||
51 | * Get a single line |
||
52 | * |
||
53 | * @param string $value |
||
54 | * @param int $line Line number (starts at 1) |
||
55 | * @return string |
||
56 | */ |
||
57 | 4 | public function line($value, $line = 1) |
|
67 | |||
68 | /** |
||
69 | * Cut of text on a pagebreak. |
||
70 | * |
||
71 | * @param string $value |
||
72 | * @param string $replace |
||
73 | * @param string $break |
||
74 | * @return string |
||
75 | */ |
||
76 | 4 | public function less($value, $replace = '...', $break = '<!-- pagebreak -->') |
|
85 | |||
86 | /** |
||
87 | * Cut of text if it's to long. |
||
88 | * |
||
89 | * @param string $value |
||
90 | * @param int $length |
||
91 | * @param string $replace |
||
92 | * @return string |
||
93 | */ |
||
94 | 4 | public function truncate($value, $length, $replace = '...') |
|
102 | |||
103 | /** |
||
104 | * Linkify a HTTP(S) link. |
||
105 | * |
||
106 | * @param string $protocol 'http' or 'https' |
||
107 | * @param string $text |
||
108 | * @param array $links OUTPUT |
||
109 | * @param string $attr |
||
110 | * @param string $mode |
||
111 | * @return string |
||
112 | */ |
||
113 | 6 | protected function linkifyHttp($protocol, $text, array &$links, $attr, $mode) |
|
127 | |||
128 | /** |
||
129 | * Linkify a mail link. |
||
130 | * |
||
131 | * @param string $text |
||
132 | * @param array $links OUTPUT |
||
133 | * @param string $attr |
||
134 | * @return string |
||
135 | */ |
||
136 | 5 | protected function linkifyMail($text, array &$links, $attr) |
|
145 | |||
146 | |||
147 | /** |
||
148 | * Linkify a link. |
||
149 | * |
||
150 | * @param string $protocol |
||
151 | * @param string $text |
||
152 | * @param array $links OUTPUT |
||
153 | * @param string $attr |
||
154 | * @param string $mode |
||
155 | * @return string |
||
156 | */ |
||
157 | 4 | protected function linkifyOther($protocol, $text, array &$links, $attr, $mode) |
|
172 | |||
173 | /** |
||
174 | * Turn all URLs in clickable links. |
||
175 | * |
||
176 | * @param string $value |
||
177 | * @param array $protocols 'http'/'https', 'mail' and also 'ftp', 'scp', 'tel', etc |
||
178 | * @param array $attributes HTML attributes for the link |
||
179 | * @param string $mode normal or all |
||
180 | * @return string |
||
181 | */ |
||
182 | 11 | public function linkify($value, $protocols = ['http', 'mail'], array $attributes = [], $mode = 'normal') |
|
216 | } |
||
217 |
It seems like you are assigning to a variable which was imported through a
use
statement which was not imported by reference.For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.
Change not visible in outer-scope
Change visible in outer-scope