| Conditions | 24 |
| Paths | 99 |
| Total Lines | 82 |
| Code Lines | 46 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 173 | protected function checkForValidMethods($dataObject, string $type, int $relationDepth = 0): string |
||
| 174 | { |
||
| 175 | //too many iterations! |
||
| 176 | if ($relationDepth > $this->Config()->get('max_relation_depth')) { |
||
| 177 | return ''; |
||
| 178 | } |
||
| 179 | |||
| 180 | $validMethods = $this->getValidMethods($type); |
||
| 181 | |||
| 182 | $this->relationTypesCovered[$dataObject->ClassName] = true; |
||
| 183 | |||
| 184 | // quick return |
||
| 185 | if (isset($this->cache[$type][$dataObject->ClassName]) && true !== $this->cache[$type][$dataObject->ClassName]) { |
||
| 186 | $validMethod = $this->cache[$type][$dataObject->ClassName]; |
||
| 187 | if ($dataObject->hasMethod($validMethod)) { |
||
| 188 | return (string) $dataObject->{$validMethod}(); |
||
| 189 | } |
||
| 190 | |||
| 191 | return (string) $dataObject->{$validMethod}; |
||
| 192 | } |
||
| 193 | |||
| 194 | if ($this->classCanBeIncluded($dataObject->ClassName)) { |
||
| 195 | if (empty($this->cache[$type][$dataObject->ClassName]) || true !== $this->cache[$type][$dataObject->ClassName]) { |
||
| 196 | foreach ($validMethods as $validMethod) { |
||
| 197 | $outcome = null; |
||
| 198 | if ($dataObject->hasMethod($validMethod)) { |
||
| 199 | $outcome = $dataObject->{$validMethod}(); |
||
| 200 | } elseif (!empty($dataObject->{$validMethod})) { |
||
| 201 | $outcome = $dataObject->{$validMethod}; |
||
| 202 | } |
||
| 203 | |||
| 204 | if ($outcome) { |
||
| 205 | $this->cache[$type][$dataObject->ClassName] = $validMethod; |
||
| 206 | |||
| 207 | return (string) $outcome; |
||
| 208 | } |
||
| 209 | } |
||
| 210 | } |
||
| 211 | |||
| 212 | if ('valid_methods_edit' === $type) { |
||
| 213 | if (class_exists(CMSEditLinkAPI::class)) { |
||
| 214 | $link = CMSEditLinkAPI::find_edit_link_for_object($dataObject); |
||
| 215 | if ($link) { |
||
| 216 | return (string) $link; |
||
| 217 | } |
||
| 218 | } |
||
| 219 | } |
||
| 220 | } |
||
| 221 | |||
| 222 | // there is no match for this one, but we can search relations ... |
||
| 223 | $this->cache[$type][$dataObject->ClassName] = true; |
||
| 224 | ++$relationDepth; |
||
| 225 | foreach ($this->getRelations($dataObject) as $relationName => $relType) { |
||
| 226 | $outcome = null; |
||
| 227 | //no support for link through relations yet! |
||
| 228 | if (is_array($relType)) { |
||
| 229 | continue; |
||
| 230 | } |
||
| 231 | |||
| 232 | if (!isset($this->relationTypesCovered[$relType])) { |
||
| 233 | $rels = $dataObject->{$relationName}(); |
||
| 234 | if ($rels) { |
||
| 235 | if ($rels instanceof DataList) { |
||
| 236 | $rels = $rels->first(); |
||
|
|
|||
| 237 | } elseif ($rels && $rels instanceof DataObject) { |
||
| 238 | $outcome = $this->checkForValidMethods($rels, $type, $relationDepth); |
||
| 239 | } elseif ($rels instanceof UnsavedRelationList) { |
||
| 240 | //do nothing; |
||
| 241 | } else { |
||
| 242 | print_r($rels); |
||
| 243 | user_error('Unexpected Relationship'); |
||
| 244 | die(''); |
||
| 245 | } |
||
| 246 | } |
||
| 247 | } |
||
| 248 | |||
| 249 | if ($outcome) { |
||
| 250 | return $outcome; |
||
| 251 | } |
||
| 252 | } |
||
| 253 | |||
| 254 | return ''; |
||
| 255 | } |
||
| 302 |