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 |
||
13 | class PluginTest extends PluginTestCase |
||
14 | { |
||
15 | /** |
||
16 | * Return Twig environment |
||
17 | * |
||
18 | * @return Twig_Environment |
||
19 | */ |
||
20 | private function getTwig() |
||
21 | { |
||
22 | return App::make('twig.environment'); |
||
23 | } |
||
24 | |||
25 | View Code Duplication | public function testTemplateFromStringFunction() |
|
26 | { |
||
27 | $twig = $this->getTwig(); |
||
28 | |||
29 | $template = "{% set name = 'John' %}"; |
||
30 | $template .= '{{ include(template_from_string("Hello {{ name }}")) }}'; |
||
31 | |||
32 | $twigTemplate = $twig->createTemplate($template); |
||
33 | $this->assertEquals($twigTemplate->render([]), 'Hello John'); |
||
34 | } |
||
35 | |||
36 | View Code Duplication | public function testTruncateFilterForFive() |
|
37 | { |
||
38 | $twig = $this->getTwig(); |
||
39 | |||
40 | $template = "{{ 'Gordon Freeman' | truncate(5) }}"; |
||
41 | |||
42 | $twigTemplate = $twig->createTemplate($template); |
||
43 | $this->assertEquals($twigTemplate->render([]), 'Gordo...'); |
||
44 | } |
||
45 | |||
46 | View Code Duplication | public function testTruncateFilterForDefault() |
|
47 | { |
||
48 | $twig = $this->getTwig(); |
||
49 | |||
50 | $template = "{{ 'Lorem ipsum dolor sit amet, consectetur adipiscing elit' | truncate }}"; |
||
51 | |||
52 | $twigTemplate = $twig->createTemplate($template); |
||
53 | $this->assertEquals($twigTemplate->render([]), 'Lorem ipsum dolor sit amet, co...'); |
||
54 | } |
||
55 | |||
56 | View Code Duplication | public function testTruncateFilterWithSeparator() |
|
57 | { |
||
58 | $twig = $this->getTwig(); |
||
59 | |||
60 | $template = "{{ 'Gordon Freeman' | truncate(5, false, '-') }}"; |
||
61 | |||
62 | $twigTemplate = $twig->createTemplate($template); |
||
63 | $this->assertEquals($twigTemplate->render([]), 'Gordo-'); |
||
64 | } |
||
65 | |||
66 | View Code Duplication | public function testWordWrapFilter() |
|
67 | { |
||
68 | $twig = $this->getTwig(); |
||
69 | |||
70 | $template = "{{ 'Lorem ipsum dolor sit amet, consectetur adipiscing elit' | wordwrap(10) }}"; |
||
71 | |||
72 | $twigTemplate = $twig->createTemplate($template); |
||
73 | $this->assertEquals($twigTemplate->render([]), "Lorem ipsu\nm dolor si\nt amet, co\nnsectetur \nadipiscing\n elit"); |
||
74 | } |
||
75 | |||
76 | public function testShuffleFilter() |
||
77 | { |
||
78 | $twig = $this->getTwig(); |
||
79 | |||
80 | $template = "{{ [1, 2, 3] | shuffle }}"; |
||
81 | |||
82 | $twigTemplate = $twig->createTemplate($template); |
||
83 | $this->setExpectedException('Twig_Error_Runtime', 'Array to string conversion'); |
||
84 | $twigTemplate->render([]); |
||
85 | } |
||
86 | |||
87 | View Code Duplication | public function testShuffleFilterForeach() |
|
96 | |||
97 | public function testTimeDiffFunction() |
||
98 | { |
||
99 | $twig = $this->getTwig(); |
||
100 | |||
101 | $now = Carbon::now()->subMinute(); |
||
102 | $template = "{{ '" . $now->format('Y-m-d H:i:s') . "' | time_diff }}"; |
||
103 | |||
104 | $twigTemplate = $twig->createTemplate($template); |
||
105 | $this->assertEquals($twigTemplate->render([]), '1 minute ago'); |
||
106 | } |
||
107 | |||
108 | View Code Duplication | public function testStrftimeFunction() |
|
109 | { |
||
110 | $twig = $this->getTwig(); |
||
111 | |||
112 | $template = "{{ '2016-03-24 23:05' | strftime('%d.%m.%Y %H:%M:%S') }}"; |
||
113 | |||
114 | $twigTemplate = $twig->createTemplate($template); |
||
115 | $this->assertEquals($twigTemplate->render([]), '24.03.2016 23:05:00'); |
||
116 | } |
||
117 | |||
118 | View Code Duplication | public function testUppercaseFunction() |
|
119 | { |
||
120 | $twig = $this->getTwig(); |
||
121 | |||
122 | $template = "{{ 'Jack' | uppercase }}"; |
||
123 | |||
124 | $twigTemplate = $twig->createTemplate($template); |
||
125 | $this->assertEquals($twigTemplate->render([]), 'JACK'); |
||
126 | } |
||
127 | |||
128 | View Code Duplication | public function testLowercaseFunction() |
|
129 | { |
||
130 | $twig = $this->getTwig(); |
||
131 | |||
132 | $template = "{{ 'JACK' | lowercase }}"; |
||
133 | |||
134 | $twigTemplate = $twig->createTemplate($template); |
||
135 | $this->assertEquals($twigTemplate->render([]), 'jack'); |
||
136 | } |
||
137 | |||
138 | View Code Duplication | public function testUcfirstFunction() |
|
139 | { |
||
140 | $twig = $this->getTwig(); |
||
141 | |||
142 | $template = "{{ 'jack' | ucfirst }}"; |
||
143 | |||
144 | $twigTemplate = $twig->createTemplate($template); |
||
145 | $this->assertEquals($twigTemplate->render([]), 'Jack'); |
||
146 | } |
||
147 | |||
148 | View Code Duplication | public function testLcfirstFunction() |
|
149 | { |
||
150 | $twig = $this->getTwig(); |
||
151 | |||
152 | $template = "{{ 'JACK' | lcfirst }}"; |
||
153 | |||
154 | $twigTemplate = $twig->createTemplate($template); |
||
155 | $this->assertEquals($twigTemplate->render([]), 'jACK'); |
||
156 | } |
||
157 | |||
158 | View Code Duplication | public function testLtrimFunction() |
|
159 | { |
||
160 | $twig = $this->getTwig(); |
||
161 | |||
162 | $template = "{{ ' jack' | ltrim }}"; |
||
163 | |||
164 | $twigTemplate = $twig->createTemplate($template); |
||
165 | $this->assertEquals($twigTemplate->render([]), 'jack'); |
||
166 | } |
||
167 | |||
168 | View Code Duplication | public function testRtrimFunction() |
|
169 | { |
||
170 | $twig = $this->getTwig(); |
||
171 | |||
172 | $template = "{{ 'jack ' | rtrim }}"; |
||
173 | |||
174 | $twigTemplate = $twig->createTemplate($template); |
||
175 | $this->assertEquals($twigTemplate->render([]), 'jack'); |
||
176 | } |
||
177 | |||
178 | View Code Duplication | public function testStrRepeatFunction() |
|
179 | { |
||
180 | $twig = $this->getTwig(); |
||
181 | |||
182 | $template = "{{ ' best' | str_repeat(3) }}"; |
||
183 | |||
184 | $twigTemplate = $twig->createTemplate($template); |
||
185 | $this->assertEquals($twigTemplate->render([]), ' best best best'); |
||
186 | } |
||
187 | |||
188 | View Code Duplication | public function testPluralFunction() |
|
189 | { |
||
190 | $twig = $this->getTwig(); |
||
191 | |||
192 | $template = "{{ 'mail' | plural(count) }}"; |
||
193 | |||
194 | $twigTemplate = $twig->createTemplate($template); |
||
195 | $this->assertEquals($twigTemplate->render([]), 'mails'); |
||
196 | } |
||
197 | |||
198 | View Code Duplication | public function testStrpadFunction() |
|
207 | |||
208 | View Code Duplication | public function testLeftpadFunction() |
|
217 | |||
218 | View Code Duplication | public function testRightpadFunction() |
|
227 | |||
228 | public function testVardumpFunction() |
||
229 | { |
||
230 | $twig = $this->getTwig(); |
||
231 | |||
232 | $template = "{{ var_dump('test') }}"; |
||
233 | |||
234 | $twigTemplate = $twig->createTemplate($template); |
||
235 | $this->assertContains('string(4) "test"', $twigTemplate->render([])); |
||
236 | } |
||
237 | |||
238 | View Code Duplication | public function testVardumpFilter() |
|
239 | { |
||
240 | $twig = $this->getTwig(); |
||
241 | |||
242 | $template = "{{ 'test' | var_dump }}"; |
||
243 | |||
244 | $twigTemplate = $twig->createTemplate($template); |
||
245 | $this->assertContains('string(4) "test"', $twigTemplate->render([])); |
||
247 | |||
248 | public function testConfigFunction() |
||
260 | |||
261 | View Code Duplication | public function testSessionFunction() |
|
272 | |||
273 | View Code Duplication | public function testTransFunction() |
|
283 | } |
||
284 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.