| Conditions | 25 |
| Paths | 18720 |
| Total Lines | 212 |
| Code Lines | 115 |
| 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 |
||
| 64 | public function install($config) |
||
| 65 | { |
||
| 66 | // Render header |
||
| 67 | $this->installHeader(); |
||
| 68 | |||
| 69 | $webserver = $this->findWebserver(); |
||
| 70 | $isIIS = $this->isIIS(); |
||
| 71 | $isApache = $this->isApache(); |
||
| 72 | |||
| 73 | flush(); |
||
| 74 | |||
| 75 | if (isset($config['stats'])) { |
||
| 76 | if (file_exists(FRAMEWORK_PATH . '/silverstripe_version')) { |
||
| 77 | $silverstripe_version = file_get_contents(FRAMEWORK_PATH . '/silverstripe_version'); |
||
| 78 | } else { |
||
| 79 | $silverstripe_version = "unknown"; |
||
| 80 | } |
||
| 81 | |||
| 82 | $phpVersion = urlencode(phpversion()); |
||
| 83 | $encWebserver = urlencode($webserver); |
||
| 84 | $dbType = $config['db']['type']; |
||
| 85 | |||
| 86 | // Try to determine the database version from the helper |
||
| 87 | $databaseVersion = $config['db']['type']; |
||
| 88 | $helper = $this->getDatabaseConfigurationHelper($dbType); |
||
| 89 | if ($helper && method_exists($helper, 'getDatabaseVersion')) { |
||
| 90 | $versionConfig = $config['db'][$dbType]; |
||
| 91 | $versionConfig['type'] = $dbType; |
||
| 92 | $databaseVersion = urlencode($dbType . ': ' . $helper->getDatabaseVersion($versionConfig)); |
||
| 93 | } |
||
| 94 | |||
| 95 | $url = "http://ss2stat.silverstripe.com/Installation/add?SilverStripe=$silverstripe_version&PHP=$phpVersion&Database=$databaseVersion&WebServer=$encWebserver"; |
||
| 96 | |||
| 97 | if (isset($_SESSION['StatsID']) && $_SESSION['StatsID']) { |
||
| 98 | $url .= '&ID=' . $_SESSION['StatsID']; |
||
| 99 | } |
||
| 100 | |||
| 101 | @$_SESSION['StatsID'] = file_get_contents($url); |
||
| 102 | } |
||
| 103 | |||
| 104 | if (file_exists('mysite/_config.php')) { |
||
| 105 | // Truncate the contents of _config instead of deleting it - we can't re-create it because Windows handles permissions slightly |
||
| 106 | // differently to UNIX based filesystems - it takes the permissions from the parent directory instead of retaining them |
||
| 107 | $fh = fopen('mysite/_config.php', 'wb'); |
||
| 108 | fclose($fh); |
||
| 109 | } |
||
| 110 | |||
| 111 | // Escape user input for safe insertion into PHP file |
||
| 112 | $theme = isset($_POST['template']) ? addcslashes($_POST['template'], "\'") : 'simple'; |
||
| 113 | $locale = isset($_POST['locale']) ? addcslashes($_POST['locale'], "\'") : 'en_US'; |
||
| 114 | $type = addcslashes($config['db']['type'], "\'"); |
||
| 115 | $dbConfig = $config['db'][$type]; |
||
| 116 | foreach ($dbConfig as &$configValue) { |
||
| 117 | $configValue = addcslashes($configValue, "\\\'"); |
||
| 118 | } |
||
| 119 | if (!isset($dbConfig['path'])) { |
||
| 120 | $dbConfig['path'] = ''; |
||
| 121 | } |
||
| 122 | if (!$dbConfig) { |
||
| 123 | echo "<p style=\"color: red\">Bad config submitted</p><pre>"; |
||
| 124 | print_r($config); |
||
| 125 | echo "</pre>"; |
||
| 126 | die(); |
||
| 127 | } |
||
| 128 | |||
| 129 | // Write the config file |
||
| 130 | global $usingEnv; |
||
| 131 | if ($usingEnv) { |
||
| 132 | $this->statusMessage("Setting up 'mysite/_config.php' for use with environment variables..."); |
||
| 133 | $this->writeToFile("mysite/_config.php", "<?php\n "); |
||
| 134 | } else { |
||
| 135 | $this->statusMessage("Setting up 'mysite/_config.php'..."); |
||
| 136 | // Create databaseConfig |
||
| 137 | $lines = array( |
||
| 138 | $lines[] = " 'type' => '$type'" |
||
| 139 | ); |
||
| 140 | foreach ($dbConfig as $key => $value) { |
||
| 141 | $lines[] = " '{$key}' => '$value'"; |
||
| 142 | } |
||
| 143 | $databaseConfigContent = implode(",\n", $lines); |
||
| 144 | $this->writeToFile("mysite/_config.php", <<<PHP |
||
| 145 | <?php |
||
| 146 | |||
| 147 | use SilverStripe\\ORM\\DB; |
||
| 148 | |||
| 149 | DB::setConfig([ |
||
| 150 | {$databaseConfigContent} |
||
| 151 | ]); |
||
| 152 | |||
| 153 | PHP |
||
| 154 | ); |
||
| 155 | } |
||
| 156 | |||
| 157 | $this->statusMessage("Setting up 'mysite/_config/config.yml'"); |
||
| 158 | $this->writeToFile("mysite/_config/config.yml", <<<YML |
||
| 159 | --- |
||
| 160 | Name: mysite |
||
| 161 | --- |
||
| 162 | # YAML configuration for SilverStripe |
||
| 163 | # See http://doc.silverstripe.org/framework/en/topics/configuration |
||
| 164 | # Caution: Indentation through two spaces, not tabs |
||
| 165 | SilverStripe\\View\\SSViewer: |
||
| 166 | themes: |
||
| 167 | - '$theme' |
||
| 168 | - '\$default' |
||
| 169 | SilverStripe\\i18n\\i18n: |
||
| 170 | default_locale: '$locale' |
||
| 171 | YML |
||
| 172 | ); |
||
| 173 | |||
| 174 | if (!$this->checkModuleExists('cms')) { |
||
| 175 | $this->writeToFile("mysite/code/RootURLController.php", <<<PHP |
||
| 176 | <?php |
||
| 177 | |||
| 178 | use SilverStripe\\Control\\Controller; |
||
| 179 | |||
| 180 | class RootURLController extends Controller { |
||
| 181 | |||
| 182 | public function index() { |
||
| 183 | echo "<html>Your site is now set up. Start adding controllers to mysite to get started.</html>"; |
||
| 184 | } |
||
| 185 | |||
| 186 | } |
||
| 187 | PHP |
||
| 188 | ); |
||
| 189 | } |
||
| 190 | |||
| 191 | // Write the appropriate web server configuration file for rewriting support |
||
| 192 | if ($this->hasRewritingCapability()) { |
||
| 193 | if ($isApache) { |
||
| 194 | $this->statusMessage("Setting up '.htaccess' file..."); |
||
| 195 | $this->createHtaccess(); |
||
| 196 | } elseif ($isIIS) { |
||
| 197 | $this->statusMessage("Setting up 'web.config' file..."); |
||
| 198 | $this->createWebConfig(); |
||
| 199 | } |
||
| 200 | } |
||
| 201 | |||
| 202 | // Mock request |
||
| 203 | $session = new Session(isset($_SESSION) ? $_SESSION : array()); |
||
| 204 | $request = new HTTPRequest('GET', '/'); |
||
| 205 | $request->setSession($session); |
||
| 206 | |||
| 207 | // Install kernel (fix to dev) |
||
| 208 | $kernel = new CoreKernel(BASE_PATH); |
||
| 209 | $kernel->setEnvironment(Kernel::DEV); |
||
| 210 | $app = new HTTPApplication($kernel); |
||
| 211 | |||
| 212 | // Build db within HTTPApplication |
||
| 213 | $app->execute($request, function (HTTPRequest $request) use ($config) { |
||
| 214 | // Start session and execute |
||
| 215 | $request->getSession()->init(); |
||
| 216 | |||
| 217 | // Output status |
||
| 218 | $this->statusMessage("Building database schema..."); |
||
| 219 | |||
| 220 | // Setup DB |
||
| 221 | $dbAdmin = new DatabaseAdmin(); |
||
| 222 | $dbAdmin->setRequest($request); |
||
| 223 | $dbAdmin->pushCurrent(); |
||
| 224 | $dbAdmin->doInit(); |
||
| 225 | $dbAdmin->doBuild(true); |
||
| 226 | |||
| 227 | // Create default administrator user and group in database |
||
| 228 | // (not using Security::setDefaultAdmin()) |
||
| 229 | $adminMember = DefaultAdminService::singleton()->findOrCreateDefaultAdmin(); |
||
| 230 | $adminMember->Email = $config['admin']['username']; |
||
| 231 | $adminMember->Password = $config['admin']['password']; |
||
| 232 | $adminMember->PasswordEncryption = Security::config()->get('encryption_algorithm'); |
||
| 233 | |||
| 234 | try { |
||
| 235 | $this->statusMessage('Creating default CMS admin account...'); |
||
| 236 | $adminMember->write(); |
||
| 237 | } catch (Exception $e) { |
||
| 238 | $this->statusMessage( |
||
| 239 | sprintf('Warning: Default CMS admin account could not be created (error: %s)', $e->getMessage()) |
||
| 240 | ); |
||
| 241 | } |
||
| 242 | |||
| 243 | $request->getSession()->set('username', $config['admin']['username']); |
||
| 244 | $request->getSession()->set('password', $config['admin']['password']); |
||
| 245 | $request->getSession()->save(); |
||
| 246 | }, true); |
||
| 247 | |||
| 248 | // Check result of install |
||
| 249 | if (!$this->errors) { |
||
| 250 | if (isset($_SERVER['HTTP_HOST']) && $this->hasRewritingCapability()) { |
||
| 251 | $this->statusMessage("Checking that friendly URLs work..."); |
||
| 252 | $this->checkRewrite(); |
||
| 253 | } else { |
||
| 254 | $token = new ParameterConfirmationToken('flush', $request); |
||
| 255 | $params = http_build_query($token->params()); |
||
| 256 | |||
| 257 | $destinationURL = 'index.php/' . |
||
| 258 | ($this->checkModuleExists('cms') ? "home/successfullyinstalled?$params" : "?$params"); |
||
| 259 | |||
| 260 | echo <<<HTML |
||
| 261 | <li>SilverStripe successfully installed; I am now redirecting you to your SilverStripe site...</li> |
||
| 262 | <script> |
||
| 263 | setTimeout(function() { |
||
| 264 | window.location = "$destinationURL"; |
||
| 265 | }, 2000); |
||
| 266 | </script> |
||
| 267 | <noscript> |
||
| 268 | <li><a href="$destinationURL">Click here to access your site.</a></li> |
||
| 269 | </noscript> |
||
| 270 | HTML; |
||
| 271 | } |
||
| 272 | } |
||
| 273 | |||
| 274 | return $this->errors; |
||
| 275 | } |
||
| 276 | |||
| 492 |
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call: