Conditions | 51 |
Paths | > 20000 |
Total Lines | 111 |
Lines | 6 |
Ratio | 5.41 % |
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 namespace EvolutionCMS\Support; |
||
96 | public function render() { |
||
97 | $modx = evolutionCMS(); |
||
98 | $columnHeaderStyle = ($this->columnHeaderStyle) ? "style='" . $this->columnHeaderStyle . "'" : ''; |
||
99 | $columnHeaderClass = ($this->columnHeaderClass) ? "class='" . $this->columnHeaderClass . "'" : ""; |
||
100 | $cssStyle = ($this->cssStyle) ? "style='" . $this->cssStyle . "'" : ''; |
||
101 | $cssClass = ($this->cssClass) ? "class='" . $this->cssClass . "'" : ''; |
||
102 | |||
103 | $pagerClass = ($this->pagerClass) ? "class='" . $this->pagerClass . "'" : ''; |
||
104 | $pagerStyle = ($this->pagerStyle) ? "style='" . $this->pagerStyle . "'" : "style='background-color:#ffffff;'"; |
||
105 | |||
106 | $this->_itemStyle = ($this->itemStyle) ? "style='" . $this->itemStyle . "'" : ''; |
||
107 | $this->_itemClass = ($this->itemClass) ? "class='" . $this->itemClass . "'" : ''; |
||
108 | $this->_altItemStyle = ($this->altItemStyle) ? "style='" . $this->altItemStyle . "'" : ''; |
||
109 | $this->_altItemClass = ($this->altItemClass) ? "class='" . $this->altItemClass . "'" : ''; |
||
110 | |||
111 | $this->_alt = 0; |
||
112 | $this->_total = 0; |
||
113 | |||
114 | $this->_isDataset = $modx->getDatabase()->isResult($this->ds); // if not dataset then treat as array |
||
115 | |||
116 | if(!$cssStyle && !$cssClass) { |
||
117 | $cssStyle = "style='width:100%;border:1px solid silver;font-family:verdana,arial; font-size:11px;'"; |
||
118 | } |
||
119 | if(!$columnHeaderStyle && !$columnHeaderClass) { |
||
120 | $columnHeaderStyle = "style='color:black;background-color:silver'"; |
||
121 | } |
||
122 | if(!$this->_itemStyle && !$this->_itemClass) { |
||
123 | $this->_itemStyle = "style='color:black;'"; |
||
124 | } |
||
125 | if(!$this->_altItemStyle && !$this->_altItemClass) { |
||
126 | $this->_altItemStyle = "style='color:black;background-color:#eeeeee'"; |
||
127 | } |
||
128 | |||
129 | if($this->_isDataset && !$this->columns) { |
||
130 | $cols = $modx->getDatabase()->numFields($this->ds); |
||
131 | for($i = 0; $i < $cols; $i++) $this->columns .= ($i ? "," : "") . $modx->getDatabase()->fieldName($this->ds, $i); |
||
132 | } |
||
133 | |||
134 | // start grid |
||
135 | $tblStart = "<table $cssClass $cssStyle cellpadding='" . (isset($this->cellPadding) ? (int) $this->cellPadding : 1) . "' cellspacing='" . (isset($this->cellSpacing) ? (int) $this->cellSpacing : 1) . "'>"; |
||
136 | $tblEnd = "</table>"; |
||
137 | |||
138 | // build column header |
||
139 | $this->_colnames = explode((strstr($this->columns, "||") !== false ? "||" : ","), $this->columns); |
||
140 | $this->_colwidths = explode((strstr($this->colWidths, "||") !== false ? "||" : ","), $this->colWidths); |
||
141 | $this->_colaligns = explode((strstr($this->colAligns, "||") !== false ? "||" : ","), $this->colAligns); |
||
142 | $this->_colwraps = explode((strstr($this->colWraps, "||") !== false ? "||" : ","), $this->colWraps); |
||
143 | $this->_colcolors = explode((strstr($this->colColors, "||") !== false ? "||" : ","), $this->colColors); |
||
144 | $this->_coltypes = explode((strstr($this->colTypes, "||") !== false ? "||" : ","), $this->colTypes); |
||
145 | $this->_colcount = count($this->_colnames); |
||
146 | if(!$this->_isDataset) { |
||
147 | $this->ds = explode((strstr($this->ds, "||") !== false ? "||" : ","), $this->ds); |
||
148 | $this->ds = array_chunk($this->ds, $this->_colcount); |
||
149 | } |
||
150 | $tblColHdr = "<thead><tr>"; |
||
151 | for($c = 0; $c < $this->_colcount; $c++) { |
||
152 | $name = $this->_colnames[$c]; |
||
153 | $width = $this->_colwidths[$c]; |
||
154 | $tblColHdr .= "<td $columnHeaderStyle $columnHeaderClass" . ($width ? " width='$width'" : "") . ">$name</td>"; |
||
155 | } |
||
156 | $tblColHdr .= "</tr></thead>\n"; |
||
157 | |||
158 | // build rows |
||
159 | $rowcount = $this->_isDataset ? $modx->getDatabase()->getRecordCount($this->ds) : count($this->ds); |
||
160 | $this->_fieldnames = explode(",", $this->fields); |
||
161 | if($rowcount == 0) { |
||
162 | $tblRows .= "<tr><td " . $this->_itemStyle . " " . $this->_itemClass . " colspan='" . $this->_colcount . "'>" . $this->noRecordMsg . "</td></tr>\n"; |
||
|
|||
163 | } else { |
||
164 | // render grid items |
||
165 | if($this->pageSize <= 0) { |
||
166 | for($r = 0; $r < $rowcount; $r++) { |
||
167 | $row = $this->_isDataset ? $modx->getDatabase()->getRow($this->ds) : $this->ds[$r]; |
||
168 | $tblRows .= $this->RenderRowFnc($r + 1, $row); |
||
169 | } |
||
170 | } else { |
||
171 | if(!$this->pager) { |
||
172 | $this->pager = new DataSetPager($this->id, $this->ds, $this->pageSize, $this->pageNumber); |
||
173 | $this->pager->setRenderRowFnc($this); // pass this object |
||
174 | $this->pager->cssStyle = $pagerStyle; |
||
175 | $this->pager->cssClass = $pagerClass; |
||
176 | } else { |
||
177 | $this->pager->pageSize = $this->pageSize; |
||
178 | $this->pager->pageNumber = $this->pageNumber; |
||
179 | } |
||
180 | |||
181 | $this->pager->render(); |
||
182 | $tblRows = $this->pager->getRenderedRows(); |
||
183 | $tblPager = $this->pager->getRenderedPager(); |
||
184 | } |
||
185 | } |
||
186 | |||
187 | // setup header,pager and footer |
||
188 | $o = $tblStart; |
||
189 | $ptop = (substr($this->pagerLocation, 0, 3) == "top") || (substr($this->pagerLocation, 0, 4) == "both"); |
||
190 | $pbot = (substr($this->pagerLocation, 0, 3) == "bot") || (substr($this->pagerLocation, 0, 4) == "both"); |
||
191 | if($this->header) { |
||
192 | $o .= "<tr><td bgcolor='#ffffff' colspan='" . $this->_colcount . "'>" . $this->header . "</td></tr>"; |
||
193 | } |
||
194 | View Code Duplication | if($tblPager && $ptop) { |
|
195 | $o .= "<tr><td align='" . (substr($this->pagerLocation, -4) == "left" ? "left" : "right") . "' $pagerClass $pagerStyle colspan='" . $this->_colcount . "'>" . $tblPager . " </td></tr>"; |
||
196 | } |
||
197 | $o .= $tblColHdr . $tblRows; |
||
198 | View Code Duplication | if($tblPager && $pbot) { |
|
199 | $o .= "<tr><td align='" . (substr($this->pagerLocation, -4) == "left" ? "left" : "right") . "' $pagerClass $pagerStyle colspan='" . $this->_colcount . "'>" . $tblPager . " </td></tr>"; |
||
200 | } |
||
201 | if($this->footer) { |
||
202 | $o .= "<tr><td bgcolor='#ffffff' colspan='" . $this->_colcount . "'>" . $this->footer . "</td></tr>"; |
||
203 | } |
||
204 | $o .= $tblEnd; |
||
205 | return $o; |
||
206 | } |
||
207 | |||
312 |
This error can happen if you refactor code and forget to move the variable initialization.
Let’s take a look at a simple example:
The above code is perfectly fine. Now imagine that we re-order the statements:
In that case,
$x
would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.