1 | <?php |
||||||
2 | |||||||
3 | // +---------------------------------------------------------------------- |
||||||
4 | // | ThinkLibrary 6.0 for ThinkPhP 6.0 |
||||||
5 | // +---------------------------------------------------------------------- |
||||||
6 | // | 版权所有 2017~2020 [ https://www.dtapp.net ] |
||||||
7 | // +---------------------------------------------------------------------- |
||||||
8 | // | 官方网站: https://gitee.com/liguangchun/ThinkLibrary |
||||||
9 | // +---------------------------------------------------------------------- |
||||||
10 | // | 开源协议 ( https://mit-license.org ) |
||||||
11 | // +---------------------------------------------------------------------- |
||||||
12 | // | gitee 仓库地址 :https://gitee.com/liguangchun/ThinkLibrary |
||||||
13 | // | github 仓库地址 :https://github.com/GC0202/ThinkLibrary |
||||||
14 | // | Packagist 地址 :https://packagist.org/packages/liguangchun/think-library |
||||||
15 | // +---------------------------------------------------------------------- |
||||||
16 | |||||||
17 | namespace DtApp\ThinkLibrary\service; |
||||||
18 | |||||||
19 | use DtApp\ThinkLibrary\Service; |
||||||
20 | |||||||
21 | /** |
||||||
22 | * 本地存储 |
||||||
23 | * Class StorageService |
||||||
24 | * @package DtApp\ThinkLibrary\service |
||||||
25 | */ |
||||||
26 | class StorageService extends Service |
||||||
27 | { |
||||||
28 | /** |
||||||
29 | * @var string |
||||||
30 | */ |
||||||
31 | private $path = '', $remotely = ''; |
||||||
32 | |||||||
33 | /** |
||||||
34 | * 文件夹 |
||||||
35 | * @param string $path |
||||||
36 | * @return $this |
||||||
37 | */ |
||||||
38 | public function path(string $path): self |
||||||
39 | { |
||||||
40 | $this->path = $path; |
||||||
41 | return $this; |
||||||
42 | } |
||||||
43 | |||||||
44 | /** |
||||||
45 | * 需要保存的远程文件 |
||||||
46 | * @param string $remotely |
||||||
47 | * @return $this |
||||||
48 | */ |
||||||
49 | public function remotely(string $remotely): self |
||||||
50 | { |
||||||
51 | $this->remotely = $remotely; |
||||||
52 | return $this; |
||||||
53 | } |
||||||
54 | |||||||
55 | /** |
||||||
56 | * 获取配置信息 |
||||||
57 | * @return $this |
||||||
58 | */ |
||||||
59 | private function getConfig(): self |
||||||
60 | { |
||||||
61 | $this->path = config('dtapp.storage.path'); |
||||||
0 ignored issues
–
show
|
|||||||
62 | return $this; |
||||||
63 | } |
||||||
64 | |||||||
65 | /** |
||||||
66 | * 保存文件 |
||||||
67 | * @param string $name 保存的文件名 |
||||||
68 | * @return array |
||||||
69 | */ |
||||||
70 | public function save(string $name): array |
||||||
71 | { |
||||||
72 | if (empty($this->path)) { |
||||||
73 | $this->getConfig(); |
||||||
74 | } |
||||||
75 | // 判断文件夹是否存在 |
||||||
76 | is_dir($this->path) || mkdir($concurrentDirectory = $this->path, 0777, true) || is_dir($concurrentDirectory); |
||||||
0 ignored issues
–
show
It seems like
$this->path can also be of type boolean . However, the property $path is declared as type string . Maybe add an additional type check?
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly. For example, imagine you have a variable Either this assignment is in error or a type check should be added for that assignment. class Id
{
public $id;
public function __construct($id)
{
$this->id = $id;
}
}
class Account
{
/** @var Id $id */
public $id;
}
$account_id = false;
if (starsAreRight()) {
$account_id = new Id(42);
}
$account = new Account();
if ($account instanceof Id)
{
$account->id = $account_id;
}
![]() It seems like
$concurrentDirectory = $this->path can also be of type boolean ; however, parameter $directory of mkdir() 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
![]() It seems like
$this->path can also be of type boolean ; however, parameter $filename of is_dir() 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
![]() |
|||||||
77 | $return_content = $this->http_get_data($this->remotely); |
||||||
78 | $fp = @fopen("{$this->path}{$name}", "a"); //将文件绑定到流 |
||||||
79 | fwrite($fp, $return_content); //写入文件 |
||||||
0 ignored issues
–
show
It seems like
$fp can also be of type false ; however, parameter $stream of fwrite() 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
![]() |
|||||||
80 | return [ |
||||||
81 | 'file_name' => $name, |
||||||
82 | 'path' => $this->path, |
||||||
83 | 'remotely' => $this->remotely, |
||||||
84 | 'save_path' => "{$this->path}{$name}", |
||||||
85 | 'size' => $this->bytes($name) |
||||||
86 | ]; |
||||||
87 | } |
||||||
88 | |||||||
89 | /** |
||||||
90 | * 下载 |
||||||
91 | * @param $url |
||||||
92 | * @return false|string |
||||||
93 | */ |
||||||
94 | private function http_get_data($url) |
||||||
95 | { |
||||||
96 | $ch = curl_init(); |
||||||
97 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); |
||||||
98 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
||||||
99 | curl_setopt($ch, CURLOPT_URL, $url); |
||||||
100 | ob_start(); |
||||||
101 | curl_exec($ch); |
||||||
102 | $return_content = ob_get_clean(); |
||||||
103 | $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
||||||
0 ignored issues
–
show
|
|||||||
104 | return $return_content; |
||||||
105 | } |
||||||
106 | |||||||
107 | /** |
||||||
108 | * 删除文件 |
||||||
109 | * @param string $name |
||||||
110 | * @return bool |
||||||
111 | */ |
||||||
112 | public function delete(string $name) |
||||||
113 | { |
||||||
114 | if (empty($this->path)) { |
||||||
115 | $this->getConfig(); |
||||||
116 | } |
||||||
117 | if (file_exists($name)) { |
||||||
118 | if (unlink($name)) { |
||||||
119 | return true; |
||||||
120 | } |
||||||
121 | } |
||||||
122 | return false; |
||||||
123 | } |
||||||
124 | |||||||
125 | /** |
||||||
126 | * 统计文件大小 |
||||||
127 | * @param string $name |
||||||
128 | * @return string |
||||||
129 | */ |
||||||
130 | public function bytes(string $name) |
||||||
131 | { |
||||||
132 | if (empty($this->path)) { |
||||||
133 | $this->getConfig(); |
||||||
134 | } |
||||||
135 | $bytes = filesize($this->path . $name); |
||||||
136 | if ($bytes >= 1073741824) { |
||||||
137 | $bytes = round($bytes / 1073741824 * 100) / 100 . 'GB'; |
||||||
138 | } elseif ($bytes >= 1048576) { |
||||||
139 | $bytes = round($bytes / 1048576 * 100) / 100 . 'MB'; |
||||||
140 | } elseif ($bytes >= 1024) { |
||||||
141 | $bytes = round($bytes / 1024 * 100) / 100 . 'KB'; |
||||||
142 | } else { |
||||||
143 | $bytes .= 'Bytes'; |
||||||
144 | } |
||||||
145 | return $bytes; |
||||||
146 | } |
||||||
147 | |||||||
148 | /** |
||||||
149 | * 获取文件路径 |
||||||
150 | * @return string |
||||||
151 | */ |
||||||
152 | public function getPath(): string |
||||||
153 | { |
||||||
154 | if (empty($this->path)) { |
||||||
155 | $this->getConfig(); |
||||||
156 | } |
||||||
157 | return $this->path; |
||||||
0 ignored issues
–
show
|
|||||||
158 | } |
||||||
159 | } |
||||||
160 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.