This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | /** |
||
4 | * _ __ __ _____ _____ ___ ____ _____ |
||
5 | * | | / // // ___//_ _// || __||_ _| |
||
6 | * | |/ // /(__ ) / / / /| || | | | |
||
7 | * |___//_//____/ /_/ /_/ |_||_| |_| |
||
8 | * @link http://vistart.name/ |
||
9 | * @copyright Copyright (c) 2016 vistart |
||
10 | * @license http://vistart.name/license/ |
||
11 | */ |
||
12 | |||
13 | namespace vistart\Models\traits; |
||
14 | |||
15 | use Closure; |
||
16 | use yii\base\ModelEvent; |
||
17 | use yii\behaviors\TimestampBehavior; |
||
18 | |||
19 | /** |
||
20 | * Entity features concerning timestamp. |
||
21 | * @property-read array $timestampBehaviors |
||
22 | * @property-read string|int createdAt |
||
23 | * @property-read string|int updatedAt |
||
24 | * @property-read array $createdAtRules |
||
25 | * @property-read array $updatedAtRules |
||
26 | * @property-read boolean isExpired |
||
27 | * @version 2.0 |
||
28 | * @author vistart <[email protected]> |
||
29 | */ |
||
30 | trait TimestampTrait |
||
31 | { |
||
32 | |||
33 | /** |
||
34 | * @var string the attribute that will receive datetime value |
||
35 | * Set this property to false if you do not want to record the creation time. |
||
36 | */ |
||
37 | public $createdAtAttribute = 'create_time'; |
||
38 | |||
39 | /** |
||
40 | * @var string the attribute that will receive datetime value. |
||
41 | * Set this property to false if you do not want to record the update time. |
||
42 | */ |
||
43 | public $updatedAtAttribute = 'update_time'; |
||
44 | |||
45 | /** |
||
46 | * @var integer Determine the format of timestamp. |
||
47 | */ |
||
48 | public $timeFormat = 0; |
||
49 | public static $timeFormatDatetime = 0; |
||
50 | public static $timeFormatTimestamp = 1; |
||
51 | public static $initDatetime = '1970-01-01 00:00:00'; |
||
52 | public static $initTimestamp = 0; |
||
53 | |||
54 | /** |
||
55 | * @var int|false the expiration in seconds, or false if it will not be expired. |
||
56 | */ |
||
57 | public $expiredAt = false; |
||
58 | |||
59 | /** |
||
60 | * @var Closure |
||
61 | */ |
||
62 | public $expiredRemovingCallback; |
||
63 | public static $eventExpiredRemoved = 'expiredRemoved'; |
||
64 | |||
65 | /** |
||
66 | * Check this entity whether expired. |
||
67 | * @return boolean |
||
68 | */ |
||
69 | 45 | public function getIsExpired() |
|
70 | { |
||
71 | 45 | $createdAt = $this->getCreatedAt(); |
|
72 | 45 | if ($this->expiredAt === false || $createdAt === null) { |
|
73 | 39 | return false; |
|
74 | } |
||
75 | 9 | return $this->offsetDatetime($this->currentDatetime(), -$this->expiredAt) > $createdAt; |
|
76 | } |
||
77 | |||
78 | /** |
||
79 | * Remove myself if expired. |
||
80 | * @return boolean |
||
81 | */ |
||
82 | 44 | public function removeIfExpired() |
|
83 | { |
||
84 | 44 | if ($this->getIsExpired() && !$this->getIsNewRecord()) { |
|
0 ignored issues
–
show
|
|||
85 | 1 | if (($this->expiredRemovingCallback instanceof Closure || is_array($this->expiredRemovingCallback)) && is_callable($this->expiredRemovingCallback)) { |
|
86 | 1 | $result = call_user_func($this->expiredRemovingCallback, $this); |
|
0 ignored issues
–
show
$result is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the ![]() |
|||
87 | 1 | } |
|
88 | 1 | $result = $this->delete(); |
|
0 ignored issues
–
show
It seems like
delete() must be provided by classes using this trait. How about adding it as abstract method to this trait?
This check looks for methods that are used by a trait but not required by it. To illustrate, let’s look at the following code example trait Idable {
public function equalIds(Idable $other) {
return $this->getId() === $other->getId();
}
}
The trait Adding the ![]() |
|||
89 | 1 | $this->trigger(static::$eventExpiredRemoved, new ModelEvent(['data' => ['result' => $result]])); |
|
0 ignored issues
–
show
It seems like
trigger() must be provided by classes using this trait. How about adding it as abstract method to this trait?
This check looks for methods that are used by a trait but not required by it. To illustrate, let’s look at the following code example trait Idable {
public function equalIds(Idable $other) {
return $this->getId() === $other->getId();
}
}
The trait Adding the ![]() |
|||
90 | 1 | } |
|
91 | 44 | return false; |
|
92 | } |
||
93 | |||
94 | /** |
||
95 | * We recommened you attach this event when after finding this active record. |
||
96 | * @param ModelEvent $event |
||
97 | * @return boolean |
||
98 | */ |
||
99 | 44 | public function onRemoveExpired($event) |
|
100 | { |
||
101 | 44 | return $event->sender->removeIfExpired(); |
|
102 | } |
||
103 | |||
104 | /** |
||
105 | * Get the current date & time in format of "Y-m-d H:i:s" or timestamp. |
||
106 | * You can override this method to customize the return value. |
||
107 | * @param ModelEvent $event |
||
108 | * @return string Date & Time. |
||
109 | * @since 1.1 |
||
110 | */ |
||
111 | 58 | public static function getCurrentDatetime($event) |
|
112 | { |
||
113 | 58 | $sender = $event->sender; |
|
114 | 58 | return $sender->currentDatetime(); |
|
115 | } |
||
116 | |||
117 | /** |
||
118 | * Get current date & time, by current time format. |
||
119 | * @return string|int Date & time string if format is datetime, or timestamp. |
||
120 | */ |
||
121 | 58 | public function currentDatetime() |
|
122 | { |
||
123 | 58 | if ($this->timeFormat === self::$timeFormatDatetime) { |
|
124 | 58 | return date('Y-m-d H:i:s'); |
|
125 | } |
||
126 | if ($this->timeFormat === self::$timeFormatTimestamp) { |
||
127 | return time(); |
||
128 | } |
||
129 | return null; |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * Get offset date & time, by current time format. |
||
134 | * @param string|int $time Date &time string or timestamp. |
||
135 | * @param int $offset Offset int seconds. |
||
136 | * @return string|int Date & time string if format is datetime, or timestamp. |
||
137 | */ |
||
138 | 9 | public function offsetDatetime($time = null, $offset = 0) |
|
139 | { |
||
140 | 9 | if ($this->timeFormat === self::$timeFormatDatetime) { |
|
141 | 9 | return date('Y-m-d H:i:s', strtotime(($offset >= 0 ? "+$offset" : $offset) . " seconds", is_string($time) ? strtotime($time) : (is_int($time) ? $time : time()))); |
|
142 | } |
||
143 | if ($this->timeFormat === self::$timeFormatTimestamp) { |
||
144 | return (is_int($time) ? $time : time()) + $offset; |
||
145 | } |
||
146 | return null; |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * Get init date & time in format of "Y-m-d H:i:s" or timestamp.s |
||
151 | * @param ModelEvent $event |
||
152 | * @return string|int |
||
153 | */ |
||
154 | 8 | public static function getInitDatetime($event) |
|
155 | { |
||
156 | 8 | $sender = $event->sender; |
|
157 | 8 | return $sender->initDatetime(); |
|
158 | } |
||
159 | |||
160 | /** |
||
161 | * Get init date & time, by current time format. |
||
162 | * @return string|int Date & time string if format is datetime, or timestamp. |
||
163 | */ |
||
164 | 8 | public function initDatetime() |
|
165 | { |
||
166 | 8 | if ($this->timeFormat === self::$timeFormatDatetime) { |
|
167 | 8 | return static::$initDatetime; |
|
168 | } |
||
169 | if ($this->timeFormat === self::$timeFormatTimestamp) { |
||
170 | return static::$initTimestamp; |
||
171 | } |
||
172 | return null; |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * Check whether the attribute is init datetime. |
||
177 | * @param mixed $attribute |
||
178 | * @return boolean |
||
179 | */ |
||
180 | 4 | protected function isInitDatetime($attribute) |
|
181 | { |
||
182 | 4 | if ($this->timeFormat === self::$timeFormatDatetime) { |
|
183 | 4 | return $attribute == static::$initDatetime || $attribute == null; |
|
184 | } |
||
185 | if ($this->timeFormat === self::$timeFormatTimestamp) { |
||
186 | return $attribute == static::$initTimestamp || $attribute == null; |
||
187 | } |
||
188 | return false; |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Get the current date & time in format of "Y-m-d H:i:s". |
||
193 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
194 | * override or modify it directly, unless you know the consequences. |
||
195 | * @param ModelEvent $event |
||
196 | * @return string Date & Time. |
||
197 | * @since 1.1 |
||
198 | */ |
||
199 | 58 | public function onUpdateCurrentDatetime($event) |
|
200 | { |
||
201 | 58 | return self::getCurrentDatetime($event); |
|
202 | } |
||
203 | |||
204 | /** |
||
205 | * Behaviors associated with timestamp. |
||
206 | * @return array behaviors |
||
207 | */ |
||
208 | 17 | public function getTimestampBehaviors() |
|
209 | { |
||
210 | return [ |
||
211 | [ |
||
212 | 17 | 'class' => TimestampBehavior::className(), |
|
213 | 17 | 'createdAtAttribute' => $this->createdAtAttribute, |
|
214 | 17 | 'updatedAtAttribute' => $this->updatedAtAttribute, |
|
215 | 17 | 'value' => [$this, 'onUpdateCurrentDatetime'], |
|
216 | ] |
||
217 | 17 | ]; |
|
218 | } |
||
219 | |||
220 | /** |
||
221 | * Get createdAt. |
||
222 | * @return string timestamp |
||
223 | */ |
||
224 | 45 | public function getCreatedAt() |
|
225 | { |
||
226 | 45 | $createdAtAttribute = $this->createdAtAttribute; |
|
227 | 45 | if (!is_string($createdAtAttribute)) { |
|
228 | 2 | return null; |
|
229 | } |
||
230 | 43 | return $this->$createdAtAttribute; |
|
231 | } |
||
232 | |||
233 | /** |
||
234 | * Get rules associated with createdAtAttribute. |
||
235 | * @return array rules |
||
236 | */ |
||
237 | 17 | public function getCreatedAtRules() |
|
238 | { |
||
239 | 17 | if (!$this->createdAtAttribute) { |
|
240 | 2 | return []; |
|
241 | } |
||
242 | return [ |
||
243 | 15 | [[$this->createdAtAttribute], 'safe'], |
|
244 | 15 | ]; |
|
245 | } |
||
246 | |||
247 | /** |
||
248 | * Get updatedAt. |
||
249 | * @return string timestamp |
||
250 | */ |
||
251 | public function getUpdatedAt() |
||
252 | { |
||
253 | $updatedAtAttribute = $this->updatedAtAttribute; |
||
254 | if (!is_string($updatedAtAttribute)) { |
||
255 | return null; |
||
256 | } |
||
257 | return $this->$updatedAtAttribute; |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * Get rules associated with updatedAtAttribute. |
||
262 | * @return array rules |
||
263 | */ |
||
264 | 17 | public function getUpdatedAtRules() |
|
265 | { |
||
266 | 17 | if (!$this->updatedAtAttribute) { |
|
267 | 5 | return []; |
|
268 | } |
||
269 | return [ |
||
270 | 12 | [[$this->updatedAtAttribute], 'safe'], |
|
271 | 12 | ]; |
|
272 | } |
||
273 | |||
274 | 16 | public function enabledTimestampFields() |
|
275 | { |
||
276 | 16 | $fields = []; |
|
277 | 16 | if (is_string($this->createdAtAttribute)) { |
|
278 | 15 | $fields[] = $this->createdAtAttribute; |
|
279 | 15 | } |
|
280 | |||
281 | 16 | if (is_string($this->updatedAtAttribute)) { |
|
282 | 12 | $fields[] = $this->updatedAtAttribute; |
|
283 | 12 | } |
|
284 | 16 | return $fields; |
|
285 | } |
||
286 | } |
||
287 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.