CreateClone::cloneFile()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 3
nop 5
dl 0
loc 15
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace XoopsModules\Tdmcreate\Files;
4
use XoopsModules\Tdmcreate;
5
6
/**
7
 * Class CreateClone
8
 */
9
class CreateClone
10
{
11
    /**
12
     * Delete a file or recursively delete a directory
13
     *
14
     * @param string $path Path to file or directory
15
16
    public static function deleteFileFolder($path) {
17
18
        if (is_file($path)) {
19
            return @unlink($path);
20
        }
21
        elseif (is_dir($path)) {
22
            $scan = glob(rtrim($path,'/').'/*');
23
            foreach($scan as $index=>$path) {
24
                self::deleteFileFolder($path);
25
            }
26
            return @rmdir($path);
27
        }
28
    }*/
29
30
    // recursive cloning script
31
    /**
32
     * @param $src_path
33
     * @param $dst_path
34
     * @param bool $replace_code
35
     * @param array $patKeys
36
     * @param array $patValues
37
     */
38
    public static function cloneFileFolder($src_path, $dst_path, $replace_code = false, $patKeys = [], $patValues =[])
39
    {
40
        // open the source directory
41
        $dir = opendir($src_path);
42
        // Make the destination directory if not exist
43
        @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

43
        /** @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...
44
        // Loop through the files in source directory
45
        while( $file = readdir($dir) ) {
0 ignored issues
show
Bug introduced by
It seems like $dir can also be of type false; however, parameter $dir_handle of readdir() does only seem to accept resource, 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

45
        while( $file = readdir(/** @scrutinizer ignore-type */ $dir) ) {
Loading history...
46
            if (( $file != '.' ) && ( $file != '..' )) {
47
                if ( is_dir($src_path . '/' . $file) ) {
48
                    // Recursively calling custom copy function for sub directory
49
                    self::cloneFileFolder($src_path . '/' . $file, $dst_path . '/' . $file, $replace_code, $patKeys, $patValues);
50
                } else {
51
                    self::cloneFile($src_path . '/' . $file, $dst_path . '/' . $file, $replace_code, $patKeys, $patValues);
52
                }
53
            }
54
        }
55
        closedir($dir);
0 ignored issues
show
Bug introduced by
It seems like $dir can also be of type false; however, parameter $dir_handle of closedir() does only seem to accept resource, 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

55
        closedir(/** @scrutinizer ignore-type */ $dir);
Loading history...
56
    }
57
58
    /**
59
     * @param $src_file
60
     * @param $dst_file
61
     * @param bool $replace_code
62
     * @param array $patKeys
63
     * @param array $patValues
64
     */
65
    public static function cloneFile($src_file, $dst_file, $replace_code = false, $patKeys = [], $patValues =[])
66
    {
67
        if ($replace_code) {
68
            $noChangeExtensions = ['jpeg', 'jpg', 'gif', 'png', 'zip', 'ttf'];
69
            if (in_array(mb_strtolower(pathinfo($src_file, PATHINFO_EXTENSION)), $noChangeExtensions)) {
70
                // image
71
                copy($src_file, $dst_file);
72
            } else {
73
                // file, read it
74
                $content = file_get_contents($src_file);
75
                $content = str_replace($patKeys, $patValues, $content);
76
                file_put_contents($dst_file, $content);
77
            }
78
        } else {
79
            copy($src_file, $dst_file);
80
        }
81
    }
82
}
83