Conditions | 19 |
Paths | 3072 |
Total Lines | 113 |
Code Lines | 69 |
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 | #!/usr/bin/env php |
||
87 | function execute() { |
||
88 | global $wgVersion, $wgLang, $wgAllowSchemaUpdates; |
||
89 | |||
90 | if ( !$wgAllowSchemaUpdates |
||
91 | && !( $this->hasOption( 'force' ) |
||
92 | || $this->hasOption( 'schema' ) |
||
93 | || $this->hasOption( 'noschema' ) ) |
||
94 | ) { |
||
95 | $this->error( "Do not run update.php on this wiki. If you're seeing this you should\n" |
||
96 | . "probably ask for some help in performing your schema updates or use\n" |
||
97 | . "the --noschema and --schema options to get an SQL file for someone\n" |
||
98 | . "else to inspect and run.\n\n" |
||
99 | . "If you know what you are doing, you can continue with --force\n", true ); |
||
100 | } |
||
101 | |||
102 | $this->fileHandle = null; |
||
103 | if ( substr( $this->getOption( 'schema' ), 0, 2 ) === "--" ) { |
||
104 | $this->error( "The --schema option requires a file as an argument.\n", true ); |
||
105 | } elseif ( $this->hasOption( 'schema' ) ) { |
||
106 | $file = $this->getOption( 'schema' ); |
||
107 | $this->fileHandle = fopen( $file, "w" ); |
||
108 | if ( $this->fileHandle === false ) { |
||
109 | $err = error_get_last(); |
||
110 | $this->error( "Problem opening the schema file for writing: $file\n\t{$err['message']}", true ); |
||
111 | } |
||
112 | } |
||
113 | |||
114 | $lang = Language::factory( 'en' ); |
||
115 | // Set global language to ensure localised errors are in English (bug 20633) |
||
116 | RequestContext::getMain()->setLanguage( $lang ); |
||
117 | $wgLang = $lang; // BackCompat |
||
118 | |||
119 | define( 'MW_UPDATER', true ); |
||
120 | |||
121 | $this->output( "MediaWiki {$wgVersion} Updater\n\n" ); |
||
122 | |||
123 | wfWaitForSlaves(); |
||
124 | |||
125 | if ( !$this->hasOption( 'skip-compat-checks' ) ) { |
||
126 | $this->compatChecks(); |
||
127 | } else { |
||
128 | $this->output( "Skipping compatibility checks, proceed at your own risk (Ctrl+C to abort)\n" ); |
||
129 | wfCountDown( 5 ); |
||
130 | } |
||
131 | |||
132 | // Check external dependencies are up to date |
||
133 | if ( !$this->hasOption( 'skip-external-dependencies' ) ) { |
||
134 | $composerLockUpToDate = $this->runChild( 'CheckComposerLockUpToDate' ); |
||
135 | $composerLockUpToDate->execute(); |
||
136 | } else { |
||
137 | $this->output( |
||
138 | "Skipping checking whether external dependencies are up to date, proceed at your own risk\n" |
||
139 | ); |
||
140 | } |
||
141 | |||
142 | # Attempt to connect to the database as a privileged user |
||
143 | # This will vomit up an error if there are permissions problems |
||
144 | $db = $this->getDB( DB_MASTER ); |
||
145 | |||
146 | $this->output( "Going to run database updates for " . wfWikiID() . "\n" ); |
||
147 | if ( $db->getType() === 'sqlite' ) { |
||
148 | /** @var Database|DatabaseSqlite $db */ |
||
149 | $this->output( "Using SQLite file: '{$db->getDbFilePath()}'\n" ); |
||
150 | } |
||
151 | $this->output( "Depending on the size of your database this may take a while!\n" ); |
||
152 | |||
153 | if ( !$this->hasOption( 'quick' ) ) { |
||
154 | $this->output( "Abort with control-c in the next five seconds " |
||
155 | . "(skip this countdown with --quick) ... " ); |
||
156 | wfCountDown( 5 ); |
||
157 | } |
||
158 | |||
159 | $time1 = microtime( true ); |
||
160 | |||
161 | $shared = $this->hasOption( 'doshared' ); |
||
162 | |||
163 | $updates = [ 'core', 'extensions' ]; |
||
164 | if ( !$this->hasOption( 'schema' ) ) { |
||
165 | if ( $this->hasOption( 'noschema' ) ) { |
||
166 | $updates[] = 'noschema'; |
||
167 | } |
||
168 | $updates[] = 'stats'; |
||
169 | } |
||
170 | |||
171 | $updater = DatabaseUpdater::newForDB( $db, $shared, $this ); |
||
172 | $updater->doUpdates( $updates ); |
||
173 | |||
174 | foreach ( $updater->getPostDatabaseUpdateMaintenance() as $maint ) { |
||
175 | $child = $this->runChild( $maint ); |
||
176 | |||
177 | // LoggedUpdateMaintenance is checking the updatelog itself |
||
178 | $isLoggedUpdate = $child instanceof LoggedUpdateMaintenance; |
||
179 | |||
180 | if ( !$isLoggedUpdate && $updater->updateRowExists( $maint ) ) { |
||
181 | continue; |
||
182 | } |
||
183 | |||
184 | $child->execute(); |
||
185 | if ( !$isLoggedUpdate ) { |
||
186 | $updater->insertUpdateRow( $maint ); |
||
187 | } |
||
188 | } |
||
189 | |||
190 | $updater->setFileAccess(); |
||
191 | if ( !$this->hasOption( 'nopurge' ) ) { |
||
192 | $updater->purgeCache(); |
||
193 | } |
||
194 | |||
195 | $time2 = microtime( true ); |
||
196 | |||
197 | $timeDiff = $lang->formatTimePeriod( $time2 - $time1 ); |
||
198 | $this->output( "\nDone in $timeDiff.\n" ); |
||
199 | } |
||
200 | |||
218 |
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.