Completed
Push — master ( 517a09...25d251 )
by wen
13:07
created

DateTime::setFormat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
nc 1
cc 1
eloc 3
nop 1
1
<?php
2
3
4
namespace Sco\Admin\View\Columns;
5
6
use Carbon\Carbon;
7
8
class DateTime extends Column
9
{
10
11
    /**
12
     * Datetime format.
13
     *
14
     * @var string
15
     */
16
    protected $format;
17
18
    /**
19
     * Datetime timezone.
20
     *
21
     * @var string
22
     */
23
    protected $timezone;
24
25
    /**
26
     * @return string
27
     */
28
    public function getFormat()
29
    {
30
        if ($this->format) {
31
            return $this->format;
32
        }
33
34
        return config('admin.datetime_format');
35
    }
36
37
    /**
38
     * @return string
39
     */
40
    public function getTimezone()
41
    {
42
        if ($this->timezone) {
43
            return $this->timezone;
44
        }
45
46
        return config('app.timezone');
47
    }
48
49
    /**
50
     * @param string $format
51
     *
52
     * @return $this
53
     */
54
    public function setFormat($format)
55
    {
56
        $this->format = $format;
57
58
        return $this;
59
    }
60
61
    /**
62
     * @param string $timezone
63
     *
64
     * @return $this
65
     */
66
    public function setTimezone($timezone)
67
    {
68
        $this->timezone = $timezone;
69
70
        return $this;
71
    }
72
73
    public function getValue()
74
    {
75
        $value = parent::getValue();
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\View\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\View\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\View\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