| Conditions | 18 |
| Paths | 26 |
| Total Lines | 123 |
| Code Lines | 74 |
| Lines | 34 |
| Ratio | 27.64 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 78 | protected function doDBUpdates() { |
||
| 79 | $mode = $this->getOption( 'mode', 'both' ); |
||
| 80 | $begin = $this->getOption( 'begin', '' ); |
||
| 81 | $throttle = $this->getOption( 'throttle', 0 ); |
||
| 82 | |||
| 83 | if ( !in_array( $mode, [ 'add', 'remove', 'both' ] ) ) { |
||
| 84 | $this->output( "--mode must be 'add', 'remove', or 'both'.\n" ); |
||
| 85 | return false; |
||
| 86 | } |
||
| 87 | |||
| 88 | $dbw = $this->getDB( DB_MASTER ); |
||
| 89 | |||
| 90 | $throttle = intval( $throttle ); |
||
| 91 | |||
| 92 | if ( $mode === 'add' || $mode === 'both' ) { |
||
| 93 | View Code Duplication | if ( $begin !== '' ) { |
|
| 94 | $where = [ 'page_title > ' . $dbw->addQuotes( $begin ) ]; |
||
| 95 | } else { |
||
| 96 | $where = []; |
||
| 97 | } |
||
| 98 | |||
| 99 | $this->output( "Adding empty categories with description pages...\n" ); |
||
| 100 | while ( true ) { |
||
| 101 | # Find which category to update |
||
| 102 | $rows = $dbw->select( |
||
| 103 | [ 'page', 'category' ], |
||
| 104 | 'page_title', |
||
| 105 | array_merge( $where, [ |
||
| 106 | 'page_namespace' => NS_CATEGORY, |
||
| 107 | 'cat_title' => null, |
||
| 108 | ] ), |
||
| 109 | __METHOD__, |
||
| 110 | [ |
||
| 111 | 'ORDER BY' => 'page_title', |
||
| 112 | 'LIMIT' => $this->mBatchSize, |
||
| 113 | ], |
||
| 114 | [ |
||
| 115 | 'category' => [ 'LEFT JOIN', 'page_title = cat_title' ], |
||
| 116 | ] |
||
| 117 | ); |
||
| 118 | if ( !$rows || $rows->numRows() <= 0 ) { |
||
| 119 | # Done, hopefully. |
||
| 120 | break; |
||
| 121 | } |
||
| 122 | |||
| 123 | View Code Duplication | foreach ( $rows as $row ) { |
|
| 124 | $name = $row->page_title; |
||
| 125 | $where = [ 'page_title > ' . $dbw->addQuotes( $name ) ]; |
||
| 126 | |||
| 127 | # Use the row to update the category count |
||
| 128 | $cat = Category::newFromName( $name ); |
||
| 129 | if ( !is_object( $cat ) ) { |
||
| 130 | $this->output( "The category named $name is not valid?!\n" ); |
||
| 131 | } else { |
||
| 132 | $cat->refreshCounts(); |
||
| 133 | } |
||
| 134 | } |
||
| 135 | $this->output( "--mode=$mode --begin=$name\n" ); |
||
| 136 | |||
| 137 | wfWaitForSlaves(); |
||
| 138 | usleep( $throttle * 1000 ); |
||
| 139 | } |
||
| 140 | |||
| 141 | $begin = ''; |
||
| 142 | } |
||
| 143 | |||
| 144 | if ( $mode === 'remove' || $mode === 'both' ) { |
||
| 145 | View Code Duplication | if ( $begin !== '' ) { |
|
| 146 | $where = [ 'cat_title > ' . $dbw->addQuotes( $begin ) ]; |
||
| 147 | } else { |
||
| 148 | $where = []; |
||
| 149 | } |
||
| 150 | $i = 0; |
||
| 151 | |||
| 152 | $this->output( "Removing empty categories without description pages...\n" ); |
||
| 153 | while ( true ) { |
||
| 154 | # Find which category to update |
||
| 155 | $rows = $dbw->select( |
||
| 156 | [ 'category', 'page' ], |
||
| 157 | 'cat_title', |
||
| 158 | array_merge( $where, [ |
||
| 159 | 'page_title' => null, |
||
| 160 | 'cat_pages' => 0, |
||
| 161 | ] ), |
||
| 162 | __METHOD__, |
||
| 163 | [ |
||
| 164 | 'ORDER BY' => 'cat_title', |
||
| 165 | 'LIMIT' => $this->mBatchSize, |
||
| 166 | ], |
||
| 167 | [ |
||
| 168 | 'page' => [ 'LEFT JOIN', [ |
||
| 169 | 'page_namespace' => NS_CATEGORY, 'page_title = cat_title' |
||
| 170 | ] ], |
||
| 171 | ] |
||
| 172 | ); |
||
| 173 | if ( !$rows || $rows->numRows() <= 0 ) { |
||
| 174 | # Done, hopefully. |
||
| 175 | break; |
||
| 176 | } |
||
| 177 | View Code Duplication | foreach ( $rows as $row ) { |
|
| 178 | $name = $row->cat_title; |
||
| 179 | $where = [ 'cat_title > ' . $dbw->addQuotes( $name ) ]; |
||
| 180 | |||
| 181 | # Use the row to update the category count |
||
| 182 | $cat = Category::newFromName( $name ); |
||
| 183 | if ( !is_object( $cat ) ) { |
||
| 184 | $this->output( "The category named $name is not valid?!\n" ); |
||
| 185 | } else { |
||
| 186 | $cat->refreshCounts(); |
||
| 187 | } |
||
| 188 | } |
||
| 189 | |||
| 190 | $this->output( "--mode=remove --begin=$name\n" ); |
||
| 191 | |||
| 192 | wfWaitForSlaves(); |
||
| 193 | usleep( $throttle * 1000 ); |
||
| 194 | } |
||
| 195 | } |
||
| 196 | |||
| 197 | $this->output( "Category cleanup complete.\n" ); |
||
| 198 | |||
| 199 | return true; |
||
| 200 | } |
||
| 201 | } |
||
| 205 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.