Completed
Push — master ( 8431a9...0bccd4 )
by Gabriel
04:28
created

Nip_Form_Element_Dateinput   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 30
eloc 48
c 1
b 0
f 0
dl 0
loc 111
ccs 0
cts 60
cp 0
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A validateFormat() 0 15 5
A hasTime() 0 3 1
B getData() 0 17 8
A validate() 0 5 3
A init() 0 6 1
A setFormat() 0 3 1
A setTime() 0 3 1
A getFormat() 0 3 1
A getValue() 0 11 3
A getLocale() 0 3 1
A setLocale() 0 3 1
A getUnix() 0 9 4
1
<?php
2
3
class Nip_Form_Element_Dateinput extends Nip_Form_Element_Input
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
    protected $_type = 'dateinput';
6
    protected $_locale = 'ct_EN';
7
    protected $_format = 'M d Y';
8
    protected $_hasTime = false;
9
10
    public function init()
11
    {
12
        parent::init();
13
        $localeObj = app('locale');
0 ignored issues
show
Bug introduced by
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

13
        $localeObj = /** @scrutinizer ignore-call */ app('locale');
Loading history...
14
        $this->setLocale($localeObj->getCurrent());
15
        $this->setFormat($localeObj->getOption(['time', 'dateFormat']));
16
    }
17
18
    public function getLocale()
19
    {
20
        return $this->_locale;
21
    }
22
23
    public function setLocale($format)
24
    {
25
        $this->_locale = $format;
26
    }
27
28
    public function getFormat()
29
    {
30
        return $this->_format;
31
    }
32
33
    public function setFormat($format)
34
    {
35
        $this->_format = $format;
36
    }
37
38
    public function setTime($time)
39
    {
40
        $this->_hasTime = (bool)$time;
41
    }
42
43
    public function hasTime()
44
    {
45
        return $this->_hasTime;
46
    }
47
48
    public function getData($data, $source = 'abstract')
49
    {
50
        if ($source == 'model') {
51
            if ($data && $data != '0000-00-00' && $data != '0000-00-00 00:00:00') {
52
                $dateUnix = strtotime($data);
53
                if ($dateUnix && $dateUnix !== false && $dateUnix > -62169989992) {
54
                    $this->setValue(date($this->_format, $dateUnix));
55
56
                    return $this;
57
                }
58
            }
59
            $this->setValue('');
60
61
            return $this;
62
        }
63
64
        return parent::getData($data, $source);
65
    }
66
67
    public function validate()
68
    {
69
        parent::validate();
70
        if (!$this->isError() && $this->getValue()) {
0 ignored issues
show
Bug introduced by
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...
71
            $this->validateFormat();
72
        }
73
    }
74
75
    public function getValue($requester = 'abstract')
76
    {
77
        $value = parent::getValue($requester);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $value is correct as parent::getValue($requester) targeting Nip\Form\Utility\HasAttributesTrait::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...
78
        if ($requester == 'model') {
79
            if ($value) {
0 ignored issues
show
introduced by
$value is of type null, thus it always evaluated to false.
Loading history...
80
                $unixTime = $this->getUnix();
81
                $value = date('Y-m-d', $unixTime);
82
            }
83
        }
84
85
        return $value;
86
    }
87
88
    public function getUnix($format = false)
89
    {
90
        $format = $format ? $format : $this->_format;
91
        $value = $this->getValue();
0 ignored issues
show
Bug introduced by
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...
92
        if ($value) {
0 ignored issues
show
introduced by
$value is of type null, thus it always evaluated to false.
Loading history...
93
            $date = date_create_from_format($format, $this->getValue());
94
        }
95
96
        return $date ? $date->getTimestamp() : false;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $date does not seem to be defined for all execution paths leading up to this point.
Loading history...
97
    }
98
99
    public function validateFormat($format = false)
100
    {
101
        $format = $format ? $format : $this->_format;
102
        $value = $this->getValue();
0 ignored issues
show
Bug introduced by
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...
103
104
        if ($value) {
0 ignored issues
show
introduced by
$value is of type null, thus it always evaluated to false.
Loading history...
105
            $unixTime = $this->getUnix($format);
106
            if ($unixTime) {
107
                $this->setValue(date($format, $unixTime));
108
109
                return true;
110
            }
111
            $message = $this->getForm()->getMessageTemplate('bad-'.$this->getName());
112
            $message = $message ? $message : 'I couldn\'t parse the '.strtolower($this->getLabel()).' you entered';
113
            $this->addError($message);
114
        }
115
    }
116
}
117