Completed
Push — 6.0 ( 466ef2...20652a )
by liu
03:02
created

TimeStamp::autoWriteTimestamp()   B

Complexity

Conditions 10
Paths 14

Size

Total Lines 33
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
cc 10
eloc 20
nc 14
nop 1
dl 0
loc 33
ccs 0
cts 20
cp 0
crap 110
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
// +----------------------------------------------------------------------
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
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: liu21st <[email protected]>
10
// +----------------------------------------------------------------------
11
declare (strict_types = 1);
12
13
namespace think\model\concern;
14
15
use DateTime;
16
17
/**
18
 * 自动时间戳
19
 */
5 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
20
trait TimeStamp
21
{
22
    /**
23
     * 是否需要自动写入时间戳 如果设置为字符串 则表示时间字段的类型
24
     * @var bool|string
25
     */
26
    protected $autoWriteTimestamp;
27
28
    /**
29
     * 创建时间字段 false表示关闭
30
     * @var false|string
31
     */
32
    protected $createTime = 'create_time';
33
34
    /**
35
     * 更新时间字段 false表示关闭
36
     * @var false|string
37
     */
38
    protected $updateTime = 'update_time';
39
40
    /**
41
     * 时间字段显示格式
42
     * @var string
43
     */
44
    protected $dateFormat;
45
46
    /**
47
     * 高精度时间记录
48
     * @var bool
49
     */
50
    protected $timestamp = false;
51
52
    /**
53
     * 是否需要自动写入时间字段
54
     * @access public
55
     * @param  bool|string $auto
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
56
     * @return $this
57
     */
58
    public function isAutoWriteTimestamp($auto)
59
    {
60
        $this->autoWriteTimestamp = $auto;
61
62
        return $this;
63
    }
64
65
    /**
66
     * 获取自动写入时间字段
67
     * @access public
68
     * @return bool|string
69
     */
70
    public function getAutoWriteTimestamp()
71
    {
72
        return $this->autoWriteTimestamp;
73
    }
74
75
    /**
76
     * 设置时间字段格式化
77
     * @access public
78
     * @param  string $format
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
79
     * @return $this
80
     */
81
    public function setDateFormat(string $format)
82
    {
83
        $this->dateFormat = $format;
84
85
        return $this;
86
    }
87
88
    /**
89
     * 获取自动写入时间字段
90
     * @access public
91
     * @return string
92
     */
93
    public function getDateFormat()
94
    {
95
        return $this->dateFormat;
96
    }
97
98
    /**
99
     * 自动写入时间戳
100
     * @access protected
101
     * @param  string $name 时间戳字段
102
     * @return mixed
103
     */
104
    protected function autoWriteTimestamp(string $name)
105
    {
106
        $value = time();
107
108
        if (isset($this->type[$name])) {
109
            $type = $this->type[$name];
110
111
            if (strpos($type, ':')) {
112
                list($type, $param) = explode(':', $type, 2);
113
            }
114
115
            switch ($type) {
116
                case 'datetime':
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
117
                case 'date':
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
118
                case 'timestamp':
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
119
                    $value = $this->formatDateTime('Y-m-d H:i:s.u');
120
                    break;
121
                default:
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
122
                    if (false !== strpos($type, '\\')) {
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
123
                        // 对象数据写入
124
                        $value = new $type();
125
                        if (method_exists($value, '__toString')) {
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 20 spaces, found 24
Loading history...
126
                            // 对象数据写入
127
                            $value = $value->__toString();
128
                        }
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 20 spaces, found 24
Loading history...
129
                    }
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
130
            }
131
        } elseif (is_string($this->autoWriteTimestamp) && in_array(strtolower($this->autoWriteTimestamp),
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...
132
            ['datetime', 'date', 'timestamp'])) {
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 8 spaces, but found 12.
Loading history...
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...
Coding Style introduced by
Closing parenthesis of a multi-line IF statement must be on a new line
Loading history...
133
            $value = $this->formatDateTime('Y-m-d H:i:s.u');
134
        }
135
136
        return $value;
137
    }
138
139
    /**
140
     * 时间日期字段格式化处理
141
     * @access protected
142
     * @param  mixed $format    日期格式
143
     * @param  mixed $time      时间日期表达式
144
     * @param  bool  $timestamp 时间表达式是否为时间戳
145
     * @return mixed
146
     */
147
    protected function formatDateTime($format, $time = 'now', bool $timestamp = false)
148
    {
149
        if (empty($time)) {
150
            return;
151
        }
152
153
        if (false === $format) {
154
            return $time;
155
        } elseif (false !== strpos($format, '\\')) {
156
            return new $format($time);
157
        }
158
159
        if ($time instanceof DateTime) {
160
            $dateTime = $time;
161
        } elseif ($timestamp) {
162
            $dateTime = new DateTime();
163
            $dateTime->setTimestamp((int) $time);
164
        } else {
165
            $dateTime = new DateTime($time);
166
        }
167
168
        return $dateTime->format($format);
169
    }
170
171
    /**
172
     * 获取时间字段值
173
     * @access protected
174
     * @param  mixed   $value
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
175
     * @return mixed
176
     */
177
    protected function getTimestampValue($value)
178
    {
179
        if (is_string($this->autoWriteTimestamp) && in_array(strtolower($this->autoWriteTimestamp), [
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...
180
            'datetime', 'date', 'timestamp',
181
        ])) {
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...
Coding Style introduced by
Closing parenthesis of a multi-line IF statement must be on a new line
Loading history...
182
            $value = $this->formatDateTime($this->dateFormat, $value);
183
        } else {
184
            $value = $this->formatDateTime($this->dateFormat, $value, true);
185
        }
186
187
        return $value;
188
    }
189
}
190