Completed
Push — 6.0 ( c6d21a...a43f83 )
by yun
15:10 queued 11:06
created

UploadedFile::getErrorMessage()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 24
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
eloc 20
nc 7
nop 0
dl 0
loc 24
ccs 0
cts 20
cp 0
crap 56
rs 8.6666
c 0
b 0
f 0
1
<?php
2
// +----------------------------------------------------------------------
3
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
6
// +----------------------------------------------------------------------
7
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8
// +----------------------------------------------------------------------
9
// | Author: yunwuxin <[email protected]>
10
// +----------------------------------------------------------------------
11
namespace think\file;
12
13
use think\exception\FileException;
14
use think\File;
15
16
class UploadedFile extends File
1 ignored issue
show
Coding Style introduced by
Missing doc comment for class UploadedFile
Loading history...
17
{
18
19
    private $test = false;
0 ignored issues
show
Coding Style introduced by
Private member variable "test" must be prefixed with an underscore
Loading history...
20
    private $originalName;
0 ignored issues
show
Coding Style introduced by
Private member variable "originalName" must be prefixed with an underscore
Loading history...
21
    private $mimeType;
0 ignored issues
show
Coding Style introduced by
Private member variable "mimeType" must be prefixed with an underscore
Loading history...
22
    private $error;
0 ignored issues
show
Coding Style introduced by
Private member variable "error" must be prefixed with an underscore
Loading history...
23
24
    public function __construct(string $path, string $originalName, string $mimeType = null, int $error = null, $test = false)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
25
    {
26
        $this->originalName = $originalName;
27
        $this->mimeType     = $mimeType ?: 'application/octet-stream';
28
        $this->test         = $test;
29
        $this->error        = $error ?: UPLOAD_ERR_OK;
30
        parent::__construct($path, UPLOAD_ERR_OK === $this->error);
31
    }
32
33
    public function isValid()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function isValid()
Loading history...
34
    {
35
        $isOk = UPLOAD_ERR_OK === $this->error;
36
37
        return $this->test ? $isOk : $isOk && is_uploaded_file($this->getPathname());
38
    }
39
40
    public function move(string $directory, $name = null)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function move()
Loading history...
41
    {
42
        if ($this->isValid()) {
43
            if ($this->test) {
44
                return parent::move($directory, $name);
45
            }
46
47
            $target = $this->getTargetFile($directory, $name);
48
49
            set_error_handler(function ($type, $msg) use (&$error) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
50
                $error = $msg;
51
            });
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
52
53
            $moved = move_uploaded_file($this->getPathname(), $target);
54
            restore_error_handler();
55
            if (!$moved) {
56
                throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s)', $this->getPathname(), $target, strip_tags($error)));
57
            }
58
59
            @chmod($target, 0666 & ~umask());
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for chmod(). 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

59
            /** @scrutinizer ignore-unhandled */ @chmod($target, 0666 & ~umask());

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...
60
61
            return $target;
62
        }
63
64
        throw new FileException($this->getErrorMessage());
65
    }
66
67
    /**
68
     * 获取错误信息
69
     * @access public
70
     * @return string
71
     */
72
    protected function getErrorMessage(): string
73
    {
74
        switch ($this->error) {
75
            case 1:
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
76
            case 2:
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
77
                $message = 'upload File size exceeds the maximum value';
78
                break;
79
            case 3:
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
80
                $message = 'only the portion of file is uploaded';
81
                break;
82
            case 4:
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
83
                $message = 'no file to uploaded';
84
                break;
85
            case 6:
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
86
                $message = 'upload temp dir not found';
87
                break;
88
            case 7:
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
89
                $message = 'file write error';
90
                break;
91
            default:
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
92
                $message = 'unknown upload error';
93
        }
94
95
        return $message;
96
    }
97
98
    /**
99
     * 获取上传文件类型信息
100
     * @return string
101
     */
102
    public function getOriginalMime()
103
    {
104
        return $this->mimeType;
105
    }
106
107
    /**
108
     * 上传文件名
109
     * @return string
110
     */
111
    public function getOriginalName()
112
    {
113
        return $this->originalName;
114
    }
115
116
    /**
117
     * 获取上传文件扩展名
118
     * @return string
119
     */
120
    public function getOriginalExtension()
121
    {
122
        return pathinfo($this->originalName, PATHINFO_EXTENSION);
123
    }
124
125
    /**
126
     * 获取文件扩展名
127
     * @return string
128
     */
129
    public function extension()
130
    {
131
        return $this->getOriginalExtension();
132
    }
133
}
134