| Conditions | 11 |
| Paths | 37 |
| Total Lines | 116 |
| Code Lines | 74 |
| Lines | 24 |
| Ratio | 20.69 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
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 |
||
| 26 | public function editAction(Request $request) { |
||
| 27 | |||
| 28 | ################################################ SETTINGS ################################################ |
||
| 29 | $name = 'user'; |
||
| 30 | $repo = 'VivaitAuthBundle:User'; |
||
| 31 | $formtpl['title'] = 'Add/Edit ' . ucfirst($name); |
||
|
|
|||
| 32 | $obj = new User(); |
||
| 33 | $key = $request->query->get('id', 0); |
||
| 34 | $foreign_objs = array( # array( |
||
| 35 | # 'repo' => 'VivaBravoBundle:Product', |
||
| 36 | # 'key' => $request->query->get('pid', 0), |
||
| 37 | # 'method' => 'setProduct', |
||
| 38 | # 'name' => 'product'), |
||
| 39 | ); |
||
| 40 | ############################################################################################################ |
||
| 41 | |||
| 42 | View Code Duplication | if(!$key) { |
|
| 43 | ### CREATING A NEW OBJECT ### |
||
| 44 | |||
| 45 | #if there are foreign objects that should be bound to this object, bind them all here |
||
| 46 | foreach($foreign_objs as $fo) { |
||
| 47 | $foreign_obj = $this->getDoctrine() |
||
| 48 | ->getRepository($fo['repo']) |
||
| 49 | ->find($fo['key']); |
||
| 50 | if(!$foreign_obj) { |
||
| 51 | $this->get('session')->getFlashBag()->add('error', sprintf("Could not find the %s", $fo['name'])); |
||
| 52 | return $this->redirect($request->query->get('parent', $request->request->get('parent', $request->headers->get('referer')))); |
||
| 53 | } |
||
| 54 | call_user_func(array($obj, $fo['method'], $foreign_obj)); |
||
| 55 | } |
||
| 56 | } else { |
||
| 57 | ### EDITING AN EXISTING OBJECT ### |
||
| 58 | $obj = $this->getDoctrine() |
||
| 59 | ->getRepository($repo) |
||
| 60 | ->find($key); |
||
| 61 | |||
| 62 | if(!$obj) { |
||
| 63 | $this->get('session')->getFlashBag()->add('error', sprintf("Could not find the %s", $name)); |
||
| 64 | } |
||
| 65 | } |
||
| 66 | |||
| 67 | if($this->getUser()->getTenants()->count() > 1) { |
||
| 68 | $tenant_namefield = 'tenantedFullname'; |
||
| 69 | } else { |
||
| 70 | $tenant_namefield = 'fullname'; |
||
| 71 | } |
||
| 72 | |||
| 73 | ############################################## CREATE FORM ############################################### |
||
| 74 | |||
| 75 | $form = $this->createFormBuilder($obj) |
||
| 76 | ->add('username', 'text', array('label' => 'Username')) |
||
| 77 | ->add('initials', 'text', array('label' => 'Initials')) |
||
| 78 | ->add('fullname', 'text', array('label' => 'Full Name')) |
||
| 79 | ->add('email', 'email', array('label' => 'Email Address')) |
||
| 80 | ->add('password', 'password', array('label' => 'New Password')) |
||
| 81 | ->add('active', 'checkbox', array('label' => 'Active')) |
||
| 82 | ->add('jobtitle', 'text', array('label' => 'Job Title', 'required' => false)) |
||
| 83 | ->add('department', 'text', array('label' => 'Department', 'required' => false)) |
||
| 84 | ->add('location', 'text', array('label' => 'Location', 'required' => false)) |
||
| 85 | ->add('telephone', 'text', array('label' => 'Telephone', 'required' => false)) |
||
| 86 | ->add('groups', 'entity', array( |
||
| 87 | 'class' => 'VivaitAuthBundle:Group', |
||
| 88 | 'property' => 'name', |
||
| 89 | 'multiple' => true, |
||
| 90 | 'required' => true, |
||
| 91 | 'attr' => array('size' => 15), |
||
| 92 | 'label' => 'Groups' |
||
| 93 | )) |
||
| 94 | ->add('tenants', 'entity', array( |
||
| 95 | 'class' => 'VivaitAuthBundle:Tenant', |
||
| 96 | 'property' => 'tenant', |
||
| 97 | 'multiple' => true, |
||
| 98 | 'attr' => array('size' => 15), |
||
| 99 | 'required' => true, |
||
| 100 | 'label' => 'Tenants' |
||
| 101 | )) |
||
| 102 | ->getForm(); |
||
| 103 | ############################################################################################################ |
||
| 104 | |||
| 105 | if($request->isMethod('POST')) { |
||
| 106 | // get a copy of the previous object before fields have been modified |
||
| 107 | $prevobj = clone $obj; |
||
| 108 | |||
| 109 | $form->handleRequest($request); |
||
| 110 | if($form->isValid()) { |
||
| 111 | |||
| 112 | ###### RESET PASSWORD IF NEW PASSWORD IS SET ###### |
||
| 113 | if(strlen($obj->getPassword())) { |
||
| 114 | # new password set |
||
| 115 | $obj->newSalt(); |
||
| 116 | $factory = $this->get('security.encoder_factory'); |
||
| 117 | $encoder = $factory->getEncoder($this); |
||
| 118 | $password = $encoder->encodePassword($obj->getPassword(), $obj->getSalt()); |
||
| 119 | $obj->setPassword($password); |
||
| 120 | } else { |
||
| 121 | #retain existing password |
||
| 122 | $obj->setPassword($prevobj->getPassword()); |
||
| 123 | } |
||
| 124 | #################################################### |
||
| 125 | |||
| 126 | |||
| 127 | $em = $this->getDoctrine()->getManager(); |
||
| 128 | $em->persist($obj); |
||
| 129 | $em->flush(); |
||
| 130 | $this->get('session')->getFlashBag()->add('success', sprintf('The %s has been %s successfully', $name, $key ? 'modified' : 'created')); |
||
| 131 | return $this->render('VivaitBootstrapBundle:Default:redirect.html.twig', array('redirect' => $request->query->get('parent', $request->request->get('parent', $request->headers->get('referer'))))); |
||
| 132 | } |
||
| 133 | } |
||
| 134 | if(isset($form)) { |
||
| 135 | $formtpl['form'] = $form->createView(); |
||
| 136 | } |
||
| 137 | $formtpl['action'] = $this->generateUrl($this->container->get('request')->get('_route'), $request->query->all()); |
||
| 138 | |||
| 139 | return $this->render('VivaitBootstrapBundle:Default:form.html.twig', array( |
||
| 140 | 'form' => array_merge($formtpl, array('parent' => $request->query->get('parent', $request->request->get('parent', $request->headers->get('referer'))))))); |
||
| 141 | } |
||
| 142 | |||
| 284 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.