Completed
Push — master ( 650783...0c9838 )
by wen
15:06
created

DateTime::getFormatValue()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 8.7624
c 0
b 0
f 0
cc 5
eloc 12
nc 9
nop 1
1
<?php
2
3
namespace Sco\Admin\Display\Columns;
4
5
use Carbon\Carbon;
6
7
class DateTime extends Column
8
{
9
10
    /**
11
     * Datetime format.
12
     *
13
     * @var string
14
     */
15
    protected $format;
16
17
    /**
18
     * Datetime timezone.
19
     *
20
     * @var string
21
     */
22
    protected $timezone;
23
24
    /**
25
     * @return string
26
     */
27
    public function getFormat()
28
    {
29
        if ($this->format) {
30
            return $this->format;
31
        }
32
33
        return config('admin.datetime_format');
34
    }
35
36
    /**
37
     * @param string $format
38
     *
39
     * @return DateTime
40
     */
41
    public function setFormat(string $format)
42
    {
43
        $this->format = $format;
44
45
        return $this;
46
    }
47
48
    /**
49
     * @return string
50
     */
51
    public function getTimezone()
52
    {
53
        if ($this->timezone) {
54
            return $this->timezone;
55
        }
56
57
        return config('app.timezone');
58
    }
59
60
    /**
61
     * @param string $timezone
62
     *
63
     * @return DateTime
64
     */
65
    public function setTimezone($timezone)
66
    {
67
        $this->timezone = $timezone;
68
69
        return $this;
70
    }
71
72
    public function getValue()
73
    {
74
        $value = parent::getValue();
75
76
        return $this->getFormatValue($value);
77
    }
78
79
    protected function getFormatValue($date)
80
    {
81
        if (! empty($date)) {
82
            if (is_numeric($date)) {
83
                $date = Carbon::createFromTimestamp($date);
84
            }
85
            if (! ($date instanceof Carbon)) {
86
                $date = Carbon::parse($date);
87
            }
88
89
            $date->timezone($this->getTimezone());
0 ignored issues
show
Bug introduced by
The method timezone does only exist in Carbon\Carbon, but not in Sco\Admin\Display\Columns\DateTime.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
90
91
            if ($this->getFormat() == 'humans') {
92
                $date = $date->diffForHumans();
0 ignored issues
show
Bug introduced by
The method diffForHumans does only exist in Carbon\Carbon, but not in Sco\Admin\Display\Columns\DateTime.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
93
            } else {
94
                $date = $date->format($this->getFormat());
0 ignored issues
show
Bug introduced by
The method format does only exist in Carbon\Carbon, but not in Sco\Admin\Display\Columns\DateTime.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
95
            }
96
        }
97
98
        return $date;
99
    }
100
}
101