| Conditions | 9 |
| Paths | 16 |
| Total Lines | 77 |
| Code Lines | 46 |
| Lines | 35 |
| Ratio | 45.45 % |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 27 | public function editAction(Request $request) { |
||
| 28 | ################################################ SETTINGS ################################################ |
||
| 29 | $name = 'group'; |
||
| 30 | $repo = 'VivaitAuthBundle:Group'; |
||
| 31 | $formtpl['title'] = 'Add/Edit ' . ucfirst($name); |
||
| 32 | $obj = new Group(); |
||
| 33 | $key = $request->query->get('id', 0); |
||
| 34 | $foreign_objs = array( |
||
| 35 | # array( |
||
| 36 | # 'repo' => 'VivaBravoBundle:Product', |
||
| 37 | # 'key' => $request->query->get('pid', 0), |
||
| 38 | # 'method' => 'setProduct', |
||
| 39 | # 'name' => 'product'), |
||
| 40 | ); |
||
| 41 | ############################################################################################################ |
||
| 42 | |||
| 43 | |||
| 44 | View Code Duplication | if(!$key) { |
|
| 45 | ### CREATING A NEW OBJECT ### |
||
| 46 | |||
| 47 | #if there are foreign objects that should be bound to this object, bind them all here |
||
| 48 | foreach($foreign_objs as $fo) { |
||
| 49 | $foreign_obj = $this->getDoctrine() |
||
| 50 | ->getRepository($fo['repo']) |
||
| 51 | ->find($fo['key']); |
||
| 52 | if(!$foreign_obj) { |
||
| 53 | $this->get('session')->getFlashBag()->add('error', sprintf("Could not find the %s", $fo['name'])); |
||
| 54 | return $this->redirect($request->query->get('parent', $request->request->get('parent', $request->headers->get('referer')))); |
||
| 55 | } |
||
| 56 | call_user_func(array($obj, $fo['method'], $foreign_obj)); |
||
| 57 | } |
||
| 58 | } else { |
||
| 59 | ### EDITING AN EXISTING OBJECT ### |
||
| 60 | $obj = $this->getDoctrine() |
||
| 61 | ->getRepository($repo) |
||
| 62 | ->find($key); |
||
| 63 | |||
| 64 | if(!$obj) { |
||
| 65 | $this->get('session')->getFlashBag()->add('error', sprintf("Could not find the %s", $name)); |
||
| 66 | } |
||
| 67 | } |
||
| 68 | |||
| 69 | |||
| 70 | ############################################## CREATE FORM ############################################### |
||
| 71 | $form = $this->createFormBuilder($obj) |
||
| 72 | ->add('name', 'text',array('label' => 'Name')) |
||
| 73 | ->add('role', 'text',array('label' => 'Role')) |
||
| 74 | ->add('users', 'entity', array( |
||
| 75 | 'class' => 'VivaitAuthBundle:User', |
||
| 76 | 'property' => 'fullname', |
||
| 77 | 'multiple' => true, |
||
| 78 | 'required' => true, |
||
| 79 | 'label' => 'Users', |
||
| 80 | 'by_reference' => false #USE THIS ALONG WITH A $owning->addInverse($this); IN THE addOwning() FUNCTION TO TRIGGER AN UPDATE WHEN ON INVERSE SIDE |
||
| 81 | )) |
||
| 82 | ->getForm(); |
||
| 83 | ############################################################################################################ |
||
| 84 | |||
| 85 | View Code Duplication | if($request->isMethod('POST')) { |
|
| 86 | $form->bind($request); |
||
| 87 | if($form->isValid()) { |
||
| 88 | $em = $this->getDoctrine()->getManager(); |
||
| 89 | |||
| 90 | $em->persist($obj); |
||
| 91 | $em->flush(); |
||
| 92 | $this->get('session')->getFlashBag()->add('success', sprintf('The %s has been %s successfully', $name, $key ? 'modified' : 'created')); |
||
| 93 | return $this->render('VivaitBootstrapBundle:Default:redirect.html.twig', array('redirect' => $request->query->get('parent', $request->request->get('parent', $request->headers->get('referer'))))); |
||
| 94 | } |
||
| 95 | } |
||
| 96 | if(isset($form)) { |
||
| 97 | $formtpl['form'] = $form->createView(); |
||
| 98 | } |
||
| 99 | $formtpl['action'] = $this->generateUrl($this->container->get('request')->get('_route'),$request->query->all()); |
||
| 100 | |||
| 101 | return $this->render('VivaitBootstrapBundle:Default:form.html.twig', array( |
||
| 102 | 'form' => array_merge($formtpl, array('parent' => $request->query->get('parent', $request->request->get('parent', $request->headers->get('referer'))))))); |
||
| 103 | } |
||
| 104 | |||
| 131 |
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.