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 |
||
| 28 | class TestController |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * @Method("GET") |
||
| 32 | * @Route("/detail2/{id}", requirements={"id" = "\d+"}) |
||
| 33 | * //@Security("has_role('ROLE_ADMIN')") |
||
| 34 | */ |
||
| 35 | public function testMethod(Application $app, Request $request, $id = 0) |
||
| 36 | { |
||
| 37 | return new Response('test Method: '.$id); |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @Route("/initialize/{id}", requirements={"id" = "\d+"}) |
||
| 42 | */ |
||
| 43 | public function initialize(Application $app, Request $request, $id = 0) |
||
| 44 | { |
||
| 45 | dump('initialize'); |
||
| 46 | $t = new \Eccube\Entity\Csv(); |
||
| 47 | $t->getDispName($id); |
||
| 48 | |||
| 49 | return $app->forward($app->path('test/new'), ['param_init' => $id]); |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @Method("GET") |
||
| 54 | * @Route("/", name="test/index") |
||
| 55 | */ |
||
| 56 | public function index(Application $app, Request $request) |
||
| 57 | { |
||
| 58 | dump('/'); |
||
| 59 | $id = 1; |
||
| 60 | $app->forwardChain('/test/initialize/'.$id) |
||
| 61 | ->forwardchain($app->path('test/new'), ['param_init' => $id], $response) |
||
| 62 | ->forwardChain($app->path('test/new'), ['param_init' => $id], $response) |
||
| 63 | ->forwardChain($app->path('test/new'), ['param_init' => $id], $response); |
||
| 64 | |||
| 65 | return $response; |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @Method("GET") |
||
| 70 | * @Route("/new", name="test/new") |
||
| 71 | */ |
||
| 72 | public function newAction(Application $app, Request $request) |
||
| 73 | { |
||
| 74 | dump('new'); |
||
| 75 | $t = $app['request_scope']->get('csv'); |
||
| 76 | dump($t); |
||
| 77 | |||
| 78 | return ['id' => $t->getDispName()]; |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @Method("GET") |
||
| 83 | * @Route("/post") |
||
| 84 | */ |
||
| 85 | public function postAction(Application $app, Request $request) |
||
| 86 | { |
||
| 87 | dump('post'); |
||
| 88 | $app->forward('/test/initialize/5'); |
||
| 89 | |||
| 90 | return ['id' => $app['request_scope']->get('csv')->getDispName()]; |
||
| 91 | } |
||
| 92 | } |
||
| 93 |