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 |
||
20 | class SWPUpdaterExtensionTest extends \PHPUnit_Framework_TestCase |
||
21 | { |
||
22 | /** |
||
23 | * @covers SWP\UpdaterBundle\SWPUpdaterBundle |
||
24 | * @covers SWP\UpdaterBundle\DependencyInjection\SWPUpdaterExtension::load |
||
25 | * @covers SWP\UpdaterBundle\DependencyInjection\Configuration::getConfigTreeBuilder |
||
26 | * @covers SWP\UpdaterBundle\DependencyInjection\SWPUpdaterExtension::<private> |
||
27 | */ |
||
28 | public function testLoad() |
||
29 | { |
||
30 | $curlOptions = array( |
||
31 | 'curl' => array( |
||
32 | '10203' => 'somehost:localhost', |
||
33 | ), |
||
34 | ); |
||
35 | |||
36 | $data = array( |
||
37 | 'swp_updater.version.class' => 'Acme\DemoBundle\Version\Version', |
||
38 | 'swp_updater.client.options' => $curlOptions, |
||
39 | ); |
||
40 | |||
41 | $container = $this->createContainer($data); |
||
42 | $loader = $this->createLoader(); |
||
43 | $config = $this->getConfig(); |
||
44 | |||
45 | $loader->load(array($config), $container); |
||
46 | |||
47 | $this->assertEquals('Acme\DemoBundle\Version\Version', $container->getParameter('swp_updater.version_class')); |
||
48 | $this->assertEquals(array('base_uri' => 'http://example.com'), $container->getParameter('swp_updater.client')); |
||
49 | $this->assertEquals($curlOptions, $container->getParameter('swp_updater.client.options')); |
||
50 | $this->assertEquals('Acme\DemoBundle\Version\Version', $container->getParameter('swp_updater.version.class')); |
||
51 | $this->assertEquals($container->getParameter('kernel.cache_dir'), $container->getParameter('swp_updater.temp_dir')); |
||
52 | $this->assertEquals($container->getParameter('kernel.root_dir').'/../', $container->getParameter('swp_updater.target_dir')); |
||
53 | $this->assertFalse($container->hasParameter('swp_updater.monolog_channel')); |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * @covers SWP\UpdaterBundle\DependencyInjection\SWPUpdaterExtension::load |
||
58 | * @covers SWP\UpdaterBundle\DependencyInjection\SWPUpdaterExtension::<private> |
||
59 | */ |
||
60 | public function testLoadWhenTempDirAndTargetDirAreSet() |
||
82 | |||
83 | /** |
||
84 | * @covers SWP\UpdaterBundle\DependencyInjection\SWPUpdaterExtension::load |
||
85 | * @covers SWP\UpdaterBundle\DependencyInjection\SWPUpdaterExtension::<private> |
||
86 | */ |
||
87 | View Code Duplication | public function testLoadIfMonologChannelDefined() |
|
|
|||
88 | { |
||
89 | $container = $this->createContainer(); |
||
90 | $loader = $this->createLoader(); |
||
91 | $config = $this->getConfig(); |
||
92 | $tempConfig = array( |
||
93 | 'version_class' => 'Acme\DemoBundle\Version\Version', |
||
94 | 'monolog_channel' => true, |
||
95 | ); |
||
96 | |||
97 | $loader->load(array(array_merge($config, $tempConfig)), $container); |
||
98 | $this->assertTrue($container->hasParameter('swp_updater.monolog_channel')); |
||
99 | $this->assertTrue($container->getParameter('swp_updater.monolog_channel')); |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * @covers SWP\UpdaterBundle\DependencyInjection\SWPUpdaterExtension::load |
||
104 | * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException |
||
105 | */ |
||
106 | public function testLoadWhenVersionClassIsRequired() |
||
107 | { |
||
108 | $container = $this->createContainer(); |
||
109 | $loader = $this->createLoader(); |
||
110 | |||
111 | $loader->load(array(array()), $container); |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * @covers SWP\UpdaterBundle\DependencyInjection\SWPUpdaterExtension::load |
||
116 | * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException |
||
117 | */ |
||
118 | public function testLoadWhenClientBaseUriIsRequiredAndCannotBeEmpty() |
||
119 | { |
||
120 | $container = $this->createContainer(); |
||
121 | $loader = $this->createLoader(); |
||
122 | |||
123 | $config = array( |
||
124 | 'version_class' => 'Acme\DemoBundle\Version\Version', |
||
125 | 'client' => array(), |
||
126 | ); |
||
127 | |||
128 | $loader->load(array($config), $container); |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * @covers SWP\UpdaterBundle\DependencyInjection\SWPUpdaterExtension::load |
||
133 | * @covers SWP\UpdaterBundle\DependencyInjection\SWPUpdaterExtension::<private> |
||
134 | * @expectedException \LogicException |
||
135 | */ |
||
136 | public function testLoadWhenClientTypeIsNotSupported() |
||
137 | { |
||
138 | $container = $this->createContainer(); |
||
139 | $loader = $this->createLoader(); |
||
140 | |||
141 | $tempConfig = array( |
||
142 | 'client' => array( |
||
143 | 'type' => 'some_fake_name', |
||
144 | 'base_uri' => 'http://example.com', |
||
145 | ), |
||
146 | ); |
||
147 | |||
148 | $config = $this->getConfig(); |
||
149 | $loader->load(array(array_merge($config, $tempConfig)), $container); |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * @covers SWP\UpdaterBundle\DependencyInjection\SWPUpdaterExtension::load |
||
154 | * @covers SWP\UpdaterBundle\DependencyInjection\SWPUpdaterExtension::<private> |
||
155 | * @expectedException \LogicException |
||
156 | */ |
||
157 | public function testLoadWhenGuzzleIsNotInstalledButUsed() |
||
179 | |||
180 | /** |
||
181 | * @covers SWP\UpdaterBundle\DependencyInjection\SWPUpdaterExtension::load |
||
182 | * @covers SWP\UpdaterBundle\DependencyInjection\SWPUpdaterExtension::<private> |
||
183 | */ |
||
184 | View Code Duplication | public function testLoadForDefaultClient() |
|
185 | { |
||
186 | $container = $this->createContainer(); |
||
187 | $loader = $this->createLoader(); |
||
188 | $config = $this->getConfig(); |
||
189 | $tempConfig = array( |
||
190 | 'client' => array( |
||
191 | 'base_uri' => 'http://example.com', |
||
192 | ), |
||
193 | ); |
||
194 | |||
195 | $loader->load(array(array_merge($config, $tempConfig)), $container); |
||
196 | $this->assertEquals( |
||
197 | $container->getDefinition('swp_updater.client')->getClass(), |
||
198 | "SWP\UpdaterBundle\Client\DefaultClient" |
||
199 | ); |
||
200 | } |
||
201 | |||
202 | protected function createLoader() |
||
206 | |||
207 | protected function getConfig() |
||
208 | { |
||
209 | return array( |
||
210 | 'version_class' => 'Acme\DemoBundle\Version\Version', |
||
211 | 'client' => array( |
||
212 | 'base_uri' => 'http://example.com', |
||
216 | |||
217 | protected function createContainer(array $data = array()) |
||
224 | } |
||
225 |
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.