| Conditions | 14 |
| Paths | 39 |
| Total Lines | 104 |
| Code Lines | 53 |
| 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 |
||
| 73 | public function pivots($group = null, $type = null, callable $callback = null) |
||
|
|
|||
| 74 | { |
||
| 75 | if ($group === null) { |
||
| 76 | $group = 0; |
||
| 77 | } elseif (!is_string($group)) { |
||
| 78 | throw new InvalidArgumentException('The $group must be a string.'); |
||
| 79 | } |
||
| 80 | |||
| 81 | if ($type === null) { |
||
| 82 | $type = 0; |
||
| 83 | } else { |
||
| 84 | if (!is_string($type)) { |
||
| 85 | throw new InvalidArgumentException('The $type must be a string.'); |
||
| 86 | } |
||
| 87 | |||
| 88 | $type = preg_replace('/([a-z])([A-Z])/', '$1-$2', $type); |
||
| 89 | $type = strtolower(str_replace('\\', '/', $type)); |
||
| 90 | } |
||
| 91 | |||
| 92 | if (isset($this->pivots[$group][$type])) { |
||
| 93 | return $this->pivots[$group][$type]; |
||
| 94 | } |
||
| 95 | |||
| 96 | $sourceObjectType = $this->objType(); |
||
| 97 | $sourceObjectId = $this->id(); |
||
| 98 | |||
| 99 | $pivotProto = $this->modelFactory()->get(Pivot::class); |
||
| 100 | $pivotTable = $pivotProto->source()->table(); |
||
| 101 | |||
| 102 | if (!$pivotProto->source()->tableExists()) { |
||
| 103 | return []; |
||
| 104 | } |
||
| 105 | |||
| 106 | $widget = $this->relationWidget(); |
||
| 107 | |||
| 108 | $targetObjectTypes = $this->targetObjectTypes($group); |
||
| 109 | |||
| 110 | if (!is_array($targetObjectTypes) || empty($targetObjectTypes)) { |
||
| 111 | throw new RuntimeException('No target object types are set for this object.'); |
||
| 112 | } |
||
| 113 | |||
| 114 | $cases = []; |
||
| 115 | $joins = []; |
||
| 116 | $collection = new Collection; |
||
| 117 | foreach ($targetObjectTypes as $targetObjectType => $metadata) { |
||
| 118 | $parts = explode('/', str_replace('-', '_', $targetObjectType)); |
||
| 119 | $targetObjectIdent = end($parts).'_obj'; |
||
| 120 | |||
| 121 | $targetObjectProto = $this->modelFactory()->get($targetObjectType); |
||
| 122 | $targetObjectTable = $targetObjectProto->source()->table(); |
||
| 123 | |||
| 124 | if (!$targetObjectProto->source()->tableExists() || !$targetObjectProto instanceof PivotableInterface) { |
||
| 125 | continue; |
||
| 126 | } |
||
| 127 | |||
| 128 | $query = ' |
||
| 129 | SELECT |
||
| 130 | target_obj.*, |
||
| 131 | pivot_obj.id as pivotObjId, |
||
| 132 | pivot_obj.position as position |
||
| 133 | FROM |
||
| 134 | `'.$targetObjectTable.'` AS target_obj |
||
| 135 | LEFT JOIN |
||
| 136 | `'.$pivotTable.'` AS pivot_obj |
||
| 137 | ON |
||
| 138 | pivot_obj.target_object_id = target_obj.id |
||
| 139 | WHERE |
||
| 140 | 1 = 1'; |
||
| 141 | |||
| 142 | // Disable `active` check in admin |
||
| 143 | if (!$widget instanceof RelationWidget) { |
||
| 144 | $query .= ' |
||
| 145 | AND |
||
| 146 | target_obj.active = 1'; |
||
| 147 | } |
||
| 148 | |||
| 149 | $query .= ' |
||
| 150 | AND |
||
| 151 | pivot_obj.source_object_type = "'.$sourceObjectType.'" |
||
| 152 | AND |
||
| 153 | pivot_obj.source_object_id = "'.$sourceObjectId.'" |
||
| 154 | AND |
||
| 155 | pivot_obj.target_object_type = "'.$targetObjectType.'"'; |
||
| 156 | |||
| 157 | if ($group) { |
||
| 158 | $query .= ' |
||
| 159 | AND |
||
| 160 | pivot_obj.group = "'.$group.'"'; |
||
| 161 | } |
||
| 162 | |||
| 163 | $query .= ' |
||
| 164 | ORDER BY pivot_obj.position'; |
||
| 165 | |||
| 166 | $loader = $this->collectionLoader(); |
||
| 167 | $loader->setModel($targetObjectProto); |
||
| 168 | |||
| 169 | $targetCollection = $loader->loadFromQuery($query); |
||
| 170 | $collection->merge($targetCollection); |
||
| 171 | } |
||
| 172 | |||
| 173 | $this->pivots[$group][$type] = $collection->sortBy('position'); |
||
| 174 | |||
| 175 | return $this->pivots[$group][$type]; |
||
| 176 | } |
||
| 177 | |||
| 315 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.