Completed
Push — master ( ed463f...5ddcb3 )
by Kenji
8s
created

Installer::install()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 8
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
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 View Code Duplication
    public static function install($app = 'application')
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...
16
    {
17
        self::recursiveCopy(
18
            dirname(__FILE__) . '/application/tests',
19
            $app . '/' . static::TEST_FOLDER
20
        );
21
        self::fixPath($app);
22
    }
23
24
    /**
25
     * Fix paths in Bootstrap.php
26
     */
27
    private static function fixPath($app = 'application')
28
    {
29
        $file = $app . '/' . static::TEST_FOLDER . '/Bootstrap.php';
30
        $contents = file_get_contents($file);
31
        
32
        if (! file_exists('system')) {
33
            if (file_exists('vendor/codeigniter/framework/system')) {
34
                $contents = str_replace(
35
                    '$system_path = \'../../system\';',
36
                    '$system_path = \'../../vendor/codeigniter/framework/system\';',
37
                    $contents
38
                );
39
            } else {
40
                throw new Exception('Can\'t find "system" folder.');
41
            }
42
        }
43
        
44
        if (! file_exists('index.php')) {
45
            if (file_exists('public/index.php')) {
46
                // CodeIgniter 3.0.6 and after
47
                $contents = str_replace(
48
                    "define('FCPATH', realpath(dirname(__FILE__).'/../..').DIRECTORY_SEPARATOR);",
49
                    "define('FCPATH', realpath(dirname(__FILE__).'/../../public').DIRECTORY_SEPARATOR);",
50
                    $contents
51
                );
52
                // CodeIgniter 3.0.5 and before
53
                $contents = str_replace(
54
                    "define('FCPATH', realpath(dirname(__FILE__).'/../..').'/');",
55
                    "define('FCPATH', realpath(dirname(__FILE__).'/../../public').'/');",
56
                    $contents
57
                );
58
            } elseif (file_exists($app . '/public/index.php')) {
59
                // CodeIgniter 3.0.6 and after
60
                $contents = str_replace(
61
                    "define('FCPATH', realpath(dirname(__FILE__).'/../..').DIRECTORY_SEPARATOR);",
62
                    "define('FCPATH', realpath(dirname(__FILE__).'/../public').DIRECTORY_SEPARATOR);",
63
                    $contents
64
                );
65
                // CodeIgniter 3.0.5 and before
66
                $contents = str_replace(
67
                    "define('FCPATH', realpath(dirname(__FILE__).'/../..').'/');",
68
                    "define('FCPATH', realpath(dirname(__FILE__).'/../public').'/');",
69
                    $contents
70
                );
71
                if ($app != 'application') {
72
                    $contents = str_replace(
73
                        "\$application_folder = '../../application';",
74
                        "\$application_folder = '../../{$app}';",
75
                        $contents
76
                    );
77
                }
78
            } else {
79
                throw new Exception('Can\'t find "index.php".');
80
            }
81
        }
82
        
83
        file_put_contents($file, $contents);
84
    }
85
86 View Code Duplication
    public static function update($app = 'application')
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...
87
    {
88
        $target_dir = $app . '/' . static::TEST_FOLDER . '/_ci_phpunit_test';
89
        self::recursiveUnlink($target_dir);
90
        self::recursiveCopy(
91
            dirname(__FILE__) . '/application/tests/_ci_phpunit_test',
92
            $target_dir
93
        );
94
    }
95
96
    /**
97
     * Recursive Copy
98
     *
99
     * @param string $src
100
     * @param string $dst
101
     */
102
    private static function recursiveCopy($src, $dst)
103
    {
104
        @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...
105
        
106
        $iterator = new \RecursiveIteratorIterator(
107
            new \RecursiveDirectoryIterator($src, \RecursiveDirectoryIterator::SKIP_DOTS),
108
            \RecursiveIteratorIterator::SELF_FIRST
109
        );
110
        
111
        foreach ($iterator as $file) {
112
            if ($file->isDir()) {
113
                @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...
114
            } else {
115
                $success = copy($file, $dst . '/' . $iterator->getSubPathName());
116
                if ($success) {
117
                    echo 'copied: ' . $dst . '/' . $iterator->getSubPathName() . PHP_EOL;
118
                }
119
            }
120
        }
121
    }
122
123
    /**
124
     * Recursive Unlink
125
     *
126
     * @param string $dir
127
     */
128 View Code Duplication
    private static 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...
129
    {
130
        $iterator = new \RecursiveIteratorIterator(
131
            new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS),
132
            \RecursiveIteratorIterator::CHILD_FIRST
133
        );
134
        
135
        foreach ($iterator as $file) {
136
            if ($file->isDir()) {
137
                rmdir($file);
138
            } else {
139
                unlink($file);
140
            }
141
        }
142
        
143
        rmdir($dir);
144
    }
145
}
146