Conditions | 10 |
Paths | 55 |
Total Lines | 67 |
Code Lines | 45 |
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 |
||
133 | private function pokeFile( $orig, $new ) { |
||
134 | $path = $this->filePath( $orig ); |
||
135 | if ( !file_exists( $path ) ) { |
||
136 | $this->output( "missing file: $path\n" ); |
||
137 | $this->killRow( $orig ); |
||
138 | |||
139 | return; |
||
140 | } |
||
141 | |||
142 | $db = $this->getDB( DB_MASTER ); |
||
143 | |||
144 | /* |
||
145 | * To prevent key collisions in the update() statements below, |
||
146 | * if the target title exists in the image table, or if both the |
||
147 | * original and target titles exist in the page table, append |
||
148 | * increasing version numbers until the target title exists in |
||
149 | * neither. (See also bug 16916.) |
||
150 | */ |
||
151 | $version = 0; |
||
152 | $final = $new; |
||
153 | $conflict = ( $this->imageExists( $final, $db ) || |
||
154 | ( $this->pageExists( $orig, $db ) && $this->pageExists( $final, $db ) ) ); |
||
155 | |||
156 | while ( $conflict ) { |
||
157 | $this->output( "Rename conflicts with '$final'...\n" ); |
||
158 | $version++; |
||
159 | $final = $this->appendTitle( $new, "_$version" ); |
||
160 | $conflict = ( $this->imageExists( $final, $db ) || $this->pageExists( $final, $db ) ); |
||
161 | } |
||
162 | |||
163 | $finalPath = $this->filePath( $final ); |
||
164 | |||
165 | if ( $this->dryrun ) { |
||
166 | $this->output( "DRY RUN: would rename $path to $finalPath\n" ); |
||
167 | } else { |
||
168 | $this->output( "renaming $path to $finalPath\n" ); |
||
169 | // @todo FIXME: Should this use File::move()? |
||
170 | $this->beginTransaction( $db, __METHOD__ ); |
||
171 | $db->update( 'image', |
||
172 | [ 'img_name' => $final ], |
||
173 | [ 'img_name' => $orig ], |
||
174 | __METHOD__ ); |
||
175 | $db->update( 'oldimage', |
||
176 | [ 'oi_name' => $final ], |
||
177 | [ 'oi_name' => $orig ], |
||
178 | __METHOD__ ); |
||
179 | $db->update( 'page', |
||
180 | [ 'page_title' => $final ], |
||
181 | [ 'page_title' => $orig, 'page_namespace' => NS_FILE ], |
||
182 | __METHOD__ ); |
||
183 | $dir = dirname( $finalPath ); |
||
184 | if ( !file_exists( $dir ) ) { |
||
185 | if ( !wfMkdirParents( $dir, null, __METHOD__ ) ) { |
||
186 | $this->output( "RENAME FAILED, COULD NOT CREATE $dir" ); |
||
187 | $this->rollbackTransaction( $db, __METHOD__ ); |
||
188 | |||
189 | return; |
||
190 | } |
||
191 | } |
||
192 | if ( rename( $path, $finalPath ) ) { |
||
193 | $this->commitTransaction( $db, __METHOD__ ); |
||
194 | } else { |
||
195 | $this->error( "RENAME FAILED" ); |
||
196 | $this->rollbackTransaction( $db, __METHOD__ ); |
||
197 | } |
||
198 | } |
||
199 | } |
||
200 | |||
225 |
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.