Conditions | 18 |
Paths | 13 |
Total Lines | 88 |
Code Lines | 50 |
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 |
||
210 | protected function _getRelationshipValue($relationship) |
||
211 | { |
||
212 | if (!isset($this->_relatedObjects[$relationship])) { |
||
213 | $rel = static::$_classRelationships[get_called_class()][$relationship]; |
||
214 | |||
215 | if ($rel['type'] == 'one-one') { |
||
216 | if ($value = $this->_getFieldValue($rel['local'])) { |
||
217 | $this->_relatedObjects[$relationship] = $rel['class']::getByField($rel['foreign'], $value); |
||
218 | |||
219 | // hook relationship for invalidation |
||
220 | static::$_classFields[get_called_class()][$rel['local']]['relationships'][$relationship] = true; |
||
221 | } else { |
||
222 | $this->_relatedObjects[$relationship] = null; |
||
223 | } |
||
224 | } elseif ($rel['type'] == 'one-many') { |
||
225 | if (!empty($rel['indexField']) && !$rel['class']::fieldExists($rel['indexField'])) { |
||
226 | $rel['indexField'] = false; |
||
227 | } |
||
228 | |||
229 | $this->_relatedObjects[$relationship] = $rel['class']::getAllByWhere( |
||
230 | array_merge($rel['conditions'], [ |
||
231 | $rel['foreign'] => $this->_getFieldValue($rel['local']), |
||
232 | ]), |
||
233 | [ |
||
234 | 'indexField' => $rel['indexField'], |
||
235 | 'order' => $rel['order'], |
||
236 | 'conditions' => $rel['conditions'], |
||
237 | ] |
||
238 | ); |
||
239 | |||
240 | |||
241 | // hook relationship for invalidation |
||
242 | static::$_classFields[get_called_class()][$rel['local']]['relationships'][$relationship] = true; |
||
243 | } elseif ($rel['type'] == 'context-children') { |
||
244 | if (!empty($rel['indexField']) && !$rel['class']::fieldExists($rel['indexField'])) { |
||
245 | $rel['indexField'] = false; |
||
246 | } |
||
247 | |||
248 | $conditions = array_merge($rel['conditions'], [ |
||
249 | 'ContextClass' => $rel['contextClass'], |
||
250 | 'ContextID' => $this->_getFieldValue($rel['local']), |
||
251 | ]); |
||
252 | |||
253 | $this->_relatedObjects[$relationship] = $rel['class']::getAllByWhere( |
||
254 | $conditions, |
||
255 | [ |
||
256 | 'indexField' => $rel['indexField'], |
||
257 | 'order' => $rel['order'], |
||
258 | ] |
||
259 | ); |
||
260 | |||
261 | // hook relationship for invalidation |
||
262 | static::$_classFields[get_called_class()][$rel['local']]['relationships'][$relationship] = true; |
||
263 | } elseif ($rel['type'] == 'context-parent') { |
||
264 | $className = $this->_getFieldValue($rel['classField']); |
||
265 | $this->_relatedObjects[$relationship] = $className ? $className::getByID($this->_getFieldValue($rel['local'])) : null; |
||
266 | |||
267 | // hook both relationships for invalidation |
||
268 | static::$_classFields[get_called_class()][$rel['classField']]['relationships'][$relationship] = true; |
||
269 | static::$_classFields[get_called_class()][$rel['local']]['relationships'][$relationship] = true; |
||
270 | } elseif ($rel['type'] == 'many-many') { |
||
271 | if (!empty($rel['indexField']) && !$rel['class']::fieldExists($rel['indexField'])) { |
||
272 | $rel['indexField'] = false; |
||
273 | } |
||
274 | |||
275 | // TODO: support indexField, conditions, and order |
||
276 | |||
277 | $this->_relatedObjects[$relationship] = $rel['class']::getAllByQuery( |
||
278 | 'SELECT Related.* FROM `%s` Link JOIN `%s` Related ON (Related.`%s` = Link.%s) WHERE Link.`%s` = %u AND %s', |
||
279 | [ |
||
280 | $rel['linkClass']::$tableName, |
||
281 | $rel['class']::$tableName, |
||
282 | $rel['foreign'], |
||
283 | $rel['linkForeign'], |
||
284 | $rel['linkLocal'], |
||
285 | $this->_getFieldValue($rel['local']), |
||
286 | $rel['conditions'] ? join(' AND ', $rel['conditions']) : '1', |
||
287 | ] |
||
288 | ); |
||
289 | |||
290 | // hook relationship for invalidation |
||
291 | static::$_classFields[get_called_class()][$rel['local']]['relationships'][$relationship] = true; |
||
292 | } elseif ($rel['type'] == 'history' && static::isVersioned()) { |
||
293 | $this->_relatedObjects[$relationship] = $rel['class']::getRevisionsByID($this->getPrimaryKeyValue(), $rel); |
||
294 | } |
||
295 | } |
||
296 | |||
297 | return $this->_relatedObjects[$relationship]; |
||
298 | } |
||
300 |