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.

Build::execute()   C
last analyzed

Complexity

Conditions 16
Paths 164

Size

Total Lines 114
Code Lines 75

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
cc 16
eloc 75
c 3
b 0
f 1
nc 164
nop 0
dl 0
loc 114
rs 5.0333

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
 * This file is part of the O2System PHP Framework package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @author         Steeve Andrian Salim
9
 * @copyright      Copyright (c) Steeve Andrian Salim
10
 */
11
12
// ------------------------------------------------------------------------
13
14
namespace O2System\Reactor\Cli\Commanders;
15
16
// ------------------------------------------------------------------------
17
18
use O2System\Kernel\Cli\Writers\Format;
19
use O2System\Reactor\Cli\Commander;
20
21
/**
22
 * Class Build
23
 * @package O2System\Reactor\Cli\Commanders
24
 */
25
class Build extends Commander
26
{
27
    /**
28
     * Build::$commandVersion
29
     *
30
     * Command version.
31
     *
32
     * @var string
33
     */
34
    protected $commandVersion = '1.0.0';
35
36
    /**
37
     * Build::$commandDescription
38
     *
39
     * Command description.
40
     *
41
     * @var string
42
     */
43
    protected $commandDescription = 'CLI_BUILD_DESC';
44
45
    /**
46
     * Build::$commandOptions
47
     *
48
     * Command options.
49
     *
50
     * @var array
51
     */
52
    protected $commandOptions = [
53
        'filename' => [
54
            'description' => 'CLI_BUILD_FILENAME_HELP',
55
            'required'    => false,
56
        ],
57
        'main'     => [
58
            'description' => 'CLI_BUILD_MAIN_HELP',
59
            'required'    => false,
60
        ],
61
        'force'    => [
62
            'description' => 'CLI_BUILD_FORCE_HELP',
63
            'required'    => false,
64
        ],
65
    ];
66
67
    /**
68
     * Build::$optionFilename
69
     *
70
     * Build filename.
71
     *
72
     * @var string
73
     */
74
    protected $optionFilename;
75
76
    /**
77
     * Build::$optionMain
78
     *
79
     * Build main script.
80
     *
81
     * @var string
82
     */
83
    protected $optionMain;
84
85
    /**
86
     * Build::optionFilename
87
     *
88
     * @param string $name
89
     */
90
    public function optionFilename($filename)
91
    {
92
        $this->optionFilename = str_replace('.phar', '', $filename) . '.phar';
93
    }
94
95
    // ------------------------------------------------------------------------
96
97
    /**
98
     * Build::optionMain
99
     *
100
     * @param string $name
101
     */
102
    public function optionMain($main)
103
    {
104
        $this->optionMain = $main;
105
    }
106
107
    // ------------------------------------------------------------------------
108
109
    /**
110
     * Build::execute
111
     *
112
     * @throws \ReflectionException
113
     */
114
    public function execute()
115
    {
116
        $this->__callOptions();
117
118
        $filename = empty($this->optionFilename) ? 'app.phar' : $this->optionFilename;
119
        $filename = str_replace('.phar', '', $filename);
120
        $filePath = PATH_ROOT . 'build' . DIRECTORY_SEPARATOR . $filename;
0 ignored issues
show
Bug introduced by
The constant O2System\Reactor\Cli\Commanders\PATH_ROOT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
121
122
        if (ini_get('phar.readonly') == 1 and $this->optionForce === false) {
0 ignored issues
show
Bug Best Practice introduced by
The property optionForce does not exist on O2System\Reactor\Cli\Commanders\Build. Did you maybe forget to declare it?
Loading history...
123
            output()->write(
0 ignored issues
show
Bug introduced by
The method write() does not exist on O2System\Kernel\Http\Output. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

123
            output()->/** @scrutinizer ignore-call */ write(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
124
                (new Format())
125
                    ->setContextualClass(Format::DANGER)
126
                    ->setString(language()->getLine('CLI_BUILD_PHAR_READONLY'))
127
                    ->setNewLinesAfter(1)
128
            );
129
            exit(EXIT_ERROR);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
130
        }
131
132
        output()->verbose(
0 ignored issues
show
Bug introduced by
The method verbose() does not exist on O2System\Kernel\Http\Output. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

132
        output()->/** @scrutinizer ignore-call */ verbose(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
133
            (new Format())
134
                ->setContextualClass(Format::INFO)
135
                ->setString(language()->getLine('CLI_BUILD_START'))
136
                ->setNewLinesAfter(1)
137
        );
138
139
        // Remove build directory
140
        $fileDirectory = dirname($filePath) . DIRECTORY_SEPARATOR;
141
        if (is_dir($fileDirectory)) {
142
            $directoryHandle = opendir($fileDirectory);
143
            if ( ! $directoryHandle) {
0 ignored issues
show
introduced by
$directoryHandle is of type false|resource, thus it always evaluated to false.
Loading history...
144
                return false;
145
            }
146
            while ($file = readdir($directoryHandle)) {
147
                if ($file != '.' && $file != '..') {
148
                    if (is_file($fileDirectory . $file)) {
149
                        unlink($fileDirectory . $file);
150
                    }
151
                }
152
            }
153
            closedir($directoryHandle);
154
            rmdir($fileDirectory);
155
        }
156
157
        if ( ! is_writable(dirname($filePath))) {
158
            @mkdir(dirname($filePath), 0777, true);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for mkdir(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

158
            /** @scrutinizer ignore-unhandled */ @mkdir(dirname($filePath), 0777, true);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
159
160
            output()->verbose(
161
                (new Format())
162
                    ->setContextualClass(Format::INFO)
163
                    ->setString(language()->getLine('CLI_BUILD_MAKE_DIRECTORY'))
164
                    ->setNewLinesAfter(1)
165
            );
166
        }
167
168
        try {
169
            $pharData = new \PharData($filePath . '.tar');
170
            $phar = $pharData->convertToExecutable(\Phar::PHAR);
171
172
            // Build from PATH_ROOT using Recursive Directory Iterator
173
            $phar->buildFromIterator(new \RecursiveIteratorIterator(new \RecursiveCallbackFilterIterator(
174
                new \RecursiveDirectoryIterator(PATH_ROOT,
175
                    \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS),
176
                function ($current, $key, $iterator) {
0 ignored issues
show
Bug introduced by
function(...) { /* ... */ } of type callable is incompatible with the type string expected by parameter $callback of RecursiveCallbackFilterIterator::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

176
                /** @scrutinizer ignore-type */ function ($current, $key, $iterator) {
Loading history...
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

176
                function ($current, /** @scrutinizer ignore-unused */ $key, $iterator) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $iterator is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

176
                function ($current, $key, /** @scrutinizer ignore-unused */ $iterator) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
177
                    if ($current->isDir()) {
178
                        // exclude build directory
179
                        if ($current->getFilename() === 'build') {
180
                            return false;
181
                        }
182
                    }
183
184
                    return true;
185
                })), PATH_ROOT);
186
187
            // Define main script
188
            $main = 'public/index.php';
189
            if (empty($this->optionMain)) {
190
                // Default Carbon Boilerplate Detection
191
                if (is_file(PATH_APP . 'console')) {
192
                    $main = 'app/console';
193
194
                    output()->verbose(
195
                        (new Format())
196
                            ->setContextualClass(Format::WARNING)
197
                            ->setString(language()->getLine('CLI_BUILD_USE_CARBON_DEFAULT'))
198
                            ->setNewLinesAfter(1)
199
                    );
200
                }
201
            } else {
202
                $main = $this->optionMain;
203
            }
204
205
            $phar->setStub($phar->createDefaultStub($main));
206
207
            output()->verbose(
208
                (new Format())
209
                    ->setContextualClass(Format::INFO)
210
                    ->setString(language()->getLine('CLI_BUILD_START_GZ_COMPRESSION'))
211
                    ->setNewLinesAfter(1)
212
            );
213
214
            $pharData->convertToExecutable(\Phar::TAR, \Phar::GZ, '.phar.tgz');
215
216
            output()->write(
217
                (new Format())
218
                    ->setContextualClass(Format::SUCCESS)
219
                    ->setString(language()->getLine('CLI_BUILD_SUCCESSFUL'))
220
                    ->setNewLinesAfter(1)
221
            );
222
        } catch (\Exception $exception) {
223
            output()->write(
224
                (new Format())
225
                    ->setContextualClass(Format::DANGER)
226
                    ->setString($exception->getMessage())
227
                    ->setNewLinesAfter(1)
228
            );
229
        }
230
    }
231
}