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 | final class CompleteTest extends PHPUnit_Framework_TestCase |
||
17 | { |
||
18 | /** |
||
19 | * @var ControllerResolver |
||
20 | */ |
||
21 | private $controllerResolver; |
||
22 | |||
23 | protected function setUp() |
||
24 | { |
||
25 | $kernel = new AppKernel('test_env', true); |
||
26 | $kernel->boot(); |
||
27 | |||
28 | $this->controllerResolver = $kernel->getContainer() |
||
29 | ->get('controller_resolver'); |
||
30 | } |
||
31 | |||
32 | public function testControllerResolver() |
||
33 | { |
||
34 | $this->assertInstanceOf(ControllerResolver::class, $this->controllerResolver); |
||
35 | } |
||
36 | |||
37 | public function testMissingControllerParameter() |
||
38 | { |
||
39 | $request = new Request(); |
||
40 | $this->assertFalse($this->controllerResolver->getController($request)); |
||
41 | } |
||
42 | |||
43 | View Code Duplication | public function testGetAutowiredController() |
|
|
|||
44 | { |
||
45 | $request = new Request(); |
||
46 | $request->attributes->set('_controller', SomeController::class.'::someAction'); |
||
47 | |||
48 | /** @var SomeController $controller */ |
||
49 | $controller = $this->controllerResolver->getController($request)[0]; |
||
50 | |||
51 | $this->assertInstanceOf(SomeController::class, $controller); |
||
52 | $this->assertInstanceOf(SomeService::class, $controller->getSomeService()); |
||
53 | } |
||
54 | |||
55 | View Code Duplication | public function testGetContainerAwareController() |
|
56 | { |
||
57 | $request = new Request(); |
||
58 | $request->attributes->set('_controller', ContainerAwareController::class.'::someAction'); |
||
59 | |||
60 | /** @var ContainerAwareController $controller */ |
||
61 | $controller = $this->controllerResolver->getController($request)[0]; |
||
62 | |||
63 | $this->assertInstanceOf(ContainerAwareController::class, $controller); |
||
64 | $this->assertInstanceOf(ContainerInterface::class, $controller->getContainer()); |
||
65 | } |
||
66 | |||
67 | public function testGetControllerServiceMissing() |
||
68 | { |
||
69 | $request = new Request(); |
||
70 | $request->attributes->set('_controller', 'some.missing.controller.service:someAction'); |
||
71 | |||
72 | $this->setExpectedException(ServiceNotFoundException::class); |
||
73 | $this->controllerResolver->getController($request); |
||
74 | } |
||
75 | |||
76 | View Code Duplication | public function testGetControllerServiceRegisteredInConfig() |
|
77 | { |
||
78 | $request = new Request(); |
||
79 | $request->attributes->set('_controller', 'some.controller.service:someAction'); |
||
80 | |||
81 | $controller = $this->controllerResolver->getController($request)[0]; |
||
82 | $this->assertInstanceOf(SomeRegisteredController::class, $controller); |
||
83 | } |
||
84 | } |
||
85 |
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.