Completed
Push — master ( a41be1...cd8884 )
by wen
12:14
created

Date::isEditable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
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
    /**
12
     * Date Picker format
13
     *
14
     * @var string
15
     */
16
    protected $pickerFormat = 'yyyy-MM-dd';
17
18
    /**
19
     * Datetime timezone.
20
     *
21
     * @var string
22
     */
23
    protected $timezone;
24
25
    protected $editable = false;
26
27
    public function getFormat()
28
    {
29
        return $this->convertPickerFormat();
30
    }
31
32
    /*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...
33
    {
34
        $this->format = $value;
35
36
        return $this;
37
    }*/
38
39
    protected function convertPickerFormat()
40
    {
41
        return strtr($this->getPickerFormat(), [
42
            'yyyy' => 'Y',
43
            //'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...
44
            'MM'   => 'm',
45
            //'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...
46
            'dd'   => 'd',
47
            //'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...
48
            'HH'   => 'H',
49
            //'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...
50
            'mm'   => 'i',
51
            'ss'   => 's',
52
        ]);
53
    }
54
55
    protected function getPickerFormat()
56
    {
57
        return $this->pickerFormat;
58
    }
59
60
    /**
61
     * @param string $value
62
     *
63
     * @return $this
64
     */
65
    public function setPickerFormat($value)
66
    {
67
        $this->pickerFormat = $value;
68
69
        return $this;
70
    }
71
72
    /**
73
     * @return string
74
     */
75
    protected function getTimezone()
76
    {
77
        if ($this->timezone) {
78
            return $this->timezone;
79
        }
80
81
        return config('app.timezone');
82
    }
83
84
    /**
85
     * @param string $timezone
86
     *
87
     * @return $this
88
     */
89
    public function setTimezone($timezone)
90
    {
91
        $this->timezone = $timezone;
92
93
        return $this;
94
    }
95
96
    protected function isEditable()
97
    {
98
        return $this->editable;
99
    }
100
101
    /**
102
     * Allow edit
103
     *
104
     * @return $this
105
     */
106
    public function enableEdit()
107
    {
108
        $this->editable = true;
109
110
        return $this;
111
    }
112
113
    public function toArray()
114
    {
115
        return parent::toArray() + [
116
                //'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...
117
                'pickerFormat' => $this->getPickerFormat(),
118
                'editable' => $this->isEditable(),
119
            ];
120
    }
121
122
    protected function getDefaultValue()
123
    {
124
        return Carbon::now()
125
            ->timezone($this->getTimezone())
126
            ->format($this->getFormat());
127
    }
128
129
    public function getValue()
130
    {
131
        $value = parent::getValue();
132
        if (empty($value)) {
133
            return '';
134
        }
135
        return $this->dateToString($value);
136
    }
137
138
    protected function dateToString($value)
139
    {
140
        if (!($value instanceof Carbon)) {
141
            $value = Carbon::parse($value);
142
        }
143
144
        $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...
145
146
        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...
147
    }
148
149
    protected function prepareValue($value)
150
    {
151
        $value = Carbon::parse($value);
152
        $value->timezone($this->getTimezone());
153
        return $value;
154
    }
155
}
156