Completed
Push — master ( 34e3bf...f1b122 )
by Kenji
02:35
created

Installer.php (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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':
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':
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
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
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...
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
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);
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());
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)
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