Conditions | 6 |
Paths | 7 |
Total Lines | 59 |
Code Lines | 40 |
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 |
||
46 | public function doDBUpdates() { |
||
47 | $startTime = microtime( true ); |
||
48 | $dbw = $this->getDB( DB_MASTER ); |
||
49 | $table = 'filearchive'; |
||
50 | $conds = [ 'fa_sha1' => '', 'fa_storage_key IS NOT NULL' ]; |
||
51 | |||
52 | if ( !$dbw->fieldExists( $table, 'fa_sha1', __METHOD__ ) ) { |
||
53 | $this->output( "fa_sha1 column does not exist\n\n", true ); |
||
54 | |||
55 | return false; |
||
56 | } |
||
57 | |||
58 | $this->output( "Populating fa_sha1 field from fa_storage_key\n" ); |
||
59 | $endId = $dbw->selectField( $table, 'MAX(fa_id)', false, __METHOD__ ); |
||
60 | |||
61 | $batchSize = $this->mBatchSize; |
||
62 | $done = 0; |
||
63 | |||
64 | do { |
||
65 | $res = $dbw->select( |
||
66 | $table, |
||
67 | [ 'fa_id', 'fa_storage_key' ], |
||
68 | $conds, |
||
69 | __METHOD__, |
||
70 | [ 'LIMIT' => $batchSize ] |
||
71 | ); |
||
72 | |||
73 | $i = 0; |
||
74 | foreach ( $res as $row ) { |
||
75 | if ( $row->fa_storage_key == '' ) { |
||
76 | // Revision was missing pre-deletion |
||
77 | continue; |
||
78 | } |
||
79 | $sha1 = LocalRepo::getHashFromKey( $row->fa_storage_key ); |
||
80 | $dbw->update( $table, |
||
81 | [ 'fa_sha1' => $sha1 ], |
||
82 | [ 'fa_id' => $row->fa_id ], |
||
83 | __METHOD__ |
||
84 | ); |
||
85 | $lastId = $row->fa_id; |
||
86 | $i++; |
||
87 | } |
||
88 | |||
89 | $done += $i; |
||
90 | if ( $i !== $batchSize ) { |
||
91 | break; |
||
92 | } |
||
93 | |||
94 | // print status and let replica DBs catch up |
||
95 | $this->output( sprintf( |
||
96 | "id %d done (up to %d), %5.3f%% \r", $lastId, $endId, $lastId / $endId * 100 ) ); |
||
97 | wfWaitForSlaves(); |
||
98 | } while ( true ); |
||
99 | |||
100 | $processingTime = microtime( true ) - $startTime; |
||
101 | $this->output( sprintf( "\nDone %d files in %.1f seconds\n", $done, $processingTime ) ); |
||
102 | |||
103 | return true; // we only updated *some* files, don't log |
||
104 | } |
||
105 | } |
||
109 |
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.