GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — integration (#2604)
by Brendan
05:28
created

Updater::run()   D

Complexity

Conditions 16
Paths 112

Size

Total Lines 107
Code Lines 55

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 16
eloc 55
c 2
b 1
f 0
nc 112
nop 0
dl 0
loc 107
rs 4.681

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
/**
3
 * @package install
4
 */
5
namespace SymphonyCms\Installer\Lib;
6
7
use DatabaseException;
8
use DirectoryIterator;
9
use Exception;
10
use General;
11
use Lang;
12
use Symphony;
13
14
class Updater extends Installer
15
{
16
    /**
17
     * This function returns an instance of the Updater class. It is the only way to
18
     * create a new Updater, as it implements the Singleton interface
19
     *
20
     * @return Updater
21
     */
22
    public static function instance()
23
    {
24
        if (!(self::$_instance instanceof Updater)) {
25
            self::$_instance = new Updater;
26
        }
27
28
        return self::$_instance;
29
    }
30
31
    /**
32
     * Initialises the language by looking at the existing configuration
33
     */
34
    public static function initialiseLang()
35
    {
36
        Lang::set(Symphony::Configuration()->get('lang', 'symphony'), false);
0 ignored issues
show
Bug introduced by
It seems like \Symphony::Configuration...get('lang', 'symphony') targeting Configuration::get() can also be of type array; however, Lang::set() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
37
    }
38
39
    /**
40
     * Initialises the configuration object by loading the existing
41
     * website config file
42
     *
43
     * @param array $data
44
     */
45
    public static function initialiseConfiguration(array $data = array())
46
    {
47
        parent::initialiseConfiguration();
48
    }
49
50
    /**
51
     * Overrides the `initialiseLog()` method and writes logs to `manifest/logs/update`
52
     *
53
     * @param string $filename
54
     * @return bool|void
55
     * @throws Exception
56
     */
57 View Code Duplication
    public static function initialiseLog($filename = null)
58
    {
59
        if (is_dir(INSTALL_LOGS) || General::realiseDirectory(
60
            INSTALL_LOGS,
61
            self::Configuration()->get('write_mode', 'directory')
0 ignored issues
show
Documentation introduced by
self::Configuration()->g...ite_mode', 'directory') is of type array|string, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
62
        )
63
        ) {
64
            parent::initialiseLog(INSTALL_LOGS . '/update');
65
        }
66
    }
67
68
    /**
69
     * Overrides the default `initialiseDatabase()` method
70
     * This allows us to still use the normal accessor
71
     */
72
    public static function initialiseDatabase()
73
    {
74
        self::setDatabase();
75
76
        $details = Symphony::Configuration()->get('database');
77
78
        try {
79
            Symphony::Database()->connect(
80
                $details['host'],
81
                $details['user'],
82
                $details['password'],
83
                $details['port'],
84
                $details['db']
85
            );
86
        } catch (DatabaseException $e) {
87
            self::__abort(
88
                'There was a problem while trying to establish a connection to the MySQL server. Please check your settings.',
89
                time()
90
            );
91
        }
92
93
        // MySQL: Setting prefix & character encoding
94
        Symphony::Database()->setPrefix($details['tbl_prefix']);
95
    }
96
97
    public function run()
0 ignored issues
show
Coding Style introduced by
run uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
98
    {
99
        // Initialize log
100
        if (is_null(Symphony::Log())) {
101
            self::__render(new UpdaterPage('missing-log'));
102
        }
103
104
        // Get available migrations. This will only contain the migrations
105
        // that are applicable to the current install.
106
        $migrations = array();
107
108
        foreach (new DirectoryIterator(INSTALL . '/migrations') as $m) {
109
            if ($m->isDot() || $m->isDir() || General::getExtension($m->getFilename()) !== 'php') {
110
                continue;
111
            }
112
113
            $version = str_replace('.php', '', $m->getFilename());
114
115
            // Include migration so we can see what the version is
116
            include_once($m->getPathname());
117
            $classname = 'SymphonyCms\\Installer\\Migrations\\migration_' . str_replace('.', '', $version);
118
119
            $m = new $classname();
120
121
            if (version_compare(
122
                Symphony::Configuration()->get('version', 'symphony'),
123
                call_user_func(array($m, 'getVersion')),
124
                '<'
125
            )) {
126
                $migrations[call_user_func(array($m, 'getVersion'))] = $m;
127
            }
128
        }
129
130
        // The DirectoryIterator may return files in a sporadic order
131
        // on different servers. This will ensure the array is sorted
132
        // correctly using `version_compare`
133
        uksort($migrations, 'version_compare');
134
135
        // If there are no applicable migrations then this is up to date
136
        if (empty($migrations)) {
137
            Symphony::Log()->info('Updater - Already up-to-date');
138
139
            self::__render(new UpdaterPage('uptodate'));
140
        } // Show start page
141
        elseif (!isset($_POST['action']['update'])) {
142
            $notes = array();
143
144
            // Loop over all available migrations showing there
145
            // pre update notes.
146
            foreach ($migrations as $version => $m) {
147
                $n = call_user_func(array($m, 'preUpdateNotes'));
148
                if (!empty($n)) {
149
                    $notes[$version] = $n;
150
                }
151
            }
152
153
            // Show the update ready page, which will display the
154
            // version and release notes of the most recent migration
155
            self::__render(new UpdaterPage('ready', array(
156
                'pre-notes' => $notes,
157
                'version' => call_user_func(array($m, 'getVersion')),
0 ignored issues
show
Bug introduced by
The variable $m does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
158
                'release-notes' => call_user_func(array($m, 'getReleaseNotes'))
159
            )));
160
        } // Upgrade Symphony
161
        else {
162
            $notes = array();
163
            $canProceed = true;
164
165
            // Loop over all the available migrations incrementally applying
166
            // the upgrades. If any upgrade throws an uncaught exception or
167
            // returns false, this will break and the failure page shown
168
            foreach ($migrations as $version => $m) {
169
                $n = call_user_func(array($m, 'postUpdateNotes'));
170
                if (!empty($n)) {
171
                    $notes[$version] = $n;
172
                }
173
174
                $canProceed = call_user_func(
175
                    array($m, 'run'),
176
                    'upgrade',
177
                    Symphony::Configuration()->get('version', 'symphony')
178
                );
179
180
                Symphony::Log()->info(
181
                    sprintf(
182
                        'Updater - Migration to %s was %s',
183
                        $version,
184
                        $canProceed ? 'successful' : 'unsuccessful'
185
                    )
186
                );
187
188
                if (!$canProceed) {
189
                    break;
190
                }
191
            }
192
193
            if (!$canProceed) {
194
                self::__render(new UpdaterPage('failure'));
195
            } else {
196
                self::__render(new UpdaterPage('success', array(
197
                    'post-notes' => $notes,
198
                    'version' => call_user_func(array($m, 'getVersion')),
199
                    'release-notes' => call_user_func(array($m, 'getReleaseNotes'))
200
                )));
201
            }
202
        }
203
    }
204
}
205