• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taobig / php-helper / 10331617378

10 Aug 2024 11:02AM UTC coverage: 95.108% (-0.1%) from 95.238%
10331617378

push

github

taobig
Update DatetimeHelper::convertTimezone()

7 of 8 new or added lines in 1 file covered. (87.5%)

6 existing lines in 1 file now uncovered.

486 of 511 relevant lines covered (95.11%)

1.51 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.67
/src/FileHelper.php
1
<?php
2

3
namespace taobig\helpers;
4

5
use taobig\helpers\exception\io\FileNotFoundException;
6
use taobig\helpers\exception\io\IOException;
7

8
class FileHelper
9
{
10

11
    public static function recurseCopy(string $src, string $dst)
12
    {
13
        if (file_exists($dst)) {
3✔
14
            if (!is_dir($dst)) {
1✔
15
                throw new \ErrorException("{$dst} has existed, but it not a dir");
1✔
16
            }
17
        } else {
18
//            mkdir
19
//            Emits an E_WARNING level error if the directory already exists.
20
//            Emits an E_WARNING level error if the relevant permissions prevent creating the directory.
21
            mkdir($dst, 0777, true);
2✔
22
            if (!is_writable($dst)) {
2✔
23
                throw new \ErrorException("create dir({$dst}) failed");
1✔
24
            }
25
        }
26
        $dir = opendir($src);
1✔
27
        while (false !== ($file = readdir($dir))) {
1✔
28
            if (($file != '.') && ($file != '..')) {
1✔
29
                $_path = $src . '/' . $file;
1✔
30
                if (is_dir($_path)) {
1✔
31
                    self::recurseCopy($_path, $dst . '/' . $file);
1✔
32
                } else {
33
                    //If the destination file already exists, it will be overwritten.
34
                    copy($_path, $dst . '/' . $file);
1✔
35
                }
36
            }
37
        }
38
        closedir($dir);
1✔
39
    }
40

41
    public static function recurseRemove(string $src)
42
    {
43
        $dir = opendir($src);
1✔
44
        while (false !== ($file = readdir($dir))) {
1✔
45
            if (($file != '.') && ($file != '..')) {
1✔
46
                $_path = $src . '/' . $file;
1✔
47
                if (is_dir($_path)) {
1✔
48
                    self::recurseRemove($_path);
1✔
49
                } else {
50
                    unlink($_path);
1✔
51
                }
52
            }
53
        }
54
        closedir($dir);
1✔
55
        rmdir($src);
1✔
56
    }
57

58
    public static function getLastNLines(string $filePath, int $lines = 1, string $eol = "\n"): string
59
    {
60
        if ($lines <= 0) {
4✔
61
            throw new \ValueError();
2✔
62
        }
63
        if (strlen($eol) != 1) {
3✔
64
            throw new \ValueError();
2✔
65
        }
66
        $content = "";
1✔
67
        $resource = fopen($filePath, 'r');
1✔
68
        if ($resource) {
1✔
69
            try {
70
                fseek($resource, -1, SEEK_END);
1✔
71
                $rowCount = 1;
1✔
72
                do {
73
                    $str = fgetc($resource);
1✔
74
                    if ($str === false) {
1✔
75
                        break;
1✔
76
                    }
77
                    if ($str === $eol) {
1✔
78
                        ++$rowCount;
1✔
79
                        if ($rowCount > $lines) {
1✔
80
                            break;
1✔
81
                        }
82
                    }
83
                    $content = $str . $content;
1✔
84
                    fseek($resource, -2, SEEK_CUR);
1✔
85
                } while (true);
1✔
86
            } finally {
87
                fclose($resource);
1✔
88
            }
89
        }
90
        return $content;
1✔
91
    }
92

93

94
    /**
95
     * @deprecated unsafe operation
96
     * @param string $filePath
97
     * @param int $lines
98
     * @return false|string|null
99
     */
100
    public static function getLastNLinesByTailf(string $filePath, int $lines = 1)
101
    {
102
        $cmd = "tail -n {$lines} " . escapeshellarg($filePath);
1✔
103
        return shell_exec($cmd);
1✔
104
    }
105

106
    public static function readCsvFile(string $filePath): array
107
    {
108
        if (!file_exists($filePath)) {
3✔
109
            throw new FileNotFoundException($filePath);
2✔
110
        }
111
//        if (!is_readable($filePath)) {
112
//            throw new IOException($filePath);
113
//        }
114
        $oldErrorHandler = set_error_handler(function ($errno, $str) use ($filePath) {
1✔
UNCOV
115
            if (!(error_reporting() & $errno)) {
×
UNCOV
116
                return false;
×
117
            }
UNCOV
118
            throw new IOException($filePath, $str, $errno);
×
119
        });
1✔
120
        try {
121
            $lineList = [];
1✔
122
            if (($handle = fopen($filePath, "r")) !== FALSE) {//fopen: Upon failure, an E_WARNING is emitted.
1✔
123
                try {
124
                    while (($data = fgetcsv($handle)) !== FALSE) {
1✔
125
                        $lineList[] = $data;
1✔
126
                    }
127
                } finally {
128
                    fclose($handle);
1✔
129
                }
130
                return $lineList;
1✔
131
            } else {
UNCOV
132
                throw new IOException($filePath);
×
133
            }
134
        } finally {
135
            set_error_handler($oldErrorHandler);
1✔
136
        }
137
    }
138

139
    public static function writeCsvFile(string $filePath, array $list, string $mode = 'a+'): void
140
    {
141
        $oldErrorHandler = set_error_handler(function ($errno, $str) use ($filePath) {
2✔
142
            if (!(error_reporting() & $errno)) {
1✔
UNCOV
143
                return false;
×
144
            }
145
            throw new IOException($filePath, $str, $errno);
1✔
146
        });
2✔
147
        try {
148
            if (($handle = fopen($filePath, $mode)) !== FALSE) {//fopen: Upon failure, an E_WARNING is emitted.
2✔
149
                try {
150
                    foreach ($list as $fields) {
1✔
151
                        fputcsv($handle, $fields);
1✔
152
                    }
153
                } finally {
154
                    fclose($handle);
1✔
155
                }
156
            } else {
UNCOV
157
                throw new IOException($filePath);
×
158
            }
159
        } finally {
160
            set_error_handler($oldErrorHandler);
2✔
161
        }
162
    }
163

164
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc