DateTimeType::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
ccs 10
cts 10
cp 1
cc 1
eloc 17
nc 1
nop 8
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
/*
4
 * This file is part of the Lug package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Lug\Component\Grid\Column\Type;
13
14
use Lug\Component\Grid\Column\Type\Formatter\FormatterInterface;
15
use Lug\Component\Grid\Exception\InvalidTypeException;
16
use Symfony\Component\OptionsResolver\OptionsResolver;
17
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
18
19
/**
20
 * @author GeLo <[email protected]>
21
 */
22
class DateTimeType extends AbstractType
23
{
24
    /**
25
     * @var FormatterInterface
26
     */
27
    private $formatter;
28
29
    /**
30
     * @var int
31
     */
32
    private $dateFormat;
33
34
    /**
35
     * @var int
36
     */
37
    private $timeFormat;
38
39
    /**
40
     * @var \IntlTimeZone|\DateTimeZone|string|null
41
     */
42
    private $timezone;
43
44
    /**
45
     * @var \IntlCalendar|int|null
46
     */
47
    private $calendar;
48
49
    /**
50
     * @var string|null
51
     */
52
    private $pattern;
53
54
    /**
55
     * @var bool
56
     */
57
    private $lenient;
58
59
    /**
60
     * @param PropertyAccessorInterface               $propertyAccessor
61
     * @param FormatterInterface                      $formatter
62
     * @param int                                     $dateFormat
63
     * @param int                                     $timeFormat
64
     * @param \IntlTimeZone|\DateTimeZone|string|null $timezone
65
     * @param \IntlCalendar|int|null                  $calendar
66
     * @param string|null                             $pattern
67
     * @param bool                                    $lenient
68
     */
69 21
    public function __construct(
70
        PropertyAccessorInterface $propertyAccessor,
71
        FormatterInterface $formatter,
72
        $dateFormat = \IntlDateFormatter::MEDIUM,
73
        $timeFormat = \IntlDateFormatter::MEDIUM,
74
        $timezone = null,
75
        $calendar = null,
76
        $pattern = null,
77
        $lenient = false
78
    ) {
79 21
        parent::__construct($propertyAccessor);
80
81 21
        $this->formatter = $formatter;
82 21
        $this->dateFormat = $dateFormat;
83 21
        $this->timeFormat = $timeFormat;
84 21
        $this->timezone = $timezone;
85 21
        $this->calendar = $calendar;
86 21
        $this->pattern = $pattern;
87 21
        $this->lenient = $lenient;
88 21
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93 3 View Code Duplication
    public function render($data, array $options)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
94
    {
95 3
        $dateTime = $this->getValue($data, $options);
96
97 3
        if ($dateTime === null) {
98 1
            return;
99
        }
100
101 2
        if (!$dateTime instanceof \DateTimeInterface) {
102 1
            throw new InvalidTypeException(sprintf(
103 1
                'The "%s" %s column type expects a "\DateTimeInterface" value, got "%s".',
104 1
                $options['column']->getName(),
105 1
                $this->getName(),
106 1
                is_object($dateTime) ? get_class($dateTime) : gettype($dateTime)
107 1
            ));
108
        }
109
110 1
        return $this->formatter->format($dateTime, $options);
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116 16
    public function configureOptions(OptionsResolver $resolver)
117
    {
118 16
        parent::configureOptions($resolver);
119
120
        $resolver
121 16
            ->setDefaults([
122 16
                'calendar'    => $this->calendar,
123 16
                'date_format' => $this->dateFormat,
124 16
                'lenient'     => $this->lenient,
125 16
                'pattern'     => $this->pattern,
126 16
                'timezone'    => $this->timezone,
127 16
                'time_format' => $this->timeFormat,
128 16
            ])
129 16
            ->setAllowedTypes('calendar', ['IntlCalendar', 'integer', 'null'])
130 16
            ->setAllowedTypes('lenient', 'boolean')
131 16
            ->setAllowedTypes('pattern', ['string', 'null'])
132 16
            ->setAllowedTypes('timezone', ['IntlTimeZone', 'DateTimeZone', 'string', 'null'])
133 16
            ->setAllowedValues('date_format', $formats = [
134 16
                \IntlDateFormatter::NONE,
135 16
                \IntlDateFormatter::SHORT,
136 16
                \IntlDateFormatter::MEDIUM,
137 16
                \IntlDateFormatter::LONG,
138 16
                \IntlDateFormatter::FULL,
139 16
            ])
140 16
            ->setAllowedValues('time_format', $formats);
141 16
    }
142
143
    /**
144
     * {@inheritdoc}
145
     */
146 2
    public function getName()
147
    {
148 2
        return 'datetime';
149
    }
150
}
151