| Conditions | 28 |
| Paths | 924 |
| Total Lines | 169 |
| Code Lines | 110 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 81 | public function getPossibleSelections() |
||
| 82 | { |
||
| 83 | $data = [ |
||
| 84 | 'propertyIds' => [], |
||
| 85 | 'propertyStaticValueIds' => [], |
||
| 86 | ]; |
||
| 87 | if ($this->onlyAvailableFilters) { |
||
| 88 | Yii::beginProfile("onlyAvailableFilters"); |
||
| 89 | $object = Object::findById($this->objectId); |
||
| 90 | if (!is_null($object) && isset($this->currentSelections['last_category_id'])) { |
||
| 91 | |||
| 92 | $cacheKey = 'FilterWidget: ' . $object->id . ':' . $this->currentSelections['last_category_id'] . ':' |
||
| 93 | . Json::encode($this->currentSelections['properties']); |
||
| 94 | $data = Yii::$app->cache->get($cacheKey); |
||
| 95 | if ($data === false) { |
||
| 96 | $data = []; |
||
| 97 | Yii::beginProfile("ObjectIds for this category"); |
||
| 98 | |||
| 99 | $psv = []; |
||
| 100 | array_walk_recursive($this->currentSelections['properties'], function ($item) use (&$psv) { |
||
| 101 | if (is_array($item)) { |
||
| 102 | continue; |
||
| 103 | } |
||
| 104 | $psv[] = $item; |
||
| 105 | }); |
||
| 106 | |||
| 107 | $query = (new Query) |
||
| 108 | ->select('pc.object_model_id') |
||
| 109 | ->from($object->categories_table_name . ' pc') |
||
| 110 | ->innerJoin(Product::tableName() . ' product', 'product.id = pc.object_model_id') |
||
| 111 | ->where([ |
||
| 112 | 'pc.category_id' => $this->currentSelections['last_category_id'], |
||
| 113 | 'product.active' => 1, |
||
| 114 | ]); |
||
| 115 | |||
| 116 | if (count($this->currentSelections['properties'])) { |
||
| 117 | $query->innerJoin([ |
||
| 118 | 'osvm' => (new Query) |
||
| 119 | ->select('object_model_id') |
||
| 120 | ->distinct() |
||
| 121 | ->from(ObjectStaticValues::tableName()) |
||
| 122 | ->where([ |
||
| 123 | 'object_id' => $object->id, |
||
| 124 | 'property_static_value_id' => $psv |
||
| 125 | ]) |
||
| 126 | ->groupBy('object_model_id') |
||
| 127 | ->having(['count(object_model_id)' => count($psv)]) |
||
| 128 | ], |
||
| 129 | 'osvm.object_model_id = pc.object_model_id' |
||
| 130 | ); |
||
| 131 | } |
||
| 132 | |||
| 133 | Yii::endProfile("ObjectIds for this category"); |
||
| 134 | |||
| 135 | $ids = array_map('intval', $query->column()); |
||
| 136 | $query = null; |
||
| 137 | |||
| 138 | Yii::beginProfile("all PSV ids"); |
||
| 139 | $data['propertyStaticValueIds'] = []; |
||
| 140 | if (count($ids) !== 0) { |
||
| 141 | $q4psv = (new Query()) |
||
| 142 | ->select('property_static_value_id') |
||
| 143 | ->from(ObjectStaticValues::tableName()) |
||
| 144 | ->distinct() |
||
| 145 | ->where(['object_id' => $object->id]) |
||
| 146 | ->andWhere('object_model_id in (' . implode(',', $ids) . ')'); |
||
| 147 | |||
| 148 | |||
| 149 | $data['propertyStaticValueIds'] = array_map('intval', $q4psv->column()); |
||
| 150 | } |
||
| 151 | Yii::endProfile("all PSV ids"); |
||
| 152 | |||
| 153 | |||
| 154 | $ids = null; |
||
| 155 | |||
| 156 | Yii::beginProfile("Property ids from PSV ids"); |
||
| 157 | $data['propertyIds'] = []; |
||
| 158 | if (count($data['propertyStaticValueIds']) !== 0) { |
||
| 159 | $data['propertyIds'] = PropertyStaticValues::find() |
||
| 160 | ->select('property_id') |
||
| 161 | ->distinct() |
||
| 162 | ->where(['dont_filter' => 0]) |
||
| 163 | ->andWhere('id IN (' . implode(',', $data['propertyStaticValueIds']) . ')') |
||
| 164 | ->asArray() |
||
| 165 | ->column(); |
||
| 166 | } |
||
| 167 | Yii::endProfile("Property ids from PSV ids"); |
||
| 168 | |||
| 169 | Yii::$app->cache->set( |
||
| 170 | $cacheKey, |
||
| 171 | $data, |
||
| 172 | 86400, |
||
| 173 | new TagDependency([ |
||
| 174 | 'tags' => [ |
||
| 175 | \devgroup\TagDependencyHelper\ActiveRecordHelper::getCommonTag($object->object_class) |
||
| 176 | ], |
||
| 177 | ]) |
||
| 178 | ); |
||
| 179 | $object = null; |
||
| 180 | } |
||
| 181 | } |
||
| 182 | Yii::endProfile("onlyAvailableFilters"); |
||
| 183 | } |
||
| 184 | |||
| 185 | $this->possibleSelections = []; |
||
| 186 | |||
| 187 | $groups = PropertyGroup::getForObjectId($this->objectId); |
||
| 188 | |||
| 189 | foreach ($groups as $group) { |
||
| 190 | |||
| 191 | if ($this->onlyGroupsIds !== null) { |
||
| 192 | if (in_array($group->id, $this->onlyGroupsIds) === false) { |
||
| 193 | // skip this group |
||
| 194 | continue; |
||
| 195 | } |
||
| 196 | } |
||
| 197 | |||
| 198 | if ($group->is_internal) { |
||
| 199 | continue; |
||
| 200 | } |
||
| 201 | $this->possibleSelections[$group->id] = [ |
||
| 202 | 'group' => $group, |
||
| 203 | 'selections' => [], |
||
| 204 | 'static_selections' => [], |
||
| 205 | 'dynamic_selections' => [], |
||
| 206 | ]; |
||
| 207 | $props = Property::getForGroupId($group->id); |
||
| 208 | foreach ($props as $p) { |
||
| 209 | |||
| 210 | if ($this->onlyAvailableFilters && !in_array($p->id, $data['propertyIds'])) { |
||
| 211 | if ($this->disableInsteadOfHide === false) { |
||
| 212 | continue; |
||
| 213 | } |
||
| 214 | } |
||
| 215 | if ($p->dont_filter) { |
||
| 216 | continue; |
||
| 217 | } |
||
| 218 | if ($p->has_static_values) { |
||
| 219 | $propertyStaticValues = PropertyStaticValues::getValuesForPropertyId($p->id); |
||
| 220 | foreach ($propertyStaticValues as $key => $propertyStaticValue) { |
||
| 221 | |||
| 222 | if ($propertyStaticValue['dont_filter']) { |
||
| 223 | unset($propertyStaticValues[$key]); |
||
| 224 | } |
||
| 225 | } |
||
| 226 | if ($this->onlyAvailableFilters) { |
||
| 227 | foreach ($propertyStaticValues as $key => $propertyStaticValue) { |
||
| 228 | |||
| 229 | if (!in_array($propertyStaticValue['id'], $data['propertyStaticValueIds'])) { |
||
| 230 | if ($this->disableInsteadOfHide === true) { |
||
| 231 | $this->disabled_ids[]=$propertyStaticValue['id']; |
||
| 232 | } else { |
||
| 233 | unset($propertyStaticValues[$key]); |
||
| 234 | } |
||
| 235 | } |
||
| 236 | } |
||
| 237 | } |
||
| 238 | |||
| 239 | $this->possibleSelections[$group->id]['static_selections'][$p->id] = $propertyStaticValues; |
||
| 240 | } elseif ($p->is_column_type_stored && $p->value_type == 'NUMBER') { |
||
| 241 | $this->possibleSelections[$group->id]['dynamic_selections'][] = $p->id; |
||
| 242 | } |
||
| 243 | |||
| 244 | } |
||
| 245 | if (count($this->possibleSelections[$group->id]) === 0) { |
||
| 246 | unset($this->possibleSelections[$group->id]); |
||
| 247 | } |
||
| 248 | } |
||
| 249 | } |
||
| 250 | } |
||
| 251 |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.