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 |
||
12 | class ResponsiveFactoryTest extends \PHPUnit_Framework_TestCase |
||
13 | { |
||
14 | |||
15 | /** |
||
16 | * @var ResponsiveFactoryConfigurator |
||
17 | */ |
||
18 | private $configurator; |
||
19 | |||
20 | /** |
||
21 | * @var Filesystem |
||
22 | */ |
||
23 | private $fs; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | public $publicPath = './tests/public'; |
||
29 | |||
30 | public function __construct() { |
||
35 | |||
36 | public function __destruct() { |
||
41 | |||
42 | public function setUp() { |
||
54 | |||
55 | public function test_simple_construct() { |
||
58 | |||
59 | public function test_create() { |
||
67 | |||
68 | public function test_create_sets_correct_src() { |
||
75 | |||
76 | public function test_create_doesnt_render_full_width_srcset() { |
||
84 | |||
85 | View Code Duplication | public function test_create_sets_correct_srcset() { |
|
94 | |||
95 | View Code Duplication | public function test_create_sets_default_srcset() { |
|
104 | |||
105 | public function test_optimizer() { |
||
134 | |||
135 | // public function test_async() { |
||
136 | // $testCase = $this; |
||
137 | // $url = 'img/image.jpeg'; |
||
138 | // $factory = new ResponsiveFactory(new DefaultConfigurator([ |
||
139 | // 'publicPath' => $this->publicPath, |
||
140 | // 'engine' => 'gd', |
||
141 | // 'stepModifier' => 0.5, |
||
142 | // 'scaler' => 'filesize', |
||
143 | // 'enableCache' => false, |
||
144 | // 'async' => true, |
||
145 | // ])); |
||
146 | // |
||
147 | // $responsiveImage = $factory->create($url); |
||
148 | // |
||
149 | // $this->assertTrue(count($responsiveImage->getSrcset()) > 1); |
||
150 | // $this->assertEquals("/{$url}", $responsiveImage->src()); |
||
151 | // |
||
152 | // $responsiveImage->onSave(function () use ($testCase, $responsiveImage) { |
||
153 | // $fs = new Filesystem(); |
||
154 | // |
||
155 | // foreach ($responsiveImage->getSrcset() as $src) { |
||
156 | // $src = trim($src, '/'); |
||
157 | // $testCase->assertTrue($fs->exists("{$testCase->publicPath}/{$src}")); |
||
158 | // } |
||
159 | // }); |
||
160 | // |
||
161 | // \Amp\wait($responsiveImage->getPromise()); |
||
162 | // } |
||
163 | } |
||
164 |
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.