| Conditions | 31 |
| Paths | > 20000 |
| Total Lines | 142 |
| Code Lines | 101 |
| Lines | 25 |
| Ratio | 17.61 % |
| 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 |
||
| 100 | private function run( $resultPageSet = null ) { |
||
| 101 | $db = $this->getDB(); |
||
| 102 | $params = $this->extractRequestParams(); |
||
| 103 | |||
| 104 | $pfx = $this->tablePrefix; |
||
| 105 | $fieldTitle = $this->fieldTitle; |
||
| 106 | $prop = array_flip( $params['prop'] ); |
||
| 107 | $fld_ids = isset( $prop['ids'] ); |
||
| 108 | $fld_title = isset( $prop['title'] ); |
||
| 109 | if ( $this->hasNamespace ) { |
||
| 110 | $namespace = $params['namespace']; |
||
| 111 | } else { |
||
| 112 | $namespace = $this->dfltNamespace; |
||
| 113 | } |
||
| 114 | |||
| 115 | if ( $params['unique'] ) { |
||
| 116 | $matches = array_intersect_key( $prop, $this->props + [ 'ids' => 1 ] ); |
||
| 117 | if ( $matches ) { |
||
| 118 | $p = $this->getModulePrefix(); |
||
| 119 | $this->dieUsage( |
||
| 120 | "Cannot use {$p}prop=" . implode( '|', array_keys( $matches ) ) . " with {$p}unique", |
||
| 121 | 'params' |
||
| 122 | ); |
||
| 123 | } |
||
| 124 | $this->addOption( 'DISTINCT' ); |
||
| 125 | } |
||
| 126 | |||
| 127 | $this->addTables( $this->table ); |
||
| 128 | if ( $this->hasNamespace ) { |
||
| 129 | $this->addWhereFld( $pfx . 'namespace', $namespace ); |
||
| 130 | } |
||
| 131 | |||
| 132 | $continue = !is_null( $params['continue'] ); |
||
| 133 | if ( $continue ) { |
||
| 134 | $continueArr = explode( '|', $params['continue'] ); |
||
| 135 | $op = $params['dir'] == 'descending' ? '<' : '>'; |
||
| 136 | if ( $params['unique'] ) { |
||
| 137 | $this->dieContinueUsageIf( count( $continueArr ) != 1 ); |
||
| 138 | $continueTitle = $db->addQuotes( $continueArr[0] ); |
||
| 139 | $this->addWhere( "{$pfx}{$fieldTitle} $op= $continueTitle" ); |
||
| 140 | } else { |
||
| 141 | $this->dieContinueUsageIf( count( $continueArr ) != 2 ); |
||
| 142 | $continueTitle = $db->addQuotes( $continueArr[0] ); |
||
| 143 | $continueFrom = intval( $continueArr[1] ); |
||
| 144 | $this->addWhere( |
||
| 145 | "{$pfx}{$fieldTitle} $op $continueTitle OR " . |
||
| 146 | "({$pfx}{$fieldTitle} = $continueTitle AND " . |
||
| 147 | "{$pfx}from $op= $continueFrom)" |
||
| 148 | ); |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | // 'continue' always overrides 'from' |
||
| 153 | $from = ( $continue || $params['from'] === null ? null : |
||
| 154 | $this->titlePartToKey( $params['from'], $namespace ) ); |
||
| 155 | $to = ( $params['to'] === null ? null : |
||
| 156 | $this->titlePartToKey( $params['to'], $namespace ) ); |
||
| 157 | $this->addWhereRange( $pfx . $fieldTitle, 'newer', $from, $to ); |
||
| 158 | |||
| 159 | if ( isset( $params['prefix'] ) ) { |
||
| 160 | $this->addWhere( $pfx . $fieldTitle . $db->buildLike( $this->titlePartToKey( |
||
| 161 | $params['prefix'], $namespace ), $db->anyString() ) ); |
||
| 162 | } |
||
| 163 | |||
| 164 | $this->addFields( [ 'pl_title' => $pfx . $fieldTitle ] ); |
||
| 165 | $this->addFieldsIf( [ 'pl_from' => $pfx . 'from' ], !$params['unique'] ); |
||
| 166 | foreach ( $this->props as $name => $field ) { |
||
| 167 | $this->addFieldsIf( $field, isset( $prop[$name] ) ); |
||
| 168 | } |
||
| 169 | |||
| 170 | if ( $this->useIndex ) { |
||
| 171 | $this->addOption( 'USE INDEX', $this->useIndex ); |
||
| 172 | } |
||
| 173 | $limit = $params['limit']; |
||
| 174 | $this->addOption( 'LIMIT', $limit + 1 ); |
||
| 175 | |||
| 176 | $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' ); |
||
| 177 | $orderBy = []; |
||
| 178 | $orderBy[] = $pfx . $fieldTitle . $sort; |
||
| 179 | if ( !$params['unique'] ) { |
||
| 180 | $orderBy[] = $pfx . 'from' . $sort; |
||
| 181 | } |
||
| 182 | $this->addOption( 'ORDER BY', $orderBy ); |
||
| 183 | |||
| 184 | $res = $this->select( __METHOD__ ); |
||
| 185 | |||
| 186 | $pageids = []; |
||
| 187 | $titles = []; |
||
| 188 | $count = 0; |
||
| 189 | $result = $this->getResult(); |
||
| 190 | foreach ( $res as $row ) { |
||
| 191 | View Code Duplication | if ( ++$count > $limit ) { |
|
| 192 | // We've reached the one extra which shows that there are |
||
| 193 | // additional pages to be had. Stop here... |
||
| 194 | if ( $params['unique'] ) { |
||
| 195 | $this->setContinueEnumParameter( 'continue', $row->pl_title ); |
||
| 196 | } else { |
||
| 197 | $this->setContinueEnumParameter( 'continue', $row->pl_title . '|' . $row->pl_from ); |
||
| 198 | } |
||
| 199 | break; |
||
| 200 | } |
||
| 201 | |||
| 202 | if ( is_null( $resultPageSet ) ) { |
||
| 203 | $vals = [ |
||
| 204 | ApiResult::META_TYPE => 'assoc', |
||
| 205 | ]; |
||
| 206 | if ( $fld_ids ) { |
||
| 207 | $vals['fromid'] = intval( $row->pl_from ); |
||
| 208 | } |
||
| 209 | if ( $fld_title ) { |
||
| 210 | $title = Title::makeTitle( $namespace, $row->pl_title ); |
||
| 211 | ApiQueryBase::addTitleInfo( $vals, $title ); |
||
| 212 | } |
||
| 213 | foreach ( $this->props as $name => $field ) { |
||
| 214 | if ( isset( $prop[$name] ) && $row->$field !== null && $row->$field !== '' ) { |
||
| 215 | $vals[$name] = $row->$field; |
||
| 216 | } |
||
| 217 | } |
||
| 218 | $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals ); |
||
| 219 | View Code Duplication | if ( !$fit ) { |
|
| 220 | if ( $params['unique'] ) { |
||
| 221 | $this->setContinueEnumParameter( 'continue', $row->pl_title ); |
||
| 222 | } else { |
||
| 223 | $this->setContinueEnumParameter( 'continue', $row->pl_title . '|' . $row->pl_from ); |
||
| 224 | } |
||
| 225 | break; |
||
| 226 | } |
||
| 227 | } elseif ( $params['unique'] ) { |
||
| 228 | $titles[] = Title::makeTitle( $namespace, $row->pl_title ); |
||
| 229 | } else { |
||
| 230 | $pageids[] = $row->pl_from; |
||
| 231 | } |
||
| 232 | } |
||
| 233 | |||
| 234 | View Code Duplication | if ( is_null( $resultPageSet ) ) { |
|
| 235 | $result->addIndexedTagName( [ 'query', $this->getModuleName() ], $this->indexTag ); |
||
| 236 | } elseif ( $params['unique'] ) { |
||
| 237 | $resultPageSet->populateFromTitles( $titles ); |
||
| 238 | } else { |
||
| 239 | $resultPageSet->populateFromPageIDs( $pageids ); |
||
| 240 | } |
||
| 241 | } |
||
| 242 | |||
| 309 |
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.