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 |
||
16 | class ResolveCacheTest extends WebTestCase |
||
17 | { |
||
18 | protected $client; |
||
19 | |||
20 | protected $webRoot; |
||
21 | |||
22 | protected $filesystem; |
||
23 | |||
24 | protected $cacheRoot; |
||
25 | |||
26 | View Code Duplication | public function setUp() |
|
|
|||
27 | { |
||
28 | parent::setUp(); |
||
29 | |||
30 | $this->client = $this->createClient(); |
||
31 | |||
32 | $this->webRoot = self::$kernel->getContainer()->getParameter('kernel.root_dir').'/web'; |
||
33 | $this->cacheRoot = $this->webRoot.'/media/cache'; |
||
34 | |||
35 | $this->filesystem = new Filesystem(); |
||
36 | $this->filesystem->remove($this->cacheRoot); |
||
37 | } |
||
38 | |||
39 | View Code Duplication | public function testShouldResolveWithEmptyCache() |
|
40 | { |
||
41 | $this->assertFileNotExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg'); |
||
42 | |||
43 | $output = $this->executeConsole( |
||
44 | new ResolveCacheCommand(), |
||
45 | array( |
||
46 | 'paths' => array('images/cats.jpeg'), |
||
47 | '--filters' => array('thumbnail_web_path'), ) |
||
48 | ); |
||
49 | |||
50 | $this->assertFileExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg'); |
||
51 | $this->assertFileNotExists($this->cacheRoot.'/thumbnail_default/images/cats.jpeg'); |
||
52 | $this->assertContains('http://localhost/media/cache/thumbnail_web_path/images/cats.jpeg', $output); |
||
53 | } |
||
54 | |||
55 | View Code Duplication | public function testShouldResolveWithCacheExists() |
|
56 | { |
||
57 | $this->filesystem->dumpFile( |
||
58 | $this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg', |
||
59 | 'anImageContent' |
||
60 | ); |
||
61 | |||
62 | $output = $this->executeConsole( |
||
63 | new ResolveCacheCommand(), |
||
64 | array( |
||
65 | 'paths' => array('images/cats.jpeg'), |
||
66 | '--filters' => array('thumbnail_web_path'), ) |
||
67 | ); |
||
68 | |||
69 | $this->assertFileExists($this->cacheRoot.'/thumbnail_web_path/images/cats.jpeg'); |
||
70 | $this->assertFileNotExists($this->cacheRoot.'/thumbnail_default/images/cats.jpeg'); |
||
71 | $this->assertContains('http://localhost/media/cache/thumbnail_web_path/images/cats.jpeg', $output); |
||
72 | } |
||
73 | |||
74 | public function testShouldResolveWithFewPathsAndSingleFilter() |
||
75 | { |
||
76 | $output = $this->executeConsole( |
||
77 | new ResolveCacheCommand(), |
||
78 | array( |
||
79 | 'paths' => array('images/cats.jpeg', 'images/cats2.jpeg'), |
||
80 | '--filters' => array('thumbnail_web_path'), ) |
||
81 | ); |
||
82 | |||
83 | $this->assertContains('http://localhost/media/cache/thumbnail_web_path/images/cats.jpeg', $output); |
||
84 | $this->assertContains('http://localhost/media/cache/thumbnail_web_path/images/cats2.jpeg', $output); |
||
85 | } |
||
86 | |||
87 | public function testShouldResolveWithFewPathsSingleFilterAndPartiallyFullCache() |
||
109 | |||
110 | public function testShouldResolveWithFewPathsAndFewFilters() |
||
111 | { |
||
112 | $output = $this->executeConsole( |
||
113 | new ResolveCacheCommand(), |
||
124 | |||
125 | public function testShouldResolveWithFewPathsAndWithoutFilters() |
||
137 | |||
138 | /** |
||
139 | * Helper function return the result of command execution. |
||
140 | * |
||
141 | * @param Command $command |
||
142 | * @param array $arguments |
||
143 | * @param array $options |
||
144 | * |
||
145 | * @return string |
||
146 | */ |
||
147 | View Code Duplication | protected function executeConsole(Command $command, array $arguments = array(), array $options = array()) |
|
162 | } |
||
163 |
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.