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 |
||
| 46 | final class EngineAsArgumentInKernelControllerTest extends TestCase |
||
| 47 | { |
||
| 48 | public function testCanBeCreated() |
||
| 56 | |||
| 57 | public function testKernelInterface() |
||
| 65 | |||
| 66 | public function FrameworkReturnsResponse() // failling: no more a test |
||
| 67 | { |
||
| 68 | $this->assertInstanceOf( |
||
| 69 | // Response::class, // 5.4 < php |
||
| 70 | 'Symfony\Component\HttpFoundation\Response', |
||
| 71 | (new AppKernel('dev', true))->handle(Request::create('/', 'GET')) |
||
| 72 | ); |
||
| 73 | } |
||
| 74 | |||
| 75 | View Code Duplication | public function testControllerResponse() |
|
| 76 | { // From: https://symfony.com/doc/current/create_framework/unit_testing.html |
||
| 77 | // TODO: Try with a real matcher |
||
| 78 | // TODO: Use real controller to be tested! |
||
| 79 | $matcher = $this->createMock(UrlMatcherInterface::class); |
||
| 80 | // use getMock() on PHPUnit 5.3 or below |
||
| 81 | // $matcher = $this->getMock(UrlMatcherInterface::class); |
||
| 82 | |||
| 83 | $matcher |
||
| 84 | ->expects($this->once()) |
||
| 85 | ->method('match') |
||
| 86 | ->will($this->returnValue([ |
||
| 87 | '_route' => 'foo', |
||
| 88 | 'name' => 'Fabien', |
||
| 89 | '_controller' => function ($name) { |
||
| 90 | return new Response('Hello '.$name); |
||
| 91 | }, |
||
| 92 | ])) |
||
| 93 | ; |
||
| 94 | $matcher |
||
| 95 | ->expects($this->once()) |
||
| 96 | ->method('getContext') |
||
| 97 | ->will($this->returnValue($this->createMock(RequestContext::class))) |
||
| 98 | ; |
||
| 99 | |||
| 100 | $c = $this->container(); |
||
| 101 | $c->compile(); |
||
| 102 | $requestStack = new RequestStack(); |
||
| 103 | $dispatcher = new EventDispatcher(); |
||
| 104 | $dispatcher->addSubscriber(new RouterListener( |
||
| 105 | $matcher, |
||
| 106 | $requestStack |
||
| 107 | )); // Returns nothing. |
||
| 108 | $dispatcher->addSubscriber(new ResponseListener('UTF-8')); |
||
| 109 | $response = (new HttpKernel( |
||
| 110 | $dispatcher, |
||
| 111 | new ContainerControllerResolver($c), |
||
| 112 | // new ControllerResolver(), |
||
| 113 | $requestStack, |
||
| 114 | new ArgumentResolver( |
||
| 115 | new ArgumentMetadataFactory(), |
||
| 116 | [ |
||
| 117 | new RequestAttributeValueResolver(), |
||
| 118 | new RequestValueResolver(), |
||
| 119 | new SessionValueResolver(), |
||
| 120 | new ServiceValueResolver($c), |
||
| 121 | new DefaultValueResolver(), |
||
| 122 | new VariadicValueResolver(), |
||
| 123 | ] |
||
| 124 | ) |
||
| 125 | // ))->handle(Request::create('/', 'GET')); |
||
| 126 | ))->handle(new Request()); |
||
| 127 | |||
| 128 | $this->assertSame(200, $response->getStatusCode()); |
||
| 129 | $this->assertContains('Hello Fabien', $response->getContent()); |
||
| 130 | } |
||
| 131 | |||
| 132 | public function testContainerCanBeCreated() |
||
| 140 | |||
| 141 | public function testContainerInterface() |
||
| 149 | |||
| 150 | public function ComponentReturnsResponse() // Not yet a test! |
||
| 151 | { |
||
| 152 | // TODO: Use real controller to be tested! |
||
| 153 | $c = $this->container(); |
||
| 154 | $c->compile(); |
||
| 155 | $requestStack = new RequestStack(); |
||
| 156 | $dispatcher = new EventDispatcher(); |
||
| 157 | $dispatcher->addSubscriber(new RouterListener( |
||
| 158 | new UrlMatcher( |
||
| 159 | // $this->loadJustHelloRoutes(), |
||
| 160 | $this->loadRoutes(), |
||
| 161 | new RequestContext() |
||
| 162 | ), |
||
| 163 | $requestStack |
||
| 164 | )); |
||
| 165 | $dispatcher->addSubscriber(new ResponseListener('UTF-8')); |
||
| 166 | |||
| 167 | $this->assertInstanceOf( |
||
| 168 | // Response::class, // 5.4 < php |
||
| 169 | 'Symfony\Component\HttpFoundation\Response', |
||
| 170 | (new HttpKernel( |
||
| 171 | $dispatcher, |
||
| 172 | new ContainerControllerResolver($c), |
||
| 173 | // new ControllerResolver(), |
||
| 174 | $requestStack, |
||
| 175 | new ArgumentResolver( |
||
| 176 | new ArgumentMetadataFactory(), |
||
| 177 | [ |
||
| 178 | new RequestAttributeValueResolver(), |
||
| 179 | new RequestValueResolver(), |
||
| 180 | new SessionValueResolver(), |
||
| 181 | new ServiceValueResolver($c), |
||
| 182 | new DefaultValueResolver(), |
||
| 183 | new VariadicValueResolver(), |
||
| 184 | ] |
||
| 185 | ) |
||
| 186 | ))->handle(Request::create('/', 'GET')) |
||
| 187 | ); |
||
| 188 | } |
||
| 189 | |||
| 190 | private function configureRoutes(RouteCollectionBuilder $routes) |
||
| 197 | |||
| 198 | private function loadRoutes(LoaderInterface $loader = null) |
||
| 205 | |||
| 206 | private function configureJustHelloRoutes(RouteCollectionBuilder $routes) |
||
| 218 | |||
| 219 | private function loadJustHelloRoutes(LoaderInterface $loader = null) |
||
| 226 | |||
| 227 | private function container() |
||
| 274 | } |
||
| 275 | |||
| 276 | // http://api.symfony.com/3.3/Symfony/Bridge/Twig/TwigEngine.html |
||
| 277 | // http://api.symfony.com/3.3/Symfony/Bundle/TwigBundle/TwigEngine.html |
||
| 278 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: