Issues (138)

src/Elements/Timeselect.php (4 issues)

1
<?php
2
3
class Nip_Form_Element_Timeselect extends Nip_Form_Element_MultiElement
4
{
5
    protected $_type = 'timeselect';
6
7 1
    public function init()
8
    {
9 1
        parent::init();
10
11 1
        $this->initSelects();
12 1
    }
13
14 1
    public function initSelects()
15
    {
16 1
        if (! isset($this->elements['hours'])) {
17 1
            $hoursElement = $this->getForm()->getNewSelectElement();
18
19 1
            $hoursElement->addOption('-', 'HH');
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

19
            $hoursElement->/** @scrutinizer ignore-call */ 
20
                           addOption('-', 'HH');
Loading history...
20 1
            for ($i = 0; $i <= 24; $i++) {
21 1
                $hoursElement->addOption($i, $i . 'h');
22
            }
23 1
            $hoursElement->setValue('-');
24
25 1
            $this->elements['hours'] = $hoursElement;
26
        }
27
28
29 1
        if (! isset($this->elements['minutes'])) {
30 1
            $minutesElement = $this->getForm()->getNewSelectElement();
31
32 1
            $minutesElement->addOption('-', 'MM');
33 1
            for ($i = 0; $i <= 59; $i++) {
34 1
                $minutesElement->addOption($i, $i . 'm');
35
            }
36 1
            $minutesElement->setValue('-');
37
38 1
            $this->elements['minutes'] = $minutesElement;
39
        }
40
41 1
        if (! isset($this->elements['seconds'])) {
42 1
            $secondsElement = $this->getForm()->getNewSelectElement();
43
44 1
            $secondsElement->addOption('-', 'SS');
45 1
            for ($i = 0; $i <= 59; $i++) {
46 1
                $secondsElement->addOption($i, $i . 's');
47
            }
48 1
            $secondsElement->setValue('-');
49
50 1
            $this->elements['seconds'] = $secondsElement;
51
        }
52 1
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57
    public function setName($name)
58
    {
59
        $return = parent::setName($name);
60
        $this->updateNameSelects();
61
62
        return $return;
63
    }
64
65
    public function updateNameSelects()
66
    {
67
        $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...
68
        $this->elements['hours']->setName($inputName . '[hours]');
69
        $this->elements['minutes']->setName($inputName . '[minutes]');
70
        $this->elements['seconds']->setName($inputName . '[seconds]');
71
    }
72
73
    /**
74
     * @inheritdoc
75
     */
76
    public function getData($data, $source = 'abstract')
77
    {
78
        if ($source == 'model') {
79
            $dateUnix = strtotime($data);
80
            if ($dateUnix && $dateUnix !== false && $dateUnix > -62169989992) {
81
                $this->elements['hours']->setValue(date('H', $dateUnix));
82
                $this->elements['minutes']->setValue(date('i', $dateUnix));
83
                $this->elements['seconds']->setValue(date('s', $dateUnix));
84
            }
85
86
            return $this;
87
        }
88
89
        return parent::getData($data, $source);
90
    }
91
92
    /** @noinspection PhpMissingParentCallCommonInspection
93
     * @inheritdoc
94
     */
95 1
    public function getDataFromRequest($request)
96
    {
97 1
        if (is_array($request)) {
98 1
            $elements = $this->getElements();
99 1
            foreach ($elements as $key => $element) {
100 1
                if (isset($request[$key])) {
101 1
                    $value = $request[$key];
102 1
                    $element->setValue($value);
103
                }
104
            }
105
        }
106
107 1
        return $this;
108
    }
109
110
    public function validate()
111
    {
112
        parent::validate();
113
        if (! $this->isError()) {
114
            $value = $this->getValue();
115
            if ($value) {
116
                $expectedValue = str_pad(intval($this->elements['hours']->getValue()), 2, "0", STR_PAD_LEFT);
117
                $expectedValue .= ':' . str_pad(intval($this->elements['minutes']->getValue()), 2, "0", STR_PAD_LEFT);
118
                $expectedValue .= ':' . str_pad(intval($this->elements['seconds']->getValue()), 2, "0", STR_PAD_LEFT);
119
                if ($expectedValue != $value) {
120
                    $message = $this->getForm()->getMessageTemplate('bad-' . $this->getName());
0 ignored issues
show
Are you sure the usage of $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 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...
121
                    $message = $message ? $message : 'I couldn\'t parse the ' . strtolower($this->getLabel()) . ' you entered';
122
                    $this->addError($message);
123
                }
124
            }
125
        }
126
    }
127
128
    /** @noinspection PhpMissingDocCommentInspection
129
     * @inheritdoc
130
     */
131 1
    public function getValue($requester = 'abstract')
132
    {
133 1
        $unixTime = $this->getUnix();
134 1
        $format   = $requester == 'model' ? 'H:i:s' : 'H:i:s';
135
136 1
        $value = ($unixTime) ? date($format, $unixTime) : null;
137
138 1
        return $value;
139
    }
140
141
142
    /** @noinspection PhpMissingDocCommentInspection
143
     * @inheritdoc
144
     */
145 1
    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

145
    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...
146
    {
147 1
        $hour    = intval($this->elements['hours']->getValue());
148 1
        $minutes = intval($this->elements['minutes']->getValue());
149 1
        $seconds = intval($this->elements['seconds']->getValue());
150 1
        if ($hour + $minutes + $seconds > 0) {
151 1
            return mktime($hour, $minutes, $seconds);
152
        }
153
154 1
        return false;
155
    }
156
}
157