Complex classes like Sort often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Sort, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
80 | class Sort extends BaseObject |
||
81 | { |
||
82 | /** |
||
83 | * @var bool whether the sorting can be applied to multiple attributes simultaneously. |
||
84 | * Defaults to `false`, which means each time the data can only be sorted by one attribute. |
||
85 | */ |
||
86 | public $enableMultiSort = false; |
||
87 | /** |
||
88 | * @var array list of attributes that are allowed to be sorted. Its syntax can be |
||
89 | * described using the following example: |
||
90 | * |
||
91 | * ```php |
||
92 | * [ |
||
93 | * 'age', |
||
94 | * 'name' => [ |
||
95 | * 'asc' => ['first_name' => SORT_ASC, 'last_name' => SORT_ASC], |
||
96 | * 'desc' => ['first_name' => SORT_DESC, 'last_name' => SORT_DESC], |
||
97 | * 'default' => SORT_DESC, |
||
98 | * 'label' => 'Name', |
||
99 | * ], |
||
100 | * ] |
||
101 | * ``` |
||
102 | * |
||
103 | * In the above, two attributes are declared: `age` and `name`. The `age` attribute is |
||
104 | * a simple attribute which is equivalent to the following: |
||
105 | * |
||
106 | * ```php |
||
107 | * 'age' => [ |
||
108 | * 'asc' => ['age' => SORT_ASC], |
||
109 | * 'desc' => ['age' => SORT_DESC], |
||
110 | * 'default' => SORT_ASC, |
||
111 | * 'label' => Inflector::camel2words('age'), |
||
112 | * ] |
||
113 | * ``` |
||
114 | * |
||
115 | * Since 2.0.12 particular sort direction can be also specified as direct sort expression, like following: |
||
116 | * |
||
117 | * ```php |
||
118 | * 'name' => [ |
||
119 | * 'asc' => '[[last_name]] ASC NULLS FIRST', // PostgreSQL specific feature |
||
120 | * 'desc' => '[[last_name]] DESC NULLS LAST', |
||
121 | * ] |
||
122 | * ``` |
||
123 | * |
||
124 | * The `name` attribute is a composite attribute: |
||
125 | * |
||
126 | * - The `name` key represents the attribute name which will appear in the URLs leading |
||
127 | * to sort actions. |
||
128 | * - The `asc` and `desc` elements specify how to sort by the attribute in ascending |
||
129 | * and descending orders, respectively. Their values represent the actual columns and |
||
130 | * the directions by which the data should be sorted by. |
||
131 | * - The `default` element specifies by which direction the attribute should be sorted |
||
132 | * if it is not currently sorted (the default value is ascending order). |
||
133 | * - The `label` element specifies what label should be used when calling [[link()]] to create |
||
134 | * a sort link. If not set, [[Inflector::camel2words()]] will be called to get a label. |
||
135 | * Note that it will not be HTML-encoded. |
||
136 | * |
||
137 | * Note that if the Sort object is already created, you can only use the full format |
||
138 | * to configure every attribute. Each attribute must include these elements: `asc` and `desc`. |
||
139 | */ |
||
140 | public $attributes = []; |
||
141 | /** |
||
142 | * @var string the name of the parameter that specifies which attributes to be sorted |
||
143 | * in which direction. Defaults to `sort`. |
||
144 | * @see params |
||
145 | */ |
||
146 | public $sortParam = 'sort'; |
||
147 | /** |
||
148 | * @var array the order that should be used when the current request does not specify any order. |
||
149 | * The array keys are attribute names and the array values are the corresponding sort directions. For example, |
||
150 | * |
||
151 | * ```php |
||
152 | * [ |
||
153 | * 'name' => SORT_ASC, |
||
154 | * 'created_at' => SORT_DESC, |
||
155 | * ] |
||
156 | * ``` |
||
157 | * |
||
158 | * @see attributeOrders |
||
159 | */ |
||
160 | public $defaultOrder; |
||
161 | /** |
||
162 | * @var string the route of the controller action for displaying the sorted contents. |
||
163 | * If not set, it means using the currently requested route. |
||
164 | */ |
||
165 | public $route; |
||
166 | /** |
||
167 | * @var string the character used to separate different attributes that need to be sorted by. |
||
168 | */ |
||
169 | public $separator = ','; |
||
170 | /** |
||
171 | * @var array parameters (name => value) that should be used to obtain the current sort directions |
||
172 | * and to create new sort URLs. If not set, `$_GET` will be used instead. |
||
173 | * |
||
174 | * In order to add hash to all links use `array_merge($_GET, ['#' => 'my-hash'])`. |
||
175 | * |
||
176 | * The array element indexed by [[sortParam]] is considered to be the current sort directions. |
||
177 | * If the element does not exist, the [[defaultOrder|default order]] will be used. |
||
178 | * |
||
179 | * @see sortParam |
||
180 | * @see defaultOrder |
||
181 | */ |
||
182 | public $params; |
||
183 | /** |
||
184 | * @var \yii\web\UrlManager the URL manager used for creating sort URLs. If not set, |
||
185 | * the `urlManager` application component will be used. |
||
186 | */ |
||
187 | public $urlManager; |
||
188 | |||
189 | |||
190 | /** |
||
191 | * Normalizes the [[attributes]] property. |
||
192 | */ |
||
193 | 66 | public function init() |
|
194 | { |
||
195 | 66 | $attributes = []; |
|
196 | 66 | foreach ($this->attributes as $name => $attribute) { |
|
197 | 14 | if (!is_array($attribute)) { |
|
198 | 10 | $attributes[$attribute] = [ |
|
199 | 10 | 'asc' => [$attribute => SORT_ASC], |
|
200 | 10 | 'desc' => [$attribute => SORT_DESC], |
|
201 | ]; |
||
202 | 11 | } elseif (!isset($attribute['asc'], $attribute['desc'])) { |
|
203 | $attributes[$name] = array_merge([ |
||
204 | 'asc' => [$name => SORT_ASC], |
||
205 | 'desc' => [$name => SORT_DESC], |
||
206 | ], $attribute); |
||
207 | } else { |
||
208 | 14 | $attributes[$name] = $attribute; |
|
209 | } |
||
210 | } |
||
211 | 66 | $this->attributes = $attributes; |
|
212 | 66 | } |
|
213 | |||
214 | /** |
||
215 | * Returns the columns and their corresponding sort directions. |
||
216 | * @param bool $recalculate whether to recalculate the sort directions |
||
217 | * @return array the columns (keys) and their corresponding sort directions (values). |
||
218 | * This can be passed to [[\yii\db\Query::orderBy()]] to construct a DB query. |
||
219 | */ |
||
220 | 57 | public function getOrders($recalculate = false) |
|
238 | |||
239 | /** |
||
240 | * @var array the currently requested sort order as computed by [[getAttributeOrders]]. |
||
241 | */ |
||
242 | private $_attributeOrders; |
||
243 | |||
244 | /** |
||
245 | * Returns the currently requested sort information. |
||
246 | * @param bool $recalculate whether to recalculate the sort directions |
||
247 | * @return array sort directions indexed by attribute names. |
||
248 | * Sort direction can be either `SORT_ASC` for ascending order or |
||
249 | * `SORT_DESC` for descending order. |
||
250 | */ |
||
251 | 66 | public function getAttributeOrders($recalculate = false) |
|
283 | |||
284 | /** |
||
285 | * Parses the value of [[sortParam]] into an array of sort attributes. |
||
286 | * |
||
287 | * The format must be the attribute name only for ascending |
||
288 | * or the attribute name prefixed with `-` for descending. |
||
289 | * |
||
290 | * For example the following return value will result in ascending sort by |
||
291 | * `category` and descending sort by `created_at`: |
||
292 | * |
||
293 | * ```php |
||
294 | * [ |
||
295 | * 'category', |
||
296 | * '-created_at' |
||
297 | * ] |
||
298 | * ``` |
||
299 | * |
||
300 | * @param string $param the value of the [[sortParam]]. |
||
301 | * @return array the valid sort attributes. |
||
302 | * @since 2.0.12 |
||
303 | * @see $separator for the attribute name separator. |
||
304 | * @see $sortParam |
||
305 | */ |
||
306 | 7 | protected function parseSortParam($param) |
|
310 | |||
311 | /** |
||
312 | * Sets up the currently sort information. |
||
313 | * @param array|null $attributeOrders sort directions indexed by attribute names. |
||
314 | * Sort direction can be either `SORT_ASC` for ascending order or |
||
315 | * `SORT_DESC` for descending order. |
||
316 | * @param bool $validate whether to validate given attribute orders against [[attributes]] and [[enableMultiSort]]. |
||
317 | * If validation is enabled incorrect entries will be removed. |
||
318 | * @since 2.0.10 |
||
319 | */ |
||
320 | 1 | public function setAttributeOrders($attributeOrders, $validate = true) |
|
336 | |||
337 | /** |
||
338 | * Returns the sort direction of the specified attribute in the current request. |
||
339 | * @param string $attribute the attribute name |
||
340 | * @return bool|null Sort direction of the attribute. Can be either `SORT_ASC` |
||
341 | * for ascending order or `SORT_DESC` for descending order. Null is returned |
||
342 | * if the attribute is invalid or does not need to be sorted. |
||
343 | */ |
||
344 | 5 | public function getAttributeOrder($attribute) |
|
350 | |||
351 | /** |
||
352 | * Generates a hyperlink that links to the sort action to sort by the specified attribute. |
||
353 | * Based on the sort direction, the CSS class of the generated hyperlink will be appended |
||
354 | * with "asc" or "desc". |
||
355 | * @param string $attribute the attribute name by which the data should be sorted by. |
||
356 | * @param array $options additional HTML attributes for the hyperlink tag. |
||
357 | * There is one special attribute `label` which will be used as the label of the hyperlink. |
||
358 | * If this is not set, the label defined in [[attributes]] will be used. |
||
359 | * If no label is defined, [[\yii\helpers\Inflector::camel2words()]] will be called to get a label. |
||
360 | * Note that it will not be HTML-encoded. |
||
361 | * @return string the generated hyperlink |
||
362 | * @throws InvalidConfigException if the attribute is unknown |
||
363 | */ |
||
364 | 3 | public function link($attribute, $options = []) |
|
391 | |||
392 | /** |
||
393 | * Creates a URL for sorting the data by the specified attribute. |
||
394 | * This method will consider the current sorting status given by [[attributeOrders]]. |
||
395 | * For example, if the current page already sorts the data by the specified attribute in ascending order, |
||
396 | * then the URL created will lead to a page that sorts the data by the specified attribute in descending order. |
||
397 | * @param string $attribute the attribute name |
||
398 | * @param bool $absolute whether to create an absolute URL. Defaults to `false`. |
||
399 | * @return string the URL for sorting. False if the attribute is invalid. |
||
400 | * @throws InvalidConfigException if the attribute is unknown |
||
401 | * @see attributeOrders |
||
402 | * @see params |
||
403 | */ |
||
404 | 4 | public function createUrl($attribute, $absolute = false) |
|
405 | { |
||
406 | 4 | if (($params = $this->params) === null) { |
|
407 | 2 | $request = Yii::$app->getRequest(); |
|
408 | 2 | $params = $request instanceof Request ? $request->getQueryParams() : []; |
|
409 | } |
||
410 | 4 | $params[$this->sortParam] = $this->createSortParam($attribute); |
|
411 | 4 | $params[0] = $this->route === null ? Yii::$app->controller->getRoute() : $this->route; |
|
412 | 4 | $urlManager = $this->urlManager === null ? Yii::$app->getUrlManager() : $this->urlManager; |
|
413 | 4 | if ($absolute) { |
|
414 | return $urlManager->createAbsoluteUrl($params); |
||
415 | } |
||
416 | |||
417 | 4 | return $urlManager->createUrl($params); |
|
418 | } |
||
419 | |||
420 | /** |
||
421 | * Creates the sort variable for the specified attribute. |
||
422 | * The newly created sort variable can be used to create a URL that will lead to |
||
423 | * sorting by the specified attribute. |
||
424 | * @param string $attribute the attribute name |
||
425 | * @return string the value of the sort variable |
||
426 | * @throws InvalidConfigException if the specified attribute is not defined in [[attributes]] |
||
427 | */ |
||
428 | 5 | public function createSortParam($attribute) |
|
455 | |||
456 | /** |
||
457 | * Returns a value indicating whether the sort definition supports sorting by the named attribute. |
||
458 | * @param string $name the attribute name |
||
459 | * @return bool whether the sort definition supports sorting by the named attribute. |
||
460 | */ |
||
461 | public function hasAttribute($name) |
||
465 | } |
||
466 |
Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.
To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.
The function can be called with either null or an array for the parameter
$needle
but will only accept an array as$haystack
.