Completed
Pull Request — master (#294)
by lucky
02:29
created

Installer::parse_args()   C

Complexity

Conditions 12
Paths 9

Size

Total Lines 56

Duplication

Lines 24
Ratio 42.86 %

Importance

Changes 0
Metric Value
cc 12
nc 9
nop 1
dl 24
loc 56
rs 6.5333
c 0
b 0
f 0

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
 * Part of ci-phpunit-test
4
 *
5
 * @author     Kenji Suzuki <https://github.com/kenjis>
6
 * @license    MIT License
7
 * @copyright  2015 Kenji Suzuki
8
 * @link       https://github.com/kenjis/ci-phpunit-test
9
 */
10
11
class Installer
12
{
13
    private $silent = false;
14
    private $app_dir = 'application';  // -a DIRECTORY
15
    private $pub_dir = 'public';       // -p DIRECTORY
16
    private $test_dir = null;          // -t DIRECTORY
17
18
    public function __construct($argv)
19
    {
20
        $this->parse_args($argv);
21
    }
22
23
    private function parse_args($argv)
24
    {
25
        $argc = count($argv);
26
27
        if ($argc === 1) {
28
            return;
29
        }
30
31
        for ($i = 1; $i <= $argc; $i++) {
32
            if (! isset($argv[$i])) {
33
                break;
34
            }
35
36
            switch ($argv[$i]) {
37
                // php install.php -s
38
                case '-s':
39
                    $this->silent = true;
40
                    break;
41
42
                // php install.php -a application
43 View Code Duplication
                case '-a':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
44
                    if (is_dir($argv[$i+1])) {
45
                        $this->app_dir = $argv[$i+1];
46
                    } else {
47
                        throw new Exception('No such application directory: '.$argv[$i+1]);
48
                    }
49
                    $i++;
50
                    break;
51
52
                // php install.php -p public
53 View Code Duplication
                case '-p':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
                    if (is_dir($argv[$i+1])) {
55
                        $this->pub_dir = $argv[$i+1];
56
                    } else {
57
                        throw new Exception('No such public directory: '.$argv[$i+1]);
58
                    }
59
                    $i++;
60
                    break;
61
				// php install.php -t application/tests
62 View Code Duplication
                case '-t':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63
                    if (is_dir($argv[$i+1])) {
64
                        $this->test_dir = $argv[$i+1];
65
                    } else {
66
                        throw new Exception('No such test directory: '.$argv[$i+1]);
67
                    }
68
                    $i++;
69
                    break;
70
71
                default:
72
                    throw new Exception('Unknown argument: '.$argv[$i]);
73
            }
74
        }
75
        if (is_null($this->test_dir)) {
76
			$test_dir = $this->app_dir.'/tests';
0 ignored issues
show
Unused Code introduced by
$test_dir is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
77
		}
78
    }
79
80
    public function install()
81
    {
82
        $this->recursiveCopy(
83
            dirname(dirname(__FILE__)).'/application/tests',
84
            $this->test_dir
85
        );
86
        $this->fixPath();
87
    }
88
89
    /**
90
     * Fix paths in Bootstrap.php
91
     */
92
    private function fixPath()
93
    {
94
        $file = $this->test_dir.'/Bootstrap.php';
95
        $contents = file_get_contents($file);
96
97
        if (! file_exists('system')) {
98
            if (file_exists('vendor/codeigniter/framework/system')) {
99
                $contents = str_replace(
100
                    '$system_path = \'../../system\';',
101
                    '$system_path = \'../../vendor/codeigniter/framework/system\';',
102
                    $contents
103
                );
104
            } else {
105
                throw new Exception('Can\'t find "system" folder.');
106
            }
107
        }
108
109
        if (! file_exists('index.php')) {
110
            if (file_exists($this->pub_dir.'/index.php')) {
111
                // CodeIgniter 3.0.6 and after
112
                $contents = str_replace(
113
                    "define('FCPATH', realpath(dirname(__FILE__).'/../..').DIRECTORY_SEPARATOR);",
114
                    "define('FCPATH', realpath(dirname(__FILE__).'/../../{$this->pub_dir}').DIRECTORY_SEPARATOR);",
115
                    $contents
116
                );
117
                // CodeIgniter 3.0.5 and before
118
                $contents = str_replace(
119
                    "define('FCPATH', realpath(dirname(__FILE__).'/../..').'/');",
120
                    "define('FCPATH', realpath(dirname(__FILE__).'/../../{$this->pub_dir}').'/');",
121
                    $contents
122
                );
123
            } elseif (file_exists($this->app_dir.'/public/index.php')) {
124
                // CodeIgniter 3.0.6 and after
125
                $contents = str_replace(
126
                    "define('FCPATH', realpath(dirname(__FILE__).'/../..').DIRECTORY_SEPARATOR);",
127
                    "define('FCPATH', realpath(dirname(__FILE__).'/../public').DIRECTORY_SEPARATOR);",
128
                    $contents
129
                );
130
                // CodeIgniter 3.0.5 and before
131
                $contents = str_replace(
132
                    "define('FCPATH', realpath(dirname(__FILE__).'/../..').'/');",
133
                    "define('FCPATH', realpath(dirname(__FILE__).'/../public').'/');",
134
                    $contents
135
                );
136
                if ($this->app_dir !== 'application') {
137
                    $contents = str_replace(
138
                        "\$application_folder = '../../application';",
139
                        "\$application_folder = '../../{$this->app_dir}';",
140
                        $contents
141
                    );
142
                }
143
            } else {
144
                throw new Exception('Can\'t find "index.php".');
145
            }
146
        }
147
148
        file_put_contents($file, $contents);
149
    }
150
151
    public function update()
152
    {
153
        $target_dir = $this->test_dir.'/_ci_phpunit_test';
154
        $this->recursiveUnlink($target_dir);
155
        $this->recursiveCopy(
156
            dirname(dirname(__FILE__)).'/application/tests/_ci_phpunit_test',
157
            $target_dir
158
        );
159
    }
160
161
    /**
162
     * Recursive Copy
163
     *
164
     * @param string $src
165
     * @param string $dst
166
     */
167
    private function recursiveCopy($src, $dst)
168
    {
169
        @mkdir($dst, 0755);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

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...
170
171
        $iterator = new \RecursiveIteratorIterator(
172
            new \RecursiveDirectoryIterator($src, \RecursiveDirectoryIterator::SKIP_DOTS),
173
            \RecursiveIteratorIterator::SELF_FIRST
174
        );
175
176
        foreach ($iterator as $file) {
177
            if ($file->isDir()) {
178
                @mkdir($dst.'/'.$iterator->getSubPathName());
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

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...
179
            } else {
180
                $success = copy($file, $dst.'/'.$iterator->getSubPathName());
181
                if ($success) {
182
                    if (! $this->silent) {
183
                        echo 'copied: '.$dst.'/'.$iterator->getSubPathName().PHP_EOL;
184
                    }
185
                }
186
            }
187
        }
188
    }
189
190
    /**
191
     * Recursive Unlink
192
     *
193
     * @param string $dir
194
     */
195 View Code Duplication
    private function recursiveUnlink($dir)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
196
    {
197
        $iterator = new \RecursiveIteratorIterator(
198
            new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS),
199
            \RecursiveIteratorIterator::CHILD_FIRST
200
        );
201
202
        foreach ($iterator as $file) {
203
            if ($file->isDir()) {
204
                rmdir($file);
205
            } else {
206
                unlink($file);
207
            }
208
        }
209
210
        rmdir($dir);
211
    }
212
}
213