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

Nip_Form_Element_Dateselect::initSelects()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 34
ccs 0
cts 23
cp 0
rs 8.6346
cc 7
nc 12
nop 0
crap 56
1
<?php
2
3
class Nip_Form_Element_Dateselect extends Nip_Form_Element_MultiElement
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 = 'dateselect';
6
    protected $_locale = 'ct_EN';
7
    protected $format = 'M d Y';
8
9
    public function init()
10
    {
11
        parent::init();
12
13
        $localeObj = Nip_Locale::instance();
0 ignored issues
show
Bug introduced by
The type Nip_Locale was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
        $this->setLocale($localeObj->getCurrent());
15
        $this->setFormat($localeObj->getOption(['time', 'dateFormat']));
16
17
        $this->initSelects();
18
    }
19
20
    public function initSelects()
21
    {
22
        $inputName = $this->getName();
0 ignored issues
show
Unused Code introduced by
The assignment to $inputName is dead and can be removed.
Loading history...
Bug introduced by
Are you sure the assignment to $inputName is correct as $this->getName() targeting Nip\Form\Elements\AbstractElement::getName() 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...
23
24
        if (!$this->hasElement('day')) {
25
            $dayElement = $this->getForm()->getNewElement('select');
26
27
            for ($i = 1; $i <= 31; $i++) {
28
                $dayElement->addOption($i, $i);
0 ignored issues
show
Bug introduced by
The method addOption() does not exist on Nip_Form_Element_Abstract. It seems like you code against a sub-type of Nip_Form_Element_Abstract such as Nip_Form_Element_Select or Nip_Form_Element_Input_Group. ( Ignorable by Annotation )

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

28
                $dayElement->/** @scrutinizer ignore-call */ 
29
                             addOption($i, $i);
Loading history...
29
            }
30
            $dayElement->setValue(date('d'));
31
            $this->elements['day'] = $dayElement;
32
        }
33
34
35
        if (!$this->hasElement('month')) {
36
            $monthElement = $this->getForm()->getNewElement('select');
37
            for ($i = 1; $i <= 12; $i++) {
38
                $monthElement->addOption($i, date('M', mktime(0, 0, 0, $i, 1, 2014)));
39
            }
40
            $monthElement->setValue(date('m'));
41
            $this->elements['month'] = $monthElement;
42
        }
43
44
        if (!$this->hasElement('year')) {
45
            $yearElement = $this->getForm()->getNewElement('select');
46
            $currentYear = date('Y');
47
            $startYear = $currentYear - 100;
48
            $endYear = $currentYear + 5;
49
            for ($i = $startYear; $i <= $endYear; $i++) {
50
                $yearElement->addOption($i, $i);
51
            }
52
            $yearElement->setValue(date('Y'));
53
            $this->elements['year'] = $yearElement;
54
        }
55
    }
56
57
    /**
58
     * @inheritdoc
59
     */
60
    public function setName($name)
61
    {
62
        $return = parent::setName($name);
63
        $this->updateNameSelects();
64
        return $return;
65
    }
66
67
    public function updateNameSelects()
68
    {
69
        $inputName = $this->getName();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $inputName is correct as $this->getName() targeting Nip\Form\Elements\AbstractElement::getName() 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...
70
        $this->getElement('day')->setName($inputName . '[day]');
71
        $this->getElement('month')->setName($inputName . '[month]');
72
        $this->getElement('year')->setName($inputName . '[year]');
73
    }
74
75
    /**
76
     * @return string
77
     */
78
    public function getLocale()
79
    {
80
        return $this->_locale;
81
    }
82
83
    /**
84
     * @param $format
85
     */
86
    public function setLocale($format)
87
    {
88
        $this->_locale = $format;
89
    }
90
91
    /**
92
     * @return string
93
     */
94
    public function getFormat()
95
    {
96
        return $this->format;
97
    }
98
99
    /**
100
     * @param $format
101
     */
102
    public function setFormat($format)
103
    {
104
        $this->format = $format;
105
    }
106
107
    /**
108
     * @param $data
109
     * @param string $source
110
     * @return Nip_Form_Element_Abstract
111
     */
112
    public function getData($data, $source = 'abstract')
113
    {
114
        if ($source == 'model') {
115
            if ($data && $data != '0000-00-00' && $data != '0000-00-00 00:00:00') {
116
                $dateUnix = strtotime($data);
117
                if ($dateUnix && $dateUnix !== false && $dateUnix > -62169989992) {
118
                    $this->getElement('day')->setValue(date('d', $dateUnix));
119
                    $this->getElement('month')->setValue(date('m', $dateUnix));
120
                    $this->getElement('year')->setValue(date('Y', $dateUnix));
121
                }
122
            }
123
            return $this;
124
        }
125
        return parent::getData($data, $source);
126
    }
127
128
    /** @noinspection PhpMissingParentCallCommonInspection
129
     * @param $request
130
     * @return $this
131
     */
132
    public function getDataFromRequest($request)
133
    {
134
        if (is_array($request)) {
135
            $elements = $this->getElements();
136
            foreach ($elements as $key => $element) {
137
                $value = $request[$key];
138
                if ($value > 0) {
139
                    $element->setValue($value);
140
                }
141
            }
142
        }
143
        return $this;
144
    }
145
146
    public function validate()
147
    {
148
        parent::validate();
149
        if (!$this->isError()) {
150
            $value = $this->getValue();
151
            if ($value) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
152
            }
153
        }
154
    }
155
156
    /** @noinspection PhpMissingParentCallCommonInspection
157
     * @param string $requester
158
     * @return string|null
159
     */
160
    public function getValue($requester = 'abstract')
161
    {
162
        $unixTime = $this->getUnix();
163
        $format = $requester == 'model' ? 'Y-m-d' : $this->format;
164
        if ($unixTime) {
165
            return date($format, $unixTime);
166
        }
167
168
        return;
169
    }
170
171
    /**
172
     * @param bool $format
173
     * @return false|int
174
     */
175
    public function getUnix($format = false)
0 ignored issues
show
Unused Code introduced by
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

175
    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...
176
    {
177
        $day = $this->elements['day']->getValue();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $day is correct as $this->elements['day']->getValue() targeting Nip\Form\Elements\AbstractElement::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...
178
        $month = $this->elements['month']->getValue();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $month is correct as $this->elements['month']->getValue() targeting Nip\Form\Elements\AbstractElement::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...
179
        $year = $this->elements['year']->getValue();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $year is correct as $this->elements['year']->getValue() targeting Nip\Form\Elements\AbstractElement::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...
180
181
        return mktime(0, 0, 0, $month, $day, $year);
182
    }
183
}
184