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