GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( f8bc47...6d880e )
by t
05:07 queued 02:53
created

FtpFile   A

Complexity

Total Complexity 42

Size/Duplication

Total Lines 235
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 74
dl 0
loc 235
ccs 0
cts 144
cp 0
rs 9.0399
c 1
b 0
f 0
wmc 42

20 Methods

Rating   Name   Duplication   Size   Complexity  
A chown() 0 3 1
A _move() 0 3 1
A getLists() 0 13 6
A downloadFile() 0 7 4
A _rmdir() 0 3 1
A chmod() 0 9 4
A _copy() 0 8 2
A close() 0 3 1
A deleteFile() 0 6 2
A getFilesize() 0 3 1
A getFileContent() 0 11 3
A getCommandResult() 0 3 1
A isDir() 0 8 2
A symlink() 0 3 1
A __construct() 0 8 1
A isFile() 0 3 1
A putFileContent() 0 15 4
A chgrp() 0 3 1
A uploadFile() 0 8 4
A _mkdir() 0 5 1

How to fix   Complexity   

Complex Class

Complex classes like FtpFile often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use FtpFile, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * Class FtpFile
4
 *
5
 * @link https://www.icy2003.com/
6
 * @author icy2003 <[email protected]>
7
 * @copyright Copyright (c) 2019, icy2003
8
 */
9
namespace icy2003\php\icomponents\file;
10
11
use icy2003\php\C;
12
use icy2003\php\I;
13
use icy2003\php\ihelpers\Arrays;
14
15
/**
16
 * FTP 文件
17
 */
18
class FtpFile extends Base
19
{
20
21
    /**
22
     * FTP 连接
23
     *
24
     * @var resource
25
     */
26
    protected $_conn;
27
28
    /**
29
     * 构造函数
30
     *
31
     * @param array $config ftp 连接配置。至少包括:host、username、password,可选:port、timeout
32
     */
33
    public function __construct($config)
34
    {
35
        C::assertTrue(function_exists('ftp_connect'), '请开启 ftp 扩展');
36
        C::assertTrue(Arrays::keyExistsAll(['host', 'username', 'password'], $config, $diff), '缺少 ' . implode(',', $diff) . ' 参数');
37
        $this->_conn = ftp_connect((string)I::get($config, 'host'), (int)I::get($config, 'port', 21), (int)I::get($config, 'timeout', 90));
38
        C::assertTrue(is_resource($this->_conn), '连接失败');
39
        C::assertTrue(@ftp_login($this->_conn, (string)I::get($config, 'username'), (string)I::get($config, 'password')), '账号密码错误');
40
        ftp_pasv($this->_conn, true);
41
    }
42
43
    /**
44
     * @ignore
45
     */
46
    public function getCommandResult($command)
47
    {
48
        return implode(',', ftp_raw($this->_conn, $command));
49
    }
50
51
    /**
52
     * @ignore
53
     */
54
    public function isFile($file)
55
    {
56
        return ftp_size($this->_conn, $file) > -1;
57
    }
58
59
    /**
60
     * @ignore
61
     */
62
    public function isDir($dir)
63
    {
64
        $current = ftp_pwd($this->_conn);
65
        if (@ftp_chdir($this->_conn, $dir)) {
66
            ftp_chdir($this->_conn, $current);
67
            return true;
68
        }
69
        return false;
70
    }
71
72
    /**
73
     * @ignore
74
     */
75
    public function getLists($dir = null, $flags = FileConstants::COMPLETE_PATH)
76
    {
77
        $list = [];
78
        null === $dir && $dir = ftp_pwd($this->_conn);
79
        $dir = rtrim($dir, '/') . '/';
80
        $files = ftp_nlist($this->_conn, $dir);
81
        foreach ($files as $file) {
82
            if (I::hasFlag($flags, FileConstants::RECURSIVE) && $this->isDir($file)) {
83
                $list = array_merge($list, $this->getLists($file, $flags));
84
            }
85
            $list[] = I::hasFlag($flags, FileConstants::COMPLETE_PATH) ? $file : $this->getBasename($file);
86
        }
87
        return $list;
88
    }
89
90
    /**
91
     * @ignore
92
     */
93
    public function getFilesize($file)
94
    {
95
        return ftp_size($this->_conn, $file);
96
    }
97
98
    /**
99
     * @ignore
100
     */
101
    public function getFileContent($file)
102
    {
103
        if ($fp = fopen('php://temp', 'r+')) {
104
            if (@ftp_fget($this->_conn, $fp, $file, FTP_BINARY, 0)) {
105
                rewind($fp);
106
                $content = stream_get_contents($fp);
107
                fclose($fp);
108
                return $content;
109
            }
110
        }
111
        return false;
112
    }
113
114
    /**
115
     * @ignore
116
     */
117
    public function putFileContent($file, $string, $mode = 0777)
118
    {
119
        if (is_array($string)) {
120
            $string = implode('', $string);
121
        } elseif (is_resource($string)) {
122
            $string = stream_get_contents($string);
123
        }
124
        $this->createDir($this->getDirname($file));
125
        if ($fp = fopen('data://text/plain,' . $string, 'r')) {
126
            $isNewFile = @ftp_fput($this->_conn, $file, $fp, FTP_BINARY);
127
            fclose($fp);
128
            $this->chmod($file, $mode, FileConstants::RECURSIVE_DISABLED);
129
            return $isNewFile;
130
        }
131
        return false;
132
    }
133
134
    /**
135
     * @ignore
136
     */
137
    public function deleteFile($file)
138
    {
139
        if ($this->isFile($file)) {
140
            return ftp_delete($this->_conn, $file);
141
        }
142
        return true;
143
    }
144
145
    /**
146
     * @ignore
147
     */
148
    public function uploadFile($toFile, $fromFile = null, $overwrite = true)
149
    {
150
        null === $fromFile && $fromFile = './' . $this->getBasename($toFile);
151
        if (false === $overwrite && $this->isFile($toFile)) {
152
            return false;
153
        }
154
        $this->createDir($this->getDirname($toFile));
155
        return ftp_put($this->_conn, $toFile, $fromFile, FTP_BINARY, 0);
156
    }
157
158
    /**
159
     * @ignore
160
     */
161
    public function downloadFile($fromFile, $toFile = null, $overwrite = true)
162
    {
163
        null === $toFile && $toFile = './' . $this->getBasename($fromFile);
164
        if (false === $overwrite && $this->isFile($toFile)) {
165
            return false;
166
        }
167
        return ftp_get($this->_conn, $toFile, $fromFile, FTP_BINARY, 0);
168
    }
169
170
    /**
171
     * @ignore
172
     */
173
    public function chown($file, $user, $flags = FileConstants::RECURSIVE_DISABLED)
174
    {
175
        return false;
176
    }
177
178
    /**
179
     * @ignore
180
     */
181
    public function chgrp($file, $group, $flags = FileConstants::RECURSIVE_DISABLED)
182
    {
183
        return false;
184
    }
185
186
    /**
187
     * @ignore
188
     */
189
    public function chmod($file, $mode = 0777, $flags = FileConstants::RECURSIVE_DISABLED)
190
    {
191
        if ($this->isDir($file) && I::hasFlag($flags, FileConstants::RECURSIVE)) {
192
            $files = $this->getLists($file, FileConstants::COMPLETE_PATH | FileConstants::RECURSIVE);
193
            foreach ($files as $subFile) {
194
                /** @scrutinizer ignore-unhandled */@ftp_chmod($this->_conn, $mode, $subFile);
195
            }
196
        }
197
        return (bool)/** @scrutinizer ignore-unhandled */@ftp_chmod($this->_conn, $mode, $file);
198
    }
199
200
    /**
201
     * @ignore
202
     */
203
    public function symlink($from, $to)
204
    {
205
        return false;
206
    }
207
208
    /**
209
     * @ignore
210
     */
211
    public function close()
212
    {
213
        return ftp_close($this->_conn);
214
    }
215
216
    /**
217
     * @ignore
218
     */
219
    protected function _copy($fromFile, $toFile)
220
    {
221
        if ($fp = fopen("php://temp", 'r+')) {
222
            /** @scrutinizer ignore-unhandled */@ftp_fget($this->_conn, $fp, $fromFile, FTP_BINARY, 0);
223
            rewind($fp);
224
            return @ftp_fput($this->_conn, $toFile, $fp, FTP_BINARY, 0);
225
        }
226
        return false;
227
    }
228
229
    /**
230
     * @ignore
231
     */
232
    protected function _move($fromFile, $toFile)
233
    {
234
        return @ftp_rename($this->_conn, $fromFile, $toFile);
235
    }
236
237
    /**
238
     * @ignore
239
     */
240
    protected function _mkdir($dir, $mode = 0777)
241
    {
242
        $isCreated = (bool)@ftp_mkdir($this->_conn, $dir);
243
        $this->chmod($dir, $mode, FileConstants::RECURSIVE_DISABLED);
244
        return $isCreated;
245
    }
246
247
    /**
248
     * @ignore
249
     */
250
    protected function _rmdir($dir)
251
    {
252
        return @ftp_rmdir($this->_conn, $dir);
253
    }
254
}
255