Issues (138)

src/Elements/Dateinput.php (11 issues)

1
<?php
2
3
class Nip_Form_Element_Dateinput extends Nip_Form_Element_Input
4
{
5
    protected $_type = 'dateinput';
6
    protected $_locale = 'ct_EN';
7
    protected $_format = 'Y-m-d';
8
    protected $_hasTime = false;
9
10
    public function init()
11
    {
12
        parent::init();
13
        $this->setAttrib('type', 'date');
14
15
        if (app()->has('locale') == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
16
            return;
17
        }
18
        $localeObj = app('locale');
19
        if (!($localeObj instanceof \Nip\Locale\Locale)) {
20
            return;
21
        }
22
        $this->setLocale($localeObj->getCurrent());
23
        $this->setFormat($localeObj->getOption(['time', 'dateFormat']));
24
    }
25
26
    public function getLocale()
27
    {
28
        return $this->_locale;
29
    }
30
31
    public function setLocale($format)
32
    {
33
        $this->_locale = $format;
34
    }
35
36
    public function getFormat()
37
    {
38
        return $this->_format;
39
    }
40
41
    public function setFormat($format)
42
    {
43
        $this->_format = $format;
44
    }
45
46
    public function setTime($time)
47
    {
48
        $this->_hasTime = (bool)$time;
49
    }
50
51
    public function hasTime()
52
    {
53
        return $this->_hasTime;
54
    }
55
56
    public function getData($data, $source = 'abstract')
57
    {
58
        if ($source != 'model') {
59
            return parent::getData($data, $source);
60
        }
61
62
        if ($data instanceof DateTime) {
63
            $this->setValue($data->format('Y-m-d'));
64
            return $this;
65
        }
66
67
        if ($data && $data != '0000-00-00' && $data != '0000-00-00 00:00:00') {
68
            $dateUnix = strtotime($data);
69
            if ($dateUnix && $dateUnix !== false && $dateUnix > -62169989992) {
70
                $this->setValue(date('Y-m-d', $dateUnix));
71
72
                return $this;
73
            }
74
        }
75
        $this->setValue('');
76
77
        return $this;
78
    }
79
80
    public function validate()
81
    {
82
        parent::validate();
83
        if (!$this->isError() && $this->getValue()) {
0 ignored issues
show
Are you sure the usage of $this->getValue() targeting Nip_Form_Element_Dateinput::getValue() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
84
            $this->validateFormat();
85
        }
86
    }
87
88
    /**
89
     * @param string $requester
90
     * @return null
91
     */
92
    public function getValue($requester = 'abstract')
93
    {
94
        $value = parent::getValue($requester);
95
        if ($requester == 'model') {
96
            if ($value) {
97
                $unixTime = $this->getUnix();
98
                $value = date('Y-m-d', $unixTime);
0 ignored issues
show
$unixTime of type false is incompatible with the type integer|null expected by parameter $timestamp of date(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

98
                $value = date('Y-m-d', /** @scrutinizer ignore-type */ $unixTime);
Loading history...
99
            }
100
        }
101
102
        return $value;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $value also could return the type string which is incompatible with the documented return type null.
Loading history...
103
    }
104
105
    /**
106
     * @param false $format
107
     * @return false|int
108
     */
109
    public function getUnix($format = false)
0 ignored issues
show
The parameter $format is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

109
    public function getUnix(/** @scrutinizer ignore-unused */ $format = false)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
110
    {
111
        $format = 'Y-m-d';
112
        $value = $this->getValue();
0 ignored issues
show
Are you sure the assignment to $value is correct as $this->getValue() targeting Nip_Form_Element_Dateinput::getValue() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
113
        $date = ($value) ? date_create_from_format($format, $this->getValue()) :false;
0 ignored issues
show
$value is of type null, thus it always evaluated to false.
Loading history...
114
        if ($date instanceof DateTime) {
0 ignored issues
show
$date is never a sub-type of DateTime.
Loading history...
115
            return $date->getTimestamp();
116
        }
117
118
        return false;
119
    }
120
121
    public function validateFormat($format = false)
0 ignored issues
show
The parameter $format is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

121
    public function validateFormat(/** @scrutinizer ignore-unused */ $format = false)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
122
    {
123
        $format = 'Y-m-d';
124
        $value = $this->getValue();
0 ignored issues
show
Are you sure the assignment to $value is correct as $this->getValue() targeting Nip_Form_Element_Dateinput::getValue() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
125
126
        if ($value) {
0 ignored issues
show
$value is of type null, thus it always evaluated to false.
Loading history...
127
            $unixTime = $this->getUnix('Y-m-d');
128
            if ($unixTime) {
129
                $this->setValue(date($format, $unixTime));
130
131
                return true;
132
            }
133
            $message = $this->getForm()->getMessageTemplate('bad-' . $this->getName());
134
            $message = $message ? $message : 'I couldn\'t parse the ' . strtolower($this->getLabel()) . ' you entered';
135
            $this->addError($message);
136
        }
137
        return false;
138
    }
139
}
140