Completed
Push — master ( e093dd...67b7b2 )
by Abdelrahman
03:07 queued 01:36
created

HasTimezones   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 10
c 0
b 0
f 0
wmc 4
lcom 0
cbo 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A asDateTime() 0 16 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Support\Traits;
6
7
use DateTimeZone;
8
use Illuminate\Support\Arr;
9
10
trait HasTimezones
11
{
12
    /**
13
     * Return a timestamp as DateTime object.
14
     *
15
     * @param  mixed  $value
16
     * @return \Illuminate\Support\Carbon
17
     */
18
    protected function asDateTime($value)
19
    {
20
        $datetime = parent::asDateTime($value);
21
22
        $setAttributeCalled = Arr::first(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 30), function ($trace) {
0 ignored issues
show
Documentation introduced by
debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 30) is of type array, but the function expects a object<Illuminate\Support\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
23
            return $trace['function'] === 'setAttribute';
24
        });
25
26
        // When setting attributes, skip custom timezone setting,
27
        // and use default application settings for consistent storage!
28
        if (! $setAttributeCalled && app()->bound('request.user') && $timezone = optional(app('request.user'))->timezone) {
29
            $datetime->setTimezone(new DateTimeZone($timezone));
30
        }
31
32
        return $datetime;
33
    }
34
}
35