Complex classes like TbJsonGridView 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 TbJsonGridView, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class TbJsonGridView extends TbGridView |
||
22 | { |
||
23 | /** |
||
24 | * @var boolean $json true when there is an AJAX request. TbJsonGridView expect a JSON response. |
||
25 | */ |
||
26 | public $json; |
||
27 | |||
28 | /** |
||
29 | * @var string $template Overridden standard template to add second pager on top. |
||
30 | */ |
||
31 | public $template = "{pager}\n{items}\n{summary}\n{pager}"; |
||
32 | |||
33 | /** |
||
34 | * @var int $cacheTTL how long we keep the responses on cache? It will depend on cacheTTLType (seconds, minutes, hours) |
||
35 | */ |
||
36 | public $cacheTTL = 1; |
||
37 | |||
38 | /** |
||
39 | * @var string the type of cache duration |
||
40 | * s: seconds |
||
41 | * m: minutes |
||
42 | * h: hours |
||
43 | */ |
||
44 | public $cacheTTLType = 's'; |
||
45 | |||
46 | /** |
||
47 | * @var bool $localCache whether we use client ajax cache or not. True by default. |
||
48 | */ |
||
49 | public $localCache = true; |
||
50 | |||
51 | /** |
||
52 | * @var array the configuration for the pager. |
||
53 | * Defaults to <code>array('class'=>'ext.booster.widgets.TbPager')</code>. |
||
54 | */ |
||
55 | public $pager = array('class' => 'booster.widgets.TbJsonPager'); |
||
56 | |||
57 | /** |
||
58 | * @var bool true when there is an AJAX request and having in template summary |
||
59 | */ |
||
60 | protected $_showSummary; |
||
61 | |||
62 | /** |
||
63 | * Initializes $json property to find out whether ajax request or not |
||
64 | */ |
||
65 | public function init() |
||
76 | |||
77 | /** |
||
78 | * Renders the view. |
||
79 | * This is the main entry of the whole view rendering. |
||
80 | * Child classes should mainly override {@link renderContent} method. |
||
81 | */ |
||
82 | public function run() |
||
91 | |||
92 | /** |
||
93 | * Renders the pager. |
||
94 | */ |
||
95 | public function renderPager() |
||
123 | |||
124 | /** |
||
125 | * Creates column objects and initializes them. |
||
126 | */ |
||
127 | protected function initColumns() |
||
137 | |||
138 | /** |
||
139 | * Renders the data items for the grid view. |
||
140 | */ |
||
141 | public function renderItems() |
||
142 | { |
||
143 | if ($this->json) |
||
144 | { |
||
145 | $renderedTableBody = $this->getRenderedTableBody(); |
||
146 | echo function_exists('json_encode') |
||
147 | ? json_encode($renderedTableBody) |
||
148 | : CJSON::encode($renderedTableBody); |
||
149 | } |
||
150 | elseif ($this->dataProvider->getItemCount() > 0 || $this->showTableOnEmpty) |
||
151 | { |
||
152 | echo "<table class=\"{$this->itemsCssClass}\">\n"; |
||
153 | $this->renderTableHeader(); |
||
154 | $this->renderTableFooter(); // TFOOT must appear before TBODY according to the standard. |
||
155 | $this->renderTableBody(); |
||
156 | echo "</table>"; |
||
157 | $this->renderTemplates(); |
||
158 | } |
||
159 | else |
||
160 | { |
||
161 | $this->renderEmptyText(); |
||
162 | } |
||
163 | } |
||
164 | |||
165 | public function renderSummary() |
||
187 | /** |
||
188 | * Renders the required templates for the client engine (jqote2 used) |
||
189 | */ |
||
190 | protected function renderTemplates() |
||
210 | |||
211 | /** |
||
212 | * Encloses the given JavaScript within a script tag. |
||
213 | * |
||
214 | * @param string $id |
||
215 | * @param string $text the JavaScript to be enclosed |
||
216 | * |
||
217 | * @return string the enclosed JavaScript |
||
218 | */ |
||
219 | public function renderTemplate($id, $text) |
||
223 | |||
224 | /** |
||
225 | * Renders the table body. |
||
226 | */ |
||
227 | public function renderTableBody() |
||
250 | |||
251 | /** |
||
252 | * Renders the body table for JSON requests - assumed ajax is for JSON |
||
253 | * |
||
254 | * @param integer $rows |
||
255 | * |
||
256 | * @return array |
||
257 | */ |
||
258 | protected function renderTableBodyJSON($rows) |
||
259 | { |
||
260 | $tbody = array( |
||
261 | 'headers' => array(), |
||
262 | 'rows' => array(), |
||
263 | 'keys' => array(), |
||
264 | 'summary' => array() |
||
265 | ); |
||
266 | foreach ($this->columns as $column) { |
||
267 | $tbody['headers'][] = $column->renderHeaderCell(); |
||
268 | } |
||
269 | |||
270 | if ($rows > 0) { |
||
271 | for ($row = 0; $row < $rows; ++$row) { |
||
272 | $tbody['rows'][] = $this->renderTableRowJSON($row); |
||
273 | } |
||
274 | |||
275 | foreach ($this->dataProvider->getKeys() as $key) { |
||
276 | $tbody['keys'][] = CHtml::encode($key); |
||
277 | } |
||
278 | |||
279 | } else { |
||
280 | ob_start(); |
||
281 | $this->renderEmptyText(); |
||
282 | $content = ob_get_contents(); |
||
283 | ob_end_clean(); |
||
284 | |||
285 | $tbody['rows'][0]['cols'][] = array( |
||
286 | 'attrs' => "colspan=\"" . count($this->columns) . "\"", |
||
287 | 'content' => $content |
||
288 | ); |
||
289 | $tbody['rows'][0]['class'] = " "; |
||
290 | } |
||
291 | $tbody['pager'] = $this->renderPager(); |
||
292 | $tbody['url'] = Yii::app()->getRequest()->getUrl(); |
||
293 | $tbody['summary'] = $this->renderSummary(); |
||
294 | return $tbody; |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * Renders a table body row. |
||
299 | * |
||
300 | * @param integer $row the row number (zero-based). |
||
301 | */ |
||
302 | public function renderTableRow($row) |
||
321 | |||
322 | /** |
||
323 | * Renders a table body row for JSON requests - assumed ajax is for JSON |
||
324 | * |
||
325 | * @param integer $row |
||
326 | * |
||
327 | * @return array |
||
328 | */ |
||
329 | protected function renderTableRowJSON($row) |
||
349 | |||
350 | /** |
||
351 | * Creates a column based on a shortcut column specification string. |
||
352 | * |
||
353 | * @param mixed $text the column specification string |
||
354 | * |
||
355 | * @return \TbJSONDataColumn|\TbDataColumn|\CDataColumn the column instance |
||
356 | * @throws CException if the column format is incorrect |
||
357 | */ |
||
358 | protected function createDataColumn($text) |
||
380 | |||
381 | /** |
||
382 | * Registers necessary client scripts. |
||
383 | */ |
||
384 | public function registerClientScript() |
||
440 | |||
441 | /** |
||
442 | * @return string |
||
443 | */ |
||
444 | private function getRenderedTableBody() |
||
451 | } |
||
452 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: