| Conditions | 29 |
| Paths | 379 |
| Total Lines | 86 |
| Code Lines | 48 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 1 | 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 |
||
| 191 | protected function checkForValidMethods($dataObject, string $type, ?int $relationDepth = 0): string |
||
| 192 | { |
||
| 193 | //too many iterations! |
||
| 194 | if ($relationDepth > (int) $this->Config()->get('max_relation_depth')) { |
||
| 195 | return ''; |
||
| 196 | } |
||
| 197 | |||
| 198 | $validMethods = $this->getValidMethods($type); |
||
| 199 | |||
| 200 | $this->relationTypesCovered[$dataObject->ClassName] = false; |
||
| 201 | |||
| 202 | // quick return |
||
| 203 | if (isset($this->cache[$type][$dataObject->ClassName]) && false !== $this->cache[$type][$dataObject->ClassName]) { |
||
| 204 | $validMethod = $this->cache[$type][$dataObject->ClassName]; |
||
| 205 | if ($dataObject->hasMethod($validMethod)) { |
||
| 206 | $link = $dataObject->{$validMethod}(); |
||
| 207 | if ($link) { |
||
| 208 | return $this->cleanupLink((string) $link); |
||
| 209 | } |
||
| 210 | } |
||
| 211 | // last resort - is there a variable with this name? |
||
| 212 | $link = $dataObject->{$validMethod}; |
||
| 213 | if ($link) { |
||
| 214 | return $this->cleanupLink((string) $link); |
||
| 215 | } |
||
| 216 | } |
||
| 217 | |||
| 218 | if ($this->classCanBeIncluded($dataObject->ClassName)) { |
||
| 219 | if (empty($this->cache[$type][$dataObject->ClassName]) || false !== $this->cache[$type][$dataObject->ClassName]) { |
||
| 220 | foreach ($validMethods as $validMethod) { |
||
| 221 | $outcome = null; |
||
| 222 | if ($dataObject->hasMethod($validMethod)) { |
||
| 223 | $outcome = $dataObject->{$validMethod}(); |
||
| 224 | } elseif (! empty($dataObject->{$validMethod})) { |
||
| 225 | $outcome = $dataObject->{$validMethod}; |
||
| 226 | } |
||
| 227 | |||
| 228 | if ($outcome) { |
||
| 229 | $this->cache[$type][$dataObject->ClassName] = $validMethod; |
||
| 230 | |||
| 231 | return $this->cleanupLink((string) $outcome); |
||
| 232 | } |
||
| 233 | } |
||
| 234 | } |
||
| 235 | |||
| 236 | if ('valid_methods_edit' === $type && class_exists(CMSEditLinkAPI::class)) { |
||
| 237 | $link = CMSEditLinkAPI::find_edit_link_for_object($dataObject); |
||
| 238 | if ($link !== '' && $link !== '0') { |
||
| 239 | return $this->cleanupLink($link); |
||
| 240 | } |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | // there is no match for this one, but we can search relations ... |
||
| 245 | $this->cache[$type][$dataObject->ClassName] = true; |
||
| 246 | ++$relationDepth; |
||
| 247 | foreach ($this->getRelations($dataObject) as $relationName => $relType) { |
||
| 248 | $outcome = null; |
||
| 249 | //TODO: no support for link through relations yet! |
||
| 250 | if (is_array($relType)) { |
||
| 251 | continue; |
||
| 252 | } |
||
| 253 | |||
| 254 | if (! isset($this->relationTypesCovered[$relType])) { |
||
| 255 | $rels = null; |
||
| 256 | if ($dataObject->hasMethod($relationName)) { |
||
| 257 | $rels = $dataObject->{$relationName}(); |
||
| 258 | } else { |
||
| 259 | user_error('Relation ' . print_r($relationName, 1) . ' does not exist on ' . $dataObject->ClassName . ' Relations are: ' . print_r($this->getRelations($dataObject), 1), E_USER_NOTICE); |
||
| 260 | } |
||
| 261 | if ($rels) { |
||
| 262 | if ($rels instanceof DataList && ! $rels instanceof UnsavedRelationList) { |
||
| 263 | $rels = $rels->first(); |
||
| 264 | } |
||
| 265 | if ($rels && $rels instanceof DataObject && $rels->exists()) { |
||
| 266 | $outcome = $this->checkForValidMethods($rels, $type, $relationDepth); |
||
| 267 | } |
||
| 268 | } |
||
| 269 | } |
||
| 270 | |||
| 271 | if ($outcome) { |
||
| 272 | return $this->cleanupLink((string) $outcome); |
||
| 273 | } |
||
| 274 | } |
||
| 275 | |||
| 276 | return ''; |
||
| 277 | } |
||
| 341 |