Conditions | 18 |
Paths | 528 |
Total Lines | 77 |
Code Lines | 48 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 342 |
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 |
||
80 | public function patch() { |
||
81 | $params = $this->request->urlParams; |
||
82 | |||
83 | $patch = $this->request->patch; |
||
84 | $response = new JSONResponse(); |
||
85 | |||
86 | $name = isset($patch['name']) ? $patch['name'] : null; |
||
87 | $value = isset($patch['value']) ? $patch['value'] : null; |
||
88 | $checksum = isset($patch['checksum']) ? $patch['checksum'] : null; |
||
89 | $parameters = isset($patch['parameters']) ? $patch['parameters'] : null; |
||
90 | |||
91 | $addressBook = $this->app->getAddressBook($params['backend'], $params['addressBookId']); |
||
92 | $contact = $addressBook->getChild($params['contactId']); |
||
93 | |||
94 | if (!$contact) { |
||
95 | return $response |
||
96 | ->setStatus(Http::STATUS_NOT_FOUND) |
||
97 | ->bailOut(App::$l10n->t('Couldn\'t find contact.')); |
||
98 | } |
||
99 | |||
100 | if (!$name) { |
||
101 | return $response |
||
102 | ->setStatus(Http::STATUS_PRECONDITION_FAILED) |
||
103 | ->bailOut(App::$l10n->t('Property name is not set.')); |
||
104 | } |
||
105 | |||
106 | if (!$checksum && in_array($name, Properties::$multiProperties)) { |
||
107 | return $response |
||
108 | ->setStatus(Http::STATUS_PRECONDITION_FAILED) |
||
109 | ->bailOut(App::$l10n->t('Property checksum is not set.')); |
||
110 | } |
||
111 | |||
112 | if (is_array($value)) { |
||
113 | // NOTE: Important, otherwise the compound value will be |
||
114 | // set in the order the fields appear in the form! |
||
115 | ksort($value); |
||
116 | } |
||
117 | |||
118 | $result = array('contactId' => $params['contactId']); |
||
119 | |||
120 | if ($checksum && in_array($name, Properties::$multiProperties)) { |
||
121 | try { |
||
122 | if(is_null($value)) { |
||
123 | $contact->unsetPropertyByChecksum($checksum); |
||
124 | } else { |
||
125 | $checksum = $contact->setPropertyByChecksum($checksum, $name, $value, $parameters); |
||
126 | $result['checksum'] = $checksum; |
||
127 | } |
||
128 | } catch(\Exception $e) { |
||
129 | return $response |
||
130 | ->setStatus(Http::STATUS_PRECONDITION_FAILED) |
||
131 | ->bailOut(App::$l10n->t('Information about vCard is incorrect. Please reload the page.')); |
||
132 | } |
||
133 | } elseif (!in_array($name, Properties::$multiProperties)) { |
||
134 | |||
135 | if (is_null($value)) { |
||
136 | unset($contact->{$name}); |
||
137 | } else { |
||
138 | |||
139 | if (!$contact->setPropertyByName($name, $value, $parameters)) { |
||
140 | return $response |
||
141 | ->setStatus(Http::STATUS_INTERNAL_SERVER_ERROR) |
||
142 | ->bailOut(App::$l10n->t('Error updating contact')); |
||
143 | } |
||
144 | |||
145 | } |
||
146 | } |
||
147 | |||
148 | if (!$contact->save()) { |
||
149 | return $response->bailOut(App::$l10n->t('Error saving contact to backend')); |
||
150 | } |
||
151 | |||
152 | $result['lastmodified'] = $contact->lastModified(); |
||
153 | |||
154 | return $response->setData($result); |
||
155 | |||
156 | } |
||
157 | |||
160 |