Completed
Push — master ( 189faf...34e3bf )
by Kenji
02:38
created

Installer::recursiveUnlink()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 17
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 3
nop 1
dl 17
loc 17
rs 9.4285
c 0
b 0
f 0
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
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
12
{
13
    const TEST_FOLDER = 'tests';
14
15
    private $silent = false;
16
    private $app_dir = 'application';
17
    private $pub_dir = 'public';
18
19
    public function __construct($argv)
20
    {
21
        $this->parse_args($argv);
22
    }
23
24
    private function parse_args($argv)
25
    {
26
        $argc = count($argv);
27
28
        if ($argc === 1) {
29
            return;
30
        }
31
32
        for ($i = 1; $i <= $argc; $i++) {
33
            if (! isset($argv[$i])) {
34
                break;
35
            }
36
37
            switch ($argv[$i]) {
38
                // php install.php -s
39
                case '-s':
40
                    $this->silent = true;
41
                    break;
42
43
                // php install.php -a application
44 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...
45
                    if (is_dir($argv[$i+1])) {
46
                        $this->app_dir = $argv[$i+1];
47
                    } else {
48
                        throw new Exception('No such directory: ' . $argv[$i+1]);
49
                    }
50
                    $i++;
51
                    break;
52
53
                // php install.php -p public
54 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...
55
                    if (is_dir($argv[$i+1])) {
56
                        $this->pub_dir = $argv[$i+1];
57
                    } else {
58
                        throw new Exception('No such directory: ' . $argv[$i+1]);
59
                    }
60
                    $i++;
61
                    break;
62
63
                default:
64
                    throw new Exception('Unknown argument: ' . $argv[$i]);
65
            }
66
        }
67
    }
68
69
    public function install()
70
    {
71
        $this->recursiveCopy(
72
            dirname(__FILE__) . '/application/tests',
73
            $this->app . '/' . static::TEST_FOLDER
0 ignored issues
show
Bug introduced by
The property app does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
74
        );
75
        $this->fixPath($this->app, $this->pub);
0 ignored issues
show
Bug introduced by
The property pub does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Unused Code introduced by
The call to Installer::fixPath() has too many arguments starting with $this->app.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
76
    }
77
78
    /**
79
     * Fix paths in Bootstrap.php
80
     */
81
    private function fixPath()
82
    {
83
        $file = $this->app . '/' . static::TEST_FOLDER . '/Bootstrap.php';
84
        $contents = file_get_contents($file);
85
86
        if (! file_exists('system')) {
87
            if (file_exists('vendor/codeigniter/framework/system')) {
88
                $contents = str_replace(
89
                    '$system_path = \'../../system\';',
90
                    '$system_path = \'../../vendor/codeigniter/framework/system\';',
91
                    $contents
92
                );
93
            } else {
94
                throw new Exception('Can\'t find "system" folder.');
95
            }
96
        }
97
98
        if (! file_exists('index.php')) {
99
            if (file_exists($pub . '/index.php')) {
100
                // CodeIgniter 3.0.6 and after
101
                $contents = str_replace(
102
                    "define('FCPATH', realpath(dirname(__FILE__).'/../..').DIRECTORY_SEPARATOR);",
103
                    "define('FCPATH', realpath(dirname(__FILE__).'/../../'. $pub).DIRECTORY_SEPARATOR);",
0 ignored issues
show
Bug introduced by
The variable $pub does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
104
                    $contents
105
                );
106
                // CodeIgniter 3.0.5 and before
107
                $contents = str_replace(
108
                    "define('FCPATH', realpath(dirname(__FILE__).'/../..').'/');",
109
                    "define('FCPATH', realpath(dirname(__FILE__).'/../../' . $pub).'/');",
110
                    $contents
111
                );
112
            } elseif (file_exists($this->app . '/public/index.php')) {
113
                // CodeIgniter 3.0.6 and after
114
                $contents = str_replace(
115
                    "define('FCPATH', realpath(dirname(__FILE__).'/../..').DIRECTORY_SEPARATOR);",
116
                    "define('FCPATH', realpath(dirname(__FILE__).'/../public').DIRECTORY_SEPARATOR);",
117
                    $contents
118
                );
119
                // CodeIgniter 3.0.5 and before
120
                $contents = str_replace(
121
                    "define('FCPATH', realpath(dirname(__FILE__).'/../..').'/');",
122
                    "define('FCPATH', realpath(dirname(__FILE__).'/../public').'/');",
123
                    $contents
124
                );
125
                if ($this->app != 'application') {
126
                    $contents = str_replace(
127
                        "\$application_folder = '../../application';",
128
                        "\$application_folder = '../../{$this->app}';",
129
                        $contents
130
                    );
131
                }
132
            } else {
133
                throw new Exception('Can\'t find "index.php".');
134
            }
135
        }
136
137
        file_put_contents($file, $contents);
138
    }
139
140
    public function update()
141
    {
142
        $target_dir = $this->app . '/' . static::TEST_FOLDER . '/_ci_phpunit_test';
143
        $this->recursiveUnlink($target_dir);
144
        $this->recursiveCopy(
145
            dirname(__FILE__) . '/application/tests/_ci_phpunit_test',
146
            $target_dir
147
        );
148
    }
149
150
    /**
151
     * Recursive Copy
152
     *
153
     * @param string $src
154
     * @param string $dst
155
     */
156
    private function recursiveCopy($src, $dst)
157
    {
158
        @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...
159
160
        $iterator = new \RecursiveIteratorIterator(
161
            new \RecursiveDirectoryIterator($src, \RecursiveDirectoryIterator::SKIP_DOTS),
162
            \RecursiveIteratorIterator::SELF_FIRST
163
        );
164
165
        foreach ($iterator as $file) {
166
            if ($file->isDir()) {
167
                @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...
168
            } else {
169
                $success = copy($file, $dst . '/' . $iterator->getSubPathName());
170
                if ($success) {
171
                    if (! $this->silent) {
172
                        echo 'copied: ' . $dst . '/' . $iterator->getSubPathName() . PHP_EOL;
173
                    }
174
                }
175
            }
176
        }
177
    }
178
179
    /**
180
     * Recursive Unlink
181
     *
182
     * @param string $dir
183
     */
184 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...
185
    {
186
        $iterator = new \RecursiveIteratorIterator(
187
            new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS),
188
            \RecursiveIteratorIterator::CHILD_FIRST
189
        );
190
191
        foreach ($iterator as $file) {
192
            if ($file->isDir()) {
193
                rmdir($file);
194
            } else {
195
                unlink($file);
196
            }
197
        }
198
199
        rmdir($dir);
200
    }
201
}
202