Conditions | 36 |
Paths | > 20000 |
Total Lines | 178 |
Code Lines | 112 |
Lines | 20 |
Ratio | 11.24 % |
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 |
||
66 | private function run( $resultPageSet = null ) { |
||
67 | $db = $this->getDB(); |
||
68 | |||
69 | $params = $this->extractRequestParams(); |
||
70 | |||
71 | // Page filters |
||
72 | $this->addTables( 'page' ); |
||
73 | |||
74 | View Code Duplication | if ( !is_null( $params['continue'] ) ) { |
|
75 | $cont = explode( '|', $params['continue'] ); |
||
76 | $this->dieContinueUsageIf( count( $cont ) != 1 ); |
||
77 | $op = $params['dir'] == 'descending' ? '<' : '>'; |
||
78 | $cont_from = $db->addQuotes( $cont[0] ); |
||
79 | $this->addWhere( "page_title $op= $cont_from" ); |
||
80 | } |
||
81 | |||
82 | if ( $params['filterredir'] == 'redirects' ) { |
||
83 | $this->addWhereFld( 'page_is_redirect', 1 ); |
||
84 | } elseif ( $params['filterredir'] == 'nonredirects' ) { |
||
85 | $this->addWhereFld( 'page_is_redirect', 0 ); |
||
86 | } |
||
87 | |||
88 | $this->addWhereFld( 'page_namespace', $params['namespace'] ); |
||
89 | $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' ); |
||
90 | $from = ( $params['from'] === null |
||
91 | ? null |
||
92 | : $this->titlePartToKey( $params['from'], $params['namespace'] ) ); |
||
93 | $to = ( $params['to'] === null |
||
94 | ? null |
||
95 | : $this->titlePartToKey( $params['to'], $params['namespace'] ) ); |
||
96 | $this->addWhereRange( 'page_title', $dir, $from, $to ); |
||
97 | |||
98 | View Code Duplication | if ( isset( $params['prefix'] ) ) { |
|
99 | $this->addWhere( 'page_title' . $db->buildLike( |
||
100 | $this->titlePartToKey( $params['prefix'], $params['namespace'] ), |
||
101 | $db->anyString() ) ); |
||
102 | } |
||
103 | |||
104 | if ( is_null( $resultPageSet ) ) { |
||
105 | $selectFields = [ |
||
106 | 'page_namespace', |
||
107 | 'page_title', |
||
108 | 'page_id' |
||
109 | ]; |
||
110 | } else { |
||
111 | $selectFields = $resultPageSet->getPageTableFields(); |
||
112 | } |
||
113 | |||
114 | $this->addFields( $selectFields ); |
||
115 | $forceNameTitleIndex = true; |
||
116 | View Code Duplication | if ( isset( $params['minsize'] ) ) { |
|
117 | $this->addWhere( 'page_len>=' . intval( $params['minsize'] ) ); |
||
118 | $forceNameTitleIndex = false; |
||
119 | } |
||
120 | |||
121 | View Code Duplication | if ( isset( $params['maxsize'] ) ) { |
|
122 | $this->addWhere( 'page_len<=' . intval( $params['maxsize'] ) ); |
||
123 | $forceNameTitleIndex = false; |
||
124 | } |
||
125 | |||
126 | // Page protection filtering |
||
127 | if ( count( $params['prtype'] ) || $params['prexpiry'] != 'all' ) { |
||
128 | $this->addTables( 'page_restrictions' ); |
||
129 | $this->addWhere( 'page_id=pr_page' ); |
||
130 | $this->addWhere( "pr_expiry > {$db->addQuotes( $db->timestamp() )} OR pr_expiry IS NULL" ); |
||
131 | |||
132 | if ( count( $params['prtype'] ) ) { |
||
133 | $this->addWhereFld( 'pr_type', $params['prtype'] ); |
||
134 | |||
135 | if ( isset( $params['prlevel'] ) ) { |
||
136 | // Remove the empty string and '*' from the prlevel array |
||
137 | $prlevel = array_diff( $params['prlevel'], [ '', '*' ] ); |
||
138 | |||
139 | if ( count( $prlevel ) ) { |
||
140 | $this->addWhereFld( 'pr_level', $prlevel ); |
||
141 | } |
||
142 | } |
||
143 | if ( $params['prfiltercascade'] == 'cascading' ) { |
||
144 | $this->addWhereFld( 'pr_cascade', 1 ); |
||
145 | } elseif ( $params['prfiltercascade'] == 'noncascading' ) { |
||
146 | $this->addWhereFld( 'pr_cascade', 0 ); |
||
147 | } |
||
148 | } |
||
149 | $forceNameTitleIndex = false; |
||
150 | |||
151 | if ( $params['prexpiry'] == 'indefinite' ) { |
||
152 | $this->addWhere( "pr_expiry = {$db->addQuotes( $db->getInfinity() )} OR pr_expiry IS NULL" ); |
||
153 | } elseif ( $params['prexpiry'] == 'definite' ) { |
||
154 | $this->addWhere( "pr_expiry != {$db->addQuotes( $db->getInfinity() )}" ); |
||
155 | } |
||
156 | |||
157 | $this->addOption( 'DISTINCT' ); |
||
158 | } elseif ( isset( $params['prlevel'] ) ) { |
||
159 | $this->dieUsage( 'prlevel may not be used without prtype', 'params' ); |
||
160 | } |
||
161 | |||
162 | if ( $params['filterlanglinks'] == 'withoutlanglinks' ) { |
||
163 | $this->addTables( 'langlinks' ); |
||
164 | $this->addJoinConds( [ 'langlinks' => [ 'LEFT JOIN', 'page_id=ll_from' ] ] ); |
||
165 | $this->addWhere( 'll_from IS NULL' ); |
||
166 | $forceNameTitleIndex = false; |
||
167 | } elseif ( $params['filterlanglinks'] == 'withlanglinks' ) { |
||
168 | $this->addTables( 'langlinks' ); |
||
169 | $this->addWhere( 'page_id=ll_from' ); |
||
170 | $this->addOption( 'STRAIGHT_JOIN' ); |
||
171 | |||
172 | // MySQL filesorts if we use a GROUP BY that works with the rules |
||
173 | // in the 1992 SQL standard (it doesn't like having the |
||
174 | // constant-in-WHERE page_namespace column in there). Using the |
||
175 | // 1999 rules works fine, but that breaks other DBs. Sigh. |
||
176 | /// @todo Once we drop support for 1992-rule DBs, we can simplify this. |
||
177 | $dbType = $db->getType(); |
||
178 | if ( $dbType === 'mysql' || $dbType === 'sqlite' ) { |
||
179 | // Ignore the rules, or 1999 rules if you count unique keys |
||
180 | // over non-NULL columns as satisfying the requirement for |
||
181 | // "functional dependency" and don't require including |
||
182 | // constant-in-WHERE columns in the GROUP BY. |
||
183 | $this->addOption( 'GROUP BY', [ 'page_title' ] ); |
||
184 | } elseif ( $dbType === 'postgres' && $db->getServerVersion() >= 9.1 ) { |
||
185 | // 1999 rules only counting primary keys |
||
186 | $this->addOption( 'GROUP BY', [ 'page_title', 'page_id' ] ); |
||
187 | } else { |
||
188 | // 1992 rules |
||
189 | $this->addOption( 'GROUP BY', $selectFields ); |
||
190 | } |
||
191 | |||
192 | $forceNameTitleIndex = false; |
||
193 | } |
||
194 | |||
195 | if ( $forceNameTitleIndex ) { |
||
196 | $this->addOption( 'USE INDEX', 'name_title' ); |
||
197 | } |
||
198 | |||
199 | $limit = $params['limit']; |
||
200 | $this->addOption( 'LIMIT', $limit + 1 ); |
||
201 | $res = $this->select( __METHOD__ ); |
||
202 | |||
203 | // Get gender information |
||
204 | if ( MWNamespace::hasGenderDistinction( $params['namespace'] ) ) { |
||
205 | $users = []; |
||
206 | foreach ( $res as $row ) { |
||
207 | $users[] = $row->page_title; |
||
208 | } |
||
209 | GenderCache::singleton()->doQuery( $users, __METHOD__ ); |
||
210 | $res->rewind(); // reset |
||
211 | } |
||
212 | |||
213 | $count = 0; |
||
214 | $result = $this->getResult(); |
||
215 | foreach ( $res as $row ) { |
||
216 | if ( ++$count > $limit ) { |
||
217 | // We've reached the one extra which shows that there are |
||
218 | // additional pages to be had. Stop here... |
||
219 | $this->setContinueEnumParameter( 'continue', $row->page_title ); |
||
220 | break; |
||
221 | } |
||
222 | |||
223 | if ( is_null( $resultPageSet ) ) { |
||
224 | $title = Title::makeTitle( $row->page_namespace, $row->page_title ); |
||
225 | $vals = [ |
||
226 | 'pageid' => intval( $row->page_id ), |
||
227 | 'ns' => intval( $title->getNamespace() ), |
||
228 | 'title' => $title->getPrefixedText() |
||
229 | ]; |
||
230 | $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals ); |
||
231 | if ( !$fit ) { |
||
232 | $this->setContinueEnumParameter( 'continue', $row->page_title ); |
||
233 | break; |
||
234 | } |
||
235 | } else { |
||
236 | $resultPageSet->processDbRow( $row ); |
||
|
|||
237 | } |
||
238 | } |
||
239 | |||
240 | if ( is_null( $resultPageSet ) ) { |
||
241 | $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'p' ); |
||
242 | } |
||
243 | } |
||
244 | |||
336 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: