| Conditions | 26 |
| Paths | 308 |
| Total Lines | 84 |
| Code Lines | 58 |
| Lines | 9 |
| Ratio | 10.71 % |
| 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 |
||
| 83 | static function xmlDeserialize(Reader $reader) { |
||
|
|
|||
| 84 | $elems = $reader->parseInnerTree([ |
||
| 85 | '{http://nextcloud.com/ns}comp-filter' => 'OCA\\DAV\\CalDAV\\Search\\Xml\\Filter\\CompFilter', |
||
| 86 | '{http://nextcloud.com/ns}prop-filter' => 'OCA\\DAV\\CalDAV\\Search\\Xml\\Filter\\PropFilter', |
||
| 87 | '{http://nextcloud.com/ns}param-filter' => 'OCA\\DAV\\CalDAV\\Search\\Xml\\Filter\\ParamFilter', |
||
| 88 | '{http://nextcloud.com/ns}search-term' => 'OCA\\DAV\\CalDAV\\Search\\Xml\\Filter\\SearchTermFilter', |
||
| 89 | '{http://nextcloud.com/ns}limit' => 'OCA\\DAV\\CalDAV\\Search\\Xml\\Filter\\LimitFilter', |
||
| 90 | '{http://nextcloud.com/ns}offset' => 'OCA\\DAV\\CalDAV\\Search\\Xml\\Filter\\OffsetFilter', |
||
| 91 | '{DAV:}prop' => 'Sabre\\Xml\\Element\\KeyValue', |
||
| 92 | ]); |
||
| 93 | |||
| 94 | $newProps = [ |
||
| 95 | 'filters' => [], |
||
| 96 | 'properties' => [], |
||
| 97 | 'limit' => null, |
||
| 98 | 'offset' => null |
||
| 99 | ]; |
||
| 100 | |||
| 101 | if (!is_array($elems)) { |
||
| 102 | $elems = []; |
||
| 103 | } |
||
| 104 | |||
| 105 | foreach ($elems as $elem) { |
||
| 106 | switch ($elem['name']) { |
||
| 107 | case '{DAV:}prop': |
||
| 108 | $newProps['properties'] = array_keys($elem['value']); |
||
| 109 | break; |
||
| 110 | case '{' . SearchPlugin::NS_Nextcloud . '}filter': |
||
| 111 | foreach ($elem['value'] as $subElem) { |
||
| 112 | if ($subElem['name'] === '{' . SearchPlugin::NS_Nextcloud . '}comp-filter') { |
||
| 113 | View Code Duplication | if (!isset($newProps['filters']['comps']) || !is_array($newProps['filters']['comps'])) { |
|
| 114 | $newProps['filters']['comps'] = []; |
||
| 115 | } |
||
| 116 | $newProps['filters']['comps'][] = $subElem['value']; |
||
| 117 | } elseif ($subElem['name'] === '{' . SearchPlugin::NS_Nextcloud . '}prop-filter') { |
||
| 118 | View Code Duplication | if (!isset($newProps['filters']['props']) || !is_array($newProps['filters']['props'])) { |
|
| 119 | $newProps['filters']['props'] = []; |
||
| 120 | } |
||
| 121 | $newProps['filters']['props'][] = $subElem['value']; |
||
| 122 | } elseif ($subElem['name'] === '{' . SearchPlugin::NS_Nextcloud . '}param-filter') { |
||
| 123 | View Code Duplication | if (!isset($newProps['filters']['params']) || !is_array($newProps['filters']['params'])) { |
|
| 124 | $newProps['filters']['params'] = []; |
||
| 125 | } |
||
| 126 | $newProps['filters']['params'][] = $subElem['value']; |
||
| 127 | } elseif ($subElem['name'] === '{' . SearchPlugin::NS_Nextcloud . '}search-term') { |
||
| 128 | $newProps['filters']['search-term'] = $subElem['value']; |
||
| 129 | } |
||
| 130 | } |
||
| 131 | break; |
||
| 132 | case '{' . SearchPlugin::NS_Nextcloud . '}limit': |
||
| 133 | $newProps['limit'] = $elem['value']; |
||
| 134 | break; |
||
| 135 | case '{' . SearchPlugin::NS_Nextcloud . '}offset': |
||
| 136 | $newProps['offset'] = $elem['value']; |
||
| 137 | break; |
||
| 138 | |||
| 139 | } |
||
| 140 | } |
||
| 141 | |||
| 142 | if (empty($newProps['filters'])) { |
||
| 143 | throw new BadRequest('The {' . SearchPlugin::NS_Nextcloud . '}filter element is required for this request'); |
||
| 144 | } |
||
| 145 | |||
| 146 | $propsOrParamsDefined = (!empty($newProps['filters']['props']) || !empty($newProps['filters']['params'])); |
||
| 147 | $noCompsDefined = empty($newProps['filters']['comps']); |
||
| 148 | if ($propsOrParamsDefined && $noCompsDefined) { |
||
| 149 | throw new BadRequest('{' . SearchPlugin::NS_Nextcloud . '}prop-filter or {' . SearchPlugin::NS_Nextcloud . '}param-filter given without any {' . SearchPlugin::NS_Nextcloud . '}comp-filter'); |
||
| 150 | } |
||
| 151 | |||
| 152 | if (!isset($newProps['filters']['search-term'])) { |
||
| 153 | throw new BadRequest('{' . SearchPlugin::NS_Nextcloud . '}search-term is required for this request'); |
||
| 154 | } |
||
| 155 | |||
| 156 | if (empty($newProps['filters']['props']) && empty($newProps['filters']['params'])) { |
||
| 157 | throw new BadRequest('At least one{' . SearchPlugin::NS_Nextcloud . '}prop-filter or {' . SearchPlugin::NS_Nextcloud . '}param-filter is required for this request'); |
||
| 158 | } |
||
| 159 | |||
| 160 | |||
| 161 | $obj = new self(); |
||
| 162 | foreach ($newProps as $key => $value) { |
||
| 163 | $obj->$key = $value; |
||
| 164 | } |
||
| 165 | return $obj; |
||
| 166 | } |
||
| 167 | } |
||
| 168 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.