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 |
||
23 | class CrudGeneratorGeneratorTest extends GeneratorTest |
||
24 | { |
||
25 | |||
26 | View Code Duplication | public function testGenerateYamlFull() |
|
|
|||
27 | { |
||
28 | $advancedConfig = new Configuration(); |
||
29 | $advancedConfig->setRoutePrefix('post'); |
||
30 | $advancedConfig->setFormat('yml'); |
||
31 | $advancedConfig->setOverwrite(true); |
||
32 | |||
33 | $this->getGenerator()->generateCrud($this->getBundle(), 'Post', $this->getMetadata(), $advancedConfig); |
||
34 | $files = array( |
||
35 | 'Controller/PostController.php', |
||
36 | 'Tests/Controller/PostControllerTest.php', |
||
37 | 'Resources/config/routing/post.yml', |
||
38 | 'Resources/views/post/index.html.twig', |
||
39 | 'Resources/views/post/show.html.twig', |
||
40 | 'Resources/views/post/new.html.twig', |
||
41 | 'Resources/views/post/edit.html.twig', |
||
42 | ); |
||
43 | foreach ($files as $file) { |
||
44 | $this->assertTrue(file_exists($this->tmpDir . '/' . $file), sprintf('%s has been generated', $file)); |
||
45 | } |
||
46 | $files = array( |
||
47 | 'Resources/config/routing/post.xml', |
||
48 | ); |
||
49 | foreach ($files as $file) { |
||
50 | $this->assertFalse(file_exists($this->tmpDir . '/' . $file), sprintf('%s has not been generated', $file)); |
||
51 | } |
||
52 | $content = file_get_contents($this->tmpDir . '/Controller/PostController.php'); |
||
53 | $strings = array( |
||
54 | 'namespace Foo\BarBundle\Controller;', |
||
55 | 'public function indexAction', |
||
56 | 'public function showAction', |
||
57 | 'public function newAction', |
||
58 | 'public function editAction', |
||
59 | ); |
||
60 | foreach ($strings as $string) { |
||
61 | $this->assertContains($string, $content); |
||
62 | } |
||
63 | |||
64 | |||
65 | $this->assertBulk(); |
||
66 | $this->assertPagination(); |
||
67 | $this->assertSorting(); |
||
68 | $this->assertPageSize(); |
||
69 | } |
||
70 | |||
71 | public function testGenerateXml() |
||
72 | { |
||
73 | $advancedConfig = new Configuration(); |
||
74 | $advancedConfig->setRoutePrefix('post'); |
||
75 | $advancedConfig->setFormat('xml'); |
||
76 | |||
77 | $this->getGenerator()->generateCrud($this->getBundle(), 'Post', $this->getMetadata(), $advancedConfig); |
||
78 | $files = array( |
||
79 | 'Controller/PostController.php', |
||
80 | 'Tests/Controller/PostControllerTest.php', |
||
81 | 'Resources/config/routing/post.xml', |
||
82 | 'Resources/views/post/index.html.twig', |
||
83 | 'Resources/views/post/show.html.twig', |
||
84 | 'Resources/views/post/new.html.twig', |
||
85 | 'Resources/views/post/edit.html.twig', |
||
86 | ); |
||
87 | foreach ($files as $file) { |
||
88 | $this->assertTrue(file_exists($this->tmpDir . '/' . $file), sprintf('%s has been generated', $file)); |
||
89 | } |
||
90 | $files = array( |
||
91 | 'Resources/config/routing/post.yml', |
||
92 | ); |
||
93 | foreach ($files as $file) { |
||
94 | $this->assertFalse(file_exists($this->tmpDir . '/' . $file), sprintf('%s has not been generated', $file)); |
||
95 | } |
||
96 | $content = file_get_contents($this->tmpDir . '/Controller/PostController.php'); |
||
97 | $strings = array( |
||
98 | 'namespace Foo\BarBundle\Controller;', |
||
99 | 'public function indexAction', |
||
100 | 'public function showAction', |
||
101 | ); |
||
102 | foreach ($strings as $string) { |
||
103 | $this->assertContains($string, $content); |
||
104 | } |
||
105 | $content = file_get_contents($this->tmpDir . '/Controller/PostController.php'); |
||
106 | $strings = array( |
||
107 | '@Route', |
||
108 | ); |
||
109 | foreach ($strings as $string) { |
||
110 | $this->assertNotContains($string, $content); |
||
111 | } |
||
112 | |||
113 | $this->assertBulk(); |
||
114 | $this->assertPagination(); |
||
115 | $this->assertSorting(); |
||
116 | $this->assertPageSize(); |
||
117 | } |
||
118 | |||
119 | View Code Duplication | public function testGenerateAnnotationWrite() |
|
120 | { |
||
121 | $advancedConfig = new Configuration(); |
||
122 | $advancedConfig->setFormat('annotation'); |
||
123 | $advancedConfig->setRoutePrefix('/post'); |
||
124 | $this->getGenerator()->generateCrud($this->getBundle(), 'Post', $this->getMetadata(), $advancedConfig); |
||
125 | $files = array( |
||
126 | 'Controller/PostController.php', |
||
127 | 'Tests/Controller/PostControllerTest.php', |
||
128 | 'Resources/views/post/index.html.twig', |
||
129 | 'Resources/views/post/show.html.twig', |
||
130 | 'Resources/views/post/new.html.twig', |
||
131 | 'Resources/views/post/edit.html.twig', |
||
132 | ); |
||
133 | foreach ($files as $file) { |
||
134 | $this->assertTrue(file_exists($this->tmpDir . '/' . $file), sprintf('%s has been generated', $file)); |
||
135 | } |
||
136 | $files = array( |
||
137 | 'Resources/config/routing/post.yml', |
||
138 | 'Resources/config/routing/post.xml', |
||
139 | ); |
||
140 | foreach ($files as $file) { |
||
141 | $this->assertFalse(file_exists($this->tmpDir . '/' . $file), sprintf('%s has not been generated', $file)); |
||
142 | } |
||
143 | $content = file_get_contents($this->tmpDir . '/Controller/PostController.php'); |
||
144 | $strings = array( |
||
145 | 'namespace Foo\BarBundle\Controller;', |
||
146 | 'public function indexAction', |
||
147 | 'public function showAction', |
||
148 | 'public function newAction', |
||
149 | 'public function editAction', |
||
150 | '@Route', |
||
151 | ); |
||
152 | foreach ($strings as $string) { |
||
153 | $this->assertContains($string, $content); |
||
154 | } |
||
155 | |||
156 | |||
157 | $this->assertBulk(); |
||
158 | $this->assertPagination(); |
||
159 | $this->assertSorting(); |
||
160 | $this->assertPageSize(); |
||
161 | } |
||
162 | |||
163 | View Code Duplication | public function testGenerateAnnotation() |
|
164 | { |
||
165 | $advancedConfig = new Configuration(); |
||
166 | $advancedConfig->setRoutePrefix('post'); |
||
167 | $advancedConfig->setFormat('annotation'); |
||
168 | $advancedConfig->setOverwrite(true); |
||
169 | |||
170 | $this->getGenerator()->generateCrud($this->getBundle(), 'Post', $this->getMetadata(), $advancedConfig); |
||
171 | $files = array( |
||
172 | 'Controller/PostController.php', |
||
173 | 'Tests/Controller/PostControllerTest.php', |
||
174 | 'Resources/views/post/index.html.twig', |
||
175 | 'Resources/views/post/show.html.twig', |
||
176 | 'Resources/views/post/new.html.twig', |
||
177 | 'Resources/views/post/edit.html.twig', |
||
178 | ); |
||
179 | foreach ($files as $file) { |
||
180 | $this->assertTrue(file_exists($this->tmpDir . '/' . $file), sprintf('%s has been generated', $file)); |
||
181 | } |
||
182 | $files = array( |
||
183 | 'Resources/config/routing/post.yml', |
||
184 | 'Resources/config/routing/post.xml', |
||
185 | ); |
||
186 | foreach ($files as $file) { |
||
187 | $this->assertFalse(file_exists($this->tmpDir . '/' . $file), sprintf('%s has not been generated', $file)); |
||
188 | } |
||
189 | $content = file_get_contents($this->tmpDir . '/Controller/PostController.php'); |
||
190 | $strings = array( |
||
191 | 'namespace Foo\BarBundle\Controller;', |
||
192 | 'public function indexAction', |
||
193 | 'public function showAction', |
||
194 | '@Route', |
||
195 | ); |
||
196 | foreach ($strings as $string) { |
||
197 | $this->assertContains($string, $content); |
||
198 | } |
||
199 | |||
200 | $this->assertBulk(); |
||
201 | $this->assertPagination(); |
||
202 | $this->assertSorting(); |
||
203 | $this->assertPageSize(); |
||
204 | } |
||
205 | |||
206 | public function testGenerateWithBaseTemplate() |
||
230 | |||
231 | public function testGenerateWithBundleViews() |
||
250 | |||
251 | |||
252 | /** |
||
253 | * @dataProvider getRoutePrefixes |
||
254 | */ |
||
255 | public function testGetRouteNamePrefix($original, $expected) |
||
260 | |||
261 | public function getRoutePrefixes() |
||
284 | |||
285 | /** |
||
286 | * |
||
287 | * @return PetkoparaCrudGenerator |
||
288 | */ |
||
289 | protected function getGenerator() |
||
295 | |||
296 | /** |
||
297 | * @return BundleInterface |
||
298 | */ |
||
299 | protected function getBundle() |
||
307 | |||
308 | /** |
||
309 | * @return ClassMetadataInfo |
||
310 | */ |
||
311 | public function getMetadata() |
||
318 | |||
319 | protected function assertPagination() |
||
329 | |||
330 | View Code Duplication | protected function assertBulk() |
|
349 | |||
350 | View Code Duplication | protected function assertSorting() |
|
370 | |||
371 | View Code Duplication | protected function assertPageSize() |
|
391 | |||
392 | } |
||
393 |
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.