CreateClone   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 76
rs 10
c 0
b 0
f 0
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A cloneFileFolder() 0 18 5
A cloneFile() 0 20 3
1
<?php
2
3
namespace XoopsModules\Modulebuilder\Files;
4
5
use XoopsModules\Modulebuilder;
6
7
/**
8
 * Class CreateClone
9
 */
10
class CreateClone
11
{
12
    /**
13
     * Delete a file or recursively delete a directory
14
     *
15
     * @param string $path Path to file or directory
16
     *
17
     * public static function deleteFileFolder($path) {
18
     *
19
     * if (is_file($path)) {
20
     * return @\unlink($path);
21
     * }
22
     * elseif (\is_dir($path)) {
23
     * $scan = glob(r\trim($path,'/').'/*');
24
     * foreach($scan as $index=>$path) {
25
     * self::deleteFileFolder($path);
26
     * }
27
     * return @\rmdir($path);
28
     * }
29
     * }*/
30
31
    // recursive cloning script
32
    /**
33
     * @param       $src_path
34
     * @param       $dst_path
35
     * @param bool  $replace_code
36
     * @param array $patKeys
37
     * @param array $patValues
38
     */
39
    public static function cloneFileFolder($src_path, $dst_path, $replace_code = false, $patKeys = [], $patValues = [])
40
    {
41
        // open the source directory
42
        $dir = \opendir($src_path);
43
        // Make the destination directory if not exist
44
        @\mkdir($dst_path);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for mkdir(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

44
        /** @scrutinizer ignore-unhandled */ @\mkdir($dst_path);

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...
45
        // Loop through the files in source directory
46
        while ($file = \readdir($dir)) {
47
            if (($file != '.') && ($file != '..')) {
48
                if (\is_dir($src_path . '/' . $file)) {
49
                    // Recursively calling custom copy function for sub directory
50
                    self::cloneFileFolder($src_path . '/' . $file, $dst_path . '/' . $file, $replace_code, $patKeys, $patValues);
51
                } else {
52
                    self::cloneFile($src_path . '/' . $file, $dst_path . '/' . $file, $replace_code, $patKeys, $patValues);
53
                }
54
            }
55
        }
56
        \closedir($dir);
57
    }
58
59
    /**
60
     * @param       $src_file
61
     * @param       $dst_file
62
     * @param bool  $replace_code
63
     * @param array $patKeys
64
     * @param array $patValues
65
     */
66
    public static function cloneFile($src_file, $dst_file, $replace_code = false, $patKeys = [], $patValues = [])
67
    {
68
        if ($replace_code) {
69
            $noChangeExtensions = ['jpeg', 'jpg', 'gif', 'png', 'zip', 'ttf'];
70
            if (\in_array(\mb_strtolower(\pathinfo($src_file, PATHINFO_EXTENSION)), $noChangeExtensions)) {
0 ignored issues
show
Bug introduced by
It seems like pathinfo($src_file, Xoop...les\PATHINFO_EXTENSION) can also be of type array; however, parameter $string of mb_strtolower() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
            if (\in_array(\mb_strtolower(/** @scrutinizer ignore-type */ \pathinfo($src_file, PATHINFO_EXTENSION)), $noChangeExtensions)) {
Loading history...
71
                // image
72
                \copy($src_file, $dst_file);
73
            } else {
74
                // file, read it and replace text
75
                $content = file_get_contents($src_file);
76
                $content = \str_replace($patKeys, $patValues, $content);
77
                //check file name whether it contains replace code
78
                $path_parts = \pathinfo($dst_file);
79
                $path       = $path_parts['dirname'];
80
                $file       = $path_parts['basename'];
81
                $dst_file   = $path . '/' . \str_replace($patKeys, $patValues, $file);
82
                file_put_contents($dst_file, $content);
83
            }
84
        } else {
85
            \copy($src_file, $dst_file);
86
        }
87
    }
88
}
89