| Conditions | 39 |
| Paths | 76 |
| Total Lines | 137 |
| Code Lines | 93 |
| Lines | 64 |
| Ratio | 46.72 % |
| 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 |
||
| 84 | protected function getQueryCriterion($key, $values) |
||
| 85 | { |
||
| 86 | if (!is_array($values)) { |
||
| 87 | $values = array($values); |
||
| 88 | } |
||
| 89 | |||
| 90 | switch ($key) { |
||
| 91 | case self::MATCH_CONTENT_ID: |
||
| 92 | return new Query\Criterion\ContentId($values); |
||
| 93 | |||
| 94 | case self::MATCH_LOCATION_ID: |
||
| 95 | // NB: seems to cause problems with EZP 2014.3 |
||
| 96 | return new Query\Criterion\LocationId(reset($values)); |
||
| 97 | |||
| 98 | case self::MATCH_CONTENT_REMOTE_ID: |
||
| 99 | return new Query\Criterion\RemoteId($values); |
||
| 100 | |||
| 101 | case self::MATCH_LOCATION_REMOTE_ID: |
||
| 102 | return new Query\Criterion\LocationRemoteId($values); |
||
| 103 | |||
| 104 | case self::MATCH_ATTRIBUTE: |
||
| 105 | $spec = reset($values); |
||
| 106 | $attribute = key($values); |
||
| 107 | $match = reset($spec); |
||
| 108 | $operator = key($spec); |
||
| 109 | if (!isset(self::$operatorsMap[$operator])) { |
||
| 110 | throw new \Exception("Can not use '$operator' as comparison operator for attributes"); |
||
| 111 | } |
||
| 112 | return new Query\Criterion\Field($attribute, self::$operatorsMap[$operator], $match); |
||
| 113 | |||
| 114 | case 'content_type_id': |
||
| 115 | case self::MATCH_CONTENT_TYPE_ID: |
||
| 116 | return new Query\Criterion\ContentTypeId($values); |
||
| 117 | |||
| 118 | case 'content_type_identifier': |
||
| 119 | case self::MATCH_CONTENT_TYPE_IDENTIFIER: |
||
| 120 | return new Query\Criterion\ContentTypeIdentifier($values); |
||
| 121 | |||
| 122 | View Code Duplication | case self::MATCH_CREATION_DATE: |
|
| 123 | $match = reset($values); |
||
| 124 | $operator = key($values); |
||
| 125 | if (!isset(self::$operatorsMap[$operator])) { |
||
| 126 | throw new \Exception("Can not use '$operator' as comparison operator for dates"); |
||
| 127 | } |
||
| 128 | return new Query\Criterion\DateMetadata(Query\Criterion\DateMetadata::CREATED, self::$operatorsMap[$operator], $match); |
||
| 129 | |||
| 130 | View Code Duplication | case self::MATCH_GROUP: |
|
| 131 | foreach($values as &$value) { |
||
| 132 | if (!ctype_digit($value)) { |
||
| 133 | $value = $this->groupMatcher->matchOneByKey($value)->id; |
||
| 134 | } |
||
| 135 | } |
||
| 136 | return new Query\Criterion\UserMetadata(Query\Criterion\UserMetadata::GROUP, Operator::IN, $values); |
||
| 137 | |||
| 138 | View Code Duplication | case self::MATCH_MODIFICATION_DATE: |
|
| 139 | $match = reset($values); |
||
| 140 | $operator = key($values); |
||
| 141 | if (!isset(self::$operatorsMap[$operator])) { |
||
| 142 | throw new \Exception("Can not use '$operator' as comparison operator for dates"); |
||
| 143 | } |
||
| 144 | return new Query\Criterion\DateMetadata(Query\Criterion\DateMetadata::MODIFIED, self::$operatorsMap[$operator], $match); |
||
| 145 | |||
| 146 | View Code Duplication | case self::MATCH_OBJECT_STATE: |
|
| 147 | foreach($values as &$value) { |
||
| 148 | if (!ctype_digit($value)) { |
||
| 149 | $value = $this->stateMatcher->matchOneByKey($value)->id; |
||
| 150 | } |
||
| 151 | } |
||
| 152 | return new Query\Criterion\ObjectStateId($values); |
||
| 153 | |||
| 154 | View Code Duplication | case self::MATCH_OWNER: |
|
| 155 | foreach($values as &$value) { |
||
| 156 | if (!ctype_digit($value)) { |
||
| 157 | $value = $this->userMatcher->matchOneByKey($value)->id; |
||
| 158 | } |
||
| 159 | } |
||
| 160 | return new Query\Criterion\UserMetadata(Query\Criterion\UserMetadata::OWNER, Operator::IN, $values); |
||
| 161 | |||
| 162 | case self::MATCH_PARENT_LOCATION_ID: |
||
| 163 | return new Query\Criterion\ParentLocationId($values); |
||
| 164 | |||
| 165 | case self::MATCH_PARENT_LOCATION_REMOTE_ID: |
||
| 166 | $locationIds = []; |
||
| 167 | foreach ($values as $remoteParentLocationId) { |
||
| 168 | $location = $this->repository->getLocationService()->loadLocationByRemoteId($remoteParentLocationId); |
||
| 169 | // unique locations |
||
| 170 | $locationIds[$location->id] = $location->id; |
||
| 171 | } |
||
| 172 | return new Query\Criterion\ParentLocationId($locationIds); |
||
| 173 | |||
| 174 | View Code Duplication | case self::MATCH_SECTION: |
|
| 175 | foreach($values as &$value) { |
||
| 176 | if (!ctype_digit($value)) { |
||
| 177 | $value = $this->sectionMatcher->matchOneByKey($value)->id; |
||
| 178 | } |
||
| 179 | } |
||
| 180 | return new Query\Criterion\SectionId($values); |
||
| 181 | |||
| 182 | case self::MATCH_SUBTREE: |
||
| 183 | return new Query\Criterion\Subtree($values); |
||
| 184 | |||
| 185 | View Code Duplication | case self::MATCH_VISIBILITY: |
|
| 186 | /// @todo error/warning if there is more than 1 value... |
||
| 187 | $value = reset($values); |
||
| 188 | if ($value) { |
||
| 189 | return new Query\Criterion\Visibility(Query\Criterion\Visibility::VISIBLE); |
||
| 190 | } else { |
||
| 191 | return new Query\Criterion\Visibility(Query\Criterion\Visibility::HIDDEN); |
||
| 192 | } |
||
| 193 | |||
| 194 | View Code Duplication | case self::MATCH_AND: |
|
| 195 | $subCriteria = array(); |
||
| 196 | foreach($values as $subCriterion) { |
||
| 197 | $value = reset($subCriterion); |
||
| 198 | $subCriteria[] = $this->getQueryCriterion(key($subCriterion), $value); |
||
| 199 | } |
||
| 200 | return new Query\Criterion\LogicalAnd($subCriteria); |
||
| 201 | |||
| 202 | View Code Duplication | case self::MATCH_OR: |
|
| 203 | $subCriteria = array(); |
||
| 204 | foreach($values as $subCriterion) { |
||
| 205 | $value = reset($subCriterion); |
||
| 206 | $subCriteria[] = $this->getQueryCriterion(key($subCriterion), $value); |
||
| 207 | } |
||
| 208 | return new Query\Criterion\LogicalOr($subCriteria); |
||
| 209 | |||
| 210 | case self::MATCH_NOT: |
||
| 211 | /// @todo throw if more than one sub-criteria found |
||
| 212 | $value = reset($values); |
||
| 213 | $subCriterion = $this->getQueryCriterion(key($values), $value); |
||
| 214 | return new Query\Criterion\LogicalNot($subCriterion); |
||
| 215 | |||
| 216 | default: |
||
| 217 | throw new \Exception($this->returns . " can not be matched because matching condition '$key' is not supported. Supported conditions are: " . |
||
| 218 | implode(', ', $this->allowedConditions)); |
||
| 219 | } |
||
| 220 | } |
||
| 221 | } |
||
| 222 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.