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