Conditions | 26 |
Paths | 15559 |
Total Lines | 162 |
Lines | 10 |
Ratio | 6.17 % |
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 |
||
26 | public static function appendPropertiesFilters( |
||
27 | $object, |
||
28 | &$query, |
||
29 | $values_by_property_id, |
||
30 | $dynamic_values_by_property_id = [], |
||
31 | $multiFilterMode = ConfigConfigurationModel::MULTI_FILTER_MODE_INTERSECTION |
||
32 | ) |
||
33 | { |
||
34 | |||
35 | // сначала сгруппируем свойства по типу хранения |
||
36 | $by_storage = [ |
||
37 | 'eav' => [], |
||
38 | 'table_inheritance' => [], |
||
39 | 'static_values' => [], |
||
40 | ]; |
||
41 | $dynamic_by_storage = [ |
||
42 | 'eav' => [], |
||
43 | 'table_inheritance' => [], |
||
44 | ]; |
||
45 | foreach ($values_by_property_id as $property_id => $values) { |
||
46 | // values может быть просто строкой(одно значение), а может уже прийти массивом |
||
47 | $values = (array)$values; |
||
48 | $property = Property::findById($property_id); |
||
49 | if ($property === null) { |
||
50 | continue; |
||
51 | } |
||
52 | if ($property->is_eav) { |
||
53 | $by_storage['eav'][] = [ |
||
54 | 'property' => $property, |
||
55 | 'values' => $values, |
||
56 | ]; |
||
57 | } elseif ($property->is_column_type_stored) { |
||
58 | $by_storage['table_inheritance'][] = [ |
||
59 | 'property' => $property, |
||
60 | 'values' => $values, |
||
61 | ]; |
||
62 | } elseif ($property->has_static_values) { |
||
63 | switch ($multiFilterMode) { |
||
64 | case ConfigConfigurationModel::MULTI_FILTER_MODE_UNION: |
||
65 | $by_storage['static_values'][] = $values; |
||
66 | break; |
||
67 | default: |
||
68 | $by_storage['static_values'] = array_merge($by_storage['static_values'], $values); |
||
69 | } |
||
70 | } else { |
||
71 | throw new \Exception("Wrong property type for " . $property->id); |
||
72 | } |
||
73 | } |
||
74 | foreach ($dynamic_values_by_property_id as $property_id => $values) { |
||
75 | $property = Property::findById($property_id); |
||
76 | if ($property) { |
||
77 | if ($property->is_eav) { |
||
78 | $dynamic_by_storage['eav'][] = [ |
||
79 | 'property' => $property, |
||
80 | 'values' => $values, |
||
81 | ]; |
||
82 | } elseif ($property->is_column_type_stored) { |
||
83 | $dynamic_by_storage['table_inheritance'][] = [ |
||
84 | 'property' => $property, |
||
85 | 'values' => $values, |
||
86 | ]; |
||
87 | } else { |
||
88 | throw new \Exception("Wrong property type for " . $property->id); |
||
89 | } |
||
90 | } |
||
91 | } |
||
92 | |||
93 | $ti_clauses = []; |
||
94 | $join_table_name = "PropertiesJoinTable1"; |
||
95 | |||
96 | foreach ($by_storage['table_inheritance'] as $item) { |
||
97 | $property = $item['property']; |
||
98 | $or_clauses = []; |
||
99 | foreach ($item['values'] as $val) { |
||
100 | $or_clauses[] = "$join_table_name." . |
||
101 | Yii::$app->db->quoteColumnName($property->key) . " = " . |
||
102 | Yii::$app->db->quoteValue($val); |
||
103 | } |
||
104 | |||
105 | $ti_clauses[] = implode(" OR ", $or_clauses); |
||
106 | |||
107 | } |
||
108 | foreach ($dynamic_by_storage['table_inheritance'] as $item) { |
||
109 | $property = $item['property']; |
||
110 | $clauses = []; |
||
111 | View Code Duplication | if (isset($item['values']['min']) && strlen($item['values']['min'])) { |
|
112 | $clauses[] = "$join_table_name." . |
||
113 | Yii::$app->db->quoteColumnName($property->key) . " >= " . |
||
114 | Yii::$app->db->quoteValue((double)($item['values']['min'])); |
||
115 | } |
||
116 | View Code Duplication | if (isset($item['values']['max']) && strlen($item['values']['max'])) { |
|
117 | $clauses[] = "$join_table_name." . |
||
118 | Yii::$app->db->quoteColumnName($property->key) . " <= " . |
||
119 | Yii::$app->db->quoteValue((double)($item['values']['max'])); |
||
120 | } |
||
121 | if (!empty($clauses)) { |
||
122 | $ti_clauses[] = '(' . implode(" AND ", $clauses) . ')'; |
||
123 | } |
||
124 | } |
||
125 | if (count($ti_clauses) > 0) { |
||
126 | $ti_clauses = implode(" AND ", $ti_clauses); |
||
127 | |||
128 | $query = $query->innerJoin( |
||
129 | $object->column_properties_table_name . " $join_table_name", |
||
130 | "$join_table_name.object_model_id = " . |
||
131 | Yii::$app->db->quoteTableName($object->object_table_name) . ".id " . |
||
132 | " AND " . $ti_clauses |
||
133 | ); |
||
134 | } |
||
135 | |||
136 | |||
137 | if (count($by_storage['static_values'])) { |
||
138 | |||
139 | switch ($multiFilterMode) { |
||
140 | case ConfigConfigurationModel::MULTI_FILTER_MODE_UNION: |
||
141 | $subQuery = new Query(); |
||
142 | $lastQuery = $subQuery; |
||
143 | $counter = 0; |
||
144 | foreach ($by_storage['static_values'] as $staticValues) { |
||
145 | $tmp = self::createSubQuery($object, $staticValues, ++$counter, $multiFilterMode); |
||
146 | $lastQuery->innerJoin( |
||
147 | ['osvm' . $counter => $tmp] |
||
148 | , 'osvm' . $counter . '.object_model_id = ' . 'osv' . ($counter - 1) . '.object_model_id' |
||
149 | ); |
||
150 | $lastQuery = $tmp; |
||
151 | } |
||
152 | |||
153 | $query->innerJoin( |
||
154 | $subQuery->join[0][1] |
||
155 | , 'osvm1.object_model_id = ' . Yii::$app->db->quoteTableName($object->object_table_name) . '.id' |
||
156 | ); |
||
157 | |||
158 | break; |
||
159 | default: |
||
160 | $query->innerJoin( |
||
161 | ['osvm' => self::createSubQuery($object, $by_storage['static_values'])] |
||
162 | , 'osvm.object_model_id = ' . Yii::$app->db->quoteTableName($object->object_table_name) . '.id' |
||
163 | ); |
||
164 | } |
||
165 | } |
||
166 | |||
167 | if (count($by_storage['eav'])) { |
||
168 | foreach ($by_storage['eav'] as $item) { |
||
169 | $joinTableName = 'EAVJoinTable' . $item['property']->id; |
||
170 | if (count($item['values']) > 0) { |
||
171 | $query = $query->innerJoin( |
||
172 | $object->eav_table_name . " " . $joinTableName, |
||
173 | "$joinTableName.object_model_id = " . |
||
174 | Yii::$app->db->quoteTableName($object->object_table_name) . ".id " |
||
175 | )->andWhere( |
||
176 | new Expression( |
||
177 | '`' . $joinTableName . '`.`value` in (' . |
||
178 | implode(', ', array_map('intval', $item['values'])) . |
||
179 | ') AND `' . $joinTableName . '`.`key` = "' . $item['property']->key . '"' |
||
180 | ) |
||
181 | ); |
||
182 | } |
||
183 | } |
||
184 | } |
||
185 | |||
186 | |||
187 | } |
||
188 | |||
229 |