Completed
Push — master ( 0c044e...5ee946 )
by Richard
10:20
created

DateTimeSelect::__construct()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 66
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 37
CRAP Score 6.2698

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 50
nc 24
nop 4
dl 0
loc 66
ccs 37
cts 46
cp 0.8043
crap 6.2698
rs 8.6045
c 1
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
namespace Xoops\Form;
13
14
/**
15
 * DateTimeSelect - date and time form element
16
 *
17
 * @category  Xoops\Form\DateTimeSelect
18
 * @package   Xoops\Form
19
 * @author    Kazumi Ono <[email protected]>
20
 * @copyright 2001-2016 XOOPS Project (http://xoops.org)
21
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
22
 * @link      http://xoops.org
23
 */
24
class DateTimeSelect extends ElementTray
25
{
26
    const SHOW_BOTH = 1;
27
    const SHOW_DATE = 0;
28
    const SHOW_TIME = 2;
29
30
    /**
31
     * __construct
32
     *
33
     * @param string|array      $caption  Caption or array of all attributes
34
     * @param string            $name     name
35
     * @param integer|\DateTime $value    unix timestamp or DateTime object
36
     * @param mixed             $showtime control display of date and time elements
37
     *                                     SHOW_BOTH, true  - show both date and time selectors
38
     *                                     SHOW_DATE, false - only show date selector
39
     *                                     SHOW_TIME        - only show time selector
40
     */
41 4
    public function __construct($caption, $name = null, $value = 0, $showtime = true)
42
    {
43
        // stash everything in the tray and sort out later
44 4
        if (is_array($caption)) {
45 1
            parent::__construct($caption);
46 1
            $this->set(':joiner', '');
47
        } else {
48 4
            parent::__construct($caption, '', $name);
49 4
            $this->set('value', $value);
50 4
            $this->set(':showtime', (int) $showtime);
51
        }
52
53 4
        $workingTime = \Xoops\Core\Locale\Time::cleanTime($this->get('value', 0));
54
55 4
        $displayDate = true;
56 4
        $displayTime = true;
57 4
        switch ((int) $this->get(':showtime', static::SHOW_BOTH)) {
58 4
            case static::SHOW_DATE:
59
                $displayTime = false;
60
                break;
61 4
            case static::SHOW_TIME:
62
                $displayDate = false;
63
                break;
64
        }
65
66
        $dateDefinition = [
67 4
            'caption' => '',
68 4
            'name' => $this->get('name') . '[date]',
69 4
            'id' => $this->get('name') . '-date',
70 4
            'size' => 15,
71 4
            'value' => $workingTime,
72 4
            ElementFactory::FORM_KEY => $this,
73
        ];
74 4
        if ($displayDate) {
75 4
            new DateSelect($dateDefinition);
76
        } else {
77
            unset($dateDefinition['size']);
78
            $dateDefinition['value'] = \Xoops\Core\Locale\Time::formatDate($workingTime);
79
            new Hidden($dateDefinition);
80
        }
81
82 4
        $minuteInterval = $this->get(':minuteinterval', 15);
83 4
        $hours    = (int) ltrim($workingTime->format('H'), '0');
84 4
        $minutes  = (int) ltrim($workingTime->format('i'), '0');
85
86
        $timeDefinition = [
87 4
            'caption' => '',
88 4
            'name' => $this->get('name') . '[time]',
89 4
            'id' => $this->get('name') . '-time',
90 4
            'size' => 1,
91 4
            'value' => \Xoops\Core\Locale\Time::formatTime(
92 4
                $hours * 3600 + 60*$minuteInterval * ceil($minutes / $minuteInterval),
93 4
                'short',
94 4
                new \DateTimeZone('UTC')
95
            ),
96 4
            ElementFactory::FORM_KEY => $this
97
        ];
98
99 4
        if ($displayTime) {
100 4
            $timeDefinition['option'] = \Xoops\Core\Lists\Time::getList($minuteInterval);
101 4
            new Select($timeDefinition);
102
        } else {
103
            unset($timeDefinition['option'], $timeDefinition['size']);
104
            new Hidden($timeDefinition);
105
        }
106 4
    }
107
}
108