Completed
Push — master ( f05a87...6b2b65 )
by wen
14:04
created

Date::getDefaultValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 6
rs 9.4285
c 1
b 0
f 1
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace Sco\Admin\Form\Elements;
4
5
use Carbon\Carbon;
6
7
class Date extends Input
8
{
9
    protected $type = 'date';
10
11
    protected $defaultValue = '';
12
13
    /**
14
     * Date Picker format
15
     *
16
     * @var string
17
     */
18
    protected $pickerFormat = 'yyyy-MM-dd';
19
20
    /**
21
     * Datetime timezone.
22
     *
23
     * @var string
24
     */
25
    protected $timezone;
26
27
    protected $editable = false;
28
29
    public function getFormat()
30
    {
31
        return $this->convertPickerFormat();
32
    }
33
34
    /*public function setFormat($value)
0 ignored issues
show
Unused Code Comprehensibility introduced by
52% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
35
    {
36
        $this->format = $value;
37
38
        return $this;
39
    }*/
40
41
    protected function convertPickerFormat()
42
    {
43
        return strtr($this->getPickerFormat(), [
44
            'yyyy' => 'Y',
45
            //'yy'   => 'y',
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
46
            'MM'   => 'm',
47
            //'M'    => 'n',
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
48
            'dd'   => 'd',
49
            //'d'    => 'j',
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
50
            'HH'   => 'H',
51
            //'H'    => 'G',
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
52
            'mm'   => 'i',
53
            'ss'   => 's',
54
        ]);
55
    }
56
57
    protected function getPickerFormat()
58
    {
59
        return $this->pickerFormat;
60
    }
61
62
    /**
63
     * @param string $value
64
     *
65
     * @return $this
66
     */
67
    public function setPickerFormat($value)
68
    {
69
        $this->pickerFormat = $value;
70
71
        return $this;
72
    }
73
74
    /**
75
     * @return string
76
     */
77
    protected function getTimezone()
78
    {
79
        if ($this->timezone) {
80
            return $this->timezone;
81
        }
82
83
        return config('app.timezone');
84
    }
85
86
    /**
87
     * @param string $timezone
88
     *
89
     * @return $this
90
     */
91
    public function setTimezone($timezone)
92
    {
93
        $this->timezone = $timezone;
94
95
        return $this;
96
    }
97
98
    protected function isEditable()
99
    {
100
        return $this->editable;
101
    }
102
103
    /**
104
     * Allow edit
105
     *
106
     * @return $this
107
     */
108
    public function enableEdit()
109
    {
110
        $this->editable = true;
111
112
        return $this;
113
    }
114
115
    public function toArray()
116
    {
117
        return parent::toArray() + [
118
                //'format' => $this->getFormat(),
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
119
                'pickerFormat' => $this->getPickerFormat(),
120
                'editable' => $this->isEditable(),
121
            ];
122
    }
123
124
    public function getValue()
125
    {
126
        $value = parent::getValue();
127
        if (empty($value)) {
128
            return '';
129
        }
130
        return $this->dateToString($value);
131
    }
132
133
    protected function dateToString($value)
134
    {
135
        if (!($value instanceof Carbon)) {
136
            $value = Carbon::parse($value);
137
        }
138
139
        $value->timezone($this->getTimezone());
0 ignored issues
show
Bug introduced by
The method timezone does only exist in Carbon\Carbon, but not in Sco\Admin\Form\Elements\Date.

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...
140
141
        return $value->format($this->getFormat());
0 ignored issues
show
Bug introduced by
The method format does only exist in Carbon\Carbon, but not in Sco\Admin\Form\Elements\Date.

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...
142
    }
143
144
    protected function prepareValue($value)
145
    {
146
        $value = Carbon::parse($value);
147
        $value->timezone($this->getTimezone());
148
        return $value;
149
    }
150
}
151