Conditions | 42 |
Paths | 2730 |
Total Lines | 213 |
Lines | 16 |
Ratio | 7.51 % |
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 |
||
32 | public function run($request) |
||
33 | { |
||
34 | |||
35 | // give us some time to run this |
||
36 | ini_set('max_execution_time', 3000); |
||
37 | $classes = ClassInfo::subclassesFor('DataObject'); |
||
38 | $array = [ |
||
39 | 'l' => 'limit', |
||
40 | 's' => 'step', |
||
41 | 'd' => 'debug', |
||
42 | 'q' => 'quickAndDirty', |
||
43 | 't' => 'testTableCustom' |
||
44 | ]; |
||
45 | foreach ($array as $getParam => $field) { |
||
46 | if (isset($_GET[$getParam])) { |
||
47 | $v = $_GET[$getParam]; |
||
48 | switch ($getParam) { |
||
49 | case 't': |
||
50 | if (in_array($v, $classes)) { |
||
51 | $this->$field = $v; |
||
52 | } |
||
53 | break; |
||
54 | default: |
||
55 | $this->$field = intval($v); |
||
56 | |||
57 | } |
||
58 | } |
||
59 | } |
||
60 | $this->flushNowQuick('<style>li {list-style: none!important;}h2.group{text-align: center;}</style>'); |
||
61 | $this->flushNow('<h3>Scroll down to bottom to see results. Output ends with <i>END</i></h3>', 'notice'); |
||
62 | $this->flushNowQuick( |
||
63 | ' |
||
64 | We run through all the summary fields for all dataobjects and select <i>limits</i> (segments) of the datalist. |
||
65 | After that we check if the same ID shows up on different segments. |
||
66 | If there are duplicates then Pagination is broken. |
||
67 | ', |
||
68 | 'notice' |
||
|
|||
69 | ); |
||
70 | $this->flushNow('<hr /><hr /><hr /><hr /><h2 class="group">SETTINGS </h2><hr /><hr /><hr /><hr />'); |
||
71 | $this->flushNow(' |
||
72 | <form method="get" action="/dev/tasks/CheckForMysqlPaginationIssuesBuildTask"> |
||
73 | <br /><br />test table:<br /><input name="t" placeholder="e.g. SiteTree" value="'.$this->testTableCustom.'" /> |
||
74 | <br /><br />limit:<br /><input name="l" placeholder="limit" value="'.$this->limit.'" /> |
||
75 | <br /><br />step:<br /><input name="s" placeholder="step" value="'.$this->step.'" /> |
||
76 | <br /><br />debug:<br /><select name="d" placeholder="debug" /><option value="0">false</option><option value="1" '.($this->debug ? 'selected="selected"' : '').'>true</option></select> |
||
77 | <br /><br />quick:<br /><select name="q" placeholder="quick" /><option value="0">false</option><option value="1" '.($this->quickAndDirty ? ' selected="selected"' : '').'>true</option></select> |
||
78 | <br /><br /><input type="submit" value="run again with variables above" /> |
||
79 | </form> |
||
80 | '); |
||
81 | $this->flushNow('<hr /><hr /><hr /><hr /><h2 class="group">CALCULATIONS </h2><hr /><hr /><hr /><hr />'); |
||
82 | // array of errors |
||
83 | $errors = []; |
||
84 | $largestTable = ''; |
||
85 | $largestTableCount = 0; |
||
86 | $skipTables = $this->Config()->get('skip_tables'); |
||
87 | // get all DataObjects and loop through them |
||
88 | |||
89 | foreach ($classes as $class) { |
||
90 | if (in_array($class, $skipTables)) { |
||
91 | continue; |
||
92 | } |
||
93 | // skip irrelevant ones |
||
94 | if ($class !== 'DataObject') { |
||
95 | //skip test ones |
||
96 | $obj = Injector::inst()->get($class); |
||
97 | if ($obj instanceof FunctionalTest || $obj instanceof TestOnly) { |
||
98 | $this->flushNowDebug('<h2>SKIPPING: '.$class.'</h2>'); |
||
99 | continue; |
||
100 | } |
||
101 | //start the process ... |
||
102 | $this->flushNowDebug('<h2>Testing '.$class.'</h2>'); |
||
103 | |||
104 | // must exist is its own table to avoid doubling-up on tests |
||
105 | // e.g. test SiteTree and Page where Page is not its own table ... |
||
106 | if ($this->tableExists($class)) { |
||
107 | $this->timePerClass[$class] = []; |
||
108 | $this->timePerClass[$class]['start'] = microtime(true); |
||
109 | // check table size |
||
110 | $count = $class::get()->count(); |
||
111 | $checkCount = DB::query('SELECT COUNT("ID") FROM "'.$class.'"')->value(); |
||
112 | if (intval($checkCount) !== intval($count)) { |
||
113 | $this->flushNow(' |
||
114 | COUNT error! |
||
115 | '.$class.' ::get: '.$count.' rows BUT |
||
116 | DB::query(...): '.$checkCount.' rows | |
||
117 | DIFFERENCE: '.abs($count - $checkCount).'', 'deleted'); |
||
118 | } |
||
119 | if ($count > $this->step) { |
||
120 | if ($count > $largestTableCount) { |
||
121 | $largestTableCount = $count; |
||
122 | $largestTable = $class; |
||
123 | } |
||
124 | $this->flushNowQuick('<br />'.$class.': '); |
||
125 | if (! isset($errors[$class])) { |
||
126 | $errors[$class] = []; |
||
127 | } |
||
128 | // get fields ... |
||
129 | $dbFields = $obj->db(); |
||
130 | if (! is_array($dbFields)) { |
||
131 | $dbFields = []; |
||
132 | } |
||
133 | // adding base fields. |
||
134 | // we do not add ID as this should work! |
||
135 | $dbFields['ClassName'] = 'ClassName'; |
||
136 | $dbFields['Created'] = 'Created'; |
||
137 | $dbFields['LastEdited'] = 'LastEdited'; |
||
138 | |||
139 | $hasOneFields = $obj->hasOne(); |
||
140 | if (! is_array($hasOneFields)) { |
||
141 | $hasOneFields = []; |
||
142 | } |
||
143 | |||
144 | //start looping through summary fields ... |
||
145 | $summaryFields = $obj->summaryFields(); |
||
146 | foreach ($summaryFields as $field => $value) { |
||
147 | if (isset($dbFields[$field]) || isset($hasOneFields[$field])) { |
||
148 | $this->flushNowQuick(' / '.$field.': '); |
||
149 | // reset comparisonArray - this is important ... |
||
150 | $comparisonArray = []; |
||
151 | //fix has one field |
||
152 | if (isset($hasOneFields[$field])) { |
||
153 | $field .= 'ID'; |
||
154 | } |
||
155 | if (! isset($errors[$class][$field])) { |
||
156 | $errors[$class][$field] = []; |
||
157 | } |
||
158 | // start loop of limits ... |
||
159 | $this->flushNowDebug('- Sorting by '.$field); |
||
160 | for ($i = 0; $i < $this->limit && $i < ($count - $this->step); $i += $this->step) { |
||
161 | |||
162 | // OPTION 1 |
||
163 | if ($this->quickAndDirty) { |
||
164 | if (DataObject::has_own_table_database_field($class, $field)) { |
||
165 | $tempRows = DB::query('SELECT "ID" FROM "'.$class.'" ORDER BY "'.$field.'" ASC LIMIT '.$i.', '.$this->step.';'); |
||
166 | foreach ($tempRows as $row) { |
||
167 | $id = $row['ID']; |
||
168 | View Code Duplication | if (isset($comparisonArray[$id])) { |
|
169 | if (! isset($errors[$class][$field][$id])) { |
||
170 | $errors[$class][$field][$id] = 1; |
||
171 | } |
||
172 | $errors[$class][$field][$id]++; |
||
173 | } else { |
||
174 | $this->flushNowQuick('.'); |
||
175 | } |
||
176 | $comparisonArray[$id] = $id; |
||
177 | } |
||
178 | } else { |
||
179 | $this->flushNowDebug('<strong>SKIP: '.$class.'.'.$field.'</strong> does not exist'); |
||
180 | break; |
||
181 | } |
||
182 | |||
183 | // OPTION 2 |
||
184 | } else { |
||
185 | $tempObjects = $class::get()->sort($field)->limit($this->step, $i); |
||
186 | foreach ($tempObjects as $tempObject) { |
||
187 | $id = $tempObject->ID; |
||
188 | View Code Duplication | if (isset($comparisonArray[$id])) { |
|
189 | if (! isset($errors[$class][$field][$id])) { |
||
190 | $errors[$class][$field][$id] = 1; |
||
191 | } |
||
192 | $errors[$class][$field][$id]++; |
||
193 | } else { |
||
194 | $this->flushNowQuick('.'); |
||
195 | } |
||
196 | $comparisonArray[$tempObject->ID] = $tempObject->ID; |
||
197 | } |
||
198 | } |
||
199 | } |
||
200 | if (count($errors[$class][$field])) { |
||
201 | $error = '<br /><strong>Found double entries in <u>'.$class.'</u> table,'. |
||
202 | ' sorting by <u>'.$field.'</u></strong> ...'; |
||
203 | foreach ($errors[$class][$field] as $tempID => $tempCount) { |
||
204 | $error .= ' ID: '.$tempID.' occurred '.$tempCount.' times /'; |
||
205 | } |
||
206 | $this->flushNowDebug($error, 'deleted'); |
||
207 | $errors[$class][$field] = $error; |
||
208 | } |
||
209 | } else { |
||
210 | $this->flushNowDebug('<strong>SKIP: '.$class.'.'.$field.' field</strong> because it is not a DB field.'); |
||
211 | } |
||
212 | } |
||
213 | } else { |
||
214 | $this->flushNowDebug('<strong>SKIP: table '.$class.'</strong> because it does not have enough records. '); |
||
215 | } |
||
216 | $this->timePerClass[$class]['end'] = microtime(true); |
||
217 | } else { |
||
218 | $this->flushNowDebug('SKIP: '.$class.' because table does not exist. '); |
||
219 | } |
||
220 | } |
||
221 | } |
||
222 | $this->flushNow('<hr /><hr /><hr /><hr /><h2 class="group">RESULTS </h2><hr /><hr /><hr /><hr />'); |
||
223 | //print out errors again ... |
||
224 | foreach ($errors as $class => $fieldValues) { |
||
225 | $this->flushNow('<h4>'.$class.'</h4>'); |
||
226 | $time = round(($this->timePerClass[$class]['end'] - $this->timePerClass[$class]['start']) * 1000); |
||
227 | $this->flushNow('Time taken: '.$time.'μs'); |
||
228 | $errorCount = 0; |
||
229 | foreach ($fieldValues as $field => $errorMessage) { |
||
230 | if (is_string($errorMessage) && $errorMessage) { |
||
231 | $errorCount++; |
||
232 | $this->flushNow($errorMessage, 'deleted'); |
||
233 | } |
||
234 | } |
||
235 | if ($errorCount === 0) { |
||
236 | $this->flushNow('No errors', 'created'); |
||
237 | } |
||
238 | } |
||
239 | if ($this->testTableCustom) { |
||
240 | $largestTable = $this->testTableCustom; |
||
241 | } |
||
242 | $this->speedComparison($largestTable); |
||
243 | $this->flushNow('<hr /><hr /><hr /><hr /><h2 class="group">END </h2><hr /><hr /><hr /><hr />'); |
||
244 | } |
||
245 | |||
349 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.