Installer::setFolderPermissions()   B
last analyzed

Complexity

Conditions 8
Paths 3

Size

Total Lines 55
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 33
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 55
ccs 0
cts 30
cp 0
crap 72
rs 8.1475

How to fix   Long Method   

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
0 ignored issues
show
Coding Style introduced by
Filename "Installer.php" doesn't match the expected filename "installer.php"
Loading history...
Coding Style introduced by
The PHP open tag does not have a corresponding PHP close tag
Loading history...
Coding Style introduced by
Header blocks must be separated by a single blank line
Loading history...
Coding Style introduced by
This file is missing a doc comment.
Loading history...
Coding Style introduced by
Class found in ".php" file; use ".inc" extension instead
Loading history...
2
declare(strict_types=1);
3
4
/**
0 ignored issues
show
Coding Style introduced by
The file-level docblock must follow the opening PHP tag in the file header
Loading history...
Coding Style introduced by
Inline doc block comments are not allowed; use "/* Comment */" or "// Comment" instead
Loading history...
Coding Style introduced by
Block comments must be started with /*
Loading history...
5
 * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
6
 * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
0 ignored issues
show
introduced by
Doc comment short description must be on a single line, further text should be a separate paragraph
Loading history...
7
 *
8
 * Licensed under The MIT License
9
 * For full copyright and license information, please see the LICENSE.txt
10
 * Redistributions of files must retain the above copyright notice.
11
 *
12
 * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
13
 * @link      https://cakephp.org CakePHP(tm) Project
0 ignored issues
show
introduced by
Tag value indented incorrectly; expected 1 space but found 6
Loading history...
Coding Style introduced by
The tag in position 2 should be the @license tag
Loading history...
14
 * @since     3.0.0
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @link tag
Loading history...
introduced by
Tag value indented incorrectly; expected 1 space but found 5
Loading history...
15
 * @license   https://opensource.org/licenses/mit-license.php MIT License
0 ignored issues
show
Coding Style introduced by
The tag in position 4 should be the @since tag
Loading history...
introduced by
Tag value indented incorrectly; expected 1 space but found 3
Loading history...
16
 */
0 ignored issues
show
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Header blocks must be separated by a single blank line
Loading history...
Coding Style introduced by
PHP version not specified
Loading history...
17
namespace App\Console;
18
19
if (!defined('STDIN')) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after NOT operator; 0 found
Loading history...
20
    define('STDIN', fopen('php://stdin', 'r'));
21
}
22
23
use Cake\Utility\Security;
24
use Composer\Script\Event;
25
use Exception;
0 ignored issues
show
introduced by
Non-namespaced classes/interfaces/traits should not be referenced with use statements
Loading history...
26
27
/**
28
 * Provides installation hooks for when this application is installed through
29
 * composer. Customize this class to suit your needs.
0 ignored issues
show
introduced by
Doc comment short description must be on a single line, further text should be a separate paragraph
Loading history...
30
 */
0 ignored issues
show
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
31
class Installer
32
{
0 ignored issues
show
introduced by
Opening brace should be on the same line as the declaration
Loading history...
Coding Style introduced by
Opening brace should be on the same line as the declaration for class Installer
Loading history...
33
    /**
34
     * An array of directories to be made writable
35
     */
36
    public const WRITABLE_DIRS = [
37
        'logs',
38
        'tmp',
39
        'tmp/cache',
40
        'tmp/cache/models',
41
        'tmp/cache/persistent',
42
        'tmp/cache/views',
43
        'tmp/sessions',
44
        'tmp/tests',
45
    ];
46
47
    /**
48
     * Does some routine installation tasks so people don't have to.
49
     *
50
     * @param \Composer\Script\Event $event The composer event object.
0 ignored issues
show
introduced by
Parameter comment must be on the next line
Loading history...
51
     * @throws \Exception Exception raised by validator.
0 ignored issues
show
introduced by
Separate the @param and @throws sections by a blank line.
Loading history...
introduced by
@throws comment must be on the next line
Loading history...
52
     * @return void
0 ignored issues
show
introduced by
If there is no return value for a function, there must not be a @return tag.
Loading history...
introduced by
Separate the @throws and @return sections by a blank line.
Loading history...
53
     */
54
    public static function postInstall(Event $event)
55
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
56
        $io = $event->getIO();
57
58
        $rootDir = dirname(dirname(__DIR__));
59
60
        static::createAppLocalConfig($rootDir, $io);
61
        static::createWritableDirectories($rootDir, $io);
62
63
        static::setFolderPermissions($rootDir, $io);
64
        static::setSecuritySalt($rootDir, $io);
65
66
        $class = 'Cake\Codeception\Console\Installer';
67
        if (class_exists($class)) {
68
            $class::customizeCodeceptionBinary($event);
69
        }
70
    }
71
72
    /**
73
     * Create config/app_local.php file if it does not exist.
74
     *
75
     * @param string $dir The application's root directory.
0 ignored issues
show
introduced by
Parameter comment must be on the next line
Loading history...
Coding Style introduced by
Expected 19 spaces after parameter type; 1 found
Loading history...
76
     * @param \Composer\IO\IOInterface $io IO interface to write to console.
0 ignored issues
show
introduced by
Parameter comment must be on the next line
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
77
     * @return void
0 ignored issues
show
introduced by
Separate the @param and @return sections by a blank line.
Loading history...
introduced by
If there is no return value for a function, there must not be a @return tag.
Loading history...
78
     */
79
    public static function createAppLocalConfig($dir, $io)
0 ignored issues
show
introduced by
Type hint "\Composer\IO\IOInterface" missing for $io
Loading history...
Coding Style introduced by
Type hint "string" missing for $dir
Loading history...
80
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
81
        $appLocalConfig = $dir . '/config/app_local.php';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
82
        $appLocalConfigTemplate = $dir . '/config/app_local.example.php';
83
        if (!file_exists($appLocalConfig)) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after NOT operator; 0 found
Loading history...
84
            copy($appLocalConfigTemplate, $appLocalConfig);
85
            $io->write('Created `config/app_local.php` file');
86
        }
87
    }
88
89
    /**
90
     * Create the `logs` and `tmp` directories.
91
     *
92
     * @param string $dir The application's root directory.
0 ignored issues
show
introduced by
Parameter comment must be on the next line
Loading history...
Coding Style introduced by
Expected 19 spaces after parameter type; 1 found
Loading history...
93
     * @param \Composer\IO\IOInterface $io IO interface to write to console.
0 ignored issues
show
introduced by
Parameter comment must be on the next line
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
94
     * @return void
0 ignored issues
show
introduced by
If there is no return value for a function, there must not be a @return tag.
Loading history...
introduced by
Separate the @param and @return sections by a blank line.
Loading history...
95
     */
96
    public static function createWritableDirectories($dir, $io)
0 ignored issues
show
Coding Style introduced by
Type hint "string" missing for $dir
Loading history...
introduced by
Type hint "\Composer\IO\IOInterface" missing for $io
Loading history...
97
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
98
        foreach (static::WRITABLE_DIRS as $path) {
99
            $path = $dir . '/' . $path;
100
            if (!file_exists($path)) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after NOT operator; 0 found
Loading history...
101
                mkdir($path);
102
                $io->write('Created `' . $path . '` directory');
103
            }
104
        }
105
    }
106
107
    /**
108
     * Set globally writable permissions on the "tmp" and "logs" directory.
109
     *
110
     * This is not the most secure default, but it gets people up and running quickly.
0 ignored issues
show
introduced by
Line exceeds 80 characters; contains 86 characters
Loading history...
111
     *
112
     * @param string $dir The application's root directory.
0 ignored issues
show
introduced by
Parameter comment must be on the next line
Loading history...
Coding Style introduced by
Expected 19 spaces after parameter type; 1 found
Loading history...
113
     * @param \Composer\IO\IOInterface $io IO interface to write to console.
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
introduced by
Parameter comment must be on the next line
Loading history...
114
     * @return void
0 ignored issues
show
introduced by
Separate the @param and @return sections by a blank line.
Loading history...
introduced by
If there is no return value for a function, there must not be a @return tag.
Loading history...
115
     */
116
    public static function setFolderPermissions($dir, $io)
0 ignored issues
show
Coding Style introduced by
Type hint "string" missing for $dir
Loading history...
introduced by
Type hint "\Composer\IO\IOInterface" missing for $io
Loading history...
117
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
118
        // ask if the permissions should be changed
0 ignored issues
show
introduced by
Inline comments must end in full-stops, exclamation marks, question marks, colons, or closing parentheses
Loading history...
Coding Style introduced by
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
Coding Style Documentation introduced by
Inline comments must start with a capital letter
Loading history...
119
        if ($io->isInteractive()) {
120
            $validator = function ($arg) {
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 12 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
121
                if (in_array($arg, ['Y', 'y', 'N', 'n'])) {
122
                    return $arg;
123
                }
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
124
                throw new Exception('This is not a valid answer. Please choose Y or n.');
125
            };
126
            $setFolderPermissions = $io->askAndValidate(
127
                '<info>Set Folder Permissions ? (Default to Y)</info> [<comment>Y,n</comment>]? ',
128
                $validator,
129
                10,
130
                'Y'
131
            );
132
133
            if (in_array($setFolderPermissions, ['n', 'N'])) {
134
                return;
135
            }
136
        }
137
138
        // Change the permissions on a path and output the results.
139
        $changePerms = function ($path) use ($io) {
140
            $currentPerms = fileperms($path) & 0777;
0 ignored issues
show
Coding Style introduced by
Operation must be bracketed
Loading history...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
141
            $worldWritable = $currentPerms | 0007;
0 ignored issues
show
Coding Style introduced by
Operation must be bracketed
Loading history...
142
            if ($worldWritable == $currentPerms) {
0 ignored issues
show
Coding Style introduced by
Operator == prohibited; use === instead
Loading history...
143
                return;
144
            }
145
146
            $res = chmod($path, $worldWritable);
147
            if ($res) {
148
                $io->write('Permissions set on ' . $path);
149
            } else {
0 ignored issues
show
introduced by
Expected newline after closing brace
Loading history...
150
                $io->write('Failed to set permissions on ' . $path);
151
            }
152
        };
153
154
        $walker = function ($dir) use (&$walker, $changePerms) {
155
            $files = array_diff(scandir($dir), ['.', '..']);
156
            foreach ($files as $file) {
157
                $path = $dir . '/' . $file;
158
159
                if (!is_dir($path)) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after NOT operator; 0 found
Loading history...
160
                    continue;
161
                }
162
163
                $changePerms($path);
164
                $walker($path);
165
            }
166
        };
167
168
        $walker($dir . '/tmp');
169
        $changePerms($dir . '/tmp');
170
        $changePerms($dir . '/logs');
171
    }
172
173
    /**
174
     * Set the security.salt value in the application's config file.
175
     *
176
     * @param string $dir The application's root directory.
0 ignored issues
show
introduced by
Parameter comment must be on the next line
Loading history...
Coding Style introduced by
Expected 19 spaces after parameter type; 1 found
Loading history...
177
     * @param \Composer\IO\IOInterface $io IO interface to write to console.
0 ignored issues
show
introduced by
Parameter comment must be on the next line
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
178
     * @return void
0 ignored issues
show
introduced by
Separate the @param and @return sections by a blank line.
Loading history...
introduced by
If there is no return value for a function, there must not be a @return tag.
Loading history...
179
     */
180
    public static function setSecuritySalt($dir, $io)
0 ignored issues
show
Coding Style introduced by
Type hint "string" missing for $dir
Loading history...
introduced by
Type hint "\Composer\IO\IOInterface" missing for $io
Loading history...
181
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
182
        $newKey = hash('sha256', Security::randomBytes(64));
183
        static::setSecuritySaltInFile($dir, $io, $newKey, 'app_local.php');
184
    }
185
186
    /**
187
     * Set the security.salt value in a given file
188
     *
189
     * @param string $dir The application's root directory.
0 ignored issues
show
Coding Style introduced by
Expected 19 spaces after parameter type; 1 found
Loading history...
introduced by
Parameter comment must be on the next line
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
190
     * @param \Composer\IO\IOInterface $io IO interface to write to console.
0 ignored issues
show
introduced by
Parameter comment must be on the next line
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter name; 1 found
Loading history...
191
     * @param string $newKey key to set in the file
0 ignored issues
show
introduced by
Parameter comment must be on the next line
Loading history...
Coding Style introduced by
Expected 19 spaces after parameter type; 1 found
Loading history...
introduced by
Parameter comment must start with a capital letter
Loading history...
introduced by
Parameter comment must end with a full stop
Loading history...
192
     * @param string $file A path to a file relative to the application's root
0 ignored issues
show
introduced by
Parameter comment must end with a full stop
Loading history...
Coding Style introduced by
Expected 19 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
introduced by
Parameter comment must be on the next line
Loading history...
193
     * @return void
0 ignored issues
show
introduced by
Separate the @param and @return sections by a blank line.
Loading history...
introduced by
If there is no return value for a function, there must not be a @return tag.
Loading history...
194
     */
195
    public static function setSecuritySaltInFile($dir, $io, $newKey, $file)
0 ignored issues
show
Coding Style introduced by
Type hint "string" missing for $dir
Loading history...
Coding Style introduced by
Type hint "string" missing for $newKey
Loading history...
Coding Style introduced by
Type hint "string" missing for $file
Loading history...
introduced by
Type hint "\Composer\IO\IOInterface" missing for $io
Loading history...
196
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
197
        $config = $dir . '/config/' . $file;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
198
        $content = file_get_contents($config);
199
200
        $content = str_replace('__SALT__', $newKey, $content, $count);
201
202
        if ($count == 0) {
0 ignored issues
show
Coding Style introduced by
Operator == prohibited; use === instead
Loading history...
203
            $io->write('No Security.salt placeholder to replace.');
204
205
            return;
206
        }
207
208
        $result = file_put_contents($config, $content);
209
        if ($result) {
210
            $io->write('Updated Security.salt value in config/' . $file);
211
212
            return;
213
        }
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
214
        $io->write('Unable to update Security.salt value.');
215
    }
216
217
    /**
218
     * Set the APP_NAME value in a given file
219
     *
220
     * @param string $dir The application's root directory.
0 ignored issues
show
introduced by
Parameter comment must be on the next line
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter name; 1 found
Loading history...
Coding Style introduced by
Expected 19 spaces after parameter type; 1 found
Loading history...
221
     * @param \Composer\IO\IOInterface $io IO interface to write to console.
0 ignored issues
show
introduced by
Parameter comment must be on the next line
Loading history...
Coding Style introduced by
Expected 6 spaces after parameter name; 1 found
Loading history...
222
     * @param string $appName app name to set in the file
0 ignored issues
show
Coding Style introduced by
Expected 19 spaces after parameter type; 1 found
Loading history...
introduced by
Parameter comment must start with a capital letter
Loading history...
introduced by
Parameter comment must end with a full stop
Loading history...
introduced by
Parameter comment must be on the next line
Loading history...
223
     * @param string $file A path to a file relative to the application's root
0 ignored issues
show
Coding Style introduced by
Expected 19 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
introduced by
Parameter comment must end with a full stop
Loading history...
introduced by
Parameter comment must be on the next line
Loading history...
224
     * @return void
0 ignored issues
show
introduced by
Separate the @param and @return sections by a blank line.
Loading history...
introduced by
If there is no return value for a function, there must not be a @return tag.
Loading history...
225
     */
226
    public static function setAppNameInFile($dir, $io, $appName, $file)
0 ignored issues
show
Coding Style introduced by
Type hint "string" missing for $appName
Loading history...
introduced by
Type hint "\Composer\IO\IOInterface" missing for $io
Loading history...
Coding Style introduced by
Type hint "string" missing for $dir
Loading history...
Coding Style introduced by
Type hint "string" missing for $file
Loading history...
227
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
228
        $config = $dir . '/config/' . $file;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
229
        $content = file_get_contents($config);
230
        $content = str_replace('__APP_NAME__', $appName, $content, $count);
231
232
        if ($count == 0) {
0 ignored issues
show
Coding Style introduced by
Operator == prohibited; use === instead
Loading history...
233
            $io->write('No __APP_NAME__ placeholder to replace.');
234
235
            return;
236
        }
237
238
        $result = file_put_contents($config, $content);
239
        if ($result) {
240
            $io->write('Updated __APP_NAME__ value in config/' . $file);
241
242
            return;
243
        }
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
244
        $io->write('Unable to update __APP_NAME__ value.');
245
    }
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line after function; 0 found
Loading history...
246
}
247