| Conditions | 14 |
| Paths | 336 |
| Total Lines | 64 |
| Code Lines | 47 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 55 | protected function runQuery( $resultPageSet, $limit, $start, $startId, $end ) { |
||
| 56 | $params = $this->extractRequestParams(); |
||
| 57 | |||
| 58 | $this->resetQueryParams(); |
||
| 59 | $this->addTables( 'page' ); |
||
| 60 | $this->addFields( [ 'page_id', 'page_random' ] ); |
||
| 61 | if ( is_null( $resultPageSet ) ) { |
||
| 62 | $this->addFields( [ 'page_title', 'page_namespace' ] ); |
||
| 63 | } else { |
||
| 64 | $this->addFields( $resultPageSet->getPageTableFields() ); |
||
| 65 | } |
||
| 66 | $this->addWhereFld( 'page_namespace', $params['namespace'] ); |
||
| 67 | if ( $params['redirect'] || $params['filterredir'] === 'redirects' ) { |
||
| 68 | $this->addWhereFld( 'page_is_redirect', 1 ); |
||
| 69 | } elseif ( $params['filterredir'] === 'nonredirects' ) { |
||
| 70 | $this->addWhereFld( 'page_is_redirect', 0 ); |
||
| 71 | } elseif ( is_null( $resultPageSet ) ) { |
||
| 72 | $this->addFields( [ 'page_is_redirect' ] ); |
||
| 73 | } |
||
| 74 | $this->addOption( 'LIMIT', $limit + 1 ); |
||
| 75 | |||
| 76 | if ( $start !== null ) { |
||
| 77 | $start = $this->getDB()->addQuotes( $start ); |
||
| 78 | if ( $startId !== null ) { |
||
| 79 | $startId = (int)$startId; |
||
| 80 | $this->addWhere( "page_random = $start AND page_id >= $startId OR page_random > $start" ); |
||
| 81 | } else { |
||
| 82 | $this->addWhere( "page_random >= $start" ); |
||
| 83 | } |
||
| 84 | } |
||
| 85 | if ( $end !== null ) { |
||
| 86 | $this->addWhere( 'page_random < ' . $this->getDB()->addQuotes( $end ) ); |
||
| 87 | } |
||
| 88 | $this->addOption( 'ORDER BY', [ 'page_random', 'page_id' ] ); |
||
| 89 | |||
| 90 | $result = $this->getResult(); |
||
| 91 | $path = [ 'query', $this->getModuleName() ]; |
||
| 92 | |||
| 93 | $res = $this->select( __METHOD__ ); |
||
| 94 | $count = 0; |
||
| 95 | foreach ( $res as $row ) { |
||
| 96 | if ( $count++ >= $limit ) { |
||
| 97 | return [ 0, "{$row->page_random}|{$row->page_id}" ]; |
||
| 98 | } |
||
| 99 | if ( is_null( $resultPageSet ) ) { |
||
| 100 | $title = Title::makeTitle( $row->page_namespace, $row->page_title ); |
||
| 101 | $page = [ |
||
| 102 | 'id' => (int)$row->page_id, |
||
| 103 | ]; |
||
| 104 | ApiQueryBase::addTitleInfo( $page, $title ); |
||
| 105 | if ( isset( $row->page_is_redirect ) ) { |
||
| 106 | $page['redirect'] = (bool)$row->page_is_redirect; |
||
| 107 | } |
||
| 108 | $fit = $result->addValue( $path, null, $page ); |
||
| 109 | if ( !$fit ) { |
||
| 110 | return [ 0, "{$row->page_random}|{$row->page_id}" ]; |
||
| 111 | } |
||
| 112 | } else { |
||
| 113 | $resultPageSet->processDbRow( $row ); |
||
|
|
|||
| 114 | } |
||
| 115 | } |
||
| 116 | |||
| 117 | return [ $limit - $count, null ]; |
||
| 118 | } |
||
| 119 | |||
| 215 |
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: