Conditions | 29 |
Paths | 1260 |
Total Lines | 172 |
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 |
||
82 | public function getPossibleSelections() |
||
83 | { |
||
84 | $data = [ |
||
85 | 'propertyIds' => [], |
||
86 | 'propertyStaticValueIds' => [], |
||
87 | ]; |
||
88 | if ($this->onlyAvailableFilters) { |
||
89 | Yii::beginProfile("onlyAvailableFilters"); |
||
90 | $object = BaseObject::findById($this->objectId); |
||
91 | if (!is_null($object) && isset($this->currentSelections['last_category_id'])) { |
||
92 | |||
93 | $cacheKey = 'FilterWidget: ' . $object->id . ':' . $this->currentSelections['last_category_id'] . ':' |
||
94 | . Json::encode($this->currentSelections['properties']); |
||
95 | $data = Yii::$app->cache->get($cacheKey); |
||
96 | if ($data === false) { |
||
97 | $data = []; |
||
98 | Yii::beginProfile("ObjectIds for this category"); |
||
99 | if (count($this->currentObjects) == 0) { |
||
100 | $psv = []; |
||
101 | array_walk_recursive($this->currentSelections['properties'], function ($item) use (&$psv) { |
||
102 | if (is_array($item) === false) { |
||
103 | $psv[] = $item; |
||
104 | } |
||
105 | }); |
||
106 | $product = Yii::$container->get(Product::class); |
||
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 | } else { |
||
138 | $ids = ArrayHelper::getColumn($this->currentObjects, 'id'); |
||
139 | } |
||
140 | |||
141 | |||
142 | Yii::beginProfile("all PSV ids"); |
||
143 | $data['propertyStaticValueIds'] = []; |
||
144 | if (count($ids) !== 0) { |
||
145 | $q4psv = (new Query()) |
||
146 | ->select('property_static_value_id') |
||
147 | ->from(ObjectStaticValues::tableName()) |
||
148 | ->distinct() |
||
149 | ->where(['object_id' => $object->id]) |
||
150 | ->andWhere('object_model_id in (' . implode(',', $ids) . ')'); |
||
151 | |||
152 | |||
153 | $data['propertyStaticValueIds'] = array_map('intval', $q4psv->column()); |
||
154 | } |
||
155 | Yii::endProfile("all PSV ids"); |
||
156 | |||
157 | |||
158 | $ids = null; |
||
159 | |||
160 | Yii::beginProfile("Property ids from PSV ids"); |
||
161 | $data['propertyIds'] = []; |
||
162 | if (count($data['propertyStaticValueIds']) !== 0) { |
||
163 | $data['propertyIds'] = PropertyStaticValues::find() |
||
164 | ->select('property_id') |
||
165 | ->distinct() |
||
166 | ->where(['dont_filter' => 0]) |
||
167 | ->andWhere('id IN (' . implode(',', $data['propertyStaticValueIds']) . ')') |
||
168 | ->asArray() |
||
169 | ->column(); |
||
170 | } |
||
171 | Yii::endProfile("Property ids from PSV ids"); |
||
172 | |||
173 | Yii::$app->cache->set( |
||
174 | $cacheKey, |
||
175 | $data, |
||
176 | 86400, |
||
177 | new TagDependency([ |
||
178 | 'tags' => [ |
||
179 | \devgroup\TagDependencyHelper\ActiveRecordHelper::getCommonTag($object->object_class) |
||
180 | ], |
||
181 | ]) |
||
182 | ); |
||
183 | $object = null; |
||
184 | } |
||
185 | } |
||
186 | Yii::endProfile("onlyAvailableFilters"); |
||
187 | } |
||
188 | |||
189 | $this->possibleSelections = []; |
||
190 | |||
191 | $groups = PropertyGroup::getForObjectId($this->objectId); |
||
192 | |||
193 | foreach ($groups as $group) { |
||
194 | |||
195 | if ($this->onlyGroupsIds !== null) { |
||
196 | if (in_array($group->id, $this->onlyGroupsIds) === false) { |
||
197 | // skip this group |
||
198 | continue; |
||
199 | } |
||
200 | } |
||
201 | |||
202 | if ($group->is_internal) { |
||
203 | continue; |
||
204 | } |
||
205 | $this->possibleSelections[$group->id] = [ |
||
206 | 'group' => $group, |
||
207 | 'selections' => [], |
||
208 | 'static_selections' => [], |
||
209 | 'dynamic_selections' => [], |
||
210 | ]; |
||
211 | $props = Property::getForGroupId($group->id); |
||
212 | foreach ($props as $p) { |
||
213 | |||
214 | if ($this->onlyAvailableFilters && !in_array($p->id, $data['propertyIds'])) { |
||
215 | if ($this->disableInsteadOfHide === false) { |
||
216 | continue; |
||
217 | } |
||
218 | } |
||
219 | if ($p->dont_filter) { |
||
220 | continue; |
||
221 | } |
||
222 | if ($p->has_static_values) { |
||
223 | $propertyStaticValues = PropertyStaticValues::getValuesForPropertyId($p->id); |
||
224 | foreach ($propertyStaticValues as $key => $propertyStaticValue) { |
||
225 | |||
226 | if ($propertyStaticValue['dont_filter']) { |
||
227 | unset($propertyStaticValues[$key]); |
||
228 | } |
||
229 | } |
||
230 | if ($this->onlyAvailableFilters) { |
||
231 | foreach ($propertyStaticValues as $key => $propertyStaticValue) { |
||
232 | |||
233 | if (!in_array($propertyStaticValue['id'], $data['propertyStaticValueIds'])) { |
||
234 | if ($this->disableInsteadOfHide === true) { |
||
235 | $this->disabled_ids[]=$propertyStaticValue['id']; |
||
236 | } else { |
||
237 | unset($propertyStaticValues[$key]); |
||
238 | } |
||
239 | } |
||
240 | } |
||
241 | } |
||
242 | |||
243 | $this->possibleSelections[$group->id]['static_selections'][$p->id] = $propertyStaticValues; |
||
244 | } elseif ($p->is_column_type_stored && $p->value_type == 'NUMBER') { |
||
245 | $this->possibleSelections[$group->id]['dynamic_selections'][] = $p->id; |
||
246 | } |
||
247 | |||
248 | } |
||
249 | if (count($this->possibleSelections[$group->id]) === 0) { |
||
250 | unset($this->possibleSelections[$group->id]); |
||
251 | } |
||
252 | } |
||
253 | } |
||
254 | } |
||
255 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.