Issues (138)

src/Elements/Dateselect.php (7 issues)

1
<?php
2
3
class Nip_Form_Element_Dateselect extends Nip_Form_Element_MultiElement
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();
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
The assignment to $inputName is dead and can be removed.
Loading history...
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
            $dayElement->addOption('', '--');
0 ignored issues
show
The method addOption() does not exist on Nip\Form\Elements\AbstractElement. It seems like you code against a sub-type of Nip\Form\Elements\AbstractElement 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

26
            $dayElement->/** @scrutinizer ignore-call */ 
27
                         addOption('', '--');
Loading history...
27
28
            for ($i = 1; $i <= 31; $i++) {
29
                $dayElement->addOption($i, $i);
30
            }
31
//            $dayElement->setValue(date('d'));
32
            $this->elements['day'] = $dayElement;
33
        }
34
35
36
        if (!$this->hasElement('month')) {
37
            $monthElement = $this->getForm()->getNewElement('select');
38
            $monthElement->addOption('', '--');
39
40
            for ($i = 1; $i <= 12; $i++) {
41
                $monthElement->addOption($i, date('M', mktime(0, 0, 0, $i, 1, 2014)));
42
            }
43
//            $monthElement->setValue(date('m'));
44
            $this->elements['month'] = $monthElement;
45
        }
46
47
        if (!$this->hasElement('year')) {
48
            $yearElement = $this->getForm()->getNewElement('select');
49
            $yearElement->addOption('', '--');
50
51
            $years = $this->getOption('years', range((int)date('Y') - 100, date('Y') + 5));
0 ignored issues
show
Are you sure the assignment to $years is correct as $this->getOption('years'... - 100, date('Y') + 5)) targeting Nip\Form\Elements\AbstractElement::getOption() 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...
52
53
            foreach ($years as $year) {
0 ignored issues
show
The expression $years of type null is not traversable.
Loading history...
54
                $yearElement->addOption($year, $year);
55
            }
56
//            $yearElement->setValue(date('Y'));
57
            $this->elements['year'] = $yearElement;
58
        }
59
    }
60
61
    /**
62
     * @inheritdoc
63
     */
64
    public function setName($name)
65
    {
66
        $return = parent::setName($name);
67
        $this->updateNameSelects();
68
        return $return;
69
    }
70
71
    public function updateNameSelects()
72
    {
73
        $inputName = $this->getName();
0 ignored issues
show
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...
74
        $this->getElement('day')->setName($inputName . '[day]');
75
        $this->getElement('month')->setName($inputName . '[month]');
76
        $this->getElement('year')->setName($inputName . '[year]');
77
    }
78
79
    /**
80
     * @return string
81
     */
82
    public function getLocale()
83
    {
84
        return $this->_locale;
85
    }
86
87
    /**
88
     * @param $format
89
     */
90
    public function setLocale($format)
91
    {
92
        $this->_locale = $format;
93
    }
94
95
    /**
96
     * @return string
97
     */
98
    public function getFormat()
99
    {
100
        return $this->format;
101
    }
102
103
    /**
104
     * @param $format
105
     */
106
    public function setFormat($format)
107
    {
108
        $this->format = $format;
109
    }
110
111
    /**
112
     * @param $data
113
     * @param string $source
114
     * @return Nip\Form\Elements\AbstractElement
115
     */
116
    public function getData($data, $source = 'abstract')
117
    {
118
        if ($source == 'model') {
119
            if ($data && $data != '0000-00-00' && $data != '0000-00-00 00:00:00') {
120
                $dateUnix = strtotime($data);
121
                if ($dateUnix && $dateUnix !== false && $dateUnix > -62169989992) {
122
                    $this->getElement('day')->setValue(date('d', $dateUnix));
123
                    $this->getElement('month')->setValue(date('m', $dateUnix));
124
                    $this->getElement('year')->setValue(date('Y', $dateUnix));
125
                }
126
            }
127
            return $this;
128
        }
129
        return parent::getData($data, $source);
130
    }
131
132
    /** @noinspection PhpMissingParentCallCommonInspection
133
     * @param $request
134
     * @return $this
135
     */
136
    public function getDataFromRequest($request)
137
    {
138
        if (is_array($request)) {
139
            $elements = $this->getElements();
140
            foreach ($elements as $key => $element) {
141
                $value = $request[$key];
142
                if ($value > 0) {
143
                    $element->setValue($value);
144
                }
145
            }
146
        }
147
        return $this;
148
    }
149
150
    public function validate()
151
    {
152
        parent::validate();
153
        if (!$this->isError()) {
154
            $value = $this->getValue();
155
            if ($value) {
156
            }
157
        }
158
    }
159
160
    /** @noinspection PhpMissingParentCallCommonInspection
161
     * @param string $requester
162
     * @return string|null
163
     */
164
    public function getValue($requester = 'abstract')
165
    {
166
        $unixTime = $this->getUnix();
167
        $format = $requester == 'model' ? 'Y-m-d' : $this->format;
168
        if ($unixTime) {
169
            return date($format, $unixTime);
170
        }
171
172
        return;
173
    }
174
175
    /**
176
     * @param bool $format
177
     * @return false|int
178
     */
179
    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

179
    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...
180
    {
181
        $day = $this->elements['day']->getValue();
182
        $month = $this->elements['month']->getValue();
183
        $year = $this->elements['year']->getValue();
184
185
        if ($day < 1 || $month < 1 || $year < 1) {
186
            return 0;
187
        }
188
189
        return mktime(0, 0, 0, $month, $day, $year);
190
    }
191
}
192