Complex classes like GridView 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 GridView, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
47 | class GridView extends BaseListView |
||
48 | { |
||
49 | const FILTER_POS_HEADER = 'header'; |
||
50 | const FILTER_POS_FOOTER = 'footer'; |
||
51 | const FILTER_POS_BODY = 'body'; |
||
52 | |||
53 | /** |
||
54 | * @var string the default data column class if the class name is not explicitly specified when configuring a data column. |
||
55 | * Defaults to 'yii\grid\DataColumn'. |
||
56 | */ |
||
57 | public $dataColumnClass = DataColumn::class; |
||
58 | /** |
||
59 | * @var string the caption of the grid table |
||
60 | * @see captionOptions |
||
61 | */ |
||
62 | public $caption; |
||
63 | /** |
||
64 | * @var array the HTML attributes for the caption element. |
||
65 | * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
||
66 | * @see caption |
||
67 | */ |
||
68 | public $captionOptions = []; |
||
69 | /** |
||
70 | * @var array the HTML attributes for the grid table element. |
||
71 | * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
||
72 | */ |
||
73 | public $tableOptions = ['class' => 'table table-striped table-bordered']; |
||
74 | /** |
||
75 | * @var array the HTML attributes for the container tag of the grid view. |
||
76 | * The "tag" element specifies the tag name of the container element and defaults to "div". |
||
77 | * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
||
78 | */ |
||
79 | public $options = ['class' => 'grid-view']; |
||
80 | /** |
||
81 | * @var array the HTML attributes for the table header row. |
||
82 | * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
||
83 | */ |
||
84 | public $headerRowOptions = []; |
||
85 | /** |
||
86 | * @var array the HTML attributes for the table footer row. |
||
87 | * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
||
88 | */ |
||
89 | public $footerRowOptions = []; |
||
90 | /** |
||
91 | * @var array|Closure the HTML attributes for the table body rows. This can be either an array |
||
92 | * specifying the common HTML attributes for all body rows, or an anonymous function that |
||
93 | * returns an array of the HTML attributes. The anonymous function will be called once for every |
||
94 | * data model returned by [[dataProvider]]. It should have the following signature: |
||
95 | * |
||
96 | * ```php |
||
97 | * function ($model, $key, $index, $grid) |
||
98 | * ``` |
||
99 | * |
||
100 | * - `$model`: the current data model being rendered |
||
101 | * - `$key`: the key value associated with the current data model |
||
102 | * - `$index`: the zero-based index of the data model in the model array returned by [[dataProvider]] |
||
103 | * - `$grid`: the GridView object |
||
104 | * |
||
105 | * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
||
106 | */ |
||
107 | public $rowOptions = []; |
||
108 | /** |
||
109 | * @var Closure an anonymous function that is called once BEFORE rendering each data model. |
||
110 | * It should have the similar signature as [[rowOptions]]. The return result of the function |
||
111 | * will be rendered directly. |
||
112 | */ |
||
113 | public $beforeRow; |
||
114 | /** |
||
115 | * @var Closure an anonymous function that is called once AFTER rendering each data model. |
||
116 | * It should have the similar signature as [[rowOptions]]. The return result of the function |
||
117 | * will be rendered directly. |
||
118 | */ |
||
119 | public $afterRow; |
||
120 | /** |
||
121 | * @var bool whether to show the header section of the grid table. |
||
122 | */ |
||
123 | public $showHeader = true; |
||
124 | /** |
||
125 | * @var bool whether to show the footer section of the grid table. |
||
126 | */ |
||
127 | public $showFooter = false; |
||
128 | /** |
||
129 | * @var bool whether to place footer after body in DOM if $showFooter is true |
||
130 | * @since 2.0.14 |
||
131 | */ |
||
132 | public $placeFooterAfterBody = false; |
||
133 | /** |
||
134 | * @var bool whether to show the grid view if [[dataProvider]] returns no data. |
||
135 | */ |
||
136 | public $showOnEmpty = true; |
||
137 | /** |
||
138 | * @var array|Formatter the formatter used to format model attribute values into displayable texts. |
||
139 | * This can be either an instance of [[Formatter]] or an configuration array for creating the [[Formatter]] |
||
140 | * instance. If this property is not set, the "formatter" application component will be used. |
||
141 | */ |
||
142 | public $formatter; |
||
143 | /** |
||
144 | * @var array grid column configuration. Each array element represents the configuration |
||
145 | * for one particular grid column. For example, |
||
146 | * |
||
147 | * ```php |
||
148 | * [ |
||
149 | * ['class' => \yii\grid\SerialColumn::class], |
||
150 | * [ |
||
151 | * 'class' => \yii\grid\DataColumn::class, // this line is optional |
||
152 | * 'attribute' => 'name', |
||
153 | * 'format' => 'text', |
||
154 | * 'label' => 'Name', |
||
155 | * ], |
||
156 | * ['class' => \yii\grid\CheckboxColumn::class], |
||
157 | * ] |
||
158 | * ``` |
||
159 | * |
||
160 | * If a column is of class [[DataColumn]], the "class" element can be omitted. |
||
161 | * |
||
162 | * As a shortcut format, a string may be used to specify the configuration of a data column |
||
163 | * which only contains [[DataColumn::attribute|attribute]], [[DataColumn::format|format]], |
||
164 | * and/or [[DataColumn::label|label]] options: `"attribute:format:label"`. |
||
165 | * For example, the above "name" column can also be specified as: `"name:text:Name"`. |
||
166 | * Both "format" and "label" are optional. They will take default values if absent. |
||
167 | * |
||
168 | * Using the shortcut format the configuration for columns in simple cases would look like this: |
||
169 | * |
||
170 | * ```php |
||
171 | * [ |
||
172 | * 'id', |
||
173 | * 'amount:currency:Total Amount', |
||
174 | * 'created_at:datetime', |
||
175 | * ] |
||
176 | * ``` |
||
177 | * |
||
178 | * When using a [[dataProvider]] with active records, you can also display values from related records, |
||
179 | * e.g. the `name` attribute of the `author` relation: |
||
180 | * |
||
181 | * ```php |
||
182 | * // shortcut syntax |
||
183 | * 'author.name', |
||
184 | * // full syntax |
||
185 | * [ |
||
186 | * 'attribute' => 'author.name', |
||
187 | * // ... |
||
188 | * ] |
||
189 | * ``` |
||
190 | */ |
||
191 | public $columns = []; |
||
192 | /** |
||
193 | * @var string the HTML display when the content of a cell is empty. |
||
194 | * This property is used to render cells that have no defined content, |
||
195 | * e.g. empty footer or filter cells. |
||
196 | * |
||
197 | * Note that this is not used by the [[DataColumn]] if a data item is `null`. In that case |
||
198 | * the [[\yii\i18n\Formatter::nullDisplay|nullDisplay]] property of the [[formatter]] will |
||
199 | * be used to indicate an empty data value. |
||
200 | */ |
||
201 | public $emptyCell = ' '; |
||
202 | /** |
||
203 | * @var \yii\base\Model the model that keeps the user-entered filter data. When this property is set, |
||
204 | * the grid view will enable column-based filtering. Each data column by default will display a text field |
||
205 | * at the top that users can fill in to filter the data. |
||
206 | * |
||
207 | * Note that in order to show an input field for filtering, a column must have its [[DataColumn::attribute]] |
||
208 | * property set and the attribute should be active in the current scenario of $filterModel or have |
||
209 | * [[DataColumn::filter]] set as the HTML code for the input field. |
||
210 | * |
||
211 | * When this property is not set (null) the filtering feature is disabled. |
||
212 | */ |
||
213 | public $filterModel; |
||
214 | /** |
||
215 | * @var string|array the URL for returning the filtering result. [[Url::to()]] will be called to |
||
216 | * normalize the URL. If not set, the current controller action will be used. |
||
217 | * When the user makes change to any filter input, the current filtering inputs will be appended |
||
218 | * as GET parameters to this URL. |
||
219 | */ |
||
220 | public $filterUrl; |
||
221 | /** |
||
222 | * @var string whether the filters should be displayed in the grid view. Valid values include: |
||
223 | * |
||
224 | * - [[FILTER_POS_HEADER]]: the filters will be displayed on top of each column's header cell. |
||
225 | * - [[FILTER_POS_BODY]]: the filters will be displayed right below each column's header cell. |
||
226 | * - [[FILTER_POS_FOOTER]]: the filters will be displayed below each column's footer cell. |
||
227 | */ |
||
228 | public $filterPosition = self::FILTER_POS_BODY; |
||
229 | /** |
||
230 | * @var array the HTML attributes for the filter row element. |
||
231 | * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
||
232 | */ |
||
233 | public $filterRowOptions = ['class' => 'filters']; |
||
234 | /** |
||
235 | * @var array the options for rendering the filter error summary. |
||
236 | * Please refer to [[Html::errorSummary()]] for more details about how to specify the options. |
||
237 | * @see renderErrors() |
||
238 | */ |
||
239 | public $filterErrorSummaryOptions = ['class' => 'error-summary']; |
||
240 | /** |
||
241 | * @var array the options for rendering every filter error message. |
||
242 | * This is mainly used by [[Html::error()]] when rendering an error message next to every filter input field. |
||
243 | */ |
||
244 | public $filterErrorOptions = ['class' => 'help-block']; |
||
245 | /** |
||
246 | * @var string the layout that determines how different sections of the grid view should be organized. |
||
247 | * The following tokens will be replaced with the corresponding section contents: |
||
248 | * |
||
249 | * - `{summary}`: the summary section. See [[renderSummary()]]. |
||
250 | * - `{errors}`: the filter model error summary. See [[renderErrors()]]. |
||
251 | * - `{items}`: the list items. See [[renderItems()]]. |
||
252 | * - `{sorter}`: the sorter. See [[renderSorter()]]. |
||
253 | * - `{pager}`: the pager. See [[renderPager()]]. |
||
254 | */ |
||
255 | public $layout = "{summary}\n{items}\n{pager}"; |
||
256 | |||
257 | |||
258 | /** |
||
259 | * Initializes the grid view. |
||
260 | * This method will initialize required property values and instantiate [[columns]] objects. |
||
261 | */ |
||
262 | 22 | public function init() |
|
279 | |||
280 | /** |
||
281 | * Renders validator errors of filter model. |
||
282 | * @return string the rendering result. |
||
283 | */ |
||
284 | public function renderErrors() |
||
292 | |||
293 | /** |
||
294 | * {@inheritdoc} |
||
295 | */ |
||
296 | 6 | public function renderSection($name) |
|
305 | |||
306 | /** |
||
307 | * Renders the data models for the grid view. |
||
308 | */ |
||
309 | 6 | public function renderItems() |
|
338 | |||
339 | /** |
||
340 | * Renders the caption element. |
||
341 | * @return bool|string the rendered caption element or `false` if no caption element should be rendered. |
||
342 | */ |
||
343 | 6 | public function renderCaption() |
|
351 | |||
352 | /** |
||
353 | * Renders the column group HTML. |
||
354 | * @return bool|string the column group HTML or `false` if no column group should be rendered. |
||
355 | */ |
||
356 | 6 | public function renderColumnGroup() |
|
372 | |||
373 | /** |
||
374 | * Renders the table header. |
||
375 | * @return string the rendering result. |
||
376 | */ |
||
377 | 2 | public function renderTableHeader() |
|
393 | |||
394 | /** |
||
395 | * Renders the table footer. |
||
396 | * @return string the rendering result. |
||
397 | */ |
||
398 | 1 | public function renderTableFooter() |
|
412 | |||
413 | /** |
||
414 | * Renders the filter. |
||
415 | * @return string the rendering result. |
||
416 | */ |
||
417 | 2 | public function renderFilters() |
|
431 | |||
432 | /** |
||
433 | * Renders the table body. |
||
434 | * @return string the rendering result. |
||
435 | */ |
||
436 | 6 | public function renderTableBody() |
|
468 | |||
469 | /** |
||
470 | * Renders a table row with the given data model and key. |
||
471 | * @param mixed $model the data model to be rendered |
||
472 | * @param mixed $key the key associated with the data model |
||
473 | * @param int $index the zero-based index of the data model among the model array returned by [[dataProvider]]. |
||
474 | * @return string the rendering result |
||
475 | */ |
||
476 | 2 | public function renderTableRow($model, $key, $index) |
|
492 | |||
493 | /** |
||
494 | * Creates column objects and initializes them. |
||
495 | */ |
||
496 | 22 | protected function initColumns() |
|
517 | |||
518 | /** |
||
519 | * Creates a [[DataColumn]] object based on a string in the format of "attribute:format:label". |
||
520 | * @param string $text the column specification string |
||
521 | * @return DataColumn the column instance |
||
522 | * @throws InvalidConfigException if the column specification is invalid |
||
523 | */ |
||
524 | 4 | protected function createDataColumn($text) |
|
538 | |||
539 | /** |
||
540 | * This function tries to guess the columns to show from the given data |
||
541 | * if [[columns]] are not explicitly specified. |
||
542 | */ |
||
543 | 16 | protected function guessColumns() |
|
555 | } |
||
556 |