Conditions | 20 |
Paths | 1728 |
Total Lines | 87 |
Code Lines | 59 |
Lines | 4 |
Ratio | 4.6 % |
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 |
||
72 | public function execute() { |
||
73 | // Extensions will do stuff like $wgResourceModules += array(...) which is a |
||
74 | // fatal unless an array is already set. So set an empty value. |
||
75 | // And use the weird $__settings name to avoid any conflicts |
||
76 | // with real poorly named settings. |
||
77 | $__settings = array_merge( $this->getAllGlobals(), array_keys( $this->custom ) ); |
||
78 | foreach ( $__settings as $var ) { |
||
79 | $var = 'wg' . $var; |
||
80 | $$var = []; |
||
81 | } |
||
82 | unset( $var ); |
||
83 | $arg = $this->getArg( 0 ); |
||
84 | if ( !is_file( $arg ) ) { |
||
85 | $this->error( "$arg is not a file.", true ); |
||
86 | } |
||
87 | require $arg; |
||
88 | unset( $arg ); |
||
89 | // Try not to create any local variables before this line |
||
90 | $vars = get_defined_vars(); |
||
91 | unset( $vars['this'] ); |
||
92 | unset( $vars['__settings'] ); |
||
93 | $this->dir = dirname( realpath( $this->getArg( 0 ) ) ); |
||
94 | $this->json = []; |
||
95 | $globalSettings = $this->getAllGlobals(); |
||
96 | $configPrefix = $this->getOption( 'config-prefix', 'wg' ); |
||
97 | if ( $configPrefix !== 'wg' ) { |
||
98 | $this->json['config']['_prefix'] = $configPrefix; |
||
99 | } |
||
100 | foreach ( $vars as $name => $value ) { |
||
101 | $realName = substr( $name, 2 ); // Strip 'wg' |
||
102 | if ( $realName === false ) { |
||
103 | continue; |
||
104 | } |
||
105 | |||
106 | // If it's an empty array that we likely set, skip it |
||
107 | if ( is_array( $value ) && count( $value ) === 0 && in_array( $realName, $__settings ) ) { |
||
108 | continue; |
||
109 | } |
||
110 | |||
111 | if ( isset( $this->custom[$realName] ) ) { |
||
112 | call_user_func_array( [ $this, $this->custom[$realName] ], |
||
113 | [ $realName, $value, $vars ] ); |
||
114 | } elseif ( in_array( $realName, $globalSettings ) ) { |
||
115 | $this->json[$realName] = $value; |
||
116 | } elseif ( array_key_exists( $realName, $this->noLongerSupportedGlobals ) ) { |
||
117 | $this->output( 'Warning: Skipped global "' . $name . '" (' . |
||
118 | $this->noLongerSupportedGlobals[$realName] . '). ' . |
||
119 | "Please update the entry point before convert to registration.\n" ); |
||
120 | $this->hasWarning = true; |
||
121 | } elseif ( strpos( $name, $configPrefix ) === 0 ) { |
||
122 | // Most likely a config setting |
||
123 | $this->json['config'][substr( $name, strlen( $configPrefix ) )] = [ 'value' => $value ]; |
||
124 | } elseif ( $configPrefix !== 'wg' && strpos( $name, 'wg' ) === 0 ) { |
||
125 | // Warn about this |
||
126 | $this->output( 'Warning: Skipped global "' . $name . '" (' . |
||
127 | 'config prefix is "' . $configPrefix . '"). ' . |
||
128 | "Please check that this setting isn't needed.\n" ); |
||
129 | } |
||
130 | } |
||
131 | |||
132 | // check, if the extension requires composer libraries |
||
133 | if ( $this->needsComposerAutoloader( dirname( $this->getArg( 0 ) ) ) ) { |
||
134 | // set the load composer autoloader automatically property |
||
135 | $this->output( "Detected composer dependencies, setting 'load_composer_autoloader' to true.\n" ); |
||
136 | $this->json['load_composer_autoloader'] = true; |
||
137 | } |
||
138 | |||
139 | // Move some keys to the top |
||
140 | $out = []; |
||
141 | foreach ( $this->promote as $key ) { |
||
142 | View Code Duplication | if ( isset( $this->json[$key] ) ) { |
|
143 | $out[$key] = $this->json[$key]; |
||
144 | unset( $this->json[$key] ); |
||
145 | } |
||
146 | } |
||
147 | $out += $this->json; |
||
148 | // Put this at the bottom |
||
149 | $out['manifest_version'] = ExtensionRegistry::MANIFEST_VERSION; |
||
150 | $type = $this->hasOption( 'skin' ) ? 'skin' : 'extension'; |
||
151 | $fname = "{$this->dir}/$type.json"; |
||
152 | $prettyJSON = FormatJson::encode( $out, "\t", FormatJson::ALL_OK ); |
||
153 | file_put_contents( $fname, $prettyJSON . "\n" ); |
||
154 | $this->output( "Wrote output to $fname.\n" ); |
||
155 | if ( $this->hasWarning ) { |
||
156 | $this->output( "Found warnings! Please resolve the warnings and rerun this script.\n" ); |
||
157 | } |
||
158 | } |
||
159 | |||
308 |
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.