Completed
Branch master (00e474)
by Michael
05:07
created

clone.php ➔ cloneFileFolder()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 18
nc 6
nop 1
dl 0
loc 31
rs 6.7272
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 30 and the first side effect is on line 17.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/*
3
4
Usage:
5
6
    Copy clone.php in <xoops_root>
7
    Change current working directory to <xoops_root>
8
    Update mappings as per new modulename.
9
10
    php -q clone.php
11
12
*/
13
14
// ##########################################################
15
//	Define your mapping here
16
// ##########################################################
17
$patterns = array(
18
    // first one must be module directory name
19
    'oledrion' => 'bouquins',
20
    'OLEDRION' => 'BOUQUINS',
21
    'Oledrion' => 'Bouquins',
22
    'Oledrion' => 'Bouquins'
23
);
24
25
$patKeys = array_keys($patterns);
26
$patValues = array_values($patterns);
27
28
// work around for PHP < 5.0.x
29
if (!function_exists('file_put_contents')) {
30
    function file_put_contents($filename, $data, $file_append = false)
31
    {
32
        $fp = fopen($filename, (!$file_append ? 'w+' : 'a+'));
33
        if (!$fp) {
34
            trigger_error('file_put_contents cannot write in file.', E_USER_ERROR);
35
36
            return;
37
        }
38
        fputs($fp, $data);
39
        fclose($fp);
40
    }
41
}
42
43
// recursive clonning script
44
function cloneFileFolder($path)
45
{
46
    global $patKeys;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
47
    global $patValues;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
48
49
    $newPath = str_replace($patKeys[0], $patValues[0], $path);
50
51
    if (is_dir($path)) {
52
        // create new dir
53
        mkdir($newPath);
54
55
        // check all files in dir, and process it
56
        if ($handle = opendir($path)) {
57
            while ($file = readdir($handle)) {
58
                if ($file != '.' && $file != '..') {
59
                    cloneFileFolder("$path/$file");
60
                }
61
            }
62
            closedir($handle);
63
        }
64
    } else {
65
        if (preg_match('/(.jpg|.gif|.png|.zip)$/i', $path)) {
66
            copy($path, $newPath);
67
        } else {
68
            // file, read it
69
            $content = file_get_contents($path);
70
            $content = str_replace($patKeys, $patValues, $content);
71
            file_put_contents($newPath, $content);
72
        }
73
    }
74
}
75
76
cloneFileFolder('modules/oledrion');
77
78
echo "Happy cloning...\n";
79
echo "check directory modules/" . $patterns['oledrion'] . " for cloned module \n";
80
echo "Consider modifying new module by editing language/english/modinfo.php and assets/images/oledrion_logo.png manually (if you care)\n";
81