| Conditions | 10 |
| Paths | 14 |
| Total Lines | 55 |
| Code Lines | 36 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 38 | private function updateContacts(Request $request, $object) |
||
| 39 | { |
||
| 40 | $arr1 = []; |
||
| 41 | //шматок гавнокода |
||
| 42 | $postData = $request->request->all(); |
||
| 43 | foreach ($postData as $arr) { |
||
| 44 | $arr1 = $arr; |
||
| 45 | break; |
||
| 46 | } |
||
| 47 | $tmpTest = ''; |
||
| 48 | |||
| 49 | if ($postData) { |
||
|
|
|||
| 50 | $oldContacts = null; |
||
| 51 | $newContacts = null; |
||
| 52 | $oldContacts = $object->getContacts(); |
||
| 53 | $em = $this->getDoctrine()->getManager(); |
||
| 54 | if (array_key_exists('contacts', $arr1)) { |
||
| 55 | $newContacts = $arr1['contacts']; |
||
| 56 | |||
| 57 | // Викидуем з массива нових контактів, які прийшли з POST, ті що вже були |
||
| 58 | // і видаляем з бази контакти які видалені у клієнта |
||
| 59 | foreach ($oldContacts as $contact) { |
||
| 60 | $k = array_search($contact->getId(), $newContacts); |
||
| 61 | if (false === $k) { |
||
| 62 | $object->removeContact($contact); |
||
| 63 | $em->remove($contact); |
||
| 64 | } else { |
||
| 65 | unset($newContacts[$k]); |
||
| 66 | } |
||
| 67 | } |
||
| 68 | |||
| 69 | foreach ($newContacts as $contactId) { |
||
| 70 | $contact = $em->getRepository('ClientBundle:Contact')->find($contactId); |
||
| 71 | if (!$contact) { |
||
| 72 | throw $this->createNotFoundException(sprintf('не знайдений об\'єкт з id : %s', $contactId)); |
||
| 73 | } else { |
||
| 74 | $object->addContact($contact); |
||
| 75 | $contact->setClient($object); |
||
| 76 | } |
||
| 77 | } |
||
| 78 | } else { |
||
| 79 | // Post прийшов без контактів - видаляем всі контакти клієнта |
||
| 80 | foreach ($oldContacts as $contact) { |
||
| 81 | $object->removeContact($contact); |
||
| 82 | $em->remove($contact); |
||
| 83 | } |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | if ('' === $tmpTest) { |
||
| 88 | return null; |
||
| 89 | } else { |
||
| 90 | return new Response($tmpTest); |
||
| 91 | } |
||
| 92 | } |
||
| 93 | } |
||
| 94 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.