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