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 | final class ControllerResolverTest extends PHPUnit_Framework_TestCase |
||
13 | { |
||
14 | /** |
||
15 | * @var ControllerResolver |
||
16 | */ |
||
17 | private $controllerResolver; |
||
18 | |||
19 | protected function setUp() |
||
20 | { |
||
21 | $this->controllerResolver = $this->createControllerResolverWithMocks(); |
||
22 | } |
||
23 | |||
24 | View Code Duplication | public function testGetController() |
|
|
|||
25 | { |
||
26 | $request = new Request(); |
||
27 | $request->attributes->set('_controller', 'SomeController::someAction'); |
||
28 | |||
29 | $controller = $this->controllerResolver->getController($request); |
||
30 | $this->assertNull($controller); |
||
31 | } |
||
32 | |||
33 | public function testGetArguments() |
||
34 | { |
||
35 | $this->assertNull( |
||
36 | $this->controllerResolver->getArguments(new Request(), 'missing') |
||
37 | ); |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * @return ControllerResolver |
||
42 | */ |
||
43 | private function createControllerResolverWithMocks() |
||
44 | { |
||
45 | $parentControllerResolverMock = $this->prophesize(ControllerResolverInterface::class); |
||
46 | $containerMock = $this->prophesize(ContainerInterface::class); |
||
47 | $controllerNameParser = $this->prophesize(ControllerNameParser::class); |
||
48 | |||
49 | return new ControllerResolver( |
||
50 | $parentControllerResolverMock->reveal(), |
||
51 | $containerMock->reveal(), |
||
52 | $controllerNameParser->reveal() |
||
53 | ); |
||
54 | } |
||
55 | } |
||
56 |
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.