| Conditions | 16 |
| Paths | 128 |
| Total Lines | 94 |
| Code Lines | 58 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 87 | public function getFieldIntrospection($field) |
||
| 88 | { |
||
| 89 | $fullfield = str_replace('.', '_', $field); |
||
| 90 | $classes = $this->index->getClass(); |
||
| 91 | |||
| 92 | $found = []; |
||
| 93 | |||
| 94 | if (strpos($field, '.') !== false) { |
||
| 95 | $lookups = explode('.', $field); |
||
| 96 | $field = array_pop($lookups); |
||
| 97 | |||
| 98 | foreach ($lookups as $lookup) { |
||
| 99 | $next = []; |
||
| 100 | |||
| 101 | foreach ($classes as $source) { |
||
| 102 | list($class, $singleton, $next) = $this->getRelationIntrospection($source, $lookup, $next); |
||
| 103 | } |
||
| 104 | |||
| 105 | if (!$next) { |
||
| 106 | return $next; |
||
| 107 | } // Early out to avoid excessive empty looping |
||
| 108 | $classes = $next; |
||
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 112 | foreach ($classes as $class => $fieldoptions) { |
||
| 113 | if (is_int($class)) { |
||
| 114 | $class = $fieldoptions; |
||
| 115 | $fieldoptions = []; |
||
| 116 | } |
||
| 117 | $class = $this->getSourceName($class); |
||
| 118 | $dataclasses = self::getHierarchy($class); |
||
| 119 | |||
| 120 | while (count($dataclasses)) { |
||
| 121 | $dataclass = array_shift($dataclasses); |
||
| 122 | $type = null; |
||
| 123 | |||
| 124 | $fields = DataObject::getSchema()->databaseFields($class); |
||
| 125 | |||
| 126 | if (isset($fields[$field])) { |
||
| 127 | $type = $fields[$field]; |
||
| 128 | $fieldoptions['lookup_chain'][] = [ |
||
| 129 | 'call' => 'property', |
||
| 130 | 'property' => $field |
||
| 131 | ]; |
||
| 132 | } else { |
||
| 133 | $singleton = singleton($dataclass); |
||
| 134 | |||
| 135 | if ($singleton->hasMethod("get$field") || $singleton->hasField($field)) { |
||
| 136 | $type = $singleton->castingClass($field); |
||
| 137 | if (!$type) { |
||
| 138 | $type = 'String'; |
||
| 139 | } |
||
| 140 | |||
| 141 | if ($singleton->hasMethod("get$field")) { |
||
| 142 | $fieldoptions['lookup_chain'][] = [ |
||
| 143 | 'call' => 'method', |
||
| 144 | 'method' => "get$field" |
||
| 145 | ]; |
||
| 146 | } else { |
||
| 147 | $fieldoptions['lookup_chain'][] = [ |
||
| 148 | 'call' => 'property', |
||
| 149 | 'property' => $field |
||
| 150 | ]; |
||
| 151 | } |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | if ($type) { |
||
| 156 | // Don't search through child classes of a class we matched on. TODO: Should we? |
||
| 157 | $dataclasses = array_diff($dataclasses, array_values(ClassInfo::subclassesFor($dataclass))); |
||
| 158 | // Trim arguments off the type string |
||
| 159 | if (preg_match('/^(\w+)\(/', $type, $match)) { |
||
| 160 | $type = $match[1]; |
||
| 161 | } |
||
| 162 | // Get the origin |
||
| 163 | $origin = isset($fieldoptions['origin']) ?? $dataclass; |
||
| 164 | |||
| 165 | $origin = ClassInfo::shortName($origin); |
||
| 166 | $found["{$origin}_{$fullfield}"] = array( |
||
| 167 | 'name' => "{$origin}_{$fullfield}", |
||
| 168 | 'field' => $field, |
||
| 169 | 'fullfield' => $fullfield, |
||
| 170 | 'origin' => $origin, |
||
| 171 | 'class' => $dataclass, |
||
| 172 | 'lookup_chain' => $fieldoptions['lookup_chain'], |
||
| 173 | 'type' => $type, |
||
| 174 | 'multi_valued' => isset($fieldoptions['multi_valued']) ? true : false, |
||
| 175 | ); |
||
| 176 | } |
||
| 177 | } |
||
| 178 | } |
||
| 179 | |||
| 180 | return $found; |
||
| 181 | } |
||
| 317 |