DateTime::getFormat()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 6
1
<?php
2
3
namespace Sco\Admin\Display\Columns;
4
5
use Carbon\Carbon;
6
7
class DateTime extends Column
8
{
9
    protected $type = 'datetime';
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
     * @param string $format
39
     *
40
     * @return DateTime
41
     */
42
    public function setFormat(string $format)
43
    {
44
        $this->format = $format;
45
46
        return $this;
47
    }
48
49
    /**
50
     * @return string
51
     */
52
    public function getTimezone()
53
    {
54
        if ($this->timezone) {
55
            return $this->timezone;
56
        }
57
58
        return config('app.timezone');
59
    }
60
61
    /**
62
     * @param string $timezone
63
     *
64
     * @return DateTime
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
77
        return $this->getFormatValue($value);
78
    }
79
80
    protected function getFormatValue($date)
81
    {
82
        if (! empty($date)) {
83
            if (is_numeric($date)) {
84
                $date = Carbon::createFromTimestamp($date);
85
            }
86
            if (! ($date instanceof Carbon)) {
87
                $date = Carbon::parse($date);
88
            }
89
90
            $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...
91
92
            if ($this->getFormat() == 'humans') {
93
                $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...
94
            } else {
95
                $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...
96
            }
97
        }
98
99
        return $date;
100
    }
101
}
102