Conditions | 17 |
Paths | 505 |
Total Lines | 105 |
Code Lines | 70 |
Lines | 14 |
Ratio | 13.33 % |
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 |
||
75 | private function run( $resultPageSet = null ) { |
||
76 | if ( $this->getPageSet()->getGoodTitleCount() == 0 ) { |
||
77 | return; // nothing to do |
||
78 | } |
||
79 | |||
80 | $params = $this->extractRequestParams(); |
||
81 | |||
82 | $this->addFields( [ |
||
83 | 'pl_from' => $this->prefix . '_from', |
||
84 | 'pl_namespace' => $this->prefix . '_namespace', |
||
85 | 'pl_title' => $this->prefix . '_title' |
||
86 | ] ); |
||
87 | |||
88 | $this->addTables( $this->table ); |
||
89 | $this->addWhereFld( $this->prefix . '_from', array_keys( $this->getPageSet()->getGoodTitles() ) ); |
||
90 | $this->addWhereFld( $this->prefix . '_namespace', $params['namespace'] ); |
||
91 | |||
92 | if ( !is_null( $params[$this->titlesParam] ) ) { |
||
93 | $lb = new LinkBatch; |
||
94 | foreach ( $params[$this->titlesParam] as $t ) { |
||
95 | $title = Title::newFromText( $t ); |
||
96 | if ( !$title ) { |
||
97 | $this->setWarning( "\"$t\" is not a valid title" ); |
||
98 | } else { |
||
99 | $lb->addObj( $title ); |
||
100 | } |
||
101 | } |
||
102 | $cond = $lb->constructSet( $this->prefix, $this->getDB() ); |
||
103 | if ( $cond ) { |
||
104 | $this->addWhere( $cond ); |
||
105 | } |
||
106 | } |
||
107 | |||
108 | if ( !is_null( $params['continue'] ) ) { |
||
109 | $cont = explode( '|', $params['continue'] ); |
||
110 | $this->dieContinueUsageIf( count( $cont ) != 3 ); |
||
111 | $op = $params['dir'] == 'descending' ? '<' : '>'; |
||
112 | $plfrom = intval( $cont[0] ); |
||
113 | $plns = intval( $cont[1] ); |
||
114 | $pltitle = $this->getDB()->addQuotes( $cont[2] ); |
||
115 | $this->addWhere( |
||
116 | "{$this->prefix}_from $op $plfrom OR " . |
||
117 | "({$this->prefix}_from = $plfrom AND " . |
||
118 | "({$this->prefix}_namespace $op $plns OR " . |
||
119 | "({$this->prefix}_namespace = $plns AND " . |
||
120 | "{$this->prefix}_title $op= $pltitle)))" |
||
121 | ); |
||
122 | } |
||
123 | |||
124 | $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' ); |
||
125 | // Here's some MySQL craziness going on: if you use WHERE foo='bar' |
||
126 | // and later ORDER BY foo MySQL doesn't notice the ORDER BY is pointless |
||
127 | // but instead goes and filesorts, because the index for foo was used |
||
128 | // already. To work around this, we drop constant fields in the WHERE |
||
129 | // clause from the ORDER BY clause |
||
130 | $order = []; |
||
131 | if ( count( $this->getPageSet()->getGoodTitles() ) != 1 ) { |
||
132 | $order[] = $this->prefix . '_from' . $sort; |
||
133 | } |
||
134 | if ( count( $params['namespace'] ) != 1 ) { |
||
135 | $order[] = $this->prefix . '_namespace' . $sort; |
||
136 | } |
||
137 | |||
138 | $order[] = $this->prefix . '_title' . $sort; |
||
139 | $this->addOption( 'ORDER BY', $order ); |
||
140 | $this->addOption( 'USE INDEX', $this->prefix . '_from' ); |
||
141 | $this->addOption( 'LIMIT', $params['limit'] + 1 ); |
||
142 | |||
143 | $res = $this->select( __METHOD__ ); |
||
144 | |||
145 | if ( is_null( $resultPageSet ) ) { |
||
146 | $count = 0; |
||
147 | foreach ( $res as $row ) { |
||
148 | View Code Duplication | if ( ++$count > $params['limit'] ) { |
|
149 | // We've reached the one extra which shows that |
||
150 | // there are additional pages to be had. Stop here... |
||
151 | $this->setContinueEnumParameter( 'continue', |
||
152 | "{$row->pl_from}|{$row->pl_namespace}|{$row->pl_title}" ); |
||
153 | break; |
||
154 | } |
||
155 | $vals = []; |
||
156 | ApiQueryBase::addTitleInfo( $vals, Title::makeTitle( $row->pl_namespace, $row->pl_title ) ); |
||
157 | $fit = $this->addPageSubItem( $row->pl_from, $vals ); |
||
158 | if ( !$fit ) { |
||
159 | $this->setContinueEnumParameter( 'continue', |
||
160 | "{$row->pl_from}|{$row->pl_namespace}|{$row->pl_title}" ); |
||
161 | break; |
||
162 | } |
||
163 | } |
||
164 | } else { |
||
165 | $titles = []; |
||
166 | $count = 0; |
||
167 | foreach ( $res as $row ) { |
||
168 | View Code Duplication | if ( ++$count > $params['limit'] ) { |
|
169 | // We've reached the one extra which shows that |
||
170 | // there are additional pages to be had. Stop here... |
||
171 | $this->setContinueEnumParameter( 'continue', |
||
172 | "{$row->pl_from}|{$row->pl_namespace}|{$row->pl_title}" ); |
||
173 | break; |
||
174 | } |
||
175 | $titles[] = Title::makeTitle( $row->pl_namespace, $row->pl_title ); |
||
176 | } |
||
177 | $resultPageSet->populateFromTitles( $titles ); |
||
178 | } |
||
179 | } |
||
180 | |||
228 |
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.