| Conditions | 11 |
| Paths | 288 |
| Total Lines | 65 |
| Code Lines | 41 |
| 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 |
||
| 94 | public function transformI18nFile( $phpfile, $jsondir = null ) { |
||
| 95 | if ( !$jsondir ) { |
||
| 96 | // Assume the json directory should be in the same directory as the |
||
| 97 | // .i18n.php file. |
||
| 98 | $jsondir = dirname( $phpfile ) . "/i18n"; |
||
| 99 | } |
||
| 100 | if ( !is_dir( $jsondir ) ) { |
||
| 101 | $this->output( "Creating directory $jsondir.\n" ); |
||
| 102 | $success = mkdir( $jsondir ); |
||
| 103 | if ( !$success ) { |
||
| 104 | $this->error( "Could not create directory $jsondir", 1 ); |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | if ( !is_readable( $phpfile ) ) { |
||
| 109 | $this->error( "Error reading $phpfile", 1 ); |
||
| 110 | } |
||
| 111 | include $phpfile; |
||
| 112 | $phpfileContents = file_get_contents( $phpfile ); |
||
| 113 | |||
| 114 | if ( !isset( $messages ) ) { |
||
| 115 | $this->error( "PHP file $phpfile does not define \$messages array", 1 ); |
||
| 116 | } |
||
| 117 | |||
| 118 | if ( !$messages ) { |
||
| 119 | $this->error( "PHP file $phpfile contains an empty \$messages array. " . |
||
| 120 | "Maybe it was already converted?", 1 ); |
||
| 121 | } |
||
| 122 | |||
| 123 | if ( !isset( $messages['en'] ) || !is_array( $messages['en'] ) ) { |
||
| 124 | $this->error( "PHP file $phpfile does not set language codes", 1 ); |
||
| 125 | } |
||
| 126 | |||
| 127 | foreach ( $messages as $langcode => $langmsgs ) { |
||
| 128 | $authors = $this->getAuthorsFromComment( $this->findCommentBefore( |
||
| 129 | "\$messages['$langcode'] =", |
||
| 130 | $phpfileContents |
||
| 131 | ) ); |
||
| 132 | // Make sure the @metadata key is the first key in the output |
||
| 133 | $langmsgs = array_merge( |
||
| 134 | [ '@metadata' => [ 'authors' => $authors ] ], |
||
| 135 | $langmsgs |
||
| 136 | ); |
||
| 137 | |||
| 138 | $jsonfile = "$jsondir/$langcode.json"; |
||
| 139 | $success = file_put_contents( |
||
| 140 | $jsonfile, |
||
| 141 | FormatJson::encode( $langmsgs, "\t", FormatJson::ALL_OK ) . "\n" |
||
| 142 | ); |
||
| 143 | if ( $success === false ) { |
||
| 144 | $this->error( "FAILED to write $jsonfile", 1 ); |
||
| 145 | } |
||
| 146 | $this->output( "$jsonfile\n" ); |
||
| 147 | } |
||
| 148 | |||
| 149 | $this->output( |
||
| 150 | "All done. To complete the conversion, please do the following:\n" . |
||
| 151 | "* Add \$wgMessagesDirs['YourExtension'] = __DIR__ . '/i18n';\n" . |
||
| 152 | "* Remove \$wgExtensionMessagesFiles['YourExtension']\n" . |
||
| 153 | "* Delete the old PHP message file\n" . |
||
| 154 | "This script no longer generates backward compatibility shims! If you need\n" . |
||
| 155 | "compatibility with MediaWiki 1.22 and older, use the MediaWiki 1.23 version\n" . |
||
| 156 | "of this script instead, or create a shim manually.\n" |
||
| 157 | ); |
||
| 158 | } |
||
| 159 | |||
| 196 |
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.